forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInspector.h
212 lines (180 loc) · 5.93 KB
/
Inspector.h
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
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Yul inspector.
*/
#include <test/tools/yulInterpreter/Interpreter.h>
#include <libyul/AST.h>
#include <string>
#pragma once
namespace solidity::yul::test
{
/**
* Inspector class to respond to queries by the user for:
*
* * Stepping through and over instructions.
* * Printing a specific or all variables.
* * Inspecting memory, storage and calldata memory.
*/
class Inspector
{
public:
enum class NodeAction
{
RunNode,
StepThroughNode,
};
Inspector(std::string const& _source, InterpreterState const& _state)
:m_source(_source), m_state(_state) {}
/* Asks the user what action to take.
* @returns NodeAction::RunNode if the current AST node (and all children nodes!) should be
* processed without stopping, else NodeAction::StepThroughNode.
*/
NodeAction queryUser(langutil::DebugData const& _data, std::map<YulName, u256> const& _variables);
void stepMode(NodeAction _action) { m_stepMode = _action; }
std::string const& source() const { return m_source; }
void interactiveVisit(langutil::DebugData const& _debugData, std::map<YulName, u256> const& _variables, std::function<void()> _visitNode)
{
Inspector::NodeAction action = queryUser(_debugData, _variables);
if (action == NodeAction::RunNode)
{
// user requested to run the whole node without stopping
stepMode(Inspector::NodeAction::RunNode);
_visitNode();
// Reset step mode back
stepMode(Inspector::NodeAction::StepThroughNode);
}
else
_visitNode();
}
private:
std::string currentSource(langutil::DebugData const& _data) const;
/// Source of the file
std::string const& m_source;
/// State of the interpreter
InterpreterState const& m_state;
/// Last user query command
std::string m_lastInput;
/// Used to run AST nodes without user interaction
NodeAction m_stepMode = NodeAction::StepThroughNode;
};
/**
* Yul Interpreter with inspection. Allows the user to go through the code step
* by step and inspect the state using the `Inspector` class
*/
class InspectedInterpreter: public Interpreter
{
public:
static void run(
std::shared_ptr<Inspector> _inspector,
InterpreterState& _state,
AST const& _ast,
bool _disableExternalCalls,
bool _disableMemoryTracing
);
InspectedInterpreter(
std::shared_ptr<Inspector> _inspector,
InterpreterState& _state,
Dialect const& _dialect,
Scope& _scope,
bool _disableExternalCalls,
bool _disableMemoryTracing,
std::map<YulName, u256> _variables = {}
):
Interpreter(_state, _dialect, _scope, _disableExternalCalls, _disableMemoryTracing, _variables),
m_inspector(_inspector)
{
}
void operator()(ExpressionStatement const& _node) override { helper(_node); }
void operator()(Assignment const& _node) override { helper(_node); }
void operator()(VariableDeclaration const& _node) override { helper(_node); }
void operator()(If const& _node) override { helper(_node); }
void operator()(Switch const& _node) override { helper(_node); }
void operator()(ForLoop const& _node) override { helper(_node); }
void operator()(Break const& _node) override { helper(_node); }
void operator()(Continue const& _node) override { helper(_node); }
void operator()(Leave const& _node) override { helper(_node); }
void operator()(Block const& _node) override { helper(_node); }
protected:
/// Asserts that the expression evaluates to exactly one value and returns it.
u256 evaluate(Expression const& _expression) override;
/// Evaluates the expression and returns its value.
std::vector<u256> evaluateMulti(Expression const& _expression) override;
private:
std::shared_ptr<Inspector> m_inspector;
template <typename ConcreteNode>
void helper(ConcreteNode const& _node)
{
m_inspector->interactiveVisit(*_node.debugData, m_variables, [&]() {
Interpreter::operator()(_node);
});
}
};
class InspectedExpressionEvaluator: public ExpressionEvaluator
{
public:
InspectedExpressionEvaluator(
std::shared_ptr<Inspector> _inspector,
InterpreterState& _state,
Dialect const& _dialect,
Scope& _scope,
std::map<YulName, u256> const& _variables,
bool _disableExternalCalls,
bool _disableMemoryTrace
):
ExpressionEvaluator(_state, _dialect, _scope, _variables, _disableExternalCalls, _disableMemoryTrace),
m_inspector(_inspector)
{}
template <typename ConcreteNode>
void helper(ConcreteNode const& _node)
{
m_inspector->interactiveVisit(*_node.debugData, m_variables, [&]() {
ExpressionEvaluator::operator()(_node);
});
}
void operator()(Literal const& _node) override { helper(_node); }
void operator()(Identifier const& _node) override { helper(_node); }
void operator()(FunctionCall const& _node) override { helper(_node); }
protected:
std::unique_ptr<Interpreter> makeInterpreterCopy(std::map<YulName, u256> _variables = {}) const override
{
return std::make_unique<InspectedInterpreter>(
m_inspector,
m_state,
m_dialect,
m_scope,
m_disableExternalCalls,
m_disableMemoryTrace,
std::move(_variables)
);
}
std::unique_ptr<Interpreter> makeInterpreterNew(InterpreterState& _state, Scope& _scope) const override
{
return std::make_unique<InspectedInterpreter>(
std::make_unique<Inspector>(
m_inspector->source(),
_state
),
_state,
m_dialect,
_scope,
m_disableExternalCalls,
m_disableMemoryTrace
);
}
private:
std::shared_ptr<Inspector> m_inspector;
};
}