Skip to content

Commit

Permalink
eof: Fix immutables for EOF. Pass m_immutablesVariables and `m_libr…
Browse files Browse the repository at this point in the history
…aryAddressOffset` to `Deployed` context of IRGenerationContext for EOF
  • Loading branch information
rodiazet committed Dec 9, 2024
1 parent 58bffbb commit 6447619
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
7 changes: 6 additions & 1 deletion libsolidity/codegen/ir/IRGenerationContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ size_t IRGenerationContext::reservedMemory()
immutableVariablesSize += var->type()->sizeOnStack() * 32;
}

solAssert(immutableVariablesSize == reservedMemory);
// In Creation context check that only immutable variables or library address are stored in the reserved memory.
// In Deployed context (for EOF) m_immutableVariables contains offsets in EOF data section.
solAssert(
m_executionContext == ExecutionContext::Deployed ||
immutableVariablesSize + m_libraryAddressImmutableOffset.has_value() ? 32 : 0 == reservedMemory
);

m_reservedMemory = std::nullopt;
return reservedMemory;
Expand Down
15 changes: 14 additions & 1 deletion libsolidity/codegen/ir/IRGenerationContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ class IRGenerationContext

langutil::DebugInfoSelection debugInfoSelection() const { return m_debugInfoSelection; }
langutil::CharStreamProvider const* soliditySourceProvider() const { return m_soliditySourceProvider; }
std::map<VariableDeclaration const*, size_t> const& immutableVariables() const { return m_immutableVariables; }
void setImmutableVariables(std::map<VariableDeclaration const*, size_t> _immutableVariables)
{
solAssert(m_eofVersion.has_value());
solAssert(m_executionContext == ExecutionContext::Deployed);
m_immutableVariables = std::move(_immutableVariables);
}
void setLibraryAddressImmutableOffset(size_t _libraryAddressImmutableOffset)
{
solAssert(m_eofVersion.has_value());
solAssert(m_executionContext == ExecutionContext::Deployed);
m_libraryAddressImmutableOffset = _libraryAddressImmutableOffset;
}

private:
langutil::EVMVersion m_evmVersion;
Expand All @@ -176,7 +189,7 @@ class IRGenerationContext
ContractDefinition const* m_mostDerivedContract = nullptr;
std::map<VariableDeclaration const*, IRVariable> m_localVariables;
/// Memory offsets reserved for the values of immutable variables during contract creation.
/// This map is empty in the runtime context.
/// This map is empty in the legacy runtime context and may be not empty in EOF runtime context.
std::map<VariableDeclaration const*, size_t> m_immutableVariables;
std::optional<size_t> m_libraryAddressImmutableOffset;
/// Total amount of reserved memory. Reserved memory is used to store
Expand Down
7 changes: 7 additions & 0 deletions libsolidity/codegen/ir/IRGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,13 @@ void IRGenerator::resetContext(ContractDefinition const& _contract, ExecutionCon
m_context.debugInfoSelection(),
m_context.soliditySourceProvider()
);
if (m_eofVersion.has_value() && _context == ExecutionContext::Deployed)
{
newContext.setImmutableVariables(m_context.immutableVariables());
if (_contract.isLibrary())
newContext.setLibraryAddressImmutableOffset(m_context.libraryAddressImmutableOffset());
}

m_context = std::move(newContext);

m_context.setMostDerivedContract(_contract);
Expand Down
2 changes: 2 additions & 0 deletions test/libsolidity/semanticTests/immutable/stub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ contract C {
return (x+x,y);
}
}
// ====
// bytecodeFormat: legacy,>=EOFv1
// ----
// f() -> 84, 23

0 comments on commit 6447619

Please sign in to comment.