Skip to content

Commit

Permalink
eof: Fix container id in case of unreferenced containers in yul
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet committed Nov 28, 2024
1 parent 8ea8efd commit 270cece
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 10 deletions.
20 changes: 12 additions & 8 deletions libevmasm/Assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ void appendBigEndianUint16(bytes& _dest, ValueT _value)
}
}

std::tuple<bytes, std::vector<size_t>, size_t> Assembly::createEOFHeader(std::set<uint16_t> const& _referencedSubIds) const
std::tuple<bytes, std::vector<size_t>, size_t> Assembly::createEOFHeader(std::set<ContainerID> const& _referencedSubIds) const
{
bytes retBytecode;
std::vector<size_t> codeSectionSizePositions;
Expand Down Expand Up @@ -1330,9 +1330,9 @@ LinkerObject const& Assembly::assembleLegacy() const
return ret;
}

std::map<uint16_t, uint16_t> Assembly::findReferencedContainers() const
std::map<ContainerID, ContainerID> Assembly::findReferencedContainers() const
{
std::set<uint16_t> referencedSubcontainersIds;
std::set<ContainerID> referencedSubcontainersIds;
solAssert(m_subs.size() <= 0x100); // According to EOF spec

for (auto&& codeSection: m_codeSections)
Expand All @@ -1344,12 +1344,12 @@ std::map<uint16_t, uint16_t> Assembly::findReferencedContainers() const
referencedSubcontainersIds.insert(containerId);
}

std::map<uint16_t, uint16_t> replacements;
std::map<ContainerID, ContainerID> replacements;
uint8_t nUnreferenced = 0;
for (uint8_t i = 0; i < static_cast<uint16_t>(m_subs.size()); ++i)
for (uint8_t i = 0; i < static_cast<ContainerID>(m_subs.size()); ++i)
{
if (referencedSubcontainersIds.count(i) > 0)
replacements[i] = static_cast<uint16_t>(i - nUnreferenced);
replacements[i] = static_cast<ContainerID>(i - nUnreferenced);
else
nUnreferenced++;
}
Expand Down Expand Up @@ -1441,13 +1441,17 @@ LinkerObject const& Assembly::assembleEOF() const
case EOFCreate:
{
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::EOFCREATE));
ret.bytecode.push_back(static_cast<uint8_t>(item.data()));
auto const containerID = static_cast<ContainerID>(item.data());
solAssert(subIdsReplacements.count(containerID) == 1);
ret.bytecode.push_back(subIdsReplacements.at(containerID));
break;
}
case ReturnContract:
{
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::RETURNCONTRACT));
ret.bytecode.push_back(static_cast<uint8_t>(item.data()));
auto const containerID = static_cast<ContainerID>(item.data());
solAssert(subIdsReplacements.count(containerID) == 1);
ret.bytecode.push_back(subIdsReplacements.at(containerID));
break;
}
case VerbatimBytecode:
Expand Down
4 changes: 2 additions & 2 deletions libevmasm/Assembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,13 @@ class Assembly
std::shared_ptr<std::string const> sharedSourceName(std::string const& _name) const;

/// Returns EOF header bytecode | code section sizes offsets | data section size offset
std::tuple<bytes, std::vector<size_t>, size_t> createEOFHeader(std::set<uint16_t> const& _referencedSubIds) const;
std::tuple<bytes, std::vector<size_t>, size_t> createEOFHeader(std::set<ContainerID> const& _referencedSubIds) const;

LinkerObject const& assembleLegacy() const;
LinkerObject const& assembleEOF() const;

/// Returns map from m_subs to an index of subcontainer in the final EOF bytecode
std::map<uint16_t, uint16_t> findReferencedContainers() const;
std::map<ContainerID, ContainerID> findReferencedContainers() const;
/// Returns max AuxDataLoadN offset for the assembly.
std::optional<uint16_t> findMaxAuxDataLoadNOffset() const;

Expand Down
81 changes: 81 additions & 0 deletions test/libyul/objectCompiler/eof/prune_unreferenced_container.yul
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
object "a" {
code {
mstore(0, eofcreate("c", 0, 0, 0, 0))
return(0, 32)
}

object "b" {
code {
mstore(0, 0x1122334455667788990011223344556677889900112233445566778899001122)
mstore(32, 0x1122334455667788990011223344556677889900112233445566778899001122)
}
}

object "c" {
code {
mstore(0, 0x1122334455667788990011223344556677889900112233445566778899001122)
mstore(32, 0x1122334455667788990011223344556677889900112233445566778899001122)
}
}
}

// ====
// EVMVersion: >=prague
// bytecodeFormat: >=EOFv1
// ----
// Assembly:
// /* "source":80:81 */
// 0x00
// /* "source":56:82 */
// dup1
// dup1
// dup1
// eofcreate{1}
// /* "source":53:54 */
// 0x00
// /* "source":46:83 */
// mstore
// /* "source":106:108 */
// 0x20
// /* "source":103:104 */
// 0x00
// /* "source":96:109 */
// return
// stop
//
// sub_0: assembly {
// /* "source":198:264 */
// 0x1122334455667788990011223344556677889900112233445566778899001122
// /* "source":195:196 */
// 0x00
// /* "source":188:265 */
// mstore
// /* "source":293:359 */
// 0x1122334455667788990011223344556677889900112233445566778899001122
// /* "source":289:291 */
// 0x20
// /* "source":282:360 */
// mstore
// /* "source":156:384 */
// stop
// }
//
// sub_1: assembly {
// /* "source":463:529 */
// 0x1122334455667788990011223344556677889900112233445566778899001122
// /* "source":460:461 */
// 0x00
// /* "source":453:530 */
// mstore
// /* "source":558:624 */
// 0x1122334455667788990011223344556677889900112233445566778899001122
// /* "source":554:556 */
// 0x20
// /* "source":547:625 */
// mstore
// /* "source":421:649 */
// stop
// }
// Bytecode: ef0001010004020001000c030001005b040000000080ffff5f808080ec005f5260205ff3ef00010100040200010048040000000080ffff7f11223344556677889900112233445566778899001122334455667788990011225f527f112233445566778899001122334455667788990011223344556677889900112260205200
// Opcodes: 0xEF STOP ADD ADD STOP DIV MUL STOP ADD STOP 0xC SUB STOP ADD STOP JUMPDEST DIV STOP STOP STOP STOP DUP1 SELFDESTRUCT SELFDESTRUCT PUSH0 DUP1 DUP1 DUP1 EOFCREATE 0x0 PUSH0 MSTORE PUSH1 0x20 PUSH0 RETURN 0xEF STOP ADD ADD STOP DIV MUL STOP ADD STOP BASEFEE DIV STOP STOP STOP STOP DUP1 SELFDESTRUCT SELFDESTRUCT PUSH32 0x1122334455667788990011223344556677889900112233445566778899001122 PUSH0 MSTORE PUSH32 0x1122334455667788990011223344556677889900112233445566778899001122 PUSH1 0x20 MSTORE STOP
// SourceMappings: 80:1:0:-:0;56:26;;;;53:1;46:37;106:2;103:1;96:13

0 comments on commit 270cece

Please sign in to comment.