yulRun: Fix unexpected return value for call* instructions and add tests

This commit is contained in:
Marenz 2022-09-13 13:16:59 +02:00
parent b8699e7687
commit d10d967008
9 changed files with 73 additions and 5 deletions

View File

@ -51,6 +51,7 @@ YulInterpreterTest::YulInterpreterTest(string const& _filename):
{
m_source = m_reader.source();
m_expectation = m_reader.simpleExpectations();
m_simulateExternalCallsToSelf = m_reader.boolSetting("simulateExternalCall", false);
}
TestCase::TestResult YulInterpreterTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted)
@ -98,8 +99,8 @@ string YulInterpreterTest::interpret()
state,
EVMDialect::strictAssemblyForEVMObjects(langutil::EVMVersion{}),
*m_ast,
/*disableExternalCalls=*/true,
/*disableMemoryTracing=*/false
/*disableExternalCalls=*/ !m_simulateExternalCallsToSelf,
/*disableMemoryTracing=*/ false
);
}
catch (InterpreterTerminatedGeneric const&)

View File

@ -47,6 +47,7 @@ private:
std::shared_ptr<Block> m_ast;
std::shared_ptr<AsmAnalysisInfo> m_analysisInfo;
bool m_simulateExternalCallsToSelf = false;
};
}

View File

@ -0,0 +1,22 @@
{
mstore(0x40, 0x42)
if iszero(calldatasize()) {
let x := call(gas(), address(), 0, 0x40, 0x20, 0x100, 0x20)
sstore(0x64, calldataload(0))
sstore(0x100, x)
return(0x0, 0)
}
return(0x40, 0x20)
}
// ====
// simulateExternalCall: true
// ----
// Trace:
// CALL(153, 0x11111111, 0, 64, 32, 256, 32)
// RETURN(0, 0)
// Memory dump:
// 40: 0000000000000000000000000000000000000000000000000000000000000042
// 100: 0000000000000000000000000000000000000000000000000000000000000042
// Storage dump:
// 0000000000000000000000000000000000000000000000000000000000000100: 0000000000000000000000000000000000000000000000000000000000000001

View File

@ -1,6 +1,6 @@
{
let x := call(gas(), 0x45, 0x5, 0, 0x20, 0x30, 0x20)
sstore(100, x)
sstore(0x64, x)
}
// ----
// Trace:

View File

@ -0,0 +1,10 @@
{
let x := callcode(gas(), 0x45, 0x5, 0, 0x20, 0x30, 0x20)
sstore(100, x)
}
// ----
// Trace:
// CALLCODE(153, 69, 5, 0, 32, 48, 32)
// Memory dump:
// Storage dump:
// 0000000000000000000000000000000000000000000000000000000000000064: 0000000000000000000000000000000000000000000000000000000000000001

View File

@ -0,0 +1,10 @@
{
let x := delegatecall(gas(), 0x45, 0, 0x20, 0x30, 0x20)
sstore(100, x)
}
// ----
// Trace:
// DELEGATECALL(153, 69, 0, 32, 48, 32)
// Memory dump:
// Storage dump:
// 0000000000000000000000000000000000000000000000000000000000000064: 0000000000000000000000000000000000000000000000000000000000000001

View File

@ -0,0 +1,12 @@
{
let x := staticcall(gas(), 0x45, 0, 0x20, 0x30, 0x20)
sstore(0x64, x)
}
// ====
// EVMVersion: >=byzantium
// ----
// Trace:
// STATICCALL(153, 69, 0, 32, 48, 32)
// Memory dump:
// Storage dump:
// 0000000000000000000000000000000000000000000000000000000000000064: 0000000000000000000000000000000000000000000000000000000000000001

View File

@ -329,13 +329,24 @@ u256 EVMInstructionInterpreter::eval(
accessMemory(arg[3], arg[4]);
accessMemory(arg[5], arg[6]);
logTrace(_instruction, arg);
return arg[0] & 1;
// Randomly fail based on the called address if it isn't a call to self.
// Used for fuzzing.
return (
(arg[0] > 0) &&
(arg[1] == util::h160::Arith(m_state.address) || (arg[1] & 1))
) ? 1 : 0;
case Instruction::DELEGATECALL:
case Instruction::STATICCALL:
accessMemory(arg[2], arg[3]);
accessMemory(arg[4], arg[5]);
logTrace(_instruction, arg);
return 0;
// Randomly fail based on the called address if it isn't a call to self.
// Used for fuzzing.
return (
(arg[0] > 0) &&
(arg[1] == util::h160::Arith(m_state.address) || (arg[1] & 1))
) ? 1 : 0;
case Instruction::RETURN:
{
m_state.returndata = {};

View File

@ -500,5 +500,6 @@ void ExpressionEvaluator::runExternalCall(evmasm::Instruction _instruction)
0,
memOutSize.convert_to<size_t>()
);
m_state.returndata = newInterpreter->returnData();
}
}