def instantiateAndEval(inv, vals, var_names = None, const_names = None): if (var_names == None): var_names = ["_sv_x", "_sv_y", "_sv_z"] if (const_names == None): const_names = ["_sc_a", "_sc_b", "_sc_c"] res = [] symVs = [ x for x in expr_read(inv) if x in var_names ] symConsts = [ x for x in expr_read(inv) if x in const_names ] nonSymVs = set(expr_read(inv)).difference(set(symVs))\ .difference(set(symConsts)) traceVs = vals[0].keys() prms = permutations(range(len(traceVs)), len(symVs)) typeEnv = { str(x) + str(i) : Int for x in vals[0].keys() for i in xrange(len(vals)) } typeEnv.update({ str(c) : Int for c in symConsts }) for prm in prms: varM = { symVs[i]: traceVs[prm[i]] for i in xrange(len(symVs)) } varM.update({ nonSymV: nonSymV for nonSymV in nonSymVs }) inst_inv = replace(inv, { AstId(x) : AstId(varM[x]) for x in symVs }) p = [ AstAssume(env_to_expr(x, str(i))) for (i,x) in enumerate(vals) ] p += [ AstAssert(replace(inst_inv, { AstId(x) : AstId(x + str(i)) for x in varM.values() })) for i in xrange(len(vals)) ] m = maybeModel(And(map(lambda s: stmt_to_z3(s, typeEnv), p))) if (m): const_vals = { AstId(x) : AstNumber(m[x]) for x in symConsts } res.append(replace(inst_inv, const_vals)) return res
def wp_stmt(stmt, pred, typeEnv): if (isinstance(stmt, AstLabel)): stmt = stmt.stmt if (isinstance(stmt, AstAssignment)): assignee = str(stmt.lhs) # Should already be SSA-ed assert(assignee not in expr_read(stmt.rhs)) lhs = typeEnv[stmt.lhs](assignee) rhs = expr_to_z3(stmt.rhs, typeEnv) return z3.substitute(pred, (lhs, rhs)) elif (isinstance(stmt, AstAssert)): return And(pred, expr_to_z3(stmt.expr, typeEnv)) elif (isinstance(stmt, AstAssume)): return Implies(expr_to_z3(stmt.expr, typeEnv), pred) else: raise Exception("Cannot handle Boogie Statement: " + str(stmt))
def sp_stmt(stmt, pred, typeEnv): if (isinstance(stmt, AstLabel)): stmt = stmt.stmt if (isinstance(stmt, AstAssignment)): assignee = str(stmt.lhs) # Should already be SSA-ed assert(assignee not in expr_read(stmt.rhs) and \ (assignee not in map(str, ids(pred)))) lhs = typeEnv[stmt.lhs](assignee) rhs = expr_to_z3(stmt.rhs, typeEnv) return And(lhs == rhs, pred) elif (isinstance(stmt, AstAssert)): return And(pred, expr_to_z3(stmt.expr, typeEnv)) elif (isinstance(stmt, AstAssume)): return And(pred, expr_to_z3(stmt.expr, typeEnv)) else: raise Exception("Cannot handle Boogie Statement: " + str(stmt))
def assignment_preds(stmt): return set([AstBinExpr(stmt.lhs, "==", stmt.rhs)]) \ if str(stmt.lhs) not in expr_read(stmt.rhs) else set()
def untouched(preds, clobbered_vars): return set([x for x in preds \ if len(clobbered_vars.intersection(expr_read(x))) == 0])