solidity/libsolidity/formal/SymbolicState.h

104 lines
2.8 KiB
C
Raw Normal View History

2020-03-24 19:15:15 +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/>.
*/
// SPDX-License-Identifier: GPL-3.0
2020-03-24 19:15:15 +00:00
#pragma once
#include <libsolidity/formal/SymbolicVariables.h>
2020-05-18 15:42:24 +00:00
#include <libsmtutil/Sorts.h>
#include <libsmtutil/SolverInterface.h>
2020-03-24 19:15:15 +00:00
namespace solidity::frontend::smt
{
class EncodingContext;
2020-09-07 14:45:14 +00:00
class SymbolicAddressVariable;
class SymbolicArrayVariable;
2020-03-24 19:15:15 +00:00
/**
2020-09-07 14:45:14 +00:00
* Symbolic representation of the blockchain context:
* - error flag
* - this (the address of the currently executing contract)
* - state, represented as a tuple of:
* - balances
* - TODO: potentially storage of contracts
* - TODO transaction variables
2020-03-24 19:15:15 +00:00
*/
class SymbolicState
{
public:
SymbolicState(EncodingContext& _context);
void reset();
/// Blockchain.
//@{
2020-09-18 16:55:23 +00:00
SymbolicIntVariable& errorFlag();
smtutil::SortPointer errorFlagSort();
/// @returns the symbolic value of the currently executing contract's address.
2020-05-19 12:14:46 +00:00
smtutil::Expression thisAddress();
2020-09-18 16:55:23 +00:00
smtutil::Expression thisAddress(unsigned _idx);
smtutil::SortPointer thisAddressSort();
/// @returns the state as a tuple.
smtutil::Expression state();
smtutil::Expression state(unsigned _idx);
smtutil::SortPointer stateSort();
void newState();
2020-09-07 14:45:14 +00:00
/// @returns the symbolic balances.
smtutil::Expression balances();
2020-03-24 19:15:15 +00:00
/// @returns the symbolic balance of address `this`.
2020-05-19 12:14:46 +00:00
smtutil::Expression balance();
2020-03-24 19:15:15 +00:00
/// @returns the symbolic balance of an address.
2020-05-19 12:14:46 +00:00
smtutil::Expression balance(smtutil::Expression _address);
2020-03-24 19:15:15 +00:00
/// Transfer _value from _from to _to.
2020-05-19 12:14:46 +00:00
void transfer(smtutil::Expression _from, smtutil::Expression _to, smtutil::Expression _value);
2020-03-24 19:15:15 +00:00
//@}
private:
/// Adds _value to _account's balance.
2020-05-19 12:14:46 +00:00
void addBalance(smtutil::Expression _account, smtutil::Expression _value);
2020-03-24 19:15:15 +00:00
2020-09-07 14:45:14 +00:00
/// Generates a new tuple where _member is assigned _value.
smtutil::Expression assignStateMember(std::string const& _member, smtutil::Expression const& _value);
2020-03-24 19:15:15 +00:00
EncodingContext& m_context;
2020-09-07 14:45:14 +00:00
SymbolicIntVariable m_error{
TypeProvider::uint256(),
TypeProvider::uint256(),
"error",
2020-03-24 19:15:15 +00:00
m_context
};
2020-09-07 14:45:14 +00:00
SymbolicAddressVariable m_thisAddress{
"this",
2020-03-24 19:15:15 +00:00
m_context
};
2020-09-07 14:45:14 +00:00
std::map<std::string, unsigned> m_componentIndices;
/// balances, TODO storage of other contracts
std::map<std::string, smtutil::SortPointer> m_stateMembers;
std::unique_ptr<SymbolicTupleVariable> m_stateTuple;
2020-03-24 19:15:15 +00:00
};
}