| 1 |
############################################################################### |
| 2 |
# Top contributors (to current version): |
| 3 |
# Yoni Zohar, Aina Niemetz, Alex Ozdemir |
| 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 |
import cvc5 |
| 23 |
from cvc5 import Kind |
| 24 |
|
| 25 |
|
| 26 |
# Test function to validate that we *cannot* obtain the heap/nil expressions |
| 27 |
# when *not* using the separation logic theory |
| 28 |
def validate_exception(): |
| 29 |
tm = cvc5.TermManager() |
| 30 |
slv = cvc5.Solver(tm) |
| 31 |
# Setup some options for cvc5 -- we explictly want to use a simplistic |
| 32 |
# theory (e.g., QF_IDL) |
| 33 |
slv.setLogic("QF_IDL") |
| 34 |
slv.setOption("produce-models", "true") |
| 35 |
slv.setOption("incremental", "false") |
| 36 |
|
| 37 |
# Our integer type |
| 38 |
integer = tm.getIntegerSort() |
| 39 |
|
| 40 |
# we intentionally do not set the separation logic heap |
| 41 |
|
| 42 |
# Our SMT constants |
| 43 |
x = tm.mkConst(integer, "x") |
| 44 |
y = tm.mkConst(integer, "y") |
| 45 |
|
| 46 |
# y > x |
| 47 |
y_gt_x = tm.mkTerm(Kind.GT, y, x) |
| 48 |
|
| 49 |
# assert it |
| 50 |
slv.assertFormula(y_gt_x) |
| 51 |
|
| 52 |
# check |
| 53 |
r = slv.checkSat() |
| 54 |
|
| 55 |
# If this is UNSAT, we have an issue so bail-out |
| 56 |
if not r.isSat(): |
| 57 |
return False |
| 58 |
|
| 59 |
# We now try to obtain our separation logic expressions from the solver -- |
| 60 |
# we want to validate that we get our expected exceptions. |
| 61 |
|
| 62 |
caught_on_heap = False |
| 63 |
caught_on_nil = False |
| 64 |
|
| 65 |
# The exception message we expect to obtain |
| 66 |
expected = \ |
| 67 |
"cannot obtain separation logic expressions if not using the separation " \ |
| 68 |
"logic theory." |
| 69 |
|
| 70 |
# test the heap expression |
| 71 |
try: |
| 72 |
heap_expr = slv.getValueSepHeap() |
| 73 |
except RuntimeError as e: |
| 74 |
caught_on_heap = True |
| 75 |
# Check we get the correct exception string |
| 76 |
if str(e) != expected: |
| 77 |
return False |
| 78 |
|
| 79 |
# test the nil expression |
| 80 |
try: |
| 81 |
nil_expr = slv.getValueSepNil() |
| 82 |
except RuntimeError as e: |
| 83 |
caught_on_nil = True |
| 84 |
|
| 85 |
# Check we get the correct exception string |
| 86 |
if str(e) != expected: |
| 87 |
return False |
| 88 |
|
| 89 |
if not caught_on_heap or not caught_on_nil: |
| 90 |
return False |
| 91 |
|
| 92 |
# All tests pass! |
| 93 |
return True |
| 94 |
|
| 95 |
|
| 96 |
# Test function to demonstrate the use of, and validate the capability, of |
| 97 |
# obtaining the heap/nil expressions when using separation logic. |
| 98 |
def validate_getters(): |
| 99 |
tm = cvc5.TermManager() |
| 100 |
slv = cvc5.Solver(tm) |
| 101 |
|
| 102 |
# Setup some options for cvc5 |
| 103 |
slv.setLogic("QF_ALL") |
| 104 |
slv.setOption("produce-models", "true") |
| 105 |
slv.setOption("incremental", "false") |
| 106 |
|
| 107 |
# Our integer type |
| 108 |
integer = tm.getIntegerSort() |
| 109 |
|
| 110 |
#* Declare the separation logic heap types |
| 111 |
slv.declareSepHeap(integer, integer) |
| 112 |
|
| 113 |
# A "random" constant |
| 114 |
random_constant = tm.mkInteger(0xDEAD) |
| 115 |
|
| 116 |
# Another random constant |
| 117 |
expr_nil_val = tm.mkInteger(0xFBAD) |
| 118 |
|
| 119 |
# Our nil term |
| 120 |
nil = tm.mkSepNil(integer) |
| 121 |
|
| 122 |
# Our SMT constants |
| 123 |
x = tm.mkConst(integer, "x") |
| 124 |
y = tm.mkConst(integer, "y") |
| 125 |
p1 = tm.mkConst(integer, "p1") |
| 126 |
p2 = tm.mkConst(integer, "p2") |
| 127 |
|
| 128 |
# Constraints on x and y |
| 129 |
x_equal_const = tm.mkTerm(Kind.EQUAL, x, random_constant) |
| 130 |
y_gt_x = tm.mkTerm(Kind.GT, y, x) |
| 131 |
|
| 132 |
# Points-to expressions |
| 133 |
p1_to_x = tm.mkTerm(Kind.SEP_PTO, p1, x) |
| 134 |
p2_to_y = tm.mkTerm(Kind.SEP_PTO, p2, y) |
| 135 |
|
| 136 |
# Heap -- the points-to have to be "starred"! |
| 137 |
heap = tm.mkTerm(Kind.SEP_STAR, p1_to_x, p2_to_y) |
| 138 |
|
| 139 |
# Constain "nil" to be something random |
| 140 |
fix_nil = tm.mkTerm(Kind.EQUAL, nil, expr_nil_val) |
| 141 |
|
| 142 |
# Add it all to the solver! |
| 143 |
slv.assertFormula(x_equal_const) |
| 144 |
slv.assertFormula(y_gt_x) |
| 145 |
slv.assertFormula(heap) |
| 146 |
slv.assertFormula(fix_nil) |
| 147 |
|
| 148 |
# Incremental is disabled due to using separation logic, so don't query |
| 149 |
# twice! |
| 150 |
|
| 151 |
r = (slv.checkSat()) |
| 152 |
|
| 153 |
# If this is UNSAT, we have an issue so bail-out |
| 154 |
if not r.isSat(): |
| 155 |
return False |
| 156 |
|
| 157 |
# Obtain our separation logic terms from the solver |
| 158 |
heap_expr = slv.getValueSepHeap() |
| 159 |
nil_expr = slv.getValueSepNil() |
| 160 |
|
| 161 |
# If the heap is not a separating conjunction, bail-out |
| 162 |
if (heap_expr.getKind() != Kind.SEP_STAR): |
| 163 |
return False |
| 164 |
|
| 165 |
# If nil is not a direct equality, bail-out |
| 166 |
if (nil_expr.getKind() != Kind.EQUAL): |
| 167 |
return False |
| 168 |
|
| 169 |
# Obtain the values for our "pointers" |
| 170 |
val_for_p1 = slv.getValue(p1) |
| 171 |
val_for_p2 = slv.getValue(p2) |
| 172 |
|
| 173 |
# We need to make sure we find both pointers in the heap |
| 174 |
checked_p1 = False |
| 175 |
checked_p2 = False |
| 176 |
|
| 177 |
# Walk all the children |
| 178 |
for child in heap_expr: |
| 179 |
# If we don't have a PTO operator, bail-out |
| 180 |
if (child.getKind() != Kind.SEP_PTO): |
| 181 |
return False |
| 182 |
|
| 183 |
# Find both sides of the PTO operator |
| 184 |
addr = slv.getValue(child[0]) |
| 185 |
value = slv.getValue(child[1]) |
| 186 |
|
| 187 |
# If the current address is the value for p1 |
| 188 |
if (addr == val_for_p1): |
| 189 |
checked_p1 = True |
| 190 |
|
| 191 |
# If it doesn't match the random constant, we have a problem |
| 192 |
if value != random_constant: |
| 193 |
return False |
| 194 |
continue |
| 195 |
|
| 196 |
if (addr == val_for_p2): |
| 197 |
checked_p2 = True |
| 198 |
|
| 199 |
# Our earlier constraint was that what p2 points to must be *greater* |
| 200 |
# than the random constant -- if we get a value that is LTE, then |
| 201 |
# something has gone wrong! |
| 202 |
|
| 203 |
if int(str(value)) <= int(str(random_constant)): |
| 204 |
return False |
| 205 |
continue |
| 206 |
|
| 207 |
# We should only have two addresses in heap, so if we haven't hit the |
| 208 |
# "continue" for p1 or p2, then bail-out |
| 209 |
|
| 210 |
return True |
| 211 |
|
| 212 |
# If we complete the loop and we haven't validated both p1 and p2, then we |
| 213 |
# have a problem |
| 214 |
if (not checked_p1 or not checked_p2): |
| 215 |
return False |
| 216 |
|
| 217 |
# We now get our value for what nil is |
| 218 |
value_for_nil = slv.getValue(nil_expr[1]) |
| 219 |
|
| 220 |
# The value for nil from the solver should be the value we originally tied |
| 221 |
# nil to |
| 222 |
|
| 223 |
if (value_for_nil != expr_nil_val): |
| 224 |
return False |
| 225 |
|
| 226 |
# All tests pass! |
| 227 |
return True |
| 228 |
|
| 229 |
|
| 230 |
# check that we get an exception when we should |
| 231 |
assert validate_exception() |
| 232 |
|
| 233 |
# check the getters |
| 234 |
assert validate_getters() |