Code generation for errors.

This commit is contained in:
chriseth 2021-01-28 12:56:22 +01:00
parent f748f44a79
commit 3f417324f1
19 changed files with 410 additions and 104 deletions

View File

@ -103,6 +103,22 @@ void CompilerUtils::revertWithStringData(Type const& _argumentType)
m_context << Instruction::REVERT;
}
void CompilerUtils::revertWithError(
string const& _signature,
vector<Type const*> const& _parameterTypes,
vector<Type const*> const& _argumentTypes
)
{
fetchFreeMemoryPointer();
m_context << util::selectorFromSignature(_signature);
m_context << Instruction::DUP2 << Instruction::MSTORE;
m_context << u256(4) << Instruction::ADD;
// Stack: <arguments...> <mem pos of encoding start>
abiEncode(_argumentTypes, _parameterTypes);
toSizeAfterFreeMemoryPointer();
m_context << Instruction::REVERT;
}
void CompilerUtils::returnDataToArray()
{
if (m_context.evmVersion().supportsReturndata())

View File

@ -69,6 +69,12 @@ public:
/// Stack post:
void revertWithStringData(Type const& _argumentType);
void revertWithError(
std::string const& _errorName,
std::vector<Type const*> const& _parameterTypes,
std::vector<Type const*> const& _argumentTypes
);
/// Allocates a new array and copies the return data to it.
/// If the EVM does not support return data, creates an empty array.
void returnDataToArray();

View File

@ -29,6 +29,7 @@
#include <libsolidity/codegen/LValue.h>
#include <libsolidity/ast/AST.h>
#include <libsolidity/ast/ASTUtils.h>
#include <libsolidity/ast/TypeProvider.h>
#include <libevmasm/GasMeter.h>
@ -786,27 +787,46 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
break;
case FunctionType::Kind::Revert:
{
if (arguments.empty())
bool usesString =
!arguments.empty() &&
arguments.front()->annotation().type->isImplicitlyConvertibleTo(*TypeProvider::stringMemory());
if (arguments.empty() || (usesString && m_context.revertStrings() == RevertStrings::Strip))
{
if (!arguments.empty() && !*arguments.front()->annotation().isPure)
{
arguments.front()->accept(*this);
utils().popStackElement(*arguments.front()->annotation().type);
}
m_context.appendRevert();
}
else
{
// function-sel(Error(string)) + encoding
solAssert(arguments.size() == 1, "");
solUnimplementedAssert(arguments.front()->annotation().type->isImplicitlyConvertibleTo(*TypeProvider::stringMemory()), "");
if (m_context.revertStrings() == RevertStrings::Strip)
arguments.front()->accept(*this);
string signature;
vector<Type const*> parameterTypes;
vector<Type const*> argumentTypes;
if (usesString)
{
if (!*arguments.front()->annotation().isPure)
{
arguments.front()->accept(*this);
utils().popStackElement(*arguments.front()->annotation().type);
}
m_context.appendRevert();
signature = "Error(string)";
parameterTypes.push_back(TypeProvider::stringMemory());
argumentTypes = vector<Type const*>{arguments.front()->annotation().type};
}
else
{
arguments.front()->accept(*this);
utils().revertWithStringData(*arguments.front()->annotation().type);
ErrorDefinition const* error = nullptr;
FunctionCall const* errorCall = dynamic_cast<FunctionCall const*>(_functionCall.arguments().back().get());
solAssert(errorCall, "");
solAssert(*errorCall->annotation().kind == FunctionCallKind::FunctionCall, "");
error = dynamic_cast<ErrorDefinition const*>(referencedDeclaration(errorCall->expression()));
solAssert(error, "");
signature = error->functionType(true)->externalSignature();
parameterTypes = error->functionType(true)->parameterTypes();
for (ASTPointer<Expression const> const& arg: errorCall->sortedArguments())
argumentTypes.push_back(arg->annotation().type);
}
utils().revertWithError(signature, parameterTypes, argumentTypes);
}
break;
}
@ -910,7 +930,10 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
}
case FunctionType::Kind::Error:
{
solAssert(false, "");
for (ASTPointer<Expression const> const& arg: arguments)
arg->accept(*this);
// will be consumed in the revert / require call.
break;
}
case FunctionType::Kind::BlockHash:
{
@ -1082,49 +1105,72 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
{
acceptAndConvert(*arguments.front(), *TypeProvider::boolean(), false);
bool haveReasonString = arguments.size() > 1 && m_context.revertStrings() != RevertStrings::Strip;
solAssert(arguments.size() == 1|| arguments.size() == 2, "");
if (arguments.size() > 1)
bool usesString =
arguments.size() >= 2 &&
arguments.at(1)->annotation().type->isImplicitlyConvertibleTo(*TypeProvider::stringMemory());
if (arguments.size() == 1 || (usesString && m_context.revertStrings() == RevertStrings::Strip))
{
// Users probably expect the second argument to be evaluated
// even if the condition is false, as would be the case for an actual
// function call.
solAssert(arguments.size() == 2, "");
solAssert(function.kind() == FunctionType::Kind::Require, "");
solUnimplementedAssert(arguments.back()->annotation().type->isImplicitlyConvertibleTo(*TypeProvider::stringMemory()), "");
if (m_context.revertStrings() == RevertStrings::Strip)
// Shortcut without reason string.
if (arguments.size() == 2 && !*arguments.at(1)->annotation().isPure)
{
if (!*arguments.at(1)->annotation().isPure)
{
arguments.at(1)->accept(*this);
utils().popStackElement(*arguments.at(1)->annotation().type);
}
arguments.at(1)->accept(*this);
utils().popStackElement(*arguments.at(1)->annotation().type);
}
m_context << Instruction::ISZERO << Instruction::ISZERO;
auto success = m_context.appendConditionalJump();
if (function.kind() == FunctionType::Kind::Assert)
// condition was not met, flag an error
m_context.appendPanic(util::PanicCode::Assert);
else
m_context.appendRevert();
m_context << success;
}
else
{
solAssert(function.kind() == FunctionType::Kind::Require, "");
solAssert(arguments.size() == 2, "");
string signature;
vector<Type const*> parameterTypes;
vector<Type const*> argumentTypes;
if (usesString)
{
signature = "Error(string)";
parameterTypes.push_back(TypeProvider::stringMemory());
argumentTypes = vector<Type const*>{arguments.at(1)->annotation().type};
arguments.at(1)->accept(*this);
utils().moveIntoStack(1, argumentTypes.front()->sizeOnStack());
}
else
{
arguments.at(1)->accept(*this);
utils().moveIntoStack(1, arguments.at(1)->annotation().type->sizeOnStack());
// Cannot use the size of the argument itself because its type is wrong.
ErrorDefinition const* error = nullptr;
FunctionCall const* errorCall = dynamic_cast<FunctionCall const*>(arguments.at(1).get());
solAssert(errorCall, "");
solAssert(*errorCall->annotation().kind == FunctionCallKind::FunctionCall, "");
error = dynamic_cast<ErrorDefinition const*>(referencedDeclaration(errorCall->expression()));
solAssert(error, "");
signature = error->functionType(true)->externalSignature();
parameterTypes = error->functionType(true)->parameterTypes();
// This uses a different visiting order, but that's the case for all named function calls.
for (ASTPointer<Expression const> const& arg: errorCall->sortedArguments())
{
arg->accept(*this);
argumentTypes.push_back(arg->annotation().type);
utils().moveIntoStack(1, arg->annotation().type->sizeOnStack());
}
}
m_context << Instruction::ISZERO << Instruction::ISZERO;
auto success = m_context.appendConditionalJump();
unsigned argSize = TupleType(argumentTypes).sizeOnStack();
utils().revertWithError(signature, parameterTypes, argumentTypes);
m_context.adjustStackOffset(static_cast<int>(argSize));
m_context << success;
utils().popStackSlots(argSize);
}
// Stack: <error string (unconverted)> <condition>
// jump if condition was met
m_context << Instruction::ISZERO << Instruction::ISZERO;
auto success = m_context.appendConditionalJump();
if (function.kind() == FunctionType::Kind::Assert)
// condition was not met, flag an error
m_context.appendPanic(util::PanicCode::Assert);
else if (haveReasonString)
{
utils().revertWithStringData(*arguments.at(1)->annotation().type);
// Here, the argument is consumed, but in the other branch, it is still there.
m_context.adjustStackOffset(static_cast<int>(arguments.at(1)->annotation().type->sizeOnStack()));
}
else
m_context.appendRevert();
// the success branch
m_context << success;
if (haveReasonString)
utils().popStackElement(*arguments.at(1)->annotation().type);
break;
}
case FunctionType::Kind::ABIEncode:

View File

@ -1043,33 +1043,24 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
}
case FunctionType::Kind::Error:
{
solAssert(false, "");
// no-op
break;
}
case FunctionType::Kind::Assert:
{
solAssert(arguments.size() == 1, "");
m_code << m_utils.requireOrAssertFunction(true) << "(" << IRVariable(*arguments[0]).name() << ")";
break;
}
case FunctionType::Kind::Require:
{
solAssert(arguments.size() > 0, "Expected at least one parameter for require/assert");
solAssert(arguments.size() <= 2, "Expected no more than two parameters for require/assert");
if (arguments.size() == 2)
solAssert(functionType->kind() == FunctionType::Kind::Require, "");
if (arguments.size() == 2)
solUnimplementedAssert(arguments.back()->annotation().type->isImplicitlyConvertibleTo(*TypeProvider::stringMemory()), "");
Type const* messageArgumentType =
arguments.size() > 1 && m_context.revertStrings() != RevertStrings::Strip ?
arguments[1]->annotation().type :
nullptr;
string requireOrAssertFunction = m_utils.requireOrAssertFunction(
functionType->kind() == FunctionType::Kind::Assert,
messageArgumentType
);
m_code << move(requireOrAssertFunction) << "(" << IRVariable(*arguments[0]).name();
if (messageArgumentType && messageArgumentType->sizeOnStack() > 0)
m_code << ", " << IRVariable(*arguments[1]).commaSeparatedList();
m_code << ")\n";
solAssert(arguments.size() <= 2, "");
m_code << "if iszero(" << IRVariable(*arguments[0]).name() << ") {\n";
if (arguments.size() == 1)
m_code << "revert(0, 0)\n";
else
revertWithError(arguments.at(1));
m_code << "}\n";
break;
}
case FunctionType::Kind::ABIEncode:
@ -1201,42 +1192,11 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
}
case FunctionType::Kind::Revert:
{
solAssert(arguments.size() <= 1, "");
if (arguments.empty())
m_code << "revert(0, 0)\n";
else
{
solAssert(arguments.size() == 1, "");
solUnimplementedAssert(arguments.front()->annotation().type->isImplicitlyConvertibleTo(*TypeProvider::stringMemory()), "");
if (m_context.revertStrings() == RevertStrings::Strip)
m_code << "revert(0, 0)\n";
else
{
solAssert(type(*arguments.front()).isImplicitlyConvertibleTo(*TypeProvider::stringMemory()),"");
Whiskers templ(R"({
let <pos> := <allocateUnbounded>()
mstore(<pos>, <hash>)
let <end> := <encode>(add(<pos>, 4) <argumentVars>)
revert(<pos>, sub(<end>, <pos>))
})");
templ("pos", m_context.newYulVariable());
templ("end", m_context.newYulVariable());
templ("hash", util::selectorFromSignature("Error(string)").str());
templ("allocateUnbounded", m_utils.allocateUnboundedFunction());
templ(
"argumentVars",
joinHumanReadablePrefixed(IRVariable{*arguments.front()}.stackSlots())
);
templ("encode", m_context.abiFunctions().tupleEncoder(
{&type(*arguments.front())},
{TypeProvider::stringMemory()}
));
m_code << templ.render();
}
}
revertWithError(arguments.front());
break;
}
// Array creation using new
@ -2007,6 +1967,7 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
solAssert(
dynamic_cast<VariableDeclaration const*>(_memberAccess.annotation().referencedDeclaration) ||
dynamic_cast<FunctionDefinition const*>(_memberAccess.annotation().referencedDeclaration) ||
dynamic_cast<ErrorDefinition const*>(_memberAccess.annotation().referencedDeclaration) ||
category == Type::Category::TypeType ||
category == Type::Category::Module,
""
@ -3113,6 +3074,62 @@ void IRGeneratorForStatements::rethrow()
m_code << "revert(0, 0) // rethrow\n"s;
}
void IRGeneratorForStatements::revertWithError(ASTPointer<Expression const> const& _error)
{
solAssert(_error, "");
bool usesString = _error->annotation().type->isImplicitlyConvertibleTo(*TypeProvider::stringMemory());
if (usesString && m_context.revertStrings() == RevertStrings::Strip)
{
m_code << "revert(0, 0)\n";
return;
}
string signature;
vector<ASTPointer<Expression const>> errorArguments;
vector<Type const*> parameterTypes;
if (usesString)
{
signature = "Error(string)";
errorArguments.push_back(_error);
parameterTypes.push_back(TypeProvider::stringMemory());
}
else
{
FunctionCall const* errorCall = dynamic_cast<FunctionCall const*>(_error.get());
solAssert(errorCall, "");
solAssert(*errorCall->annotation().kind == FunctionCallKind::FunctionCall, "");
ErrorDefinition const* error = dynamic_cast<ErrorDefinition const*>(referencedDeclaration(errorCall->expression()));
solAssert(error, "");
signature = error->functionType(true)->externalSignature();
parameterTypes = error->functionType(true)->parameterTypes();
errorArguments = errorCall->sortedArguments();
}
Whiskers templ(R"({
let <pos> := <allocateUnbounded>()
mstore(<pos>, <hash>)
let <end> := <encode>(add(<pos>, 4) <argumentVars>)
revert(<pos>, sub(<end>, <pos>))
})");
templ("pos", m_context.newYulVariable());
templ("end", m_context.newYulVariable());
templ("hash", util::selectorFromSignature(signature).str());
templ("allocateUnbounded", m_utils.allocateUnboundedFunction());
vector<string> errorArgumentVars;
vector<Type const*> errorArgumentTypes;
for (ASTPointer<Expression const> const& arg: errorArguments)
{
errorArgumentVars += IRVariable(*arg).stackSlots();
errorArgumentTypes.push_back(arg->annotation().type);
}
templ("argumentVars", joinHumanReadablePrefixed(errorArgumentVars));
templ("encode", m_context.abiFunctions().tupleEncoder(errorArgumentTypes, parameterTypes));
m_code << templ.render();
}
bool IRGeneratorForStatements::visit(TryCatchClause const& _clause)
{
_clause.block().accept(*this);

View File

@ -106,6 +106,13 @@ private:
/// Generates code to rethrow an exception.
void rethrow();
/// Generates code to revert with an error, which might be a plain string
/// or an error instance. The error arguments and the strings are assumed to
/// be already evaluated and available in local IRVariables, but not yet
/// converted.
/// Honors the revert strings setting.
void revertWithError(ASTPointer<Expression const> const& _error);
void handleVariableReference(
VariableDeclaration const& _variable,
Expression const& _referencingExpression

View File

@ -0,0 +1,24 @@
error E(uint a);
library L {
error E(uint a, uint b);
}
interface I {
error E(uint a, uint b, uint c);
}
contract C {
function f() public pure {
revert(E(1));
}
function g() public pure {
revert(L.E(1, 2));
}
function h() public pure {
revert(I.E(1, 2, 3));
}
}
// ====
// compileViaYul: also
// ----
// f() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000001"
// g() -> FAILURE, hex"85208890", hex"0000000000000000000000000000000000000000000000000000000000000001", hex"0000000000000000000000000000000000000000000000000000000000000002"
// h() -> FAILURE, hex"7924ea7c", hex"0000000000000000000000000000000000000000000000000000000000000001", hex"0000000000000000000000000000000000000000000000000000000000000002", hex"0000000000000000000000000000000000000000000000000000000000000003"

View File

@ -0,0 +1,10 @@
error E(uint a, uint b);
contract C {
function f() public pure {
revert(E({b: 7, a: 2}));
}
}
// ====
// compileViaYul: also
// ----
// f() -> FAILURE, hex"85208890", hex"0000000000000000000000000000000000000000000000000000000000000002", hex"0000000000000000000000000000000000000000000000000000000000000007"

View File

@ -0,0 +1,11 @@
error E(uint a, uint b);
contract C {
function f(bool c) public pure {
require(c, E({b: 7, a: 2}));
}
}
// ====
// compileViaYul: also
// ----
// f(bool): true ->
// f(bool): false -> FAILURE, hex"85208890", hex"0000000000000000000000000000000000000000000000000000000000000002", hex"0000000000000000000000000000000000000000000000000000000000000007"

View File

@ -0,0 +1,13 @@
==== Source: s1.sol ====
error E(uint);
==== Source: s2.sol ====
import { E as Panic } from "s1.sol";
contract C {
function a() public pure {
require(false, Panic(1));
}
}
// ====
// compileViaYul: also
// ----
// a() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000001"

View File

@ -0,0 +1,13 @@
error E(string a, uint[] b);
contract C {
uint[] x;
function f(bool c) public {
x.push(7);
require(c, E("abc", x));
}
}
// ====
// compileViaYul: also
// ----
// f(bool): false -> FAILURE, hex"59e4d4df", 0x40, 0x80, 3, "abc", 1, 7
// f(bool): true ->

View File

@ -0,0 +1,19 @@
error E(uint a, uint b);
contract C {
uint public x;
function f(bool c) public {
require(c, E(g(), 7));
}
function g() public returns (uint) {
x++;
return 2;
}
}
// ====
// compileViaYul: also
// ----
// x() -> 0
// f(bool): true ->
// x() -> 1
// f(bool): false -> FAILURE, hex"85208890", hex"0000000000000000000000000000000000000000000000000000000000000002", hex"0000000000000000000000000000000000000000000000000000000000000007"
// x() -> 1

View File

@ -0,0 +1,12 @@
error E(string a, uint[] b);
contract C {
uint[] x;
function f() public {
x.push(7);
revert(E("abc", x));
}
}
// ====
// compileViaYul: also
// ----
// f() -> FAILURE, hex"59e4d4df", 0x40, 0x80, 3, "abc", 1, 7

View File

@ -0,0 +1,10 @@
error E(uint a, uint b);
contract C {
function f() public pure {
revert(E(2, 7));
}
}
// ====
// compileViaYul: also
// ----
// f() -> FAILURE, hex"85208890", 2, 7

View File

@ -0,0 +1,11 @@
error E(uint a, uint b);
contract C {
function f(bool c) public pure {
require(c, E(2, 7));
}
}
// ====
// compileViaYul: also
// ----
// f(bool): true ->
// f(bool): false -> FAILURE, hex"85208890", hex"0000000000000000000000000000000000000000000000000000000000000002", hex"0000000000000000000000000000000000000000000000000000000000000007"

View File

@ -0,0 +1,16 @@
pragma abicoder v2;
struct S { uint a; string b; }
error E(uint a, S, uint b);
contract C {
S s;
function f(bool c) public {
s.a = 9;
s.b = "abc";
require(c, E(2, s, 7));
}
}
// ====
// compileViaYul: also
// ----
// f(bool): true ->
// f(bool): false -> FAILURE, hex"e96e07f0", 2, 0x60, 7, 9,0x40, 3, "abc"

View File

@ -0,0 +1,18 @@
contract A {
error E(uint);
}
contract X {
error E(string);
}
contract B is A {
function f() public pure { revert(E(1)); }
function g() public pure { revert(A.E(1)); }
function h() public pure { revert(X.E("abc")); }
}
// ====
// compileViaYul: also
// ----
// f() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000001"
// g() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000001"
// h() -> FAILURE, hex"3e9992c9", hex"0000000000000000000000000000000000000000000000000000000000000020", hex"0000000000000000000000000000000000000000000000000000000000000003", hex"6162630000000000000000000000000000000000000000000000000000000000"

View File

@ -0,0 +1,37 @@
==== Source: s1.sol ====
error E(uint);
==== Source: s2.sol ====
import "s1.sol" as S;
==== Source: s3.sol ====
import "s1.sol" as S;
import "s2.sol" as T;
import "s1.sol";
contract C {
function a() public pure {
require(false, E(1));
}
function b() public pure {
require(false, S.E(2));
}
function c() public pure {
require(false, T.S.E(3));
}
function x() public pure {
revert(E(1));
}
function y() public pure {
revert(S.E(2));
}
function z() public pure {
revert(T.S.E(3));
}
}
// ====
// compileViaYul: also
// ----
// a() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000001"
// b() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000002"
// c() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000003"
// x() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000001"
// y() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000002"
// z() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000003"

View File

@ -0,0 +1,10 @@
error error(uint a);
contract C {
function f() public pure {
revert(error(2));
}
}
// ====
// compileViaYul: also
// ----
// f() -> FAILURE, hex"85208890", hex"0000000000000000000000000000000000000000000000000000000000000002", hex"0000000000000000000000000000000000000000000000000000000000000007"

View File

@ -0,0 +1,10 @@
error overdiffusingness(bytes,uint256,uint256,uint256,uint256);
contract C {
function f() public pure {
revert(overdiffusingness("",1,2,3,4));
}
}
// ====
// compileViaYul: also
// ----
// f() -> FAILURE, hex"00000000", 0xa0, 1, 2, 3, 4, 0