From fba7e055d9fb20b3053362f0099d2fbaacfb7432 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 4 May 2018 17:18:02 +0200 Subject: [PATCH 1/2] Follow highest gas usage only for gas estimation. --- Changelog.md | 1 + libevmasm/PathGasMeter.cpp | 19 +++++++++++++++---- libevmasm/PathGasMeter.h | 10 +++++++++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Changelog.md b/Changelog.md index 37e0b0a1e..87669a62c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -3,6 +3,7 @@ Features: * Build System: Update internal dependency of jsoncpp to 1.8.4, which introduces more strictness and reduces memory usage. * Code Generator: Use native shift instructions on target Constantinople. + * Gas Estimator: Only explore paths with higher gas costs. This reduces accuracy but greatly improves the speed of gas estimation. * Optimizer: Remove unnecessary masking of the result of known short instructions (``ADDRESS``, ``CALLER``, ``ORIGIN`` and ``COINBASE``). * Type Checker: Deprecate the ``years`` unit denomination and raise a warning for it (or an error as experimental 0.5.0 feature). * Type Checker: Make literals (without explicit type casting) an error for tight packing as experimental 0.5.0 feature. diff --git a/libevmasm/PathGasMeter.cpp b/libevmasm/PathGasMeter.cpp index 3fe682b7f..cdadba768 100644 --- a/libevmasm/PathGasMeter.cpp +++ b/libevmasm/PathGasMeter.cpp @@ -43,7 +43,7 @@ GasMeter::GasConsumption PathGasMeter::estimateMax( auto path = unique_ptr(new GasPath()); path->index = _startIndex; path->state = _state->copy(); - m_queue.push_back(move(path)); + queue(move(path)); GasMeter::GasConsumption gas; while (!m_queue.empty() && !gas.isInfinite) @@ -51,12 +51,23 @@ GasMeter::GasConsumption PathGasMeter::estimateMax( return gas; } +void PathGasMeter::queue(std::unique_ptr&& _newPath) +{ + if ( + m_highestGasUsagePerJumpdest.count(_newPath->index) && + _newPath->gas < m_highestGasUsagePerJumpdest.at(_newPath->index) + ) + return; + m_highestGasUsagePerJumpdest[_newPath->index] = _newPath->gas; + m_queue[_newPath->index] = move(_newPath); +} + GasMeter::GasConsumption PathGasMeter::handleQueueItem() { assertThrow(!m_queue.empty(), OptimizerException, ""); - unique_ptr path = move(m_queue.back()); - m_queue.pop_back(); + unique_ptr path = move(m_queue.rbegin()->second); + m_queue.erase(--m_queue.end()); shared_ptr state = path->state; GasMeter meter(state, m_evmVersion, path->largestMemoryAccess); @@ -117,7 +128,7 @@ GasMeter::GasConsumption PathGasMeter::handleQueueItem() newPath->largestMemoryAccess = meter.largestMemoryAccess(); newPath->state = state->copy(); newPath->visitedJumpdests = path->visitedJumpdests; - m_queue.push_back(move(newPath)); + queue(move(newPath)); } if (branchStops) diff --git a/libevmasm/PathGasMeter.h b/libevmasm/PathGasMeter.h index 2527d7fb9..9537b176b 100644 --- a/libevmasm/PathGasMeter.h +++ b/libevmasm/PathGasMeter.h @@ -58,9 +58,17 @@ public: GasMeter::GasConsumption estimateMax(size_t _startIndex, std::shared_ptr const& _state); private: + /// Adds a new path item to the queue, but only if we do not already have + /// a higher gas usage at that point. + /// This is not exact as different state might influence higher gas costs at a later + /// point in time, but it greatly reduces computational overhead. + void queue(std::unique_ptr&& _newPath); GasMeter::GasConsumption handleQueueItem(); - std::vector> m_queue; + /// Map of jumpdest -> gas path, so not really a queue. We only have one queued up + /// item per jumpdest, because of the behaviour of `queue` above. + std::map> m_queue; + std::map m_highestGasUsagePerJumpdest; std::map m_tagPositions; AssemblyItems const& m_items; solidity::EVMVersion m_evmVersion; From bbae4fb0ef41361d24eadcc0a93cf02052493d10 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 4 May 2018 17:46:47 +0200 Subject: [PATCH 2/2] Test with high path complexity. --- test/libsolidity/GasMeter.cpp | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/libsolidity/GasMeter.cpp b/test/libsolidity/GasMeter.cpp index 0d66456ce..f16d9abe1 100644 --- a/test/libsolidity/GasMeter.cpp +++ b/test/libsolidity/GasMeter.cpp @@ -307,6 +307,46 @@ BOOST_AUTO_TEST_CASE(regular_functions_exclude_fallback) testCreationTimeGas(sourceCode); testRunTimeGas("x()", vector{encodeArgs()}); } + +BOOST_AUTO_TEST_CASE(complex_control_flow) +{ + // This crashed the gas estimator previously (or took a very long time). + // Now we do not follow branches if they start out with lower gas costs than the ones + // we previously considered. This of course reduces accuracy. + char const* sourceCode = R"( + contract log { + function ln(int128 x) constant returns (int128 result) { + int128 t = x / 256; + int128 y = 5545177; + x = t; + t = x * 16; if (t <= 1000000) { x = t; y = y - 2772588; } + t = x * 4; if (t <= 1000000) { x = t; y = y - 1386294; } + t = x * 2; if (t <= 1000000) { x = t; y = y - 693147; } + t = x + x / 2; if (t <= 1000000) { x = t; y = y - 405465; } + t = x + x / 4; if (t <= 1000000) { x = t; y = y - 223144; } + t = x + x / 8; if (t <= 1000000) { x = t; y = y - 117783; } + t = x + x / 16; if (t <= 1000000) { x = t; y = y - 60624; } + t = x + x / 32; if (t <= 1000000) { x = t; y = y - 30771; } + t = x + x / 64; if (t <= 1000000) { x = t; y = y - 15504; } + t = x + x / 128; if (t <= 1000000) { x = t; y = y - 7782; } + t = x + x / 256; if (t <= 1000000) { x = t; y = y - 3898; } + t = x + x / 512; if (t <= 1000000) { x = t; y = y - 1951; } + t = x + x / 1024; if (t <= 1000000) { x = t; y = y - 976; } + t = x + x / 2048; if (t <= 1000000) { x = t; y = y - 488; } + t = x + x / 4096; if (t <= 1000000) { x = t; y = y - 244; } + t = x + x / 8192; if (t <= 1000000) { x = t; y = y - 122; } + t = x + x / 16384; if (t <= 1000000) { x = t; y = y - 61; } + t = x + x / 32768; if (t <= 1000000) { x = t; y = y - 31; } + t = x + x / 65536; if (t <= 1000000) { y = y - 15; } + return y; + } + } + )"; + testCreationTimeGas(sourceCode); + // max gas is used for small x + testRunTimeGas("ln(int128)", vector{encodeArgs(0), encodeArgs(10), encodeArgs(105), encodeArgs(30000)}); +} + BOOST_AUTO_TEST_SUITE_END() }