2015-05-06 08:43:59 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-05-06 08:43:59 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-05-06 08:43:59 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2015-05-06 08:43:59 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-05-06 08:43:59 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2015-05-06 08:43:59 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Gas consumption estimator working alongside the AST.
|
|
|
|
*/
|
|
|
|
|
2018-12-17 18:24:42 +00:00
|
|
|
#include <libsolidity/interface/GasEstimator.h>
|
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include <libsolidity/ast/ASTVisitor.h>
|
|
|
|
#include <libsolidity/codegen/CompilerUtils.h>
|
2015-05-06 08:43:59 +00:00
|
|
|
|
2018-12-17 18:24:42 +00:00
|
|
|
#include <libevmasm/ControlFlowGraph.h>
|
|
|
|
#include <libevmasm/KnownState.h>
|
|
|
|
#include <libevmasm/PathGasMeter.h>
|
2022-09-27 12:13:44 +00:00
|
|
|
#include <libsolutil/FunctionSelector.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Keccak256.h>
|
2018-12-17 18:24:42 +00:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::evmasm;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::langutil;
|
2015-05-06 08:43:59 +00:00
|
|
|
|
2015-05-22 08:48:54 +00:00
|
|
|
GasEstimator::GasConsumption GasEstimator::functionalEstimation(
|
|
|
|
AssemblyItems const& _items,
|
2023-08-15 08:53:51 +00:00
|
|
|
std::string const& _signature
|
2018-03-01 11:06:36 +00:00
|
|
|
) const
|
2015-05-22 08:48:54 +00:00
|
|
|
{
|
2023-08-15 08:53:51 +00:00
|
|
|
auto state = std::make_shared<KnownState>();
|
2015-05-22 08:48:54 +00:00
|
|
|
|
2015-05-26 09:27:59 +00:00
|
|
|
if (!_signature.empty())
|
|
|
|
{
|
|
|
|
ExpressionClasses& classes = state->expressionClasses();
|
|
|
|
using Id = ExpressionClasses::Id;
|
2023-08-15 08:53:51 +00:00
|
|
|
using Ids = std::vector<Id>;
|
2022-09-27 12:13:44 +00:00
|
|
|
Id hashValue = classes.find(u256(util::selectorFromSignatureU32(_signature)));
|
2016-04-04 11:41:35 +00:00
|
|
|
Id calldata = classes.find(Instruction::CALLDATALOAD, Ids{classes.find(u256(0))});
|
2018-04-30 20:22:59 +00:00
|
|
|
if (!m_evmVersion.hasBitwiseShifting())
|
|
|
|
// div(calldataload(0), 1 << 224) equals to hashValue
|
|
|
|
classes.forceEqual(
|
|
|
|
hashValue,
|
|
|
|
Instruction::DIV,
|
|
|
|
Ids{calldata, classes.find(u256(1) << 224)}
|
|
|
|
);
|
|
|
|
else
|
|
|
|
// shr(0xe0, calldataload(0)) equals to hashValue
|
|
|
|
classes.forceEqual(
|
|
|
|
hashValue,
|
|
|
|
Instruction::SHR,
|
|
|
|
Ids{classes.find(u256(0xe0)), calldata}
|
|
|
|
);
|
2018-04-11 16:30:20 +00:00
|
|
|
// lt(calldatasize(), 4) equals to 0 (ignore the shortcut for fallback functions)
|
|
|
|
classes.forceEqual(
|
|
|
|
classes.find(u256(0)),
|
|
|
|
Instruction::LT,
|
|
|
|
Ids{classes.find(Instruction::CALLDATASIZE), classes.find(u256(4))}
|
|
|
|
);
|
2015-05-26 09:27:59 +00:00
|
|
|
}
|
2015-05-22 08:48:54 +00:00
|
|
|
|
2018-09-18 15:16:21 +00:00
|
|
|
return PathGasMeter::estimateMax(_items, m_evmVersion, 0, state);
|
2015-05-22 08:48:54 +00:00
|
|
|
}
|
|
|
|
|
2015-05-26 09:27:59 +00:00
|
|
|
GasEstimator::GasConsumption GasEstimator::functionalEstimation(
|
|
|
|
AssemblyItems const& _items,
|
|
|
|
size_t const& _offset,
|
|
|
|
FunctionDefinition const& _function
|
2018-03-01 11:06:36 +00:00
|
|
|
) const
|
2015-05-26 09:27:59 +00:00
|
|
|
{
|
2023-08-15 08:53:51 +00:00
|
|
|
auto state = std::make_shared<KnownState>();
|
2015-05-26 09:27:59 +00:00
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
unsigned parametersSize = CompilerUtils::sizeOnStack(_function.parameters());
|
2015-05-26 09:27:59 +00:00
|
|
|
if (parametersSize > 16)
|
|
|
|
return GasConsumption::infinite();
|
|
|
|
|
|
|
|
// Store an invalid return value on the stack, so that the path estimator breaks upon reaching
|
|
|
|
// the return jump.
|
|
|
|
AssemblyItem invalidTag(PushTag, u256(-0x10));
|
|
|
|
state->feedItem(invalidTag, true);
|
|
|
|
if (parametersSize > 0)
|
2016-04-04 11:41:35 +00:00
|
|
|
state->feedItem(swapInstruction(parametersSize));
|
2015-05-26 09:27:59 +00:00
|
|
|
|
2018-09-18 15:16:21 +00:00
|
|
|
return PathGasMeter::estimateMax(_items, m_evmVersion, _offset, state);
|
2015-05-26 09:27:59 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 08:53:51 +00:00
|
|
|
std::set<ASTNode const*> GasEstimator::finestNodesAtLocation(
|
|
|
|
std::vector<ASTNode const*> const& _roots
|
2015-05-19 22:27:07 +00:00
|
|
|
)
|
|
|
|
{
|
2023-08-15 08:53:51 +00:00
|
|
|
std::map<SourceLocation, ASTNode const*> locations;
|
|
|
|
std::set<ASTNode const*> nodes;
|
|
|
|
SimpleASTVisitor visitor(std::function<bool(ASTNode const&)>(), [&](ASTNode const& _n)
|
2015-05-19 22:27:07 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
if (!locations.count(_n.location()))
|
2015-05-19 22:27:07 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
locations[_n.location()] = &_n;
|
2015-05-19 22:27:07 +00:00
|
|
|
nodes.insert(&_n);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for (ASTNode const* root: _roots)
|
|
|
|
root->accept(visitor);
|
|
|
|
return nodes;
|
|
|
|
}
|