Yul interpreter: Add flag to disable memory tracing and dump for fuzzing.

Model revert in yul interpreter.
Add logTrace for a few more instructions and clear trace on revert.
This commit is contained in:
Bhargava Shastry
2022-01-04 11:56:27 +01:00
parent 13723fe2bf
commit b8ad2b2718
11 changed files with 158 additions and 50 deletions
+22 -14
View File
@@ -55,26 +55,34 @@ void InterpreterState::dumpStorage(ostream& _out) const
_out << " " << slot.first.hex() << ": " << slot.second.hex() << endl;
}
void InterpreterState::dumpTraceAndState(ostream& _out) const
void InterpreterState::dumpTraceAndState(ostream& _out, bool _disableMemoryTrace) const
{
_out << "Trace:" << endl;
for (auto const& line: trace)
_out << " " << line << endl;
_out << "Memory dump:\n";
map<u256, u256> words;
for (auto const& [offset, value]: memory)
words[(offset / 0x20) * 0x20] |= u256(uint32_t(value)) << (256 - 8 - 8 * static_cast<size_t>(offset % 0x20));
for (auto const& [offset, value]: words)
if (value != 0)
_out << " " << std::uppercase << std::hex << std::setw(4) << offset << ": " << h256(value).hex() << endl;
if (!_disableMemoryTrace)
{
_out << "Memory dump:\n";
map<u256, u256> words;
for (auto const& [offset, value]: memory)
words[(offset / 0x20) * 0x20] |= u256(uint32_t(value)) << (256 - 8 - 8 * static_cast<size_t>(offset % 0x20));
for (auto const& [offset, value]: words)
if (value != 0)
_out << " " << std::uppercase << std::hex << std::setw(4) << offset << ": " << h256(value).hex() << endl;
}
_out << "Storage dump:" << endl;
dumpStorage(_out);
}
void Interpreter::run(InterpreterState& _state, Dialect const& _dialect, Block const& _ast)
void Interpreter::run(
InterpreterState& _state,
Dialect const& _dialect,
Block const& _ast,
bool _disableMemoryTrace
)
{
Scope scope;
Interpreter{_state, _dialect, scope}(_ast);
Interpreter{_state, _dialect, scope, _disableMemoryTrace}(_ast);
}
void Interpreter::operator()(ExpressionStatement const& _expressionStatement)
@@ -209,14 +217,14 @@ void Interpreter::operator()(Block const& _block)
u256 Interpreter::evaluate(Expression const& _expression)
{
ExpressionEvaluator ev(m_state, m_dialect, *m_scope, m_variables);
ExpressionEvaluator ev(m_state, m_dialect, *m_scope, m_variables, m_disableMemoryTrace);
ev.visit(_expression);
return ev.value();
}
vector<u256> Interpreter::evaluateMulti(Expression const& _expression)
{
ExpressionEvaluator ev(m_state, m_dialect, *m_scope, m_variables);
ExpressionEvaluator ev(m_state, m_dialect, *m_scope, m_variables, m_disableMemoryTrace);
ev.visit(_expression);
return ev.values();
}
@@ -279,7 +287,7 @@ void ExpressionEvaluator::operator()(FunctionCall const& _funCall)
{
if (BuiltinFunctionForEVM const* fun = dialect->builtin(_funCall.functionName.name))
{
EVMInstructionInterpreter interpreter(m_state);
EVMInstructionInterpreter interpreter(m_state, m_disableMemoryTrace);
setValue(interpreter.evalBuiltin(*fun, _funCall.arguments, values()));
return;
}
@@ -308,7 +316,7 @@ void ExpressionEvaluator::operator()(FunctionCall const& _funCall)
variables[fun->returnVariables.at(i).name] = 0;
m_state.controlFlowState = ControlFlowState::Default;
Interpreter interpreter(m_state, m_dialect, *scope, std::move(variables));
Interpreter interpreter(m_state, m_dialect, *scope, m_disableMemoryTrace, std::move(variables));
interpreter(fun->body);
m_state.controlFlowState = ControlFlowState::Default;