forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackLimitEvader.cpp
244 lines (216 loc) · 9.5 KB
/
StackLimitEvader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
This file is part of solidity.
solidity 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.
solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libyul/optimiser/ASTCopier.h>
#include <libyul/optimiser/StackLimitEvader.h>
#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/optimiser/FunctionCallFinder.h>
#include <libyul/optimiser/NameDispenser.h>
#include <libyul/optimiser/NameCollector.h>
#include <libyul/optimiser/StackToMemoryMover.h>
#include <libyul/backends/evm/ControlFlowGraphBuilder.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/AsmAnalysis.h>
#include <libyul/AST.h>
#include <libyul/CompilabilityChecker.h>
#include <libyul/Exceptions.h>
#include <libyul/Object.h>
#include <libyul/Utilities.h>
#include <libsolutil/Algorithms.h>
#include <libsolutil/CommonData.h>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/concat.hpp>
#include <range/v3/view/take.hpp>
using namespace solidity;
using namespace solidity::yul;
namespace
{
/**
* Walks the call graph using a Depth-First-Search assigning memory slots to variables.
* - The leaves of the call graph will get the lowest slot, increasing towards the root.
* - ``slotsRequiredForFunction`` maps a function to the number of slots it requires (which is also the
* next available slot that can be used by another function that calls this function).
* - For each function starting from the root of the call graph:
* - Visit all children that are not already visited.
* - Determine the maximum value ``n`` of the values of ``slotsRequiredForFunction`` among the children.
* - If the function itself contains variables that need memory slots, but is contained in a cycle,
* abort the process as failure.
* - If not, assign each variable its slot starting from ``n`` (incrementing it).
* - Assign ``n`` to ``slotsRequiredForFunction`` of the function.
*/
struct MemoryOffsetAllocator
{
uint64_t run(FunctionHandle _function = YulName{})
{
if (slotsRequiredForFunction.count(_function))
return slotsRequiredForFunction[_function];
// Assign to zero early to guard against recursive calls.
slotsRequiredForFunction[_function] = 0;
if (!std::holds_alternative<YulName>(_function))
return 0;
uint64_t requiredSlots = 0;
if (callGraph.count(std::get<YulName>(_function)))
for (FunctionHandle const& child: callGraph.at(std::get<YulName>(_function)))
requiredSlots = std::max(run(child), requiredSlots);
if (auto const* unreachables = util::valueOrNullptr(unreachableVariables, std::get<YulName>(_function)))
{
if (FunctionDefinition const* functionDefinition = util::valueOrDefault(functionDefinitions, std::get<YulName>(_function), nullptr, util::allow_copy))
if (
size_t totalArgCount = functionDefinition->returnVariables.size() + functionDefinition->parameters.size();
totalArgCount > 16
)
for (NameWithDebugData const& var: ranges::concat_view(
functionDefinition->parameters,
functionDefinition->returnVariables
) | ranges::views::take(totalArgCount - 16))
slotAllocations[var.name] = requiredSlots++;
// Assign slots for all variables that become unreachable in the function body, if the above did not
// assign a slot for them already.
for (YulName variable: *unreachables)
// The empty case is a function with too many arguments or return values,
// which was already handled above.
if (!variable.empty() && !slotAllocations.count(variable))
slotAllocations[variable] = requiredSlots++;
}
return slotsRequiredForFunction[_function] = requiredSlots;
}
/// Maps function names to the set of unreachable variables in that function.
/// An empty variable name means that the function has too many arguments or return variables.
std::map<YulName, std::vector<YulName>> const& unreachableVariables;
/// The graph of immediate function calls of all functions.
std::map<FunctionHandle, std::vector<FunctionHandle>> const& callGraph;
/// Maps the name of each user-defined function to its definition.
std::map<YulName, FunctionDefinition const*> const& functionDefinitions;
/// Maps variable names to the memory slot the respective variable is assigned.
std::map<YulName, uint64_t> slotAllocations{};
/// Maps function names to the number of memory slots the respective function requires.
std::map<FunctionHandle, uint64_t> slotsRequiredForFunction{};
};
u256 literalArgumentValue(FunctionCall const& _call)
{
yulAssert(_call.arguments.size() == 1, "");
Literal const* literal = std::get_if<Literal>(&_call.arguments.front());
yulAssert(literal && literal->kind == LiteralKind::Number, "");
return literal->value.value();
}
}
Block StackLimitEvader::run(
OptimiserStepContext& _context,
Object const& _object
)
{
yulAssert(_object.hasCode());
auto const* evmDialect = dynamic_cast<EVMDialect const*>(&_context.dialect);
yulAssert(
evmDialect && evmDialect->providesObjectAccess(),
"StackLimitEvader can only be run on objects using the EVMDialect with object access."
);
yulAssert(
!evmDialect->eofVersion().has_value(),
"StackLimitEvader does not support EOF."
);
auto astRoot = std::get<Block>(ASTCopier{}(_object.code()->root()));
if (evmDialect && evmDialect->evmVersion().canOverchargeGasForCall())
{
yul::AsmAnalysisInfo analysisInfo = yul::AsmAnalyzer::analyzeStrictAssertCorrect(
*evmDialect,
astRoot,
_object.summarizeStructure()
);
std::unique_ptr<CFG> cfg = ControlFlowGraphBuilder::build(analysisInfo, *evmDialect, astRoot);
run(_context, astRoot, StackLayoutGenerator::reportStackTooDeep(*cfg, *evmDialect));
}
else
{
run(_context, astRoot, CompilabilityChecker{
_object,
true,
}.unreachableVariables);
}
return astRoot;
}
void StackLimitEvader::run(
OptimiserStepContext& _context,
Block& _astRoot,
std::map<YulName, std::vector<StackLayoutGenerator::StackTooDeep>> const& _stackTooDeepErrors
)
{
auto const* evmDialect = dynamic_cast<EVMDialect const*>(&_context.dialect);
yulAssert(
evmDialect && evmDialect->providesObjectAccess(),
"StackLimitEvader can only be run on objects using the EVMDialect with object access."
);
yulAssert(
!evmDialect->eofVersion().has_value(),
"StackLimitEvader does not support EOF."
);
std::map<YulName, std::vector<YulName>> unreachableVariables;
for (auto&& [function, stackTooDeepErrors]: _stackTooDeepErrors)
{
auto& unreachables = unreachableVariables[function];
// TODO: choose wisely.
for (auto const& stackTooDeepError: stackTooDeepErrors)
for (auto variable: stackTooDeepError.variableChoices | ranges::views::take(stackTooDeepError.deficit))
if (!util::contains(unreachables, variable))
unreachables.emplace_back(variable);
}
run(_context, _astRoot, unreachableVariables);
}
void StackLimitEvader::run(
OptimiserStepContext& _context,
Block& _astRoot,
std::map<YulName, std::vector<YulName>> const& _unreachableVariables
)
{
auto const* evmDialect = dynamic_cast<EVMDialect const*>(&_context.dialect);
yulAssert(
evmDialect && evmDialect->providesObjectAccess(),
"StackLimitEvader can only be run on objects using the EVMDialect with object access."
);
yulAssert(
!evmDialect->eofVersion().has_value(),
"StackLimitEvader does not support EOF."
);
auto const memoryGuardHandle = evmDialect->findBuiltin("memoryguard");
yulAssert(memoryGuardHandle, "Compiling with object access, memoryguard should be available as builtin.");
std::vector<FunctionCall*> const memoryGuardCalls = findFunctionCalls(_astRoot, *memoryGuardHandle);
// Do not optimise, if no ``memoryguard`` call is found.
if (memoryGuardCalls.empty())
return;
// Make sure all calls to ``memoryguard`` we found have the same value as argument (otherwise, abort).
u256 reservedMemory = literalArgumentValue(*memoryGuardCalls.front());
yulAssert(reservedMemory < u256(1) << 32 - 1, "");
for (FunctionCall const* memoryGuardCall: memoryGuardCalls)
if (reservedMemory != literalArgumentValue(*memoryGuardCall))
return;
CallGraph callGraph = CallGraphGenerator::callGraph(_astRoot);
// We cannot move variables in recursive functions to fixed memory offsets.
for (FunctionHandle function: callGraph.recursiveFunctions())
{
yulAssert(std::holds_alternative<YulName>(function), "Builtins are not recursive.");
if (_unreachableVariables.count(std::get<YulName>(function)))
return;
}
std::map<YulName, FunctionDefinition const*> functionDefinitions = allFunctionDefinitions(_astRoot);
MemoryOffsetAllocator memoryOffsetAllocator{_unreachableVariables, callGraph.functionCalls, functionDefinitions};
uint64_t requiredSlots = memoryOffsetAllocator.run();
yulAssert(requiredSlots < (uint64_t(1) << 32) - 1, "");
StackToMemoryMover::run(_context, reservedMemory, memoryOffsetAllocator.slotAllocations, requiredSlots, _astRoot);
reservedMemory += 32 * requiredSlots;
for (FunctionCall* memoryGuardCall: findFunctionCalls(_astRoot, *memoryGuardHandle))
{
Literal* literal = std::get_if<Literal>(&memoryGuardCall->arguments.front());
yulAssert(literal && literal->kind == LiteralKind::Number, "");
literal->value = LiteralValue{reservedMemory, toCompactHexWithPrefix(reservedMemory)};
}
}