/src/solidity/libevmasm/PathGasMeter.h
Line | Count | Source |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | /** @file PathGasMeter.cpp |
19 | | * @author Christian <c@ethdev.com> |
20 | | * @date 2015 |
21 | | */ |
22 | | |
23 | | #pragma once |
24 | | |
25 | | #include <libevmasm/GasMeter.h> |
26 | | |
27 | | #include <liblangutil/EVMVersion.h> |
28 | | |
29 | | #include <set> |
30 | | #include <vector> |
31 | | #include <memory> |
32 | | |
33 | | namespace solidity::evmasm |
34 | | { |
35 | | |
36 | | class KnownState; |
37 | | |
38 | | struct GasPath |
39 | | { |
40 | | size_t index = 0; |
41 | | std::shared_ptr<KnownState> state; |
42 | | u256 largestMemoryAccess; |
43 | | GasMeter::GasConsumption gas; |
44 | | std::set<size_t> visitedJumpdests; |
45 | | }; |
46 | | |
47 | | /** |
48 | | * Computes an upper bound on the gas usage of a computation starting at a certain position in |
49 | | * a list of AssemblyItems in a given state until the computation stops. |
50 | | * Can be used to estimate the gas usage of functions on any given input. |
51 | | */ |
52 | | class PathGasMeter |
53 | | { |
54 | | public: |
55 | | explicit PathGasMeter(AssemblyItems const& _items, langutil::EVMVersion _evmVersion); |
56 | | |
57 | | GasMeter::GasConsumption estimateMax(size_t _startIndex, std::shared_ptr<KnownState> const& _state); |
58 | | |
59 | | static GasMeter::GasConsumption estimateMax( |
60 | | AssemblyItems const& _items, |
61 | | langutil::EVMVersion _evmVersion, |
62 | | size_t _startIndex, |
63 | | std::shared_ptr<KnownState> const& _state |
64 | | ) |
65 | 0 | { |
66 | 0 | return PathGasMeter(_items, _evmVersion).estimateMax(_startIndex, _state); |
67 | 0 | } |
68 | | |
69 | | private: |
70 | | /// Adds a new path item to the queue, but only if we do not already have |
71 | | /// a higher gas usage at that point. |
72 | | /// This is not exact as different state might influence higher gas costs at a later |
73 | | /// point in time, but it greatly reduces computational overhead. |
74 | | void queue(std::unique_ptr<GasPath>&& _newPath); |
75 | | GasMeter::GasConsumption handleQueueItem(); |
76 | | |
77 | | /// Map of jumpdest -> gas path, so not really a queue. We only have one queued up |
78 | | /// item per jumpdest, because of the behaviour of `queue` above. |
79 | | std::map<size_t, std::unique_ptr<GasPath>> m_queue; |
80 | | std::map<size_t, GasMeter::GasConsumption> m_highestGasUsagePerJumpdest; |
81 | | std::map<u256, size_t> m_tagPositions; |
82 | | AssemblyItems const& m_items; |
83 | | langutil::EVMVersion m_evmVersion; |
84 | | }; |
85 | | |
86 | | } |