JNIEXPORT jint JNICALL Java_org_joogie_prover_old_Z3ProverEx_check(JNIEnv * env, jclass clazz, jstring string) { Z3_string formula = (*env)->GetStringUTFChars(env, string, NULL); Z3_ast ast = Z3_parse_smtlib2_string(context, formula, 0, 0, 0, 0, 0, 0); (*env)->ReleaseStringUTFChars(env, string, formula); if (Z3_OK != Z3_get_error_code(context)) return Z3_L_UNDEF; Z3_assert_cnstr(context, ast); Z3_check_and_get_model(context, &model); return (NULL == model) ? Z3_L_FALSE : Z3_L_TRUE; }
std::list< std::pair<std::string, unsigned long long> > SolverEngine::getModel(std::string expr) { std::list< std::pair<std::string, unsigned long long> > ret; std::stringstream formula; z3::check_result checkResult; z3::context *ctx; z3::solver *solver; /* First, set the QF_AUFBV flag */ formula << smt2lib::init(); /* Then, delcare all symbolic variables */ formula << this->symEngine->getSmt2LibVarsDecl(); /* And concat the user expression */ formula << expr; /* Create the context and AST */ ctx = new z3::context(); Z3_ast ast = Z3_parse_smtlib2_string(*ctx, formula.str().c_str(), 0, 0, 0, 0, 0, 0); z3::expr eq(*ctx, ast); /* Create a solver and add the expression */ solver = new z3::solver(*ctx); solver->add(eq); /* Check */ checkResult = solver->check(); /* Check if it is sat */ if (checkResult == z3::sat){ /* Get model */ z3::model m = solver->get_model(); /* Traversing the model */ for (unsigned i = 0; i < m.size(); i++) { unsigned long long value = 0; z3::func_decl v = m[i]; Z3_get_numeral_uint64(*ctx, m.get_const_interp(v), &value); ret.push_back(make_pair(v.name().str(), value)); } } delete solver; return ret; }
std::list<std::map<triton::uint32, SolverModel>> SolverEngine::getModels(triton::ast::AbstractNode* node, triton::uint32 limit) const { std::list<std::map<triton::uint32, SolverModel>> ret; std::ostringstream formula; z3::context ctx; z3::solver solver(ctx); triton::uint32 representationMode = triton::ast::representations::astRepresentation.getMode(); if (node == nullptr) throw triton::exceptions::SolverEngine("SolverEngine::getModels(): node cannot be null."); /* Switch into the SMT mode */ triton::ast::representations::astRepresentation.setMode(triton::ast::representations::SMT_REPRESENTATION); /* First, set the QF_AUFBV flag */ formula << "(set-logic QF_BV)"; /* Then, delcare all symbolic variables */ formula << this->symbolicEngine->getVariablesDeclaration(); /* And concat the user expression */ formula << this->symbolicEngine->getFullAst(node); /* Create the context and AST */ Z3_ast ast = Z3_parse_smtlib2_string(ctx, formula.str().c_str(), 0, 0, 0, 0, 0, 0); z3::expr eq(ctx, ast); /* Create a solver and add the expression */ solver.add(eq); /* Check if it is sat */ while (solver.check() == z3::sat && limit >= 1) { /* Get model */ z3::model m = solver.get_model(); /* Traversing the model */ std::map<triton::uint32, SolverModel> smodel; z3::expr_vector args(ctx); for (triton::uint32 i = 0; i < m.size(); i++) { /* Get the z3 variable */ z3::func_decl z3Variable = m[i]; /* Get the name as std::string from a z3 variable */ std::string varName = z3Variable.name().str(); /* Get z3 expr */ z3::expr exp = m.get_const_interp(z3Variable); /* Get the size of a z3 expr */ triton::uint32 bvSize = exp.get_sort().bv_size(); /* Get the value of a z3 expr */ std::string svalue = Z3_get_numeral_string(ctx, exp); /* Convert a string value to a integer value */ triton::uint512 value = triton::uint512(svalue); /* Create a triton model */ SolverModel trionModel = SolverModel(varName, value); /* Map the result */ smodel[trionModel.getId()] = trionModel; /* Uniq result */ if (exp.get_sort().is_bv()) args.push_back(ctx.bv_const(varName.c_str(), bvSize) != ctx.bv_val(svalue.c_str(), bvSize)); } /* Escape last models */ solver.add(triton::engines::solver::mk_or(args)); /* If there is model available */ if (smodel.size() > 0) ret.push_back(smodel); /* Decrement the limit */ limit--; } /* Restore the representation mode */ triton::ast::representations::astRepresentation.setMode(representationMode); return ret; }