Browse Source

Merge remote-tracking branch 'up/develop' into bugFix2

cl-refactor
yann300 10 years ago
parent
commit
606b656eb6
  1. 25
      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. 22
      libsolidity/Token.h
  19. 3
      mix/ClientModel.cpp
  20. 5
      mix/CodeModel.cpp
  21. 2
      mix/qml/DebuggerPaneStyle.qml
  22. 14
      mix/qml/QIntTypeView.qml
  23. 15
      mix/qml/StructView.qml
  24. 2
      mix/qml/VariablesView.qml
  25. 59
      solc/CommandLineInterface.cpp
  26. 1
      solc/CommandLineInterface.h
  27. 99
      test/libsolidity/GasMeter.cpp

25
libevmasm/ExpressionClasses.cpp

@ -57,11 +57,11 @@ ExpressionClasses::Id ExpressionClasses::find(
exp.arguments = _arguments;
exp.sequenceNumber = _sequenceNumber;
if (SemanticInformation::isDeterministic(_item))
{
if (SemanticInformation::isCommutativeOperation(_item))
sort(exp.arguments.begin(), exp.arguments.end());
if (SemanticInformation::isDeterministic(_item))
{
auto it = m_expressions.find(exp);
if (it != m_expressions.end())
return it->id;
@ -82,6 +82,27 @@ ExpressionClasses::Id ExpressionClasses::find(
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)
{
Expression exp;

5
libevmasm/ExpressionClasses.h

@ -74,6 +74,11 @@ public:
/// @returns the number of classes.
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.
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)
{
isInfinite = isInfinite || _other.isInfinite;
if (_other.isInfinite && !isInfinite)
*this = infinite();
if (isInfinite)
return *this;
bigint v = bigint(value) + _other.value;
if (v > std::numeric_limits<u256>::max())
isInfinite = true;
if (v > numeric_limits<u256>::max())
*this = infinite();
else
value = u256(v);
return *this;
@ -147,7 +148,7 @@ GasMeter::GasConsumption GasMeter::estimateMax(AssemblyItem const& _item)
if (u256 const* value = classes.knownConstant(m_state->relativeStackElement(-1)))
gas += c_expByteGas * (32 - (h256(*value).firstBitSet() / 8));
else
gas = GasConsumption::infinite();
gas += c_expByteGas * 32;
break;
default:
break;

14
libevmasm/GasMeter.h

@ -22,6 +22,7 @@
#pragma once
#include <ostream>
#include <tuple>
#include <libevmasm/ExpressionClasses.h>
#include <libevmasm/AssemblyItem.h>
@ -46,20 +47,25 @@ public:
GasConsumption(u256 _value = 0, bool _infinite = false): value(_value), isInfinite(_infinite) {}
static GasConsumption infinite() { return GasConsumption(0, true); }
GasConsumption& operator+=(GasConsumption const& _otherS);
std::ostream& operator<<(std::ostream& _str) const;
GasConsumption& operator+=(GasConsumption const& _other);
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;
bool isInfinite;
};
/// 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
/// the state.
GasConsumption estimateMax(AssemblyItem const& _item);
u256 const& largestMemoryAccess() const { return m_largestMemoryAccess; }
private:
/// @returns _multiplier * (_value + 31) / 32, if _value is a known constant and infinite otherwise.
GasConsumption wordGas(u256 const& _multiplier, ExpressionClasses::Id _value);
@ -80,7 +86,7 @@ private:
inline std::ostream& operator<<(std::ostream& _str, GasMeter::GasConsumption const& _consumption)
{
if (_consumption.isInfinite)
return _str << "inf";
return _str << "[???]";
else
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())
{
// 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::JUMPI:
case Instruction::RETURN:

2
libsolidity/ASTPrinter.cpp

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

6
libsolidity/ASTPrinter.h

@ -24,7 +24,7 @@
#include <ostream>
#include <libsolidity/ASTVisitor.h>
#include <libsolidity/StructuralGasEstimator.h>
#include <libsolidity/GasEstimator.h>
namespace dev
{
@ -42,7 +42,7 @@ public:
ASTPrinter(
ASTNode const& _ast,
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.
void print(std::ostream& _stream);
@ -133,7 +133,7 @@ private:
int m_indentation;
std::string m_source;
ASTNode const* m_ast;
StructuralGasEstimator::ASTGasConsumption m_gasCosts;
GasEstimator::ASTGasConsumption m_gasCosts;
std::ostream* m_ostream;
};

5
libsolidity/Compiler.cpp

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

4
libsolidity/Compiler.h

@ -52,6 +52,10 @@ public:
/// @returns Assembly items of the runtime compiler context
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:
/// Registers the non-function objects inside the contract with the context.
void initializeContext(ContractDefinition const& _contract,

6
libsolidity/CompilerContext.cpp

@ -99,6 +99,12 @@ eth::AssemblyItem CompilerContext::getFunctionEntryLabel(Declaration const& _dec
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)
{
solAssert(!m_inheritanceHierarchy.empty(), "No inheritance hierarchy set.");

4
libsolidity/CompilerContext.h

@ -59,7 +59,11 @@ public:
bool isLocalVariable(Declaration const* _declaration) const;
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);
/// @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; }
/// @returns the entry label of the given function and takes overrides into account.
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;
}
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)
{
CompilerStack stack;

8
libsolidity/CompilerStack.h

@ -48,6 +48,7 @@ namespace solidity
// forward declarations
class Scanner;
class ContractDefinition;
class FunctionDefinition;
class SourceUnit;
class Compiler;
class GlobalContext;
@ -131,6 +132,13 @@ public:
/// does not exist.
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
/// scanning the source code - this is useful for printing exception information.
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.
*/
#include "StructuralGasEstimator.h"
#include "GasEstimator.h"
#include <map>
#include <functional>
#include <memory>
#include <libdevcore/SHA3.h>
#include <libevmasm/ControlFlowGraph.h>
#include <libevmasm/KnownState.h>
#include <libevmasm/PathGasMeter.h>
#include <libsolidity/AST.h>
#include <libsolidity/ASTVisitor.h>
#include <libsolidity/CompilerUtils.h>
using namespace std;
using namespace dev;
using namespace dev::eth;
using namespace dev::solidity;
StructuralGasEstimator::ASTGasConsumptionSelfAccumulated StructuralGasEstimator::performEstimation(
GasEstimator::ASTGasConsumptionSelfAccumulated GasEstimator::structuralEstimation(
AssemblyItems const& _items,
vector<ASTNode const*> const& _ast
)
{
solAssert(std::count(_ast.begin(), _ast.end(), nullptr) == 0, "");
map<SourceLocation, GasMeter::GasConsumption> particularCosts;
map<SourceLocation, GasConsumption> particularCosts;
ControlFlowGraph cfg(_items);
for (BasicBlock const& block: cfg.optimisedBlocks())
@ -72,7 +75,7 @@ StructuralGasEstimator::ASTGasConsumptionSelfAccumulated StructuralGasEstimator:
return gasCosts;
}
map<ASTNode const*, GasMeter::GasConsumption> StructuralGasEstimator::breakToStatementLevel(
map<ASTNode const*, GasMeter::GasConsumption> GasEstimator::breakToStatementLevel(
ASTGasConsumptionSelfAccumulated const& _gasCosts,
vector<ASTNode const*> const& _roots
)
@ -99,7 +102,7 @@ map<ASTNode const*, GasMeter::GasConsumption> StructuralGasEstimator::breakToSta
// we use the location of a node if
// - its statement depth is 0 or
// - 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)
{
return statementDepth.count(&_node);
@ -121,7 +124,53 @@ map<ASTNode const*, GasMeter::GasConsumption> StructuralGasEstimator::breakToSta
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
)
{
@ -140,4 +189,3 @@ set<ASTNode const*> StructuralGasEstimator::finestNodesAtLocation(
root->accept(visitor);
return nodes;
}

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

@ -34,17 +34,18 @@ namespace dev
namespace solidity
{
class StructuralGasEstimator
struct GasEstimator
{
public:
using ASTGasConsumption = std::map<ASTNode const*, eth::GasMeter::GasConsumption>;
using GasConsumption = eth::GasMeter::GasConsumption;
using ASTGasConsumption = std::map<ASTNode const*, GasConsumption>;
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
/// it by source location.
/// @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,
std::vector<ASTNode const*> const& _ast
);
@ -52,14 +53,30 @@ public:
/// the following source locations are part of the mapping:
/// 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
ASTGasConsumption breakToStatementLevel(
static ASTGasConsumption breakToStatementLevel(
ASTGasConsumptionSelfAccumulated const& _gasCosts,
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:
/// @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);
};
}

22
libsolidity/Token.h

@ -142,34 +142,34 @@ namespace solidity
K(Delete, "delete", 0) \
\
/* Keywords */ \
K(Anonymous, "anonymous", 0) \
K(Break, "break", 0) \
K(Const, "constant", 0) \
K(Anonymous, "anonymous", 0) \
K(Continue, "continue", 0) \
K(Contract, "contract", 0) \
K(Default, "default", 0) \
K(Do, "do", 0) \
K(Else, "else", 0) \
K(Enum, "enum", 0) \
K(Event, "event", 0) \
K(External, "external", 0) \
K(Is, "is", 0) \
K(Indexed, "indexed", 0) \
K(For, "for", 0) \
K(Function, "function", 0) \
K(If, "if", 0) \
K(Indexed, "indexed", 0) \
K(Internal, "internal", 0) \
K(Import, "import", 0) \
K(Is, "is", 0) \
K(Mapping, "mapping", 0) \
K(Modifier, "modifier", 0) \
K(New, "new", 0) \
K(Public, "public", 0) \
K(Private, "private", 0) \
K(Internal, "internal", 0) \
K(Return, "return", 0) \
K(Returns, "returns", 0) \
K(Struct, "struct", 0) \
K(Var, "var", 0) \
K(While, "while", 0) \
K(Enum, "enum", 0) \
\
/* Ether subdenominations */ \
K(SubWei, "wei", 0) \
@ -304,15 +304,21 @@ namespace solidity
T(Identifier, NULL, 0) \
\
/* Keywords reserved for future. use*/ \
T(String, "string", 0) \
K(As, "as", 0) \
K(Case, "case", 0) \
K(Catch, "catch", 0) \
K(Final, "final", 0) \
K(Let, "let", 0) \
K(Match, "match", 0) \
K(Of, "of", 0) \
K(Relocatable, "relocatable", 0) \
T(String, "string", 0) \
K(Switch, "switch", 0) \
K(Throw, "throw", 0) \
K(Try, "try", 0) \
K(Catch, "catch", 0) \
K(Using, "using", 0) \
K(Type, "type", 0) \
K(TypeOf, "typeof", 0) \
K(Using, "using", 0) \
/* Illegal token - not able to scan. */ \
T(Illegal, "ILLEGAL", 0) \
\

3
mix/ClientModel.cpp

@ -484,7 +484,10 @@ void ClientModel::showDebuggerForTransaction(ExecutionResult const& _t)
if (!functionName.isEmpty() && ((prevInstruction.getJumpType() == AssemblyItem::JumpType::IntoFunction) || solCallStack.empty()))
solCallStack.push_front(QVariant::fromValue(functionName));
else if (prevInstruction.getJumpType() == AssemblyItem::JumpType::OutOfFunction && !solCallStack.empty())
{
solCallStack.pop_front();
solLocals.clear();
}
}
//format solidity context values

5
mix/CodeModel.cpp

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

2
mix/qml/DebuggerPaneStyle.qml

@ -9,6 +9,8 @@ QtObject {
property QtObject general: QtObject {
property int basicFontSize: absoluteSize(-2)
property string basicColor: "#4a4a4a"
property string basicFont: "monospace"
property int dataDumpFontSize: absoluteSize(-3)
}
}

14
mix/qml/QIntTypeView.qml

@ -5,24 +5,24 @@ Item
property alias value: textinput.text
property alias readOnly: textinput.readOnly
id: editRoot
height: 20
width: readOnly ? textinput.implicitWidth : 150
SourceSansProBold
{
id: boldFont
DebuggerPaneStyle {
id: dbgStyle
}
Rectangle {
anchors.fill: parent
radius: 4
TextInput {
anchors.verticalCenter: parent.verticalCenter
id: textinput
text: value
anchors.fill: parent
font.family: boldFont.name
font.family: dbgStyle.general.basicFont
clip: true
selectByMouse: true
text: value
font.pointSize: dbgStyle.general.basicFontSize
color: dbgStyle.general.basicColor
}
}
}

15
mix/qml/StructView.qml

@ -13,6 +13,11 @@ Column
property string context
Layout.fillWidth: true
spacing: 0
DebuggerPaneStyle {
id: dbgStyle
}
Repeater
{
id: repeater
@ -29,6 +34,9 @@ Column
id: typeLabel
text: modelData.type.name
anchors.verticalCenter: parent.verticalCenter
font.family: dbgStyle.general.basicFont
color: dbgStyle.general.basicColor
font.pointSize: dbgStyle.general.basicFontSize
}
DefaultLabel {
@ -36,6 +44,9 @@ Column
id: nameLabel
text: modelData.name
anchors.verticalCenter: parent.verticalCenter
font.family: dbgStyle.general.basicFont
color: dbgStyle.general.basicColor
font.pointSize: dbgStyle.general.basicFontSize
}
DefaultLabel {
@ -43,7 +54,11 @@ Column
id: equalLabel
text: "="
anchors.verticalCenter: parent.verticalCenter
font.family: dbgStyle.general.basicFont
color: dbgStyle.general.basicColor
font.pointSize: dbgStyle.general.basicFontSize
}
Loader
{
id: typeLoader

2
mix/qml/VariablesView.qml

@ -18,7 +18,7 @@ DebugInfoList
property alias members: typeLoader.members;
property alias value: typeLoader.value;
anchors.fill: parent
anchors.rightMargin: 8
anchors.leftMargin: 10
StructView
{
id: typeLoader

59
solc/CommandLineInterface.cpp

@ -34,6 +34,7 @@
#include <libdevcore/CommonData.h>
#include <libdevcore/CommonIO.h>
#include <libevmcore/Instruction.h>
#include <libevmcore/Params.h>
#include <libsolidity/Scanner.h>
#include <libsolidity/Parser.h>
#include <libsolidity/ASTPrinter.h>
@ -42,7 +43,7 @@
#include <libsolidity/Exceptions.h>
#include <libsolidity/CompilerStack.h>
#include <libsolidity/SourceReferenceFormatter.h>
#include <libsolidity/StructuralGasEstimator.h>
#include <libsolidity/GasEstimator.h>
using namespace std;
namespace po = boost::program_options;
@ -55,6 +56,7 @@ namespace solidity
static string const g_argAbiStr = "json-abi";
static string const g_argSolAbiStr = "sol-abi";
static string const g_argSignatureHashes = "hashes";
static string const g_argGas = "gas";
static string const g_argAsmStr = "asm";
static string const g_argAsmJsonStr = "asm-json";
static string const g_argAstStr = "ast";
@ -94,6 +96,7 @@ static bool needsHumanTargetedStdout(po::variables_map const& _args)
{
return
_args.count(g_argGas) ||
humanTargetedStdout(_args, g_argAbiStr) ||
humanTargetedStdout(_args, g_argSolAbiStr) ||
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)
{
// Declare the supported options.
@ -278,6 +325,8 @@ bool CommandLineInterface::parseArguments(int argc, char** argv)
"Request to output the contract's Solidity ABI interface.")
(g_argSignatureHashes.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
"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"),
"Request to output the contract's Natspec user documentation.")
(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?
if (m_args.count(_argStr))
{
StructuralGasEstimator gasEstimator;
vector<ASTNode const*> asts;
for (auto const& sourceCode: m_sourceCodes)
asts.push_back(&m_compiler->getAST(sourceCode.first));
map<ASTNode const*, eth::GasMeter::GasConsumption> gasCosts;
if (m_compiler->getRuntimeAssemblyItems())
gasCosts = gasEstimator.breakToStatementLevel(
gasEstimator.performEstimation(*m_compiler->getRuntimeAssemblyItems(), asts),
gasCosts = GasEstimator::breakToStatementLevel(
GasEstimator::structuralEstimation(*m_compiler->getRuntimeAssemblyItems(), asts),
asts
);
@ -554,6 +602,9 @@ void CommandLineInterface::actOnInput()
}
}
if (m_args.count(g_argGas))
handleGasEstimation(contract);
handleBytecode(contract);
handleSignatureHashes(contract);
handleMeta(DocumentationType::ABIInterface, contract);

1
solc/CommandLineInterface.h

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

99
test/libsolidity/GasMeter.cpp

@ -23,8 +23,9 @@
#include <test/libsolidity/solidityExecutionFramework.h>
#include <libevmasm/GasMeter.h>
#include <libevmasm/KnownState.h>
#include <libevmasm/PathGasMeter.h>
#include <libsolidity/AST.h>
#include <libsolidity/StructuralGasEstimator.h>
#include <libsolidity/GasEstimator.h>
#include <libsolidity/SourceReferenceFormatter.h>
using namespace std;
@ -47,30 +48,47 @@ public:
m_compiler.setSource(_sourceCode);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed");
StructuralGasEstimator estimator;
AssemblyItems const* items = m_compiler.getRuntimeAssemblyItems("");
ASTNode const& sourceUnit = m_compiler.getAST();
BOOST_REQUIRE(items != nullptr);
m_gasCosts = estimator.breakToStatementLevel(
estimator.performEstimation(*items, vector<ASTNode const*>({&sourceUnit})),
m_gasCosts = GasEstimator::breakToStatementLevel(
GasEstimator::structuralEstimation(*items, vector<ASTNode const*>({&sourceUnit})),
{&sourceUnit}
);
}
void testCreationTimeGas(string const& _sourceCode, string const& _contractName = "")
void testCreationTimeGas(string const& _sourceCode)
{
compileAndRun(_sourceCode);
auto state = make_shared<KnownState>();
GasMeter meter(state);
GasMeter::GasConsumption gas;
for (AssemblyItem const& item: *m_compiler.getAssemblyItems(_contractName))
gas += meter.estimateMax(item);
u256 bytecodeSize(m_compiler.getRuntimeBytecode(_contractName).size());
PathGasMeter meter(*m_compiler.getAssemblyItems());
GasMeter::GasConsumption gas = meter.estimateMax(0, state);
u256 bytecodeSize(m_compiler.getRuntimeBytecode().size());
gas += bytecodeSize * c_createDataGas;
BOOST_REQUIRE(!gas.isInfinite);
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:
map<ASTNode const*, eth::GasMeter::GasConsumption> m_gasCosts;
};
@ -149,6 +167,67 @@ BOOST_AUTO_TEST_CASE(updating_store)
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()
}

Loading…
Cancel
Save