| 1 |
/****************************************************************************** |
| 2 |
* Top contributors (to current version): |
| 3 |
* Daniel Larraz |
| 4 |
* |
| 5 |
* This file is part of the cvc5 project. |
| 6 |
* |
| 7 |
* Copyright (c) 2009-2025 by the authors listed in the file AUTHORS |
| 8 |
* in the top-level source directory and their institutional affiliations. |
| 9 |
* All rights reserved. See the file COPYING in the top-level source |
| 10 |
* directory for licensing information. |
| 11 |
* **************************************************************************** |
| 12 |
* |
| 13 |
* Two tests to validate the use of the separation logic API. |
| 14 |
* |
| 15 |
* First test validates that we cannot use the API if not using separation |
| 16 |
* logic. |
| 17 |
* |
| 18 |
* Second test validates that the expressions returned from the API are |
| 19 |
* correct and can be interrogated. |
| 20 |
* |
| 21 |
****************************************************************************/ |
| 22 |
|
| 23 |
import io.github.cvc5.*; |
| 24 |
|
| 25 |
public class SepLogApi |
| 26 |
{ |
| 27 |
/** |
| 28 |
* Test function to validate that we *cannot* obtain the heap/nil expressions |
| 29 |
* when *not* using the separation logic theory |
| 30 |
*/ |
| 31 |
public static int validateException() |
| 32 |
{ |
| 33 |
try |
| 34 |
{ |
| 35 |
TermManager tm = new TermManager(); |
| 36 |
Solver slv = new Solver(tm); |
| 37 |
|
| 38 |
/* |
| 39 |
* Setup some options for cvc5 -- we explictly want to use a simplistic |
| 40 |
* theory (e.g., QF_IDL) |
| 41 |
*/ |
| 42 |
slv.setLogic("QF_IDL"); |
| 43 |
slv.setOption("produce-models", "true"); |
| 44 |
slv.setOption("incremental", "false"); |
| 45 |
|
| 46 |
/* Our integer type */ |
| 47 |
Sort integer = tm.getIntegerSort(); |
| 48 |
|
| 49 |
/** we intentionally do not set the separation logic heap */ |
| 50 |
|
| 51 |
/* Our SMT constants */ |
| 52 |
Term x = tm.mkConst(integer, "x"); |
| 53 |
Term y = tm.mkConst(integer, "y"); |
| 54 |
|
| 55 |
/* y > x */ |
| 56 |
Term y_gt_x = tm.mkTerm(Kind.GT, y, x); |
| 57 |
|
| 58 |
/* assert it */ |
| 59 |
slv.assertFormula(y_gt_x); |
| 60 |
|
| 61 |
Result r = slv.checkSat(); |
| 62 |
/* If this is UNSAT, we have an issue; so bail-out */ |
| 63 |
if (!r.isSat()) |
| 64 |
{ |
| 65 |
return -1; |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* We now try to obtain our separation logic expressions from the solver -- |
| 70 |
* we want to validate that we get our expected exceptions. |
| 71 |
*/ |
| 72 |
boolean caughtOnHeap = false; |
| 73 |
boolean caughtOnNil = false; |
| 74 |
|
| 75 |
String expected = |
| 76 |
"cannot obtain separation logic expressions if not using the separation logic theory."; |
| 77 |
|
| 78 |
/* test the heap expression */ |
| 79 |
try |
| 80 |
{ |
| 81 |
Term heapExpr = slv.getValueSepHeap(); |
| 82 |
} |
| 83 |
catch (CVC5ApiException e) |
| 84 |
{ |
| 85 |
caughtOnHeap = true; |
| 86 |
if (!e.getMessage().equals(expected)) |
| 87 |
{ |
| 88 |
return -1; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/* test the nil expression */ |
| 93 |
try |
| 94 |
{ |
| 95 |
Term nilExpr = slv.getValueSepNil(); |
| 96 |
} |
| 97 |
catch (CVC5ApiException e) |
| 98 |
{ |
| 99 |
caughtOnNil = true; |
| 100 |
if (!e.getMessage().equals(expected)) |
| 101 |
{ |
| 102 |
return -1; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if (!caughtOnHeap || !caughtOnNil) |
| 107 |
{ |
| 108 |
return -1; |
| 109 |
} |
| 110 |
|
| 111 |
/* All tests pass! */ |
| 112 |
return 0; |
| 113 |
} |
| 114 |
catch (CVC5ApiException e) |
| 115 |
{ |
| 116 |
e.printStackTrace(); |
| 117 |
return -1; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Demonstrate using separation logic and obtaining heap/nil expressions. |
| 123 |
*/ |
| 124 |
public static int validateGetters() |
| 125 |
{ |
| 126 |
try |
| 127 |
{ |
| 128 |
TermManager tm = new TermManager(); |
| 129 |
Solver slv = new Solver(tm); |
| 130 |
|
| 131 |
/* Setup some options for cvc5 */ |
| 132 |
slv.setLogic("QF_ALL"); |
| 133 |
slv.setOption("produce-models", "true"); |
| 134 |
slv.setOption("incremental", "false"); |
| 135 |
|
| 136 |
/* Our integer type */ |
| 137 |
Sort integer = tm.getIntegerSort(); |
| 138 |
|
| 139 |
/* Declare the separation logic heap types */ |
| 140 |
slv.declareSepHeap(integer, integer); |
| 141 |
|
| 142 |
/* A "random" constant */ |
| 143 |
Term randomConstant = tm.mkInteger(0xDEADBEEF); |
| 144 |
|
| 145 |
/* Another random constant */ |
| 146 |
Term exprNilVal = tm.mkInteger(0xFBADBEEF); |
| 147 |
|
| 148 |
/* Our nil term */ |
| 149 |
Term nil = tm.mkSepNil(integer); |
| 150 |
|
| 151 |
/* Our SMT constants */ |
| 152 |
Term x = tm.mkConst(integer, "x"); |
| 153 |
Term y = tm.mkConst(integer, "y"); |
| 154 |
Term p1 = tm.mkConst(integer, "p1"); |
| 155 |
Term p2 = tm.mkConst(integer, "p2"); |
| 156 |
|
| 157 |
/* Constraints on x and y */ |
| 158 |
Term x_equal_const = tm.mkTerm(Kind.EQUAL, x, randomConstant); |
| 159 |
Term y_gt_x = tm.mkTerm(Kind.GT, y, x); |
| 160 |
|
| 161 |
/* Points-to expressions */ |
| 162 |
Term p1_to_x = tm.mkTerm(Kind.SEP_PTO, p1, x); |
| 163 |
Term p2_to_y = tm.mkTerm(Kind.SEP_PTO, p2, y); |
| 164 |
|
| 165 |
/* Heap -- the points-to have to be "starred"! */ |
| 166 |
Term heap = tm.mkTerm(Kind.SEP_STAR, p1_to_x, p2_to_y); |
| 167 |
|
| 168 |
/* Constain "nil" to be something random */ |
| 169 |
Term fix_nil = tm.mkTerm(Kind.EQUAL, nil, exprNilVal); |
| 170 |
|
| 171 |
/* Add it all to the solver! */ |
| 172 |
slv.assertFormula(x_equal_const); |
| 173 |
slv.assertFormula(y_gt_x); |
| 174 |
slv.assertFormula(heap); |
| 175 |
slv.assertFormula(fix_nil); |
| 176 |
|
| 177 |
/* |
| 178 |
* Incremental is disabled due to using separation logic, so don't query |
| 179 |
* twice! |
| 180 |
*/ |
| 181 |
Result r = slv.checkSat(); |
| 182 |
|
| 183 |
/* If this is UNSAT, we have an issue; so bail-out */ |
| 184 |
if (!r.isSat()) |
| 185 |
{ |
| 186 |
return -1; |
| 187 |
} |
| 188 |
|
| 189 |
/* Obtain our separation logic terms from the solver */ |
| 190 |
Term heapExpr = slv.getValueSepHeap(); |
| 191 |
Term nilExpr = slv.getValueSepNil(); |
| 192 |
|
| 193 |
/* If the heap is not a separating conjunction, bail-out */ |
| 194 |
if (heapExpr.getKind() != Kind.SEP_STAR) |
| 195 |
{ |
| 196 |
return -1; |
| 197 |
} |
| 198 |
|
| 199 |
/* If nil is not a direct equality, bail-out */ |
| 200 |
if (nilExpr.getKind() != Kind.EQUAL) |
| 201 |
{ |
| 202 |
return -1; |
| 203 |
} |
| 204 |
|
| 205 |
/* Obtain the values for our "pointers" */ |
| 206 |
Term valForP1 = slv.getValue(p1); |
| 207 |
Term valForP2 = slv.getValue(p2); |
| 208 |
|
| 209 |
/* We need to make sure we find both pointers in the heap */ |
| 210 |
boolean checkedP1 = false; |
| 211 |
boolean checkedP2 = false; |
| 212 |
|
| 213 |
/* Walk all the children */ |
| 214 |
for (Term child : heapExpr) |
| 215 |
{ |
| 216 |
/* If we don't have a PTO operator, bail-out */ |
| 217 |
if (child.getKind() != Kind.SEP_PTO) |
| 218 |
{ |
| 219 |
return -1; |
| 220 |
} |
| 221 |
|
| 222 |
/* Find both sides of the PTO operator */ |
| 223 |
Term addr = slv.getValue(child.getChild(0)); |
| 224 |
Term value = slv.getValue(child.getChild(1)); |
| 225 |
|
| 226 |
/* If the current address is the value for p1 */ |
| 227 |
if (addr.equals(valForP1)) |
| 228 |
{ |
| 229 |
checkedP1 = true; |
| 230 |
|
| 231 |
/* If it doesn't match the random constant, we have a problem */ |
| 232 |
if (!value.equals(randomConstant)) |
| 233 |
{ |
| 234 |
return -1; |
| 235 |
} |
| 236 |
continue; |
| 237 |
} |
| 238 |
|
| 239 |
/* If the current address is the value for p2 */ |
| 240 |
if (addr.equals(valForP2)) |
| 241 |
{ |
| 242 |
checkedP2 = true; |
| 243 |
/* |
| 244 |
* Our earlier constraint was that what p2 points to must be *greater* |
| 245 |
* than the random constant -- if we get a value that is LTE, then |
| 246 |
* something has gone wrong! |
| 247 |
*/ |
| 248 |
if (value.getIntegerValue().compareTo(randomConstant.getIntegerValue()) <= 0) |
| 249 |
{ |
| 250 |
return -1; |
| 251 |
} |
| 252 |
continue; |
| 253 |
} |
| 254 |
|
| 255 |
/* |
| 256 |
* We should only have two addresses in heap, so if we haven't hit the |
| 257 |
* "continue" for p1 or p2, then bail-out |
| 258 |
*/ |
| 259 |
return -1; |
| 260 |
} |
| 261 |
|
| 262 |
/* |
| 263 |
* If we complete the loop and we haven't validated both p1 and p2, then we |
| 264 |
* have a problem |
| 265 |
*/ |
| 266 |
if (!checkedP1 || !checkedP2) |
| 267 |
{ |
| 268 |
return -1; |
| 269 |
} |
| 270 |
|
| 271 |
/* We now get our value for what nil is */ |
| 272 |
Term valueForNil = slv.getValue(nilExpr.getChild(1)); |
| 273 |
/* |
| 274 |
* The value for nil from the solver should be the value we originally tied |
| 275 |
* nil to |
| 276 |
*/ |
| 277 |
if (!valueForNil.equals(exprNilVal)) |
| 278 |
{ |
| 279 |
return -1; |
| 280 |
} |
| 281 |
/* All tests pass! */ |
| 282 |
return 0; |
| 283 |
} |
| 284 |
catch (CVC5ApiException e) |
| 285 |
{ |
| 286 |
e.printStackTrace(); |
| 287 |
return -1; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
public static void main(String[] args) |
| 292 |
{ |
| 293 |
/* check that we get an exception when we should */ |
| 294 |
int checkException = validateException(); |
| 295 |
if (checkException != 0) |
| 296 |
{ |
| 297 |
System.exit(checkException); |
| 298 |
} |
| 299 |
|
| 300 |
/* check the getters */ |
| 301 |
int checkGetters = validateGetters(); |
| 302 |
if (checkGetters != 0) |
| 303 |
{ |
| 304 |
System.exit(checkGetters); |
| 305 |
} |
| 306 |
|
| 307 |
System.exit(0); |
| 308 |
} |
| 309 |
} |