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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2020-03-24 19:15:15 +00:00
|
|
|
|
|
|
|
#include <libsolidity/formal/SymbolicState.h>
|
|
|
|
|
2020-09-07 14:45:14 +00:00
|
|
|
#include <libsolidity/formal/SymbolicTypes.h>
|
2020-03-24 19:15:15 +00:00
|
|
|
#include <libsolidity/formal/EncodingContext.h>
|
2020-12-08 20:14:18 +00:00
|
|
|
#include <libsolidity/formal/SMTEncoder.h>
|
2020-03-24 19:15:15 +00:00
|
|
|
|
2021-10-12 09:12:18 +00:00
|
|
|
#include <libsmtutil/Sorts.h>
|
|
|
|
|
|
|
|
#include <range/v3/view.hpp>
|
|
|
|
|
2020-03-24 19:15:15 +00:00
|
|
|
using namespace std;
|
2020-05-19 12:14:46 +00:00
|
|
|
using namespace solidity;
|
2021-10-12 09:12:18 +00:00
|
|
|
using namespace solidity::util;
|
2020-09-07 14:45:14 +00:00
|
|
|
using namespace solidity::smtutil;
|
2020-03-24 19:15:15 +00:00
|
|
|
using namespace solidity::frontend::smt;
|
|
|
|
|
2020-09-29 08:36:20 +00:00
|
|
|
BlockchainVariable::BlockchainVariable(
|
|
|
|
string _name,
|
|
|
|
map<string, smtutil::SortPointer> _members,
|
|
|
|
EncodingContext& _context
|
|
|
|
):
|
2022-08-23 17:28:45 +00:00
|
|
|
m_name(std::move(_name)),
|
|
|
|
m_members(std::move(_members)),
|
2020-03-24 19:15:15 +00:00
|
|
|
m_context(_context)
|
|
|
|
{
|
2020-09-07 14:45:14 +00:00
|
|
|
vector<string> members;
|
|
|
|
vector<SortPointer> sorts;
|
2020-09-29 08:36:20 +00:00
|
|
|
for (auto const& [component, sort]: m_members)
|
2020-09-07 14:45:14 +00:00
|
|
|
{
|
|
|
|
members.emplace_back(component);
|
|
|
|
sorts.emplace_back(sort);
|
2020-07-13 20:40:33 +00:00
|
|
|
m_componentIndices[component] = static_cast<unsigned>(members.size() - 1);
|
2020-09-07 14:45:14 +00:00
|
|
|
}
|
2020-09-29 08:36:20 +00:00
|
|
|
m_tuple = make_unique<SymbolicTupleVariable>(
|
|
|
|
make_shared<smtutil::TupleSort>(m_name + "_type", members, sorts),
|
|
|
|
m_name,
|
2020-09-07 14:45:14 +00:00
|
|
|
m_context
|
|
|
|
);
|
2020-03-24 19:15:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 08:36:20 +00:00
|
|
|
smtutil::Expression BlockchainVariable::member(string const& _member) const
|
2020-03-24 19:15:15 +00:00
|
|
|
{
|
2020-09-29 08:36:20 +00:00
|
|
|
return m_tuple->component(m_componentIndices.at(_member));
|
2020-03-24 19:15:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 08:36:20 +00:00
|
|
|
smtutil::Expression BlockchainVariable::assignMember(string const& _member, smtutil::Expression const& _value)
|
2020-03-24 19:15:15 +00:00
|
|
|
{
|
2021-10-12 09:12:18 +00:00
|
|
|
smtutil::Expression newTuple = smt::assignMember(m_tuple->currentValue(), {{_member, _value}});
|
|
|
|
m_context.addAssertion(m_tuple->increaseIndex() == newTuple);
|
2020-09-29 08:36:20 +00:00
|
|
|
return m_tuple->currentValue();
|
2020-09-18 16:55:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 08:36:20 +00:00
|
|
|
void SymbolicState::reset()
|
2020-09-07 14:45:14 +00:00
|
|
|
{
|
2020-09-29 08:36:20 +00:00
|
|
|
m_error.resetIndex();
|
|
|
|
m_thisAddress.resetIndex();
|
|
|
|
m_tx.reset();
|
2020-10-13 16:00:26 +00:00
|
|
|
m_crypto.reset();
|
2021-08-27 14:45:41 +00:00
|
|
|
if (m_abi)
|
|
|
|
m_abi->reset();
|
2021-10-12 09:12:18 +00:00
|
|
|
/// We don't reset nor clear these pointers on purpose,
|
|
|
|
/// since it only helps to keep the already generated types.
|
|
|
|
if (m_state)
|
|
|
|
m_state->reset();
|
2020-09-07 14:45:14 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 08:36:20 +00:00
|
|
|
smtutil::Expression SymbolicState::balances() const
|
2020-09-18 16:55:23 +00:00
|
|
|
{
|
2021-10-12 09:12:18 +00:00
|
|
|
return m_state->member("balances");
|
2020-09-18 16:55:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 08:36:20 +00:00
|
|
|
smtutil::Expression SymbolicState::balance() const
|
2020-09-18 16:55:23 +00:00
|
|
|
{
|
2020-09-29 08:36:20 +00:00
|
|
|
return balance(thisAddress());
|
2020-09-18 16:55:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 08:36:20 +00:00
|
|
|
smtutil::Expression SymbolicState::balance(smtutil::Expression _address) const
|
2020-09-18 16:55:23 +00:00
|
|
|
{
|
2022-08-23 17:28:45 +00:00
|
|
|
return smtutil::Expression::select(balances(), std::move(_address));
|
2020-09-18 16:55:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 08:36:20 +00:00
|
|
|
smtutil::Expression SymbolicState::blockhash(smtutil::Expression _blockNumber) const
|
2020-09-18 16:55:23 +00:00
|
|
|
{
|
2022-08-23 17:28:45 +00:00
|
|
|
return smtutil::Expression::select(m_tx.member("blockhash"), std::move(_blockNumber));
|
2020-09-18 16:55:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 13:49:36 +00:00
|
|
|
void SymbolicState::newBalances()
|
|
|
|
{
|
|
|
|
auto tupleSort = dynamic_pointer_cast<TupleSort>(stateSort());
|
|
|
|
auto balanceSort = tupleSort->components.at(tupleSort->memberToIndex.at("balances"));
|
|
|
|
SymbolicVariable newBalances(balanceSort, "fresh_balances_" + to_string(m_context.newUniqueId()), m_context);
|
2021-10-12 09:12:18 +00:00
|
|
|
m_state->assignMember("balances", newBalances.currentValue());
|
2021-08-20 13:49:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
void SymbolicState::transfer(smtutil::Expression _from, smtutil::Expression _to, smtutil::Expression _value)
|
2020-03-24 19:15:15 +00:00
|
|
|
{
|
2021-10-12 09:12:18 +00:00
|
|
|
unsigned indexBefore = m_state->index();
|
2020-03-24 19:15:15 +00:00
|
|
|
addBalance(_from, 0 - _value);
|
2022-08-23 17:28:45 +00:00
|
|
|
addBalance(_to, std::move(_value));
|
2021-10-12 09:12:18 +00:00
|
|
|
unsigned indexAfter = m_state->index();
|
2020-03-24 19:15:15 +00:00
|
|
|
solAssert(indexAfter > indexBefore, "");
|
2021-10-12 09:12:18 +00:00
|
|
|
m_state->newVar();
|
2020-03-24 19:15:15 +00:00
|
|
|
/// Do not apply the transfer operation if _from == _to.
|
2020-09-07 14:45:14 +00:00
|
|
|
auto newState = smtutil::Expression::ite(
|
2022-08-23 17:28:45 +00:00
|
|
|
std::move(_from) == std::move(_to),
|
2021-10-12 09:12:18 +00:00
|
|
|
m_state->value(indexBefore),
|
|
|
|
m_state->value(indexAfter)
|
|
|
|
);
|
|
|
|
m_context.addAssertion(m_state->value() == newState);
|
|
|
|
}
|
|
|
|
|
|
|
|
smtutil::Expression SymbolicState::storage(ContractDefinition const& _contract) const
|
|
|
|
{
|
|
|
|
return smt::member(m_state->member("storage"), contractStorageKey(_contract));
|
|
|
|
}
|
|
|
|
|
|
|
|
smtutil::Expression SymbolicState::storage(ContractDefinition const& _contract, smtutil::Expression _address) const
|
|
|
|
{
|
|
|
|
return smtutil::Expression::select(storage(_contract), std::move(_address));
|
|
|
|
}
|
|
|
|
|
|
|
|
smtutil::Expression SymbolicState::addressActive(smtutil::Expression _address) const
|
|
|
|
{
|
|
|
|
return smtutil::Expression::select(m_state->member("isActive"), std::move(_address));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolicState::setAddressActive(
|
|
|
|
smtutil::Expression _address,
|
|
|
|
bool _active
|
|
|
|
)
|
|
|
|
{
|
|
|
|
m_state->assignMember("isActive", smtutil::Expression::store(
|
|
|
|
m_state->member("isActive"),
|
|
|
|
std::move(_address),
|
|
|
|
smtutil::Expression(_active))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolicState::newStorage()
|
|
|
|
{
|
|
|
|
auto newStorageVar = SymbolicTupleVariable(
|
|
|
|
m_state->member("storage").sort,
|
|
|
|
"havoc_storage_" + to_string(m_context.newUniqueId()),
|
|
|
|
m_context
|
|
|
|
);
|
|
|
|
m_state->assignMember("storage", newStorageVar.currentValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolicState::writeStateVars(ContractDefinition const& _contract, smtutil::Expression _address)
|
|
|
|
{
|
|
|
|
auto stateVars = SMTEncoder::stateVariablesIncludingInheritedAndPrivate(_contract);
|
|
|
|
if (stateVars.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
map<string, smtutil::Expression> values;
|
|
|
|
for (auto var: stateVars)
|
|
|
|
values.emplace(stateVarStorageKey(*var, _contract), m_context.variable(*var)->currentValue());
|
|
|
|
|
|
|
|
smtutil::Expression thisStorage = storage(_contract, _address);
|
|
|
|
smtutil::Expression newStorage = smt::assignMember(thisStorage, values);
|
|
|
|
auto newContractStorage = smtutil::Expression::store(
|
|
|
|
storage(_contract), std::move(_address), newStorage
|
2020-03-24 19:15:15 +00:00
|
|
|
);
|
2021-10-12 09:12:18 +00:00
|
|
|
smtutil::Expression newAllStorage = smt::assignMember(m_state->member("storage"), {{contractStorageKey(_contract), newContractStorage}});
|
|
|
|
m_state->assignMember("storage", newAllStorage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolicState::readStateVars(ContractDefinition const& _contract, smtutil::Expression _address)
|
|
|
|
{
|
|
|
|
auto stateVars = SMTEncoder::stateVariablesIncludingInheritedAndPrivate(_contract);
|
|
|
|
if (stateVars.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto contractStorage = storage(_contract, std::move(_address));
|
|
|
|
for (auto var: stateVars)
|
|
|
|
m_context.addAssertion(
|
|
|
|
m_context.variable(*var)->increaseIndex() ==
|
|
|
|
smt::member(contractStorage, stateVarStorageKey(*var, _contract))
|
|
|
|
);
|
2020-09-29 08:36:20 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 13:49:36 +00:00
|
|
|
void SymbolicState::addBalance(smtutil::Expression _address, smtutil::Expression _value)
|
|
|
|
{
|
|
|
|
auto newBalances = smtutil::Expression::store(
|
|
|
|
balances(),
|
|
|
|
_address,
|
2022-08-23 17:28:45 +00:00
|
|
|
balance(_address) + std::move(_value)
|
2021-08-20 13:49:36 +00:00
|
|
|
);
|
2021-10-12 09:12:18 +00:00
|
|
|
m_state->assignMember("balances", newBalances);
|
2021-08-20 13:49:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 08:36:20 +00:00
|
|
|
smtutil::Expression SymbolicState::txMember(string const& _member) const
|
|
|
|
{
|
|
|
|
return m_tx.member(_member);
|
|
|
|
}
|
|
|
|
|
2023-01-19 13:06:23 +00:00
|
|
|
smtutil::Expression SymbolicState::evmParisConstraints() const
|
|
|
|
{
|
|
|
|
// Ensure prevrandao range as defined by EIP-4399.
|
|
|
|
return txMember("block.prevrandao") > (u256(1) << 64);
|
|
|
|
}
|
|
|
|
|
2020-12-30 13:35:48 +00:00
|
|
|
smtutil::Expression SymbolicState::txTypeConstraints() const
|
2020-09-29 08:36:20 +00:00
|
|
|
{
|
2021-08-11 10:19:10 +00:00
|
|
|
return
|
2023-01-19 13:06:23 +00:00
|
|
|
evmParisConstraints() &&
|
2021-08-11 10:19:10 +00:00
|
|
|
smt::symbolicUnknownConstraints(m_tx.member("block.basefee"), TypeProvider::uint256()) &&
|
|
|
|
smt::symbolicUnknownConstraints(m_tx.member("block.chainid"), TypeProvider::uint256()) &&
|
2021-01-20 10:22:28 +00:00
|
|
|
smt::symbolicUnknownConstraints(m_tx.member("block.coinbase"), TypeProvider::address()) &&
|
2023-01-19 13:06:23 +00:00
|
|
|
smt::symbolicUnknownConstraints(m_tx.member("block.prevrandao"), TypeProvider::uint256()) &&
|
2020-12-29 10:53:35 +00:00
|
|
|
smt::symbolicUnknownConstraints(m_tx.member("block.gaslimit"), TypeProvider::uint256()) &&
|
|
|
|
smt::symbolicUnknownConstraints(m_tx.member("block.number"), TypeProvider::uint256()) &&
|
|
|
|
smt::symbolicUnknownConstraints(m_tx.member("block.timestamp"), TypeProvider::uint256()) &&
|
2021-01-20 10:22:28 +00:00
|
|
|
smt::symbolicUnknownConstraints(m_tx.member("msg.sender"), TypeProvider::address()) &&
|
|
|
|
smt::symbolicUnknownConstraints(m_tx.member("msg.value"), TypeProvider::uint256()) &&
|
|
|
|
smt::symbolicUnknownConstraints(m_tx.member("tx.origin"), TypeProvider::address()) &&
|
|
|
|
smt::symbolicUnknownConstraints(m_tx.member("tx.gasprice"), TypeProvider::uint256());
|
2020-12-30 13:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
smtutil::Expression SymbolicState::txNonPayableConstraint() const
|
|
|
|
{
|
|
|
|
return m_tx.member("msg.value") == 0;
|
|
|
|
}
|
2020-09-29 08:36:20 +00:00
|
|
|
|
2020-12-30 13:35:48 +00:00
|
|
|
smtutil::Expression SymbolicState::txFunctionConstraints(FunctionDefinition const& _function) const
|
|
|
|
{
|
|
|
|
smtutil::Expression conj = _function.isPayable() ? smtutil::Expression(true) : txNonPayableConstraint();
|
2020-09-29 08:36:20 +00:00
|
|
|
if (_function.isPartOfExternalInterface())
|
|
|
|
{
|
|
|
|
auto sig = TypeProvider::function(_function)->externalIdentifier();
|
|
|
|
conj = conj && m_tx.member("msg.sig") == sig;
|
|
|
|
auto b0 = sig >> (3 * 8);
|
|
|
|
auto b1 = (sig & 0x00ff0000) >> (2 * 8);
|
|
|
|
auto b2 = (sig & 0x0000ff00) >> (1 * 8);
|
|
|
|
auto b3 = (sig & 0x000000ff);
|
|
|
|
auto data = smtutil::Expression::tuple_get(m_tx.member("msg.data"), 0);
|
|
|
|
conj = conj && smtutil::Expression::select(data, 0) == b0;
|
|
|
|
conj = conj && smtutil::Expression::select(data, 1) == b1;
|
|
|
|
conj = conj && smtutil::Expression::select(data, 2) == b2;
|
|
|
|
conj = conj && smtutil::Expression::select(data, 3) == b3;
|
|
|
|
auto length = smtutil::Expression::tuple_get(m_tx.member("msg.data"), 1);
|
|
|
|
// TODO add ABI size of function input parameters here \/
|
|
|
|
conj = conj && length >= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return conj;
|
2020-03-24 19:15:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 09:12:18 +00:00
|
|
|
void SymbolicState::prepareForSourceUnit(SourceUnit const& _source, bool _storage)
|
2020-12-08 20:14:18 +00:00
|
|
|
{
|
2021-10-12 09:12:18 +00:00
|
|
|
auto allSources = _source.referencedSourceUnits(true);
|
|
|
|
allSources.insert(&_source);
|
|
|
|
set<FunctionCall const*, ASTCompareByID<FunctionCall>> abiCalls;
|
|
|
|
set<ContractDefinition const*, ASTCompareByID<ContractDefinition>> contracts;
|
|
|
|
for (auto const& source: allSources)
|
|
|
|
{
|
2020-12-08 20:14:18 +00:00
|
|
|
abiCalls += SMTEncoder::collectABICalls(source);
|
2021-10-12 09:12:18 +00:00
|
|
|
for (auto node: source->nodes())
|
|
|
|
if (auto contract = dynamic_cast<ContractDefinition const*>(node.get()))
|
|
|
|
contracts.insert(contract);
|
|
|
|
}
|
|
|
|
buildState(contracts, _storage);
|
2020-12-08 20:14:18 +00:00
|
|
|
buildABIFunctions(abiCalls);
|
|
|
|
}
|
|
|
|
|
2020-03-24 19:15:15 +00:00
|
|
|
/// Private helpers.
|
|
|
|
|
2021-10-12 09:12:18 +00:00
|
|
|
string SymbolicState::contractSuffix(ContractDefinition const& _contract) const
|
|
|
|
{
|
|
|
|
return "_" + _contract.name() + "_" + to_string(_contract.id());
|
|
|
|
}
|
|
|
|
|
|
|
|
string SymbolicState::contractStorageKey(ContractDefinition const& _contract) const
|
|
|
|
{
|
|
|
|
return "storage" + contractSuffix(_contract);
|
|
|
|
}
|
|
|
|
|
|
|
|
string SymbolicState::stateVarStorageKey(VariableDeclaration const& _var, ContractDefinition const& _contract) const
|
|
|
|
{
|
|
|
|
return _var.name() + "_" + to_string(_var.id()) + contractSuffix(_contract);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolicState::buildState(set<ContractDefinition const*, ASTCompareByID<ContractDefinition>> const& _contracts, bool _allStorages)
|
|
|
|
{
|
|
|
|
map<string, SortPointer> stateMembers{
|
|
|
|
{"balances", make_shared<smtutil::ArraySort>(smtutil::SortProvider::uintSort, smtutil::SortProvider::uintSort)}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (_allStorages)
|
|
|
|
{
|
|
|
|
vector<string> memberNames;
|
|
|
|
vector<SortPointer> memberSorts;
|
|
|
|
for (auto contract: _contracts)
|
|
|
|
{
|
|
|
|
string suffix = contractSuffix(*contract);
|
|
|
|
|
|
|
|
// z3 doesn't like empty tuples, so if the contract has 0
|
|
|
|
// state vars we can't put it there.
|
|
|
|
auto stateVars = SMTEncoder::stateVariablesIncludingInheritedAndPrivate(*contract);
|
|
|
|
if (stateVars.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto names = applyMap(stateVars, [&](auto var) {
|
|
|
|
return var->name() + "_" + to_string(var->id()) + suffix;
|
|
|
|
});
|
|
|
|
auto sorts = applyMap(stateVars, [](auto var) { return smtSortAbstractFunction(*var->type()); });
|
|
|
|
|
|
|
|
string name = "storage" + suffix;
|
|
|
|
auto storageTuple = make_shared<smtutil::TupleSort>(
|
|
|
|
name + "_type", names, sorts
|
|
|
|
);
|
|
|
|
|
|
|
|
auto storageSort = make_shared<smtutil::ArraySort>(
|
|
|
|
smtSort(*TypeProvider::address()),
|
|
|
|
storageTuple
|
|
|
|
);
|
|
|
|
|
|
|
|
memberNames.emplace_back(name);
|
|
|
|
memberSorts.emplace_back(storageSort);
|
|
|
|
}
|
|
|
|
|
|
|
|
stateMembers.emplace(
|
|
|
|
"isActive",
|
|
|
|
make_shared<smtutil::ArraySort>(smtSort(*TypeProvider::address()), smtutil::SortProvider::boolSort)
|
|
|
|
);
|
|
|
|
stateMembers.emplace(
|
|
|
|
"storage",
|
|
|
|
make_shared<smtutil::TupleSort>(
|
|
|
|
"storage_type", memberNames, memberSorts
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_state = make_unique<BlockchainVariable>(
|
|
|
|
"state",
|
|
|
|
std::move(stateMembers),
|
|
|
|
m_context
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolicState::buildABIFunctions(set<FunctionCall const*, ASTCompareByID<FunctionCall>> const& _abiFunctions)
|
2020-12-08 20:14:18 +00:00
|
|
|
{
|
|
|
|
map<string, SortPointer> functions;
|
|
|
|
|
|
|
|
for (auto const* funCall: _abiFunctions)
|
|
|
|
{
|
|
|
|
auto t = dynamic_cast<FunctionType const*>(funCall->expression().annotation().type);
|
|
|
|
|
|
|
|
auto const& args = funCall->sortedArguments();
|
|
|
|
auto const& paramTypes = t->parameterTypes();
|
|
|
|
auto const& returnTypes = t->returnParameterTypes();
|
|
|
|
|
|
|
|
|
|
|
|
auto argTypes = [](auto const& _args) {
|
2022-03-07 04:25:35 +00:00
|
|
|
return util::applyMap(_args, [](auto arg) { return arg->annotation().type; });
|
2020-12-08 20:14:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Since each abi.* function may have a different number of input/output parameters,
|
|
|
|
/// we generically compute those types.
|
2021-03-22 16:12:05 +00:00
|
|
|
vector<frontend::Type const*> inTypes;
|
|
|
|
vector<frontend::Type const*> outTypes;
|
2020-12-08 20:14:18 +00:00
|
|
|
if (t->kind() == FunctionType::Kind::ABIDecode)
|
|
|
|
{
|
|
|
|
/// abi.decode : (bytes, tuple_of_types(return_types)) -> (return_types)
|
2021-01-14 10:47:23 +00:00
|
|
|
solAssert(args.size() == 2, "Unexpected number of arguments for abi.decode");
|
2020-12-08 20:14:18 +00:00
|
|
|
inTypes.emplace_back(TypeProvider::bytesMemory());
|
2021-01-14 10:47:23 +00:00
|
|
|
auto argType = args.at(1)->annotation().type;
|
|
|
|
if (auto const* tupleType = dynamic_cast<TupleType const*>(argType))
|
|
|
|
for (auto componentType: tupleType->components())
|
|
|
|
{
|
|
|
|
auto typeType = dynamic_cast<TypeType const*>(componentType);
|
|
|
|
solAssert(typeType, "");
|
|
|
|
outTypes.emplace_back(typeType->actualType());
|
|
|
|
}
|
|
|
|
else if (auto const* typeType = dynamic_cast<TypeType const*>(argType))
|
2020-12-08 20:14:18 +00:00
|
|
|
outTypes.emplace_back(typeType->actualType());
|
2021-01-14 10:47:23 +00:00
|
|
|
else
|
|
|
|
solAssert(false, "Unexpected argument of abi.decode");
|
2020-12-08 20:14:18 +00:00
|
|
|
}
|
2021-11-11 15:21:23 +00:00
|
|
|
else if (t->kind() == FunctionType::Kind::ABIEncodeCall)
|
|
|
|
{
|
|
|
|
// abi.encodeCall : (functionPointer, tuple_of_args_or_one_non_tuple_arg(arguments)) -> bytes
|
|
|
|
solAssert(args.size() == 2, "Unexpected number of arguments for abi.encodeCall");
|
|
|
|
|
|
|
|
outTypes.emplace_back(TypeProvider::bytesMemory());
|
|
|
|
inTypes.emplace_back(args.at(0)->annotation().type);
|
|
|
|
inTypes.emplace_back(args.at(1)->annotation().type);
|
|
|
|
}
|
2020-12-08 20:14:18 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
outTypes = returnTypes;
|
|
|
|
if (
|
|
|
|
t->kind() == FunctionType::Kind::ABIEncodeWithSelector ||
|
|
|
|
t->kind() == FunctionType::Kind::ABIEncodeWithSignature
|
|
|
|
)
|
|
|
|
{
|
|
|
|
/// abi.encodeWithSelector : (bytes4, one_or_more_types) -> bytes
|
|
|
|
/// abi.encodeWithSignature : (string, one_or_more_types) -> bytes
|
|
|
|
inTypes.emplace_back(paramTypes.front());
|
|
|
|
inTypes += argTypes(vector<ASTPointer<Expression const>>(args.begin() + 1, args.end()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// abi.encode/abi.encodePacked : one_or_more_types -> bytes
|
|
|
|
solAssert(
|
|
|
|
t->kind() == FunctionType::Kind::ABIEncode ||
|
|
|
|
t->kind() == FunctionType::Kind::ABIEncodePacked,
|
|
|
|
""
|
|
|
|
);
|
|
|
|
inTypes = argTypes(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Rational numbers and string literals add the concrete values to the type name,
|
|
|
|
/// so we replace them by uint256 and bytes since those are the same as their SMT types.
|
|
|
|
/// TODO we could also replace all types by their ABI type.
|
|
|
|
auto replaceTypes = [](auto& _types) {
|
|
|
|
for (auto& t: _types)
|
|
|
|
if (t->category() == frontend::Type::Category::RationalNumber)
|
|
|
|
t = TypeProvider::uint256();
|
|
|
|
else if (t->category() == frontend::Type::Category::StringLiteral)
|
|
|
|
t = TypeProvider::bytesMemory();
|
2021-09-17 19:28:07 +00:00
|
|
|
else if (auto userType = dynamic_cast<UserDefinedValueType const*>(t))
|
|
|
|
t = &userType->underlyingType();
|
2020-12-08 20:14:18 +00:00
|
|
|
};
|
|
|
|
replaceTypes(inTypes);
|
|
|
|
replaceTypes(outTypes);
|
|
|
|
|
|
|
|
auto name = t->richIdentifier();
|
|
|
|
for (auto paramType: inTypes + outTypes)
|
|
|
|
name += "_" + paramType->richIdentifier();
|
|
|
|
|
|
|
|
m_abiMembers[funCall] = {name, inTypes, outTypes};
|
|
|
|
|
|
|
|
if (functions.count(name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/// If there is only one input or output parameter, we use that type directly.
|
|
|
|
/// Otherwise we create a tuple wrapping the necessary input or output types.
|
|
|
|
auto typesToSort = [](auto const& _types, string const& _name) -> shared_ptr<Sort> {
|
|
|
|
if (_types.size() == 1)
|
2021-01-14 12:31:30 +00:00
|
|
|
return smtSortAbstractFunction(*_types.front());
|
2020-12-08 20:14:18 +00:00
|
|
|
|
|
|
|
vector<string> inNames;
|
|
|
|
vector<SortPointer> sorts;
|
|
|
|
for (unsigned i = 0; i < _types.size(); ++i)
|
|
|
|
{
|
|
|
|
inNames.emplace_back(_name + "_input_" + to_string(i));
|
2021-01-14 12:31:30 +00:00
|
|
|
sorts.emplace_back(smtSortAbstractFunction(*_types.at(i)));
|
2020-12-08 20:14:18 +00:00
|
|
|
}
|
|
|
|
return make_shared<smtutil::TupleSort>(
|
|
|
|
_name + "_input",
|
|
|
|
inNames,
|
|
|
|
sorts
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto functionSort = make_shared<smtutil::ArraySort>(
|
|
|
|
typesToSort(inTypes, name),
|
|
|
|
typesToSort(outTypes, name)
|
|
|
|
);
|
|
|
|
|
|
|
|
functions[name] = functionSort;
|
|
|
|
}
|
|
|
|
|
2022-08-23 17:28:45 +00:00
|
|
|
m_abi = make_unique<BlockchainVariable>("abi", std::move(functions), m_context);
|
2020-12-08 20:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
smtutil::Expression SymbolicState::abiFunction(frontend::FunctionCall const* _funCall)
|
|
|
|
{
|
|
|
|
solAssert(m_abi, "");
|
|
|
|
return m_abi->member(get<0>(m_abiMembers.at(_funCall)));
|
|
|
|
}
|
|
|
|
|
|
|
|
SymbolicState::SymbolicABIFunction const& SymbolicState::abiFunctionTypes(FunctionCall const* _funCall) const
|
|
|
|
{
|
|
|
|
return m_abiMembers.at(_funCall);
|
|
|
|
}
|