def get_models(F, M): result = [] s = z3.Solver() s.add(F) print(s) while len(result) < M and s.check() == z3.sat: m = s.model() result.append(m) # Create a new constraint the blocks the current model block = [] for d in m: # XXX: hack. Should not be possible to change f without R,C changes... if str(d).startswith('f_'): continue # d is a declaration if d.arity() > 0: raise z3.Z3Exception( "uninterpreted functions are not supported") # create a constant from declaration c = d() if z3.is_array(c) or c.sort().kind() == z3.Z3_UNINTERPRETED_SORT: raise z3.Z3Exception( "arrays and uninterpreted sorts are not supported") block.append(c != m[d]) s.add(z3.Or(block)) return result
def _z3_decl_name_str(ctx, decl): # reimplementation of Z3_get_symbol_string to not try to unicode-decode lib = z3.lib() decl_name = lib.Z3_get_decl_name(ctx, decl) err = lib.Z3_get_error_code(ctx) if err != z3.Z3_OK: raise z3.Z3Exception(lib.Z3_get_error_msg(ctx, err)) symbol_name = lib.Z3_get_symbol_string(ctx, decl_name) err = lib.Z3_get_error_code(ctx) if err != z3.Z3_OK: raise z3.Z3Exception(z3.lib().Z3_get_error_msg(ctx, err)) return symbol_name
def main(): x = z3.BitVec('x', 8) y = z3.BitVec('y', 8) s = z3.Solver() s.add(((x << 2) + (3 * y)) == z3.BitVecVal(0x41, 8)) s.add(((x >> 4) - (y << 2)) > z3.BitVecVal(0x10, 8)) s.add(((x * 5) - (y * 2)) > z3.BitVecVal(0x42, 8)) s.add(x != z3.BitVecVal(0, 8)) s.add(y != z3.BitVecVal(0, 8)) while s.check() == z3.sat: # Get a model satisfying the rules. model = s.model() # Do something with the model print model # Prevent the same model from showing up again. block = [] for instance in model: if instance.arity() > 0: raise z3.Z3Exception( "uninterpreted functions are not supported") constant = instance() if z3.is_array(constant) or constant.sort().kind( ) == z3.Z3_UNINTERPRETED_SORT: raise Z3Exception( "arrays and uninterpreted sorts are not supported") block.append(constant == model[instance]) inverted_block = z3.Not(z3.And(block)) s.add(inverted_block)
def check_sat(solver: z3.Solver, pop_if_exception: bool = True) -> z3.CheckSatResult: try: ret = solver.check() if ret == z3.unknown: raise z3.Z3Exception(solver.reason_unknown()) except Exception as e: if pop_if_exception: solver.pop() raise e return ret
def get_models(F, M): result = [] s = z3.Solver() s.add(F) while len(result) < M and s.check() == z3.sat: m = s.model() result.append(m) # Create a new application the blocks the current model block = [] for d in m: # d is a declaration if d.arity() > 0: raise z3.Z3Exception("uninterpreted functions are not supported") # create a constant from declaration c = d() if z3.is_array(c) or c.sort().kind() == z3.Z3_UNINTERPRETED_SORT: raise z3.Z3Exception("arrays and uninterpreted sorts are not supported") block.append(c != m[d]) s.add(z3.Or(block)) return(result)