Browse Source

Merge pull request #2006 from chriseth/sol_functionalGasEstimator

Functional gas estimator
cl-refactor
chriseth 10 years ago
parent
commit
583146cc3e
  1. 27
      libevmasm/ExpressionClasses.cpp
  2. 5
      libevmasm/ExpressionClasses.h
  3. 9
      libevmasm/GasMeter.cpp
  4. 14
      libevmasm/GasMeter.h
  5. 128
      libevmasm/PathGasMeter.cpp
  6. 66
      libevmasm/PathGasMeter.h
  7. 2
      libevmasm/SemanticInformation.cpp
  8. 2
      libsolidity/ASTPrinter.cpp
  9. 6
      libsolidity/ASTPrinter.h
  10. 5
      libsolidity/Compiler.cpp
  11. 4
      libsolidity/Compiler.h
  12. 6
      libsolidity/CompilerContext.cpp
  13. 4
      libsolidity/CompilerContext.h
  14. 18
      libsolidity/CompilerStack.cpp
  15. 8
      libsolidity/CompilerStack.h
  16. 62
      libsolidity/GasEstimator.cpp
  17. 29
      libsolidity/GasEstimator.h
  18. 5
      mix/CodeModel.cpp
  19. 59
      solc/CommandLineInterface.cpp
  20. 1
      solc/CommandLineInterface.h
  21. 99
      test/libsolidity/GasMeter.cpp

27
libevmasm/ExpressionClasses.cpp

