Skip to content

Commit

Permalink
eof: Disallow EOF builtins in inline assembly.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet committed Jan 24, 2025
1 parent 77d292a commit 4abb38e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 42 deletions.
86 changes: 44 additions & 42 deletions libyul/backends/evm/EVMDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,50 +381,51 @@ std::vector<std::optional<BuiltinFunctionForEVM>> createBuiltins(langutil::EVMVe
}
else // EOF context
{
builtins.emplace_back(createFunction(
"auxdataloadn",
1,
1,
EVMDialect::sideEffectsOfInstruction(evmasm::Instruction::DATALOADN),
ControlFlowSideEffects::fromInstruction(evmasm::Instruction::DATALOADN),
{LiteralKind::Number},
[](
FunctionCall const& _call,
AbstractAssembly& _assembly,
BuiltinContext&
) {
yulAssert(_call.arguments.size() == 1);
Literal const* literal = std::get_if<Literal>(&_call.arguments.front());
yulAssert(literal, "");
yulAssert(literal->value.value() <= std::numeric_limits<uint16_t>::max());
_assembly.appendAuxDataLoadN(static_cast<uint16_t>(literal->value.value()));
}
));

builtins.emplace_back(createFunction(
"eofcreate",
5,
1,
EVMDialect::sideEffectsOfInstruction(evmasm::Instruction::EOFCREATE),
ControlFlowSideEffects::fromInstruction(evmasm::Instruction::EOFCREATE),
{LiteralKind::String, std::nullopt, std::nullopt, std::nullopt, std::nullopt},
[](
FunctionCall const& _call,
AbstractAssembly& _assembly,
BuiltinContext& context
) {
yulAssert(_call.arguments.size() == 5);
Literal const* literal = std::get_if<Literal>(&_call.arguments.front());
auto const formattedLiteral = formatLiteral(*literal);
yulAssert(!util::contains(formattedLiteral, '.'));
auto const* containerID = valueOrNullptr(context.subIDs, formattedLiteral);
yulAssert(containerID != nullptr);
yulAssert(*containerID <= std::numeric_limits<AbstractAssembly::ContainerID>::max());
_assembly.appendEOFCreate(static_cast<AbstractAssembly::ContainerID>(*containerID));
}
if (_objectAccess)
{
builtins.emplace_back(createFunction(
"auxdataloadn",
1,
1,
EVMDialect::sideEffectsOfInstruction(evmasm::Instruction::DATALOADN),
ControlFlowSideEffects::fromInstruction(evmasm::Instruction::DATALOADN),
{LiteralKind::Number},
[](
FunctionCall const& _call,
AbstractAssembly& _assembly,
BuiltinContext&
) {
yulAssert(_call.arguments.size() == 1);
Literal const* literal = std::get_if<Literal>(&_call.arguments.front());
yulAssert(literal, "");
yulAssert(literal->value.value() <= std::numeric_limits<uint16_t>::max());
_assembly.appendAuxDataLoadN(static_cast<uint16_t>(literal->value.value()));
}
));

if (_objectAccess)
builtins.emplace_back(createFunction(
"eofcreate",
5,
1,
EVMDialect::sideEffectsOfInstruction(evmasm::Instruction::EOFCREATE),
ControlFlowSideEffects::fromInstruction(evmasm::Instruction::EOFCREATE),
{LiteralKind::String, std::nullopt, std::nullopt, std::nullopt, std::nullopt},
[](
FunctionCall const& _call,
AbstractAssembly& _assembly,
BuiltinContext& context
) {
yulAssert(_call.arguments.size() == 5);
Literal const* literal = std::get_if<Literal>(&_call.arguments.front());
auto const formattedLiteral = formatLiteral(*literal);
yulAssert(!util::contains(formattedLiteral, '.'));
auto const* containerID = valueOrNullptr(context.subIDs, formattedLiteral);
yulAssert(containerID != nullptr);
yulAssert(*containerID <= std::numeric_limits<AbstractAssembly::ContainerID>::max());
_assembly.appendEOFCreate(static_cast<AbstractAssembly::ContainerID>(*containerID));
}
));

builtins.emplace_back(createFunction(
"returncontract",
3,
Expand All @@ -448,6 +449,7 @@ std::vector<std::optional<BuiltinFunctionForEVM>> createBuiltins(langutil::EVMVe
_assembly.appendReturnContract(static_cast<AbstractAssembly::ContainerID>(*containerID));
}
));
}
}
yulAssert(
ranges::all_of(builtins, [](std::optional<BuiltinFunctionForEVM> const& _builtinFunction){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
contract C {
function f() view public {
assembly {
eofcreate("a", 0, 0, 0, 0)
returncontract("a", 0)
auxdataloadn(0)
}
}
}
// ====
// bytecodeFormat: legacy
// ----
// DeclarationError 7223: (75-84): Builtin function "eofcreate" is only available in EOF.
// DeclarationError 7223: (114-128): Builtin function "returncontract" is only available in EOF.
// DeclarationError 7223: (149-161): Builtin function "auxdataloadn" is only available in EOF.

0 comments on commit 4abb38e

Please sign in to comment.