Run stack alloc optimisation

This commit is contained in:
Bhargava Shastry 2021-04-13 14:46:02 +02:00
parent 2e1264ffe3
commit 407d816745
3 changed files with 16 additions and 6 deletions

View File

@ -106,11 +106,14 @@ DEFINE_PROTO_FUZZER(Program const& _input)
}
solidity::frontend::OptimiserSettings settings = solidity::frontend::OptimiserSettings::none();
// Stack evader requires stack allocation to be done.
settings.optimizeStackAllocation = true;
AssemblyStack stackUnoptimized(version, AssemblyStack::Language::StrictAssembly, settings);
solAssert(
stackUnoptimized.parseAndAnalyze("source", yulSubObject),
"Parsing fuzzer generated input failed."
);
stackUnoptimized.optimize();
ostringstream unoptimizedState;
yulFuzzerUtil::TerminationReason termReason = yulFuzzerUtil::interpret(
unoptimizedState,
@ -121,11 +124,12 @@ DEFINE_PROTO_FUZZER(Program const& _input)
if (yulFuzzerUtil::resourceLimitsExceeded(termReason))
return;
AssemblyStack stackOptimized;
AssemblyStack stackOptimized(version, AssemblyStack::Language::StrictAssembly, settings);
solAssert(
stackOptimized.parseAndAnalyze("source", yulSubObject),
"Parsing fuzzer generated input failed."
);
stackOptimized.optimize();
YulOptimizerTestCommon optimizerTest(
stackOptimized.parserResult(),
EVMDialect::strictAssemblyForEVMObjects(version)
@ -150,6 +154,7 @@ DEFINE_PROTO_FUZZER(Program const& _input)
)")
("fuzzerInput", AsmPrinter{}(*astBlock))
.render();
cout << AsmPrinter{}(*astBlock) << endl;
bytes optimisedByteCode;
optimisedByteCode = YulAssembler{version, settings, optimisedProgram}.assemble();

View File

@ -233,7 +233,10 @@ void ProtoConverter::visit(Expression const& _x)
m_output << dictionaryToken();
break;
case Expression::kLowcall:
visit(_x.lowcall());
if (!m_filterStatefulInstructions)
visit(_x.lowcall());
else
m_output << dictionaryToken();
break;
case Expression::kCreate:
// Create and create2 return address of created contract which
@ -263,8 +266,9 @@ void ProtoConverter::visit(BinaryOp const& _x)
{
BinaryOp_BOp op = _x.op();
if ((op == BinaryOp::SHL || op == BinaryOp::SHR || op == BinaryOp::SAR) &&
!m_evmVersion.hasBitwiseShifting())
if (((op == BinaryOp::SHL || op == BinaryOp::SHR || op == BinaryOp::SAR) && !m_evmVersion.hasBitwiseShifting()) ||
(m_filterStatefulInstructions && op == BinaryOp::KECCAK)
)
{
m_output << dictionaryToken();
return;
@ -1282,7 +1286,8 @@ void ProtoConverter::visit(TerminatingStmt const& _x)
switch (_x.term_oneof_case())
{
case TerminatingStmt::kStopInvalid:
visit(_x.stop_invalid());
if (!m_filterStatefulInstructions)
visit(_x.stop_invalid());
break;
case TerminatingStmt::kRetRev:
visit(_x.ret_rev());

View File

@ -464,7 +464,7 @@ u256 EVMInstructionInterpreter::evalBuiltin(
}
else if (fun == "memoryguard")
{
return _evaluatedArguments.at(0);
return u256(std::get<Literal>(_arguments.at(0)).value.str());
}
else
yulAssert(false, "Unknown builtin: " + fun);