Skip to content

Commit

Permalink
Code generation for errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Mar 16, 2021
1 parent 65172ed commit 4174c6a
Show file tree
Hide file tree
Showing 16 changed files with 280 additions and 2 deletions.
16 changes: 16 additions & 0 deletions libsolidity/codegen/CompilerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 6 additions & 0 deletions libsolidity/codegen/CompilerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class CompilerUtils
/// 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();
Expand Down
9 changes: 9 additions & 0 deletions libsolidity/codegen/ContractCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,15 @@ bool ContractCompiler::visit(EmitStatement const& _emit)
return false;
}

bool ContractCompiler::visit(RevertStatement const& _revert)
{
CompilerContext::LocationSetter locationSetter(m_context, _revert);
StackHeightChecker checker(m_context);
compileExpression(_revert.errorCall());
checker.check();
return false;
}

bool ContractCompiler::visit(VariableDeclarationStatement const& _variableDeclarationStatement)
{
CompilerContext::LocationSetter locationSetter(m_context, _variableDeclarationStatement);
Expand Down
1 change: 1 addition & 0 deletions libsolidity/codegen/ContractCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class ContractCompiler: private ASTConstVisitor
bool visit(Return const& _return) override;
bool visit(Throw const& _throw) override;
bool visit(EmitStatement const& _emit) override;
bool visit(RevertStatement const& _revert) override;
bool visit(VariableDeclarationStatement const& _variableDeclarationStatement) override;
bool visit(ExpressionStatement const& _expressionStatement) override;
bool visit(PlaceholderStatement const&) override;
Expand Down
17 changes: 16 additions & 1 deletion libsolidity/codegen/ExpressionCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -914,7 +915,20 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
}
case FunctionType::Kind::Error:
{
solAssert(false, "");
_functionCall.expression().accept(*this);
vector<Type const*> argumentTypes;
for (ASTPointer<Expression const> const& arg: _functionCall.sortedArguments())
{
arg->accept(*this);
argumentTypes.push_back(arg->annotation().type);
}
solAssert(dynamic_cast<ErrorDefinition const*>(&function.declaration()), "");
utils().revertWithError(
function.externalSignature(),
function.parameterTypes(),
argumentTypes
);
break;
}
case FunctionType::Kind::BlockHash:
{
Expand Down Expand Up @@ -1832,6 +1846,7 @@ bool ExpressionCompiler::visit(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,
""
Expand Down
88 changes: 87 additions & 1 deletion libsolidity/codegen/ir/IRGeneratorForStatements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,29 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
}
case FunctionType::Kind::Error:
{
solAssert(false, "");
vector<string> arguments;
vector<Type const*> argumentTypes;
for (ASTPointer<Expression const> const& arg: _functionCall.sortedArguments())
{
argumentTypes.push_back(arg->annotation().type);
arguments += IRVariable(*arg).stackSlots();
}
ABIFunctions abi(m_context.evmVersion(), m_context.revertStrings(), m_context.functionCollector());
solAssert(dynamic_cast<ErrorDefinition const*>(&functionType->declaration()), "");
Whiskers templ(R"({
let <pos> := <allocateUnbounded>()
mstore(<pos>, <selector>)
let <end> := <encode>(add(<pos>, 4) <data>)
revert(<pos>, sub(<end>, <pos>))
})");
templ("pos", m_context.newYulVariable());
templ("selector", util::selectorFromSignature(functionType->externalSignature()).str());
templ("end", m_context.newYulVariable());
templ("allocateUnbounded", m_utils.allocateUnboundedFunction());
templ("encode", abi.tupleEncoder(argumentTypes, functionType->parameterTypes()));
templ("data", joinHumanReadablePrefixed(arguments));
m_code << templ.render();
break;
}
case FunctionType::Kind::Assert:
case FunctionType::Kind::Require:
Expand Down Expand Up @@ -1966,6 +1988,13 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
);
// the call will do the resolving
break;
case FunctionType::Kind::Error:
solAssert(
dynamic_cast<ErrorDefinition const*>(_memberAccess.annotation().referencedDeclaration),
"Error not found"
);
// The function call will resolve the selector.
break;
case FunctionType::Kind::DelegateCall:
define(IRVariable(_memberAccess).part("address"), _memberAccess.expression());
define(IRVariable(_memberAccess).part("functionSelector")) << formatNumber(memberFunctionType->externalIdentifier()) << "\n";
Expand Down Expand Up @@ -2010,6 +2039,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,
""
Expand Down Expand Up @@ -3107,6 +3137,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);
Expand Down
7 changes: 7 additions & 0 deletions libsolidity/codegen/ir/IRGeneratorForStatements.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ class IRGeneratorForStatements: public ASTConstVisitor
/// 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 10 additions & 0 deletions test/libsolidity/semanticTests/errors/named_error_args.sol
Original file line number Diff line number Diff line change
@@ -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"
13 changes: 13 additions & 0 deletions test/libsolidity/semanticTests/errors/panic_via_import.sol
Original file line number Diff line number Diff line change
@@ -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 {
revert Panic(1);
}
}
// ====
// compileViaYul: also
// ----
// a() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000001"
12 changes: 12 additions & 0 deletions test/libsolidity/semanticTests/errors/revert_conversion.sol
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions test/libsolidity/semanticTests/errors/simple.sol
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions test/libsolidity/semanticTests/errors/using_structs.sol
Original file line number Diff line number Diff line change
@@ -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";
revert E(2, s, 7);
}
}
// ====
// compileViaYul: also
// ----
// f(bool): true -> FAILURE, hex"e96e07f0", hex"0000000000000000000000000000000000000000000000000000000000000002", hex"0000000000000000000000000000000000000000000000000000000000000060", hex"0000000000000000000000000000000000000000000000000000000000000007", hex"0000000000000000000000000000000000000000000000000000000000000009", hex"0000000000000000000000000000000000000000000000000000000000000040", hex"0000000000000000000000000000000000000000000000000000000000000003", hex"6162630000000000000000000000000000000000000000000000000000000000"
// f(bool): false -> FAILURE, hex"e96e07f0", hex"0000000000000000000000000000000000000000000000000000000000000002", hex"0000000000000000000000000000000000000000000000000000000000000060", hex"0000000000000000000000000000000000000000000000000000000000000007", hex"0000000000000000000000000000000000000000000000000000000000000009", hex"0000000000000000000000000000000000000000000000000000000000000040", hex"0000000000000000000000000000000000000000000000000000000000000003", hex"6162630000000000000000000000000000000000000000000000000000000000"
18 changes: 18 additions & 0 deletions test/libsolidity/semanticTests/errors/via_contract_type.sol
Original file line number Diff line number Diff line change
@@ -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"
25 changes: 25 additions & 0 deletions test/libsolidity/semanticTests/errors/via_import.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
==== 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 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
// ----
// x() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000001"
// y() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000002"
// z() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000003"
10 changes: 10 additions & 0 deletions test/libsolidity/semanticTests/errors/weird_name.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error error(uint a);
contract C {
function f() public pure {
revert error(2);
}
}
// ====
// compileViaYul: also
// ----
// f() -> FAILURE, hex"b48fb6cf", hex"0000000000000000000000000000000000000000000000000000000000000002"

0 comments on commit 4174c6a

Please sign in to comment.