@ -57,11 +57,11 @@ ExpressionClasses::Id ExpressionClasses::find(
exp.arguments = _arguments; exp.arguments = _arguments;
exp.sequenceNumber = _sequenceNumber; exp.sequenceNumber = _sequenceNumber;
if (SemanticInformation::isCommutativeOperation(_item))
sort(exp.arguments.begin(), exp.arguments.end());
if (SemanticInformation::isDeterministic(_item)) if (SemanticInformation::isDeterministic(_item))
{ {
if (SemanticInformation::isCommutativeOperation(_item))
sort(exp.arguments.begin(), exp.arguments.end());
auto it = m_expressions.find(exp); auto it = m_expressions.find(exp);
if (it != m_expressions.end()) if (it != m_expressions.end())
return it->id; return it->id;
@ -82,6 +82,27 @@ ExpressionClasses::Id ExpressionClasses::find(
return exp.id; return exp.id;
} }
void ExpressionClasses::forceEqual(
ExpressionClasses::Id _id,
AssemblyItem const& _item,
ExpressionClasses::Ids const& _arguments,
bool _copyItem
)
{
Expression exp;
exp.id = _id;
exp.item = &_item;
exp.arguments = _arguments;
if (SemanticInformation::isCommutativeOperation(_item))
sort(exp.arguments.begin(), exp.arguments.end());
if (_copyItem)
exp.item = storeItem(_item);
m_expressions.insert(exp);
}
ExpressionClasses::Id ExpressionClasses::newClass(SourceLocation const& _location) ExpressionClasses::Id ExpressionClasses::newClass(SourceLocation const& _location)
{ {
Expression exp; Expression exp;

5
libevmasm/ExpressionClasses.h

@ -74,6 +74,11 @@ public:
/// @returns the number of classes. /// @returns the number of classes.
Id size() const { return m_representatives.size(); } Id size() const { return m_representatives.size(); }
/// Forces the given @a _item with @a _arguments to the class @a _id. This can be used to
/// add prior knowledge e.g. about CALLDATA, but has to be used with caution. Will not work as
/// expected if @a _item applied to @a _arguments already exists.
void forceEqual(Id _id, AssemblyItem const& _item, Ids const& _arguments, bool _copyItem = true);
/// @returns the id of a new class which is different to all other classes. /// @returns the id of a new class which is different to all other classes.
Id newClass(SourceLocation const& _location); Id newClass(SourceLocation const& _location);

9
libevmasm/GasMeter.cpp

@ -29,12 +29,13 @@ using namespace dev::eth;
GasMeter::GasConsumption& GasMeter::GasConsumption::operator+=(GasConsumption const& _other) GasMeter::GasConsumption& GasMeter::GasConsumption::operator+=(GasConsumption const& _other)
{ {
isInfinite = isInfinite || _other.isInfinite; if (_other.isInfinite && !isInfinite)
*this = infinite();
if (isInfinite) if (isInfinite)
return *this; return *this;
bigint v = bigint(value) + _other.value; bigint v = bigint(value) + _other.value;
if (v > std::numeric_limits<u256>::max()) if (v > numeric_limits<u256>::max())
isInfinite = true; *this = infinite();
else else
value = u256(v); value = u256(v);
return *this; return *this;
@ -147,7 +148,7 @@ GasMeter::GasConsumption GasMeter::estimateMax(AssemblyItem const& _item)
if (u256 const* value = classes.knownConstant(m_state->relativeStackElement(-1))) if (u256 const* value = classes.knownConstant(m_state->relativeStackElement(-1)))
gas += c_expByteGas * (32 - (h256(*value).firstBitSet() / 8)); gas += c_expByteGas * (32 - (h256(*value).firstBitSet() / 8));
else else
gas = GasConsumption::infinite(); gas += c_expByteGas * 32;
break; break;
default: default:
break; break;

14
libevmasm/GasMeter.h

@ -22,6 +22,7 @@
#pragma once #pragma once
#include <ostream> #include <ostream>
#include <tuple>
#include <libevmasm/ExpressionClasses.h> #include <libevmasm/ExpressionClasses.h>
#include <libevmasm/AssemblyItem.h> #include <libevmasm/AssemblyItem.h>
@ -46,20 +47,25 @@ public:
GasConsumption(u256 _value = 0, bool _infinite = false): value(_value), isInfinite(_infinite) {} GasConsumption(u256 _value = 0, bool _infinite = false): value(_value), isInfinite(_infinite) {}
static GasConsumption infinite() { return GasConsumption(0, true); } static GasConsumption infinite() { return GasConsumption(0, true); }
GasConsumption& operator+=(GasConsumption const& _otherS); GasConsumption& operator+=(GasConsumption const& _other);
std::ostream& operator<<(std::ostream& _str) const; bool operator<(GasConsumption const& _other) const { return this->tuple() < _other.tuple(); }
std::tuple<bool const&, u256 const&> tuple() const { return std::tie(isInfinite, value); }
u256 value; u256 value;
bool isInfinite; bool isInfinite;
}; };
/// Constructs a new gas meter given the current state. /// Constructs a new gas meter given the current state.
GasMeter(std::shared_ptr<KnownState> const& _state): m_state(_state) {} explicit GasMeter(std::shared_ptr<KnownState> const& _state, u256 const& _largestMemoryAccess = 0):
m_state(_state), m_largestMemoryAccess(_largestMemoryAccess) {}
/// @returns an upper bound on the gas consumed by the given instruction and updates /// @returns an upper bound on the gas consumed by the given instruction and updates
/// the state. /// the state.
GasConsumption estimateMax(AssemblyItem const& _item); GasConsumption estimateMax(AssemblyItem const& _item);
u256 const& largestMemoryAccess() const { return m_largestMemoryAccess; }
private: private:
/// @returns _multiplier * (_value + 31) / 32, if _value is a known constant and infinite otherwise. /// @returns _multiplier * (_value + 31) / 32, if _value is a known constant and infinite otherwise.
GasConsumption wordGas(u256 const& _multiplier, ExpressionClasses::Id _value); GasConsumption wordGas(u256 const& _multiplier, ExpressionClasses::Id _value);
@ -80,7 +86,7 @@ private:
inline std::ostream& operator<<(std::ostream& _str, GasMeter::GasConsumption const& _consumption) inline std::ostream& operator<<(std::ostream& _str, GasMeter::GasConsumption const& _consumption)
{ {
if (_consumption.isInfinite) if (_consumption.isInfinite)
return _str << "inf"; return _str << "[???]";
else else
return _str << std::dec << _consumption.value; return _str << std::dec << _consumption.value;
} }

128
libevmasm/PathGasMeter.cpp

@ -0,0 +1,128 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum 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.
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file PathGasMeter.cpp
* @author Christian <c@ethdev.com>
* @date 2015
*/
#include "PathGasMeter.h"
#include <libevmasm/KnownState.h>
#include <libevmasm/SemanticInformation.h>
using namespace std;
using namespace dev;
using namespace dev::eth;
PathGasMeter::PathGasMeter(AssemblyItems const& _items):
m_items(_items)
{
for (size_t i = 0; i < m_items.size(); ++i)
if (m_items[i].type() == Tag)
m_tagPositions[m_items[i].data()] = i;
}
GasMeter::GasConsumption PathGasMeter::estimateMax(
size_t _startIndex,
shared_ptr<KnownState> const& _state
)
{
auto path = unique_ptr<GasPath>(new GasPath());
path->index = _startIndex;
path->state = _state->copy();
m_queue.push_back(move(path));
GasMeter::GasConsumption gas;
while (!m_queue.empty() && !gas.isInfinite)
gas = max(gas, handleQueueItem());
return gas;
}
GasMeter::GasConsumption PathGasMeter::handleQueueItem()
{
assertThrow(!m_queue.empty(), OptimizerException, "");
unique_ptr<GasPath> path = move(m_queue.back());
m_queue.pop_back();
shared_ptr<KnownState> state = path->state;
GasMeter meter(state, path->largestMemoryAccess);
ExpressionClasses& classes = state->expressionClasses();
GasMeter::GasConsumption gas = path->gas;
size_t index = path->index;
if (index >= m_items.size() || (index > 0 && m_items.at(index).type() != Tag))
// Invalid jump usually provokes an out-of-gas exception, but we want to give an upper
// bound on the gas that is needed without changing the behaviour, so it is fine to
// return the current gas value.
return gas;
set<u256> jumpTags;
for (; index < m_items.size() && !gas.isInfinite; ++index)
{
bool branchStops = false;
jumpTags.clear();
AssemblyItem const& item = m_items.at(index);
if (item.type() == Tag || item == AssemblyItem(eth::Instruction::JUMPDEST))
{
// Do not allow any backwards jump. This is quite restrictive but should work for
// the simplest things.
if (path->visitedJumpdests.count(index))
return GasMeter::GasConsumption::infinite();
path->visitedJumpdests.insert(index);
}
else if (item == AssemblyItem(eth::Instruction::JUMP))
{
branchStops = true;
jumpTags = state->tagsInExpression(state->relativeStackElement(0));
if (jumpTags.empty()) // unknown jump destination
return GasMeter::GasConsumption::infinite();
}
else if (item == AssemblyItem(eth::Instruction::JUMPI))
{
ExpressionClasses::Id condition = state->relativeStackElement(-1);
if (classes.knownNonZero(condition) || !classes.knownZero(condition))
{
jumpTags = state->tagsInExpression(state->relativeStackElement(0));
if (jumpTags.empty()) // unknown jump destination
return GasMeter::GasConsumption::infinite();
}
branchStops = classes.knownNonZero(condition);
}
else if (SemanticInformation::altersControlFlow(item))
branchStops = true;
gas += meter.estimateMax(item);
for (u256 const& tag: jumpTags)
{
auto newPath = unique_ptr<GasPath>(new GasPath());
newPath->index = m_items.size();
if (m_tagPositions.count(tag))
newPath->index = m_tagPositions.at(tag);
newPath->gas = gas;
newPath->largestMemoryAccess = meter.largestMemoryAccess();
newPath->state = state->copy();
newPath->visitedJumpdests = path->visitedJumpdests;
m_queue.push_back(move(newPath));
}
if (branchStops)
break;
}
return gas;
}

66
libevmasm/PathGasMeter.h

@ -0,0 +1,66 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum 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.
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file PathGasMeter.cpp
* @author Christian <c@ethdev.com>
* @date 2015
*/
#pragma once
#include <set>
#include <vector>
#include <memory>
#include <libevmasm/GasMeter.h>
namespace dev
{
namespace eth
{
class KnownState;
struct GasPath
{
size_t index = 0;
std::shared_ptr<KnownState> state;
u256 largestMemoryAccess;
GasMeter::GasConsumption gas;
std::set<size_t> visitedJumpdests;
};
/**
* Computes an upper bound on the gas usage of a computation starting at a certain position in
* a list of AssemblyItems in a given state until the computation stops.
* Can be used to estimate the gas usage of functions on any given input.
*/
class PathGasMeter
{
public:
PathGasMeter(AssemblyItems const& _items);
GasMeter::GasConsumption estimateMax(size_t _startIndex, std::shared_ptr<KnownState> const& _state);
private:
GasMeter::GasConsumption handleQueueItem();
std::vector<std::unique_ptr<GasPath>> m_queue;
std::map<u256, size_t> m_tagPositions;
AssemblyItems const& m_items;
};
}
}

2
libevmasm/SemanticInformation.cpp

@ -111,7 +111,7 @@ bool SemanticInformation::altersControlFlow(AssemblyItem const& _item)
switch (_item.instruction()) switch (_item.instruction())
{ {
// note that CALL, CALLCODE and CREATE do not really alter the control flow, because we // note that CALL, CALLCODE and CREATE do not really alter the control flow, because we
// continue on the next instruction (unless an exception happens which can always happen) // continue on the next instruction
case Instruction::JUMP: case Instruction::JUMP:
case Instruction::JUMPI: case Instruction::JUMPI:
case Instruction::RETURN: case Instruction::RETURN:

2
libsolidity/ASTPrinter.cpp

@ -33,7 +33,7 @@ namespace solidity
ASTPrinter::ASTPrinter( ASTPrinter::ASTPrinter(
ASTNode const& _ast, ASTNode const& _ast,
string const& _source, string const& _source,
StructuralGasEstimator::ASTGasConsumption const& _gasCosts GasEstimator::ASTGasConsumption const& _gasCosts
): m_indentation(0), m_source(_source), m_ast(&_ast), m_gasCosts(_gasCosts) ): m_indentation(0), m_source(_source), m_ast(&_ast), m_gasCosts(_gasCosts)
{ {
} }

6
libsolidity/ASTPrinter.h

@ -24,7 +24,7 @@
#include <ostream> #include <ostream>
#include <libsolidity/ASTVisitor.h> #include <libsolidity/ASTVisitor.h>
#include <libsolidity/StructuralGasEstimator.h> #include <libsolidity/GasEstimator.h>
namespace dev namespace dev
{ {
@ -42,7 +42,7 @@ public:
ASTPrinter( ASTPrinter(
ASTNode const& _ast, ASTNode const& _ast,
std::string const& _source = std::string(), std::string const& _source = std::string(),
StructuralGasEstimator::ASTGasConsumption const& _gasCosts = StructuralGasEstimator::ASTGasConsumption() GasEstimator::ASTGasConsumption const& _gasCosts = GasEstimator::ASTGasConsumption()
); );
/// Output the string representation of the AST to _stream. /// Output the string representation of the AST to _stream.
void print(std::ostream& _stream); void print(std::ostream& _stream);
@ -133,7 +133,7 @@ private:
int m_indentation; int m_indentation;
std::string m_source; std::string m_source;
ASTNode const* m_ast; ASTNode const* m_ast;
StructuralGasEstimator::ASTGasConsumption m_gasCosts; GasEstimator::ASTGasConsumption m_gasCosts;
std::ostream* m_ostream; std::ostream* m_ostream;
}; };

5
libsolidity/Compiler.cpp

@ -71,6 +71,11 @@ void Compiler::compileContract(ContractDefinition const& _contract,
packIntoContractCreator(_contract, m_runtimeContext); packIntoContractCreator(_contract, m_runtimeContext);
} }
eth::AssemblyItem Compiler::getFunctionEntryLabel(FunctionDefinition const& _function) const
{
return m_runtimeContext.getFunctionEntryLabelIfExists(_function);
}
void Compiler::initializeContext(ContractDefinition const& _contract, void Compiler::initializeContext(ContractDefinition const& _contract,
map<ContractDefinition const*, bytes const*> const& _contracts) map<ContractDefinition const*, bytes const*> const& _contracts)
{ {

4
libsolidity/Compiler.h

@ -52,6 +52,10 @@ public:
/// @returns Assembly items of the runtime compiler context /// @returns Assembly items of the runtime compiler context
eth::AssemblyItems const& getRuntimeAssemblyItems() const { return m_runtimeContext.getAssembly().getItems(); } eth::AssemblyItems const& getRuntimeAssemblyItems() const { return m_runtimeContext.getAssembly().getItems(); }
/// @returns the entry label of the given function. Might return an AssemblyItem of type
/// UndefinedItem if it does not exist yet.
eth::AssemblyItem getFunctionEntryLabel(FunctionDefinition const& _function) const;
private: private:
/// Registers the non-function objects inside the contract with the context. /// Registers the non-function objects inside the contract with the context.
void initializeContext(ContractDefinition const& _contract, void initializeContext(ContractDefinition const& _contract,

6
libsolidity/CompilerContext.cpp

@ -99,6 +99,12 @@ eth::AssemblyItem CompilerContext::getFunctionEntryLabel(Declaration const& _dec
return res->second.tag(); return res->second.tag();
} }
eth::AssemblyItem CompilerContext::getFunctionEntryLabelIfExists(Declaration const& _declaration) const
{
auto res = m_functionEntryLabels.find(&_declaration);
return res == m_functionEntryLabels.end() ? eth::AssemblyItem(eth::UndefinedItem) : res->second.tag();
}
eth::AssemblyItem CompilerContext::getVirtualFunctionEntryLabel(FunctionDefinition const& _function) eth::AssemblyItem CompilerContext::getVirtualFunctionEntryLabel(FunctionDefinition const& _function)
{ {
solAssert(!m_inheritanceHierarchy.empty(), "No inheritance hierarchy set."); solAssert(!m_inheritanceHierarchy.empty(), "No inheritance hierarchy set.");

4
libsolidity/CompilerContext.h

@ -59,7 +59,11 @@ public:
bool isLocalVariable(Declaration const* _declaration) const; bool isLocalVariable(Declaration const* _declaration) const;
bool isStateVariable(Declaration const* _declaration) const { return m_stateVariables.count(_declaration) != 0; } bool isStateVariable(Declaration const* _declaration) const { return m_stateVariables.count(_declaration) != 0; }
/// @returns the entry label of the given function and creates it if it does not exist yet.
eth::AssemblyItem getFunctionEntryLabel(Declaration const& _declaration); eth::AssemblyItem getFunctionEntryLabel(Declaration const& _declaration);
/// @returns the entry label of the given function. Might return an AssemblyItem of type
/// UndefinedItem if it does not exist yet.
eth::AssemblyItem getFunctionEntryLabelIfExists(Declaration const& _declaration) const;
void setInheritanceHierarchy(std::vector<ContractDefinition const*> const& _hierarchy) { m_inheritanceHierarchy = _hierarchy; } void setInheritanceHierarchy(std::vector<ContractDefinition const*> const& _hierarchy) { m_inheritanceHierarchy = _hierarchy; }
/// @returns the entry label of the given function and takes overrides into account. /// @returns the entry label of the given function and takes overrides into account.
eth::AssemblyItem getVirtualFunctionEntryLabel(FunctionDefinition const& _function); eth::AssemblyItem getVirtualFunctionEntryLabel(FunctionDefinition const& _function);

18
libsolidity/CompilerStack.cpp

@ -268,6 +268,24 @@ ContractDefinition const& CompilerStack::getContractDefinition(string const& _co
return *getContract(_contractName).contract; return *getContract(_contractName).contract;
} }
size_t CompilerStack::getFunctionEntryPoint(
std::string const& _contractName,
FunctionDefinition const& _function
) const
{
shared_ptr<Compiler> const& compiler = getContract(_contractName).compiler;
if (!compiler)
return 0;
eth::AssemblyItem tag = compiler->getFunctionEntryLabel(_function);
if (tag.type() == eth::UndefinedItem)
return 0;
eth::AssemblyItems const& items = compiler->getRuntimeAssemblyItems();
for (size_t i = 0; i < items.size(); ++i)
if (items.at(i).type() == eth::Tag && items.at(i).data() == tag.data())
return i;
return 0;
}
bytes CompilerStack::staticCompile(std::string const& _sourceCode, bool _optimize) bytes CompilerStack::staticCompile(std::string const& _sourceCode, bool _optimize)
{ {
CompilerStack stack; CompilerStack stack;

8
libsolidity/CompilerStack.h

@ -48,6 +48,7 @@ namespace solidity
// forward declarations // forward declarations
class Scanner; class Scanner;
class ContractDefinition; class ContractDefinition;
class FunctionDefinition;
class SourceUnit; class SourceUnit;
class Compiler; class Compiler;
class GlobalContext; class GlobalContext;
@ -131,6 +132,13 @@ public:
/// does not exist. /// does not exist.
ContractDefinition const& getContractDefinition(std::string const& _contractName) const; ContractDefinition const& getContractDefinition(std::string const& _contractName) const;
/// @returns the offset of the entry point of the given function into the list of assembly items
/// or zero if it is not found or does not exist.
size_t getFunctionEntryPoint(
std::string const& _contractName,
FunctionDefinition const& _function
) const;
/// Compile the given @a _sourceCode to bytecode. If a scanner is provided, it is used for /// Compile the given @a _sourceCode to bytecode. If a scanner is provided, it is used for
/// scanning the source code - this is useful for printing exception information. /// scanning the source code - this is useful for printing exception information.
static bytes staticCompile(std::string const& _sourceCode, bool _optimize = false); static bytes staticCompile(std::string const& _sourceCode, bool _optimize = false);

62
libsolidity/StructuralGasEstimator.cpp → libsolidity/GasEstimator.cpp

@ -20,27 +20,30 @@
* Gas consumption estimator working alongside the AST. * Gas consumption estimator working alongside the AST.
*/ */
#include "StructuralGasEstimator.h" #include "GasEstimator.h"
#include <map> #include <map>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <libdevcore/SHA3.h>
#include <libevmasm/ControlFlowGraph.h> #include <libevmasm/ControlFlowGraph.h>
#include <libevmasm/KnownState.h> #include <libevmasm/KnownState.h>
#include <libevmasm/PathGasMeter.h>
#include <libsolidity/AST.h> #include <libsolidity/AST.h>
#include <libsolidity/ASTVisitor.h> #include <libsolidity/ASTVisitor.h>
#include <libsolidity/CompilerUtils.h>
using namespace std; using namespace std;
using namespace dev; using namespace dev;
using namespace dev::eth; using namespace dev::eth;
using namespace dev::solidity; using namespace dev::solidity;
StructuralGasEstimator::ASTGasConsumptionSelfAccumulated StructuralGasEstimator::performEstimation( GasEstimator::ASTGasConsumptionSelfAccumulated GasEstimator::structuralEstimation(
AssemblyItems const& _items, AssemblyItems const& _items,
vector<ASTNode const*> const& _ast vector<ASTNode const*> const& _ast
) )
{ {
solAssert(std::count(_ast.begin(), _ast.end(), nullptr) == 0, ""); solAssert(std::count(_ast.begin(), _ast.end(), nullptr) == 0, "");
map<SourceLocation, GasMeter::GasConsumption> particularCosts; map<SourceLocation, GasConsumption> particularCosts;
ControlFlowGraph cfg(_items); ControlFlowGraph cfg(_items);
for (BasicBlock const& block: cfg.optimisedBlocks()) for (BasicBlock const& block: cfg.optimisedBlocks())
@ -72,7 +75,7 @@ StructuralGasEstimator::ASTGasConsumptionSelfAccumulated StructuralGasEstimator:
return gasCosts; return gasCosts;
} }
map<ASTNode const*, GasMeter::GasConsumption> StructuralGasEstimator::breakToStatementLevel( map<ASTNode const*, GasMeter::GasConsumption> GasEstimator::breakToStatementLevel(
ASTGasConsumptionSelfAccumulated const& _gasCosts, ASTGasConsumptionSelfAccumulated const& _gasCosts,
vector<ASTNode const*> const& _roots vector<ASTNode const*> const& _roots
) )
@ -99,7 +102,7 @@ map<ASTNode const*, GasMeter::GasConsumption> StructuralGasEstimator::breakToSta
// we use the location of a node if // we use the location of a node if
// - its statement depth is 0 or // - its statement depth is 0 or
// - its statement depth is undefined but the parent's statement depth is at least 1 // - its statement depth is undefined but the parent's statement depth is at least 1
map<ASTNode const*, GasMeter::GasConsumption> gasCosts; map<ASTNode const*, GasConsumption> gasCosts;
auto onNodeSecondPass = [&](ASTNode const& _node) auto onNodeSecondPass = [&](ASTNode const& _node)
{ {
return statementDepth.count(&_node); return statementDepth.count(&_node);
@ -121,7 +124,53 @@ map<ASTNode const*, GasMeter::GasConsumption> StructuralGasEstimator::breakToSta
return gasCosts; return gasCosts;
} }
set<ASTNode const*> StructuralGasEstimator::finestNodesAtLocation( GasEstimator::GasConsumption GasEstimator::functionalEstimation(
AssemblyItems const& _items,
string const& _signature
)
{
auto state = make_shared<KnownState>();
if (!_signature.empty())
{
ExpressionClasses& classes = state->expressionClasses();
using Id = ExpressionClasses::Id;
using Ids = vector<Id>;
Id hashValue = classes.find(u256(FixedHash<4>::Arith(FixedHash<4>(dev::sha3(_signature)))));
Id calldata = classes.find(eth::Instruction::CALLDATALOAD, Ids{classes.find(u256(0))});
classes.forceEqual(hashValue, eth::Instruction::DIV, Ids{
calldata,
classes.find(u256(1) << (8 * 28))
});
}
PathGasMeter meter(_items);
return meter.estimateMax(0, state);
}
GasEstimator::GasConsumption GasEstimator::functionalEstimation(
AssemblyItems const& _items,
size_t const& _offset,
FunctionDefinition const& _function
)
{
auto state = make_shared<KnownState>();
unsigned parametersSize = CompilerUtils::getSizeOnStack(_function.getParameters());
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)
state->feedItem(eth::swapInstruction(parametersSize));
return PathGasMeter(_items).estimateMax(_offset, state);
}
set<ASTNode const*> GasEstimator::finestNodesAtLocation(
vector<ASTNode const*> const& _roots vector<ASTNode const*> const& _roots
) )
{ {
@ -140,4 +189,3 @@ set<ASTNode const*> StructuralGasEstimator::finestNodesAtLocation(
root->accept(visitor); root->accept(visitor);
return nodes; return nodes;
} }

29
libsolidity/StructuralGasEstimator.h → libsolidity/GasEstimator.h

@ -34,17 +34,18 @@ namespace dev
namespace solidity namespace solidity
{ {
class StructuralGasEstimator struct GasEstimator
{ {
public: public:
using ASTGasConsumption = std::map<ASTNode const*, eth::GasMeter::GasConsumption>; using GasConsumption = eth::GasMeter::GasConsumption;
using ASTGasConsumption = std::map<ASTNode const*, GasConsumption>;
using ASTGasConsumptionSelfAccumulated = using ASTGasConsumptionSelfAccumulated =
std::map<ASTNode const*, std::array<eth::GasMeter::GasConsumption, 2>>; std::map<ASTNode const*, std::array<GasConsumption, 2>>;
/// Estimates the gas consumption for every assembly item in the given assembly and stores /// Estimates the gas consumption for every assembly item in the given assembly and stores
/// it by source location. /// it by source location.
/// @returns a mapping from each AST node to a pair of its particular and syntactically accumulated gas costs. /// @returns a mapping from each AST node to a pair of its particular and syntactically accumulated gas costs.
ASTGasConsumptionSelfAccumulated performEstimation( static ASTGasConsumptionSelfAccumulated structuralEstimation(
eth::AssemblyItems const& _items, eth::AssemblyItems const& _items,
std::vector<ASTNode const*> const& _ast std::vector<ASTNode const*> const& _ast
); );
@ -52,14 +53,30 @@ public:
/// the following source locations are part of the mapping: /// the following source locations are part of the mapping:
/// 1. source locations of statements that do not contain other statements /// 1. source locations of statements that do not contain other statements
/// 2. maximal source locations that do not overlap locations coming from the first rule /// 2. maximal source locations that do not overlap locations coming from the first rule
ASTGasConsumption breakToStatementLevel( static ASTGasConsumption breakToStatementLevel(
ASTGasConsumptionSelfAccumulated const& _gasCosts, ASTGasConsumptionSelfAccumulated const& _gasCosts,
std::vector<ASTNode const*> const& _roots std::vector<ASTNode const*> const& _roots
); );
/// @returns the estimated gas consumption by the (public or external) function with the
/// given signature. If no signature is given, estimates the maximum gas usage.
static GasConsumption functionalEstimation(
eth::AssemblyItems const& _items,
std::string const& _signature = ""
);
/// @returns the estimated gas consumption by the given function which starts at the given
/// offset into the list of assembly items.
/// @note this does not work correctly for recursive functions.
static GasConsumption functionalEstimation(
eth::AssemblyItems const& _items,
size_t const& _offset,
FunctionDefinition const& _function
);
private: private:
/// @returns the set of AST nodes which are the finest nodes at their location. /// @returns the set of AST nodes which are the finest nodes at their location.
std::set<ASTNode const*> finestNodesAtLocation(std::vector<ASTNode const*> const& _roots); static std::set<ASTNode const*> finestNodesAtLocation(std::vector<ASTNode const*> const& _roots);
}; };
} }

5
mix/CodeModel.cpp

@ -33,7 +33,7 @@
#include <libsolidity/CompilerStack.h> #include <libsolidity/CompilerStack.h>
#include <libsolidity/SourceReferenceFormatter.h> #include <libsolidity/SourceReferenceFormatter.h>
#include <libsolidity/InterfaceHandler.h> #include <libsolidity/InterfaceHandler.h>
#include <libsolidity/StructuralGasEstimator.h> #include <libsolidity/GasEstimator.h>
#include <libsolidity/SourceReferenceFormatter.h> #include <libsolidity/SourceReferenceFormatter.h>
#include <libevmcore/Instruction.h> #include <libevmcore/Instruction.h>
#include <libethcore/CommonJS.h> #include <libethcore/CommonJS.h>
@ -373,8 +373,7 @@ void CodeModel::gasEstimation(solidity::CompilerStack const& _cs)
continue; continue;
dev::solidity::SourceUnit const& sourceUnit = _cs.getAST(*contractDefinition.getLocation().sourceName); dev::solidity::SourceUnit const& sourceUnit = _cs.getAST(*contractDefinition.getLocation().sourceName);
AssemblyItems const* items = _cs.getRuntimeAssemblyItems(n); AssemblyItems const* items = _cs.getRuntimeAssemblyItems(n);
StructuralGasEstimator estimator; std::map<ASTNode const*, GasMeter::GasConsumption> gasCosts = GasEstimator::breakToStatementLevel(GasEstimator::structuralEstimation(*items, std::vector<ASTNode const*>({&sourceUnit})), {&sourceUnit});
std::map<ASTNode const*, GasMeter::GasConsumption> gasCosts = estimator.breakToStatementLevel(estimator.performEstimation(*items, std::vector<ASTNode const*>({&sourceUnit})), {&sourceUnit});
for (auto gasItem = gasCosts.begin(); gasItem != gasCosts.end(); ++gasItem) for (auto gasItem = gasCosts.begin(); gasItem != gasCosts.end(); ++gasItem)
{ {
SourceLocation const& location = gasItem->first->getLocation(); SourceLocation const& location = gasItem->first->getLocation();

59
solc/CommandLineInterface.cpp

@ -34,6 +34,7 @@
#include <libdevcore/CommonData.h> #include <libdevcore/CommonData.h>
#include <libdevcore/CommonIO.h> #include <libdevcore/CommonIO.h>
#include <libevmcore/Instruction.h> #include <libevmcore/Instruction.h>
#include <libevmcore/Params.h>
#include <libsolidity/Scanner.h> #include <libsolidity/Scanner.h>
#include <libsolidity/Parser.h> #include <libsolidity/Parser.h>
#include <libsolidity/ASTPrinter.h> #include <libsolidity/ASTPrinter.h>
@ -42,7 +43,7 @@
#include <libsolidity/Exceptions.h> #include <libsolidity/Exceptions.h>
#include <libsolidity/CompilerStack.h> #include <libsolidity/CompilerStack.h>
#include <libsolidity/SourceReferenceFormatter.h> #include <libsolidity/SourceReferenceFormatter.h>
#include <libsolidity/StructuralGasEstimator.h> #include <libsolidity/GasEstimator.h>
using namespace std; using namespace std;
namespace po = boost::program_options; namespace po = boost::program_options;
@ -55,6 +56,7 @@ namespace solidity
static string const g_argAbiStr = "json-abi"; static string const g_argAbiStr = "json-abi";
static string const g_argSolAbiStr = "sol-abi"; static string const g_argSolAbiStr = "sol-abi";
static string const g_argSignatureHashes = "hashes"; static string const g_argSignatureHashes = "hashes";
static string const g_argGas = "gas";
static string const g_argAsmStr = "asm"; static string const g_argAsmStr = "asm";
static string const g_argAsmJsonStr = "asm-json"; static string const g_argAsmJsonStr = "asm-json";
static string const g_argAstStr = "ast"; static string const g_argAstStr = "ast";
@ -94,6 +96,7 @@ static bool needsHumanTargetedStdout(po::variables_map const& _args)
{ {
return return
_args.count(g_argGas) ||
humanTargetedStdout(_args, g_argAbiStr) || humanTargetedStdout(_args, g_argAbiStr) ||
humanTargetedStdout(_args, g_argSolAbiStr) || humanTargetedStdout(_args, g_argSolAbiStr) ||
humanTargetedStdout(_args, g_argSignatureHashes) || humanTargetedStdout(_args, g_argSignatureHashes) ||
@ -245,6 +248,50 @@ void CommandLineInterface::handleMeta(DocumentationType _type, string const& _co
} }
} }
void CommandLineInterface::handleGasEstimation(string const& _contract)
{
using Gas = GasEstimator::GasConsumption;
if (!m_compiler->getAssemblyItems(_contract) && !m_compiler->getRuntimeAssemblyItems(_contract))
return;
cout << "Gas estimation:" << endl;
if (eth::AssemblyItems const* items = m_compiler->getAssemblyItems(_contract))
{
Gas gas = GasEstimator::functionalEstimation(*items);
u256 bytecodeSize(m_compiler->getRuntimeBytecode(_contract).size());
cout << "construction:" << endl;
cout << " " << gas << " + " << (bytecodeSize * eth::c_createDataGas) << " = ";
gas += bytecodeSize * eth::c_createDataGas;
cout << gas << endl;
}
if (eth::AssemblyItems const* items = m_compiler->getRuntimeAssemblyItems(_contract))
{
ContractDefinition const& contract = m_compiler->getContractDefinition(_contract);
cout << "external:" << endl;
for (auto it: contract.getInterfaceFunctions())
{
string sig = it.second->externalSignature();
GasEstimator::GasConsumption gas = GasEstimator::functionalEstimation(*items, sig);
cout << " " << sig << ":\t" << gas << endl;
}
cout << "internal:" << endl;
for (auto const& it: contract.getDefinedFunctions())
{
if (it->isPartOfExternalInterface() || it->isConstructor())
continue;
size_t entry = m_compiler->getFunctionEntryPoint(_contract, *it);
GasEstimator::GasConsumption gas = GasEstimator::GasConsumption::infinite();
if (entry > 0)
gas = GasEstimator::functionalEstimation(*items, entry, *it);
FunctionType type(*it);
cout << " " << it->getName() << "(";
auto end = type.getParameterTypes().end();
for (auto it = type.getParameterTypes().begin(); it != end; ++it)
cout << (*it)->toString() << (it + 1 == end ? "" : ",");
cout << "):\t" << gas << endl;
}
}
}
bool CommandLineInterface::parseArguments(int argc, char** argv) bool CommandLineInterface::parseArguments(int argc, char** argv)
{ {
// Declare the supported options. // Declare the supported options.
@ -278,6 +325,8 @@ bool CommandLineInterface::parseArguments(int argc, char** argv)
"Request to output the contract's Solidity ABI interface.") "Request to output the contract's Solidity ABI interface.")
(g_argSignatureHashes.c_str(), po::value<OutputType>()->value_name("stdout|file|both"), (g_argSignatureHashes.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
"Request to output the contract's functions' signature hashes.") "Request to output the contract's functions' signature hashes.")
(g_argGas.c_str(),
"Request to output an estimate for each function's maximal gas usage.")
(g_argNatspecUserStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"), (g_argNatspecUserStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
"Request to output the contract's Natspec user documentation.") "Request to output the contract's Natspec user documentation.")
(g_argNatspecDevStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"), (g_argNatspecDevStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
@ -465,14 +514,13 @@ void CommandLineInterface::handleAst(string const& _argStr)
// do we need AST output? // do we need AST output?
if (m_args.count(_argStr)) if (m_args.count(_argStr))
{ {
StructuralGasEstimator gasEstimator;
vector<ASTNode const*> asts; vector<ASTNode const*> asts;
for (auto const& sourceCode: m_sourceCodes) for (auto const& sourceCode: m_sourceCodes)
asts.push_back(&m_compiler->getAST(sourceCode.first)); asts.push_back(&m_compiler->getAST(sourceCode.first));
map<ASTNode const*, eth::GasMeter::GasConsumption> gasCosts; map<ASTNode const*, eth::GasMeter::GasConsumption> gasCosts;
if (m_compiler->getRuntimeAssemblyItems()) if (m_compiler->getRuntimeAssemblyItems())
gasCosts = gasEstimator.breakToStatementLevel( gasCosts = GasEstimator::breakToStatementLevel(
gasEstimator.performEstimation(*m_compiler->getRuntimeAssemblyItems(), asts), GasEstimator::structuralEstimation(*m_compiler->getRuntimeAssemblyItems(), asts),
asts asts
); );
@ -554,6 +602,9 @@ void CommandLineInterface::actOnInput()
} }
} }
if (m_args.count(g_argGas))
handleGasEstimation(contract);
handleBytecode(contract); handleBytecode(contract);
handleSignatureHashes(contract); handleSignatureHashes(contract);
handleMeta(DocumentationType::ABIInterface, contract); handleMeta(DocumentationType::ABIInterface, contract);

1
solc/CommandLineInterface.h

@ -61,6 +61,7 @@ private:
void handleSignatureHashes(std::string const& _contract); void handleSignatureHashes(std::string const& _contract);
void handleMeta(DocumentationType _type, void handleMeta(DocumentationType _type,
std::string const& _contract); std::string const& _contract);
void handleGasEstimation(std::string const& _contract);
/// Compiler arguments variable map /// Compiler arguments variable map
boost::program_options::variables_map m_args; boost::program_options::variables_map m_args;

99
test/libsolidity/GasMeter.cpp

@ -23,8 +23,9 @@
#include <test/libsolidity/solidityExecutionFramework.h> #include <test/libsolidity/solidityExecutionFramework.h>
#include <libevmasm/GasMeter.h> #include <libevmasm/GasMeter.h>
#include <libevmasm/KnownState.h> #include <libevmasm/KnownState.h>
#include <libevmasm/PathGasMeter.h>
#include <libsolidity/AST.h> #include <libsolidity/AST.h>
#include <libsolidity/StructuralGasEstimator.h> #include <libsolidity/GasEstimator.h>
#include <libsolidity/SourceReferenceFormatter.h> #include <libsolidity/SourceReferenceFormatter.h>
using namespace std; using namespace std;
@ -47,30 +48,47 @@ public:
m_compiler.setSource(_sourceCode); m_compiler.setSource(_sourceCode);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed"); ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed");
StructuralGasEstimator estimator;
AssemblyItems const* items = m_compiler.getRuntimeAssemblyItems(""); AssemblyItems const* items = m_compiler.getRuntimeAssemblyItems("");
ASTNode const& sourceUnit = m_compiler.getAST(); ASTNode const& sourceUnit = m_compiler.getAST();
BOOST_REQUIRE(items != nullptr); BOOST_REQUIRE(items != nullptr);
m_gasCosts = estimator.breakToStatementLevel( m_gasCosts = GasEstimator::breakToStatementLevel(
estimator.performEstimation(*items, vector<ASTNode const*>({&sourceUnit})), GasEstimator::structuralEstimation(*items, vector<ASTNode const*>({&sourceUnit})),
{&sourceUnit} {&sourceUnit}
); );
} }
void testCreationTimeGas(string const& _sourceCode, string const& _contractName = "") void testCreationTimeGas(string const& _sourceCode)
{ {
compileAndRun(_sourceCode); compileAndRun(_sourceCode);
auto state = make_shared<KnownState>(); auto state = make_shared<KnownState>();
GasMeter meter(state); PathGasMeter meter(*m_compiler.getAssemblyItems());
GasMeter::GasConsumption gas; GasMeter::GasConsumption gas = meter.estimateMax(0, state);
for (AssemblyItem const& item: *m_compiler.getAssemblyItems(_contractName)) u256 bytecodeSize(m_compiler.getRuntimeBytecode().size());
gas += meter.estimateMax(item);
u256 bytecodeSize(m_compiler.getRuntimeBytecode(_contractName).size());
gas += bytecodeSize * c_createDataGas; gas += bytecodeSize * c_createDataGas;
BOOST_REQUIRE(!gas.isInfinite); BOOST_REQUIRE(!gas.isInfinite);
BOOST_CHECK(gas.value == m_gasUsed); BOOST_CHECK(gas.value == m_gasUsed);
} }
/// Compares the gas computed by PathGasMeter for the given signature (but unknown arguments)
/// against the actual gas usage computed by the VM on the given set of argument variants.
void testRunTimeGas(string const& _sig, vector<bytes> _argumentVariants)
{
u256 gasUsed = 0;
FixedHash<4> hash(dev::sha3(_sig));
for (bytes const& arguments: _argumentVariants)
{
sendMessage(hash.asBytes() + arguments, false, 0);
gasUsed = max(gasUsed, m_gasUsed);
}
GasMeter::GasConsumption gas = GasEstimator::functionalEstimation(
*m_compiler.getRuntimeAssemblyItems(),
_sig
);
BOOST_REQUIRE(!gas.isInfinite);
BOOST_CHECK(gas.value == m_gasUsed);
}
protected: protected:
map<ASTNode const*, eth::GasMeter::GasConsumption> m_gasCosts; map<ASTNode const*, eth::GasMeter::GasConsumption> m_gasCosts;
}; };
@ -149,6 +167,67 @@ BOOST_AUTO_TEST_CASE(updating_store)
testCreationTimeGas(sourceCode); testCreationTimeGas(sourceCode);
} }
BOOST_AUTO_TEST_CASE(branches)
{
char const* sourceCode = R"(
contract test {
uint data;
uint data2;
function f(uint x) {
if (x > 7)
data2 = 1;
else
data = 1;
}
}
)";
testCreationTimeGas(sourceCode);
testRunTimeGas("f(uint256)", vector<bytes>{encodeArgs(2), encodeArgs(8)});
}
BOOST_AUTO_TEST_CASE(function_calls)
{
char const* sourceCode = R"(
contract test {
uint data;
uint data2;
function f(uint x) {
if (x > 7)
data2 = g(x**8) + 1;
else
data = 1;
}
function g(uint x) internal returns (uint) {
return data2;
}
}
)";
testCreationTimeGas(sourceCode);
testRunTimeGas("f(uint256)", vector<bytes>{encodeArgs(2), encodeArgs(8)});
}
BOOST_AUTO_TEST_CASE(multiple_external_functions)
{
char const* sourceCode = R"(
contract test {
uint data;
uint data2;
function f(uint x) {
if (x > 7)
data2 = g(x**8) + 1;
else
data = 1;
}
function g(uint x) returns (uint) {
return data2;
}
}
)";
testCreationTimeGas(sourceCode);
testRunTimeGas("f(uint256)", vector<bytes>{encodeArgs(2), encodeArgs(8)});
testRunTimeGas("g(uint256)", vector<bytes>{encodeArgs(2)});
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
} }

Loading…
Cancel
Save