Filter false positives due to EVM errors.

Co-authored-by: Daniel Kirchner <daniel@ekpyron.org>
This commit is contained in:
Bhargava Shastry
2021-03-10 17:41:03 +01:00
co-authored by Daniel Kirchner
parent 34c08ea2c6
commit 24f42c5541
5 changed files with 76 additions and 19 deletions
+43 -6
View File
@@ -49,6 +49,10 @@ static evmc::VM evmone = evmc::VM{evmc_create_evmone()};
DEFINE_PROTO_FUZZER(Program const& _input)
{
// Solidity creates an invalid instruction for subobjects, so we simply
// ignore them in this fuzzer.
if (_input.has_obj())
return;
bool filterStatefulInstructions = true;
bool filterUnboundedLoops = true;
ProtoConverter converter(
@@ -95,17 +99,26 @@ DEFINE_PROTO_FUZZER(Program const& _input)
// If the fuzzer synthesized input does not contain the revert opcode which
// we lazily check by string find, the EVM call should not revert.
bool noRevertInSource = yul_source.find("revert") == string::npos;
bool noInvalidInSource = yul_source.find("invalid") == string::npos;
if (noInvalidInSource)
solAssert(
callResult.status_code != EVMC_INVALID_INSTRUCTION,
"Invalid instruction."
);
if (noRevertInSource)
solAssert(
callResult.status_code != EVMC_REVERT,
"SolidityEvmoneInterface: EVM One reverted"
);
// Out of gas errors are problematic because it is possible that the
// optimizer makes them go away, making EVM state impossible to
// compare in general.
if (callResult.status_code == EVMC_OUT_OF_GAS)
// Bail out on serious errors encountered during a call.
if (YulEvmoneUtility{}.seriousCallError(callResult.status_code))
return;
solAssert(
(callResult.status_code == EVMC_SUCCESS ||
(!noRevertInSource && callResult.status_code == EVMC_REVERT) ||
(!noInvalidInSource && callResult.status_code == EVMC_INVALID_INSTRUCTION)),
"Unoptimised call failed."
);
ostringstream unoptimizedState;
unoptimizedState << hostContext.dumpState(deployResult.create_address);
@@ -135,10 +148,34 @@ DEFINE_PROTO_FUZZER(Program const& _input)
callResultOpt.status_code != EVMC_REVERT,
"SolidityEvmoneInterface: EVM One reverted"
);
if (noInvalidInSource)
solAssert(
callResultOpt.status_code != EVMC_INVALID_INSTRUCTION,
"Invalid instruction."
);
solAssert(
(callResultOpt.status_code == EVMC_SUCCESS ||
(!noRevertInSource && callResultOpt.status_code == EVMC_REVERT) ||
(!noInvalidInSource && callResultOpt.status_code == EVMC_INVALID_INSTRUCTION)),
"Optimised call failed."
);
ostringstream optimizedState;
optimizedState << hostContext.dumpState(deployResultOpt.create_address);
int64_t constexpr tolerance = 1000;
if (callResult.gas_left > callResultOpt.gas_left)
if (callResult.gas_left - callResultOpt.gas_left > tolerance)
{
cout << "Gas differential " << callResult.gas_left - callResultOpt.gas_left << endl;
cout << "Unoptimised bytecode" << endl;
cout << util::toHex(unoptimisedByteCode) << endl;
cout << "Optimised bytecode" << endl;
cout << util::toHex(optimisedByteCode) << endl;
solAssert(false, "Optimised code consumed more than +1000 gas.");
}
solAssert(
unoptimizedState.str() == optimizedState.str(),
"Storage of unoptimised and optimised stack reused code do not match."
"State of unoptimised and optimised stack reused code do not match."
);
}
+18
View File
@@ -78,3 +78,21 @@ evmc_message YulEvmoneUtility::callMessage(evmc_address _address)
call.kind = EVMC_CALL;
return call;
}
bool YulEvmoneUtility::seriousCallError(evmc_status_code _code)
{
if (_code == EVMC_OUT_OF_GAS)
return true;
else if (_code == EVMC_STACK_OVERFLOW)
return true;
else if (_code == EVMC_STACK_UNDERFLOW)
return true;
else if (_code == EVMC_INTERNAL_ERROR)
return true;
else if (_code == EVMC_UNDEFINED_INSTRUCTION)
return true;
else if (_code == EVMC_INVALID_MEMORY_ACCESS)
return true;
else
return false;
}
+2
View File
@@ -54,5 +54,7 @@ struct YulEvmoneUtility
static evmc::result deployCode(solidity::bytes const& _input, EVMHost& _host);
/// @returns call message to be sent to @param _address.
static evmc_message callMessage(evmc_address _address);
/// @returns true if call result indicates a serious error, false otherwise.
static bool seriousCallError(evmc_status_code _code);
};
}