2019-04-17 13:55:46 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2019-04-17 13:55:46 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-03-24 19:15:15 +00:00
|
|
|
#include <libsolidity/formal/SymbolicState.h>
|
2019-04-17 13:55:46 +00:00
|
|
|
#include <libsolidity/formal/SymbolicVariables.h>
|
|
|
|
|
2020-05-18 15:42:24 +00:00
|
|
|
#include <libsmtutil/SolverInterface.h>
|
|
|
|
|
2020-11-06 11:47:31 +00:00
|
|
|
#include <map>
|
2019-05-10 14:28:29 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::frontend::smt
|
2019-04-17 13:55:46 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores the context of the SMT encoding.
|
|
|
|
*/
|
|
|
|
class EncodingContext
|
|
|
|
{
|
|
|
|
public:
|
2019-07-04 13:27:45 +00:00
|
|
|
EncodingContext();
|
2019-04-17 13:55:46 +00:00
|
|
|
|
2019-07-08 09:21:23 +00:00
|
|
|
/// Resets the entire context except for symbolic variables which stay
|
|
|
|
/// alive because of state variables and inlined function calls.
|
|
|
|
/// To be used in the beginning of a root function visit.
|
2019-04-17 13:55:46 +00:00
|
|
|
void reset();
|
2020-06-09 11:07:40 +00:00
|
|
|
/// Resets the fresh id for slack variables.
|
2020-07-10 08:28:49 +00:00
|
|
|
void resetUniqueId();
|
2020-06-09 11:07:40 +00:00
|
|
|
/// Returns the current fresh slack id and increments it.
|
2020-07-10 08:28:49 +00:00
|
|
|
unsigned newUniqueId();
|
2019-07-08 09:21:23 +00:00
|
|
|
/// Clears the entire context, erasing everything.
|
|
|
|
/// To be used before a model checking engine starts.
|
|
|
|
void clear();
|
2019-04-17 13:55:46 +00:00
|
|
|
|
2019-07-04 13:27:45 +00:00
|
|
|
/// Sets the current solver used by the current engine for
|
|
|
|
/// SMT variable declaration.
|
2020-05-19 12:14:46 +00:00
|
|
|
void setSolver(smtutil::SolverInterface* _solver)
|
2019-07-04 13:27:45 +00:00
|
|
|
{
|
|
|
|
solAssert(_solver, "");
|
|
|
|
m_solver = _solver;
|
|
|
|
}
|
|
|
|
|
2019-07-19 17:31:25 +00:00
|
|
|
/// Sets whether the context should conjoin assertions in the assertion stack.
|
|
|
|
void setAssertionAccumulation(bool _acc) { m_accumulateAssertions = _acc; }
|
|
|
|
|
2019-07-03 14:05:56 +00:00
|
|
|
/// Forwards variable creation to the solver.
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression newVariable(std::string _name, smtutil::SortPointer _sort)
|
2019-07-03 14:05:56 +00:00
|
|
|
{
|
|
|
|
solAssert(m_solver, "");
|
2022-08-23 17:28:45 +00:00
|
|
|
return m_solver->newVariable(std::move(_name), std::move(_sort));
|
2019-07-03 14:05:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 11:47:31 +00:00
|
|
|
struct IdCompare
|
|
|
|
{
|
|
|
|
bool operator()(ASTNode const* lhs, ASTNode const* rhs) const
|
|
|
|
{
|
|
|
|
return lhs->id() < rhs->id();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-21 11:20:48 +00:00
|
|
|
/// Variables.
|
2019-05-10 14:28:29 +00:00
|
|
|
//@{
|
|
|
|
/// @returns the symbolic representation of a program variable.
|
2019-12-11 16:31:36 +00:00
|
|
|
std::shared_ptr<SymbolicVariable> variable(frontend::VariableDeclaration const& _varDecl);
|
2019-05-10 14:28:29 +00:00
|
|
|
/// @returns all symbolic variables.
|
2020-11-06 11:47:31 +00:00
|
|
|
std::map<frontend::VariableDeclaration const*, std::shared_ptr<SymbolicVariable>, IdCompare> const& variables() const { return m_variables; }
|
2019-05-10 14:28:29 +00:00
|
|
|
|
|
|
|
/// Creates a symbolic variable and
|
|
|
|
/// @returns true if a variable's type is not supported and is therefore abstract.
|
2019-12-11 16:31:36 +00:00
|
|
|
bool createVariable(frontend::VariableDeclaration const& _varDecl);
|
2019-05-10 14:28:29 +00:00
|
|
|
/// @returns true if variable was created.
|
2019-12-11 16:31:36 +00:00
|
|
|
bool knownVariable(frontend::VariableDeclaration const& _varDecl);
|
2019-05-10 14:28:29 +00:00
|
|
|
|
|
|
|
/// Resets a specific variable.
|
2019-12-11 16:31:36 +00:00
|
|
|
void resetVariable(frontend::VariableDeclaration const& _variable);
|
2019-05-10 14:28:29 +00:00
|
|
|
/// Resets a set of variables.
|
2019-12-11 16:31:36 +00:00
|
|
|
void resetVariables(std::set<frontend::VariableDeclaration const*> const& _variables);
|
2019-05-10 14:28:29 +00:00
|
|
|
/// Resets variables according to a predicate.
|
2019-12-11 16:31:36 +00:00
|
|
|
void resetVariables(std::function<bool(frontend::VariableDeclaration const&)> const& _filter);
|
2019-05-10 14:28:29 +00:00
|
|
|
///Resets all variables.
|
|
|
|
void resetAllVariables();
|
2019-04-17 13:55:46 +00:00
|
|
|
|
2019-05-10 14:28:29 +00:00
|
|
|
/// Allocates a new index for the declaration, updates the current
|
|
|
|
/// index to this value and returns the expression.
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression newValue(frontend::VariableDeclaration const& _decl);
|
2019-05-10 14:28:29 +00:00
|
|
|
/// Sets the value of the declaration to zero.
|
2019-12-11 16:31:36 +00:00
|
|
|
void setZeroValue(frontend::VariableDeclaration const& _decl);
|
2019-05-10 14:28:29 +00:00
|
|
|
void setZeroValue(SymbolicVariable& _variable);
|
|
|
|
/// Resets the variable to an unknown value (in its range).
|
2019-12-11 16:31:36 +00:00
|
|
|
void setUnknownValue(frontend::VariableDeclaration const& decl);
|
2019-05-10 14:28:29 +00:00
|
|
|
void setUnknownValue(SymbolicVariable& _variable);
|
|
|
|
//@}
|
|
|
|
|
2019-06-21 11:20:48 +00:00
|
|
|
/// Expressions.
|
2019-05-13 14:53:38 +00:00
|
|
|
////@{
|
|
|
|
/// @returns the symbolic representation of an AST node expression.
|
2019-12-11 16:31:36 +00:00
|
|
|
std::shared_ptr<SymbolicVariable> expression(frontend::Expression const& _e);
|
2019-05-13 14:53:38 +00:00
|
|
|
/// @returns all symbolic expressions.
|
2020-11-06 11:47:31 +00:00
|
|
|
std::map<frontend::Expression const*, std::shared_ptr<SymbolicVariable>, IdCompare> const& expressions() const { return m_expressions; }
|
2019-05-13 14:53:38 +00:00
|
|
|
|
|
|
|
/// Creates the expression (value can be arbitrary).
|
|
|
|
/// @returns true if type is not supported.
|
2019-12-11 16:31:36 +00:00
|
|
|
bool createExpression(frontend::Expression const& _e, std::shared_ptr<SymbolicVariable> _symbExpr = nullptr);
|
2019-05-13 14:53:38 +00:00
|
|
|
/// Checks if expression was created.
|
2019-12-11 16:31:36 +00:00
|
|
|
bool knownExpression(frontend::Expression const& _e) const;
|
2019-05-13 14:53:38 +00:00
|
|
|
//@}
|
|
|
|
|
2019-06-21 11:20:48 +00:00
|
|
|
/// Global variables and functions.
|
2019-05-14 16:57:34 +00:00
|
|
|
//@{
|
|
|
|
/// Global variables and functions.
|
|
|
|
std::shared_ptr<SymbolicVariable> globalSymbol(std::string const& _name);
|
2019-06-21 11:20:48 +00:00
|
|
|
/// @returns all symbolic globals.
|
2020-11-06 11:47:31 +00:00
|
|
|
std::map<std::string, std::shared_ptr<SymbolicVariable>> const& globalSymbols() const { return m_globalContext; }
|
2019-06-21 11:20:48 +00:00
|
|
|
|
2019-05-14 16:57:34 +00:00
|
|
|
/// Defines a new global variable or function
|
|
|
|
/// and @returns true if type was abstracted.
|
2019-12-11 16:31:36 +00:00
|
|
|
bool createGlobalSymbol(std::string const& _name, frontend::Expression const& _expr);
|
2019-05-14 16:57:34 +00:00
|
|
|
/// Checks if special variable or function was seen.
|
|
|
|
bool knownGlobalSymbol(std::string const& _var) const;
|
|
|
|
//@}
|
|
|
|
|
2019-06-21 11:20:48 +00:00
|
|
|
/// Solver.
|
|
|
|
//@{
|
|
|
|
/// @returns conjunction of all added assertions.
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression assertions();
|
2019-06-21 11:20:48 +00:00
|
|
|
void pushSolver();
|
|
|
|
void popSolver();
|
2020-05-19 12:14:46 +00:00
|
|
|
void addAssertion(smtutil::Expression const& _e);
|
2020-07-13 20:40:33 +00:00
|
|
|
size_t solverStackHeigh() { return m_assertions.size(); } const
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::SolverInterface* solver()
|
2019-07-04 13:27:45 +00:00
|
|
|
{
|
|
|
|
solAssert(m_solver, "");
|
|
|
|
return m_solver;
|
|
|
|
}
|
2019-06-21 11:20:48 +00:00
|
|
|
//@}
|
|
|
|
|
2020-03-24 19:15:15 +00:00
|
|
|
SymbolicState& state() { return m_state; }
|
2019-04-11 16:14:48 +00:00
|
|
|
|
2020-03-24 19:15:15 +00:00
|
|
|
private:
|
2019-06-21 11:20:48 +00:00
|
|
|
/// Symbolic expressions.
|
|
|
|
//{@
|
2019-05-10 14:28:29 +00:00
|
|
|
/// Symbolic variables.
|
2020-11-06 11:47:31 +00:00
|
|
|
std::map<frontend::VariableDeclaration const*, std::shared_ptr<SymbolicVariable>, IdCompare> m_variables;
|
2019-05-10 14:28:29 +00:00
|
|
|
|
2019-05-13 14:53:38 +00:00
|
|
|
/// Symbolic expressions.
|
2020-11-06 11:47:31 +00:00
|
|
|
std::map<frontend::Expression const*, std::shared_ptr<SymbolicVariable>, IdCompare> m_expressions;
|
2019-05-13 14:53:38 +00:00
|
|
|
|
2019-05-14 16:57:34 +00:00
|
|
|
/// Symbolic representation of global symbols including
|
|
|
|
/// variables and functions.
|
2020-11-06 11:47:31 +00:00
|
|
|
std::map<std::string, std::shared_ptr<smt::SymbolicVariable>> m_globalContext;
|
2019-05-14 16:57:34 +00:00
|
|
|
|
2020-03-24 19:15:15 +00:00
|
|
|
/// Symbolic representation of the blockchain state.
|
|
|
|
SymbolicState m_state;
|
2019-06-21 11:20:48 +00:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/// Solver related.
|
|
|
|
//@{
|
|
|
|
/// Solver can be SMT solver or Horn solver in the future.
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::SolverInterface* m_solver = nullptr;
|
2019-06-21 11:20:48 +00:00
|
|
|
|
|
|
|
/// Assertion stack.
|
2020-05-19 12:14:46 +00:00
|
|
|
std::vector<smtutil::Expression> m_assertions;
|
2019-07-19 17:31:25 +00:00
|
|
|
|
|
|
|
/// Whether to conjoin assertions in the assertion stack.
|
|
|
|
bool m_accumulateAssertions = true;
|
2019-06-21 11:20:48 +00:00
|
|
|
//@}
|
2020-06-09 11:07:40 +00:00
|
|
|
|
2020-07-10 08:28:49 +00:00
|
|
|
/// Central source of unique ids.
|
|
|
|
unsigned m_nextUniqueId = 0;
|
2019-04-17 13:55:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|