This commit is contained in:
parent
d12732adfb
commit
db386b2a7f
|
@ -1029,10 +1029,16 @@ std::string ExprEngine::IntegerTruncation::getSymbolicExpression() const
|
|||
|
||||
#ifdef USE_Z3
|
||||
|
||||
struct ExprData {
|
||||
class ExprData {
|
||||
public:
|
||||
typedef std::map<std::string, z3::expr> ValueExpr;
|
||||
typedef std::vector<z3::expr> AssertionList;
|
||||
|
||||
class BailoutValueException {
|
||||
public:
|
||||
BailoutValueException() {}
|
||||
};
|
||||
|
||||
z3::context context;
|
||||
ValueExpr valueExpr;
|
||||
AssertionList assertionList;
|
||||
|
@ -1042,6 +1048,15 @@ struct ExprData {
|
|||
solver.add(assertExpr);
|
||||
}
|
||||
|
||||
void addConstraints(z3::solver &solver, const Data* data) {
|
||||
for (auto constraint : data->constraints) {
|
||||
try {
|
||||
solver.add(getConstraintExpr(constraint));
|
||||
} catch (const BailoutValueException &) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
z3::expr addInt(const std::string &name, int128_t minValue, int128_t maxValue) {
|
||||
z3::expr e = context.int_const(name.c_str());
|
||||
valueExpr.emplace(name, e);
|
||||
|
@ -1108,6 +1123,8 @@ struct ExprData {
|
|||
z3::expr getExpr(ExprEngine::ValuePtr v) {
|
||||
if (!v)
|
||||
throw ExprEngineException(nullptr, "Can not solve expressions, operand value is null");
|
||||
if (v->type == ExprEngine::ValueType::BailoutValue)
|
||||
throw BailoutValueException();
|
||||
if (auto intRange = std::dynamic_pointer_cast<ExprEngine::IntRange>(v)) {
|
||||
if (intRange->name[0] != '$')
|
||||
#if Z3_VERSION_INT >= GET_VERSION_INT(4,7,1)
|
||||
|
@ -1162,7 +1179,6 @@ struct ExprData {
|
|||
}
|
||||
|
||||
private:
|
||||
|
||||
z3::expr bool_expr(z3::expr e) {
|
||||
if (e.is_bool())
|
||||
return e;
|
||||
|
@ -1191,8 +1207,7 @@ bool ExprEngine::IntRange::isEqual(DataBase *dataBase, int value) const
|
|||
z3::solver solver(exprData.context);
|
||||
try {
|
||||
z3::expr e = exprData.addInt(name, minValue, maxValue);
|
||||
for (auto constraint : dynamic_cast<const Data *>(dataBase)->constraints)
|
||||
solver.add(exprData.getConstraintExpr(constraint));
|
||||
exprData.addConstraints(solver, data);
|
||||
exprData.addAssertions(solver);
|
||||
solver.add(e == value);
|
||||
return solver.check() == z3::sat;
|
||||
|
@ -1220,8 +1235,7 @@ bool ExprEngine::IntRange::isGreaterThan(DataBase *dataBase, int value) const
|
|||
z3::solver solver(exprData.context);
|
||||
try {
|
||||
z3::expr e = exprData.addInt(name, minValue, maxValue);
|
||||
for (auto constraint : dynamic_cast<const Data *>(dataBase)->constraints)
|
||||
solver.add(exprData.getConstraintExpr(constraint));
|
||||
exprData.addConstraints(solver, data);
|
||||
exprData.addAssertions(solver);
|
||||
solver.add(e > value);
|
||||
return solver.check() == z3::sat;
|
||||
|
@ -1249,8 +1263,7 @@ bool ExprEngine::IntRange::isLessThan(DataBase *dataBase, int value) const
|
|||
z3::solver solver(exprData.context);
|
||||
try {
|
||||
z3::expr e = exprData.addInt(name, minValue, maxValue);
|
||||
for (auto constraint : dynamic_cast<const Data *>(dataBase)->constraints)
|
||||
solver.add(exprData.getConstraintExpr(constraint));
|
||||
exprData.addConstraints(solver, data);
|
||||
exprData.addAssertions(solver);
|
||||
solver.add(e < value);
|
||||
return solver.check() == z3::sat;
|
||||
|
@ -1279,8 +1292,7 @@ bool ExprEngine::FloatRange::isEqual(DataBase *dataBase, int value) const
|
|||
z3::solver solver(exprData.context);
|
||||
try {
|
||||
z3::expr e = exprData.addFloat(name);
|
||||
for (auto constraint : dynamic_cast<const Data *>(dataBase)->constraints)
|
||||
solver.add(exprData.getConstraintExpr(constraint));
|
||||
exprData.addConstraints(solver, data);
|
||||
exprData.addAssertions(solver);
|
||||
solver.add(e >= value && e <= value);
|
||||
return solver.check() != z3::unsat;
|
||||
|
@ -1310,8 +1322,7 @@ bool ExprEngine::FloatRange::isGreaterThan(DataBase *dataBase, int value) const
|
|||
z3::solver solver(exprData.context);
|
||||
try {
|
||||
z3::expr e = exprData.addFloat(name);
|
||||
for (auto constraint : dynamic_cast<const Data *>(dataBase)->constraints)
|
||||
solver.add(exprData.getConstraintExpr(constraint));
|
||||
exprData.addConstraints(solver, data);
|
||||
exprData.addAssertions(solver);
|
||||
solver.add(e > value);
|
||||
return solver.check() == z3::sat;
|
||||
|
@ -1341,8 +1352,7 @@ bool ExprEngine::FloatRange::isLessThan(DataBase *dataBase, int value) const
|
|||
z3::solver solver(exprData.context);
|
||||
try {
|
||||
z3::expr e = exprData.addFloat(name);
|
||||
for (auto constraint : dynamic_cast<const Data *>(dataBase)->constraints)
|
||||
solver.add(exprData.getConstraintExpr(constraint));
|
||||
exprData.addConstraints(solver, data);
|
||||
exprData.addAssertions(solver);
|
||||
solver.add(e < value);
|
||||
return solver.check() == z3::sat;
|
||||
|
@ -1363,8 +1373,7 @@ bool ExprEngine::BinOpResult::isEqual(ExprEngine::DataBase *dataBase, int value)
|
|||
ExprData exprData;
|
||||
z3::solver solver(exprData.context);
|
||||
z3::expr e = exprData.getExpr(this);
|
||||
for (auto constraint : dynamic_cast<const Data *>(dataBase)->constraints)
|
||||
solver.add(exprData.getConstraintExpr(constraint));
|
||||
exprData.addConstraints(solver, dynamic_cast<const Data *>(dataBase));
|
||||
exprData.addAssertions(solver);
|
||||
solver.add(e == value);
|
||||
return solver.check() == z3::sat;
|
||||
|
@ -1382,8 +1391,7 @@ bool ExprEngine::BinOpResult::isGreaterThan(ExprEngine::DataBase *dataBase, int
|
|||
ExprData exprData;
|
||||
z3::solver solver(exprData.context);
|
||||
z3::expr e = exprData.getExpr(this);
|
||||
for (auto constraint : dynamic_cast<const Data *>(dataBase)->constraints)
|
||||
solver.add(exprData.getConstraintExpr(constraint));
|
||||
exprData.addConstraints(solver, dynamic_cast<const Data *>(dataBase));
|
||||
exprData.addAssertions(solver);
|
||||
solver.add(e > value);
|
||||
return solver.check() == z3::sat;
|
||||
|
@ -1405,8 +1413,7 @@ bool ExprEngine::BinOpResult::isLessThan(ExprEngine::DataBase *dataBase, int val
|
|||
ExprData exprData;
|
||||
z3::solver solver(exprData.context);
|
||||
z3::expr e = exprData.getExpr(this);
|
||||
for (auto constraint : dynamic_cast<const Data *>(dataBase)->constraints)
|
||||
solver.add(exprData.getConstraintExpr(constraint));
|
||||
exprData.addConstraints(solver, dynamic_cast<const Data *>(dataBase));
|
||||
exprData.addAssertions(solver);
|
||||
solver.add(e < value);
|
||||
return solver.check() == z3::sat;
|
||||
|
@ -1428,8 +1435,7 @@ bool ExprEngine::BinOpResult::isTrue(ExprEngine::DataBase *dataBase) const
|
|||
ExprData exprData;
|
||||
z3::solver solver(exprData.context);
|
||||
z3::expr e = exprData.getExpr(this);
|
||||
for (auto constraint : dynamic_cast<const Data *>(dataBase)->constraints)
|
||||
solver.add(exprData.getConstraintExpr(constraint));
|
||||
exprData.addConstraints(solver, dynamic_cast<const Data *>(dataBase));
|
||||
exprData.addAssertions(solver);
|
||||
return solver.check() == z3::sat;
|
||||
} catch (const z3::exception &exception) {
|
||||
|
@ -1449,8 +1455,7 @@ std::string ExprEngine::BinOpResult::getExpr(ExprEngine::DataBase *dataBase) con
|
|||
ExprData exprData;
|
||||
z3::solver solver(exprData.context);
|
||||
z3::expr e = exprData.getExpr(this);
|
||||
for (auto constraint : dynamic_cast<const Data *>(dataBase)->constraints)
|
||||
solver.add(exprData.getConstraintExpr(constraint));
|
||||
exprData.addConstraints(solver, dynamic_cast<const Data *>(dataBase));
|
||||
exprData.addAssertions(solver);
|
||||
solver.add(e);
|
||||
std::ostringstream os;
|
||||
|
@ -1767,7 +1772,11 @@ static void checkContract(Data &data, const Token *tok, const Function *function
|
|||
z3::solver solver(exprData.context);
|
||||
try {
|
||||
// Invert contract, we want to know if the contract might not be met
|
||||
solver.add(z3::ite(exprData.getConstraintExpr(data.executeContract(function, executeExpression1)), exprData.context.bool_val(false), exprData.context.bool_val(true)));
|
||||
try {
|
||||
solver.add(z3::ite(exprData.getConstraintExpr(data.executeContract(function, executeExpression1)), exprData.context.bool_val(false), exprData.context.bool_val(true)));
|
||||
} catch (const ExprData::BailoutValueException &) {
|
||||
throw ExprEngineException(tok, "Internal error: Bailout value used");
|
||||
}
|
||||
|
||||
bool bailoutValue = false;
|
||||
for (nonneg int i = 0; i < argValues.size(); ++i) {
|
||||
|
@ -1814,7 +1823,7 @@ static void checkContract(Data &data, const Token *tok, const Function *function
|
|||
}
|
||||
} catch (const z3::exception &exception) {
|
||||
std::cerr << "z3: " << exception << std::endl;
|
||||
} catch (const ExprEngineException &e) {
|
||||
} catch (const ExprEngineException &) {
|
||||
const char id[] = "internalErrorInExprEngine";
|
||||
const auto contractIt = data.settings->functionContracts.find(function->fullName());
|
||||
const std::string functionName = contractIt->first;
|
||||
|
|
Loading…
Reference in New Issue