Skip to content

Commit 270cece

Browse files
committed
eof: Fix container id in case of unreferenced containers in yul
1 parent 8ea8efd commit 270cece

File tree

3 files changed

+95
-10
lines changed

3 files changed

+95
-10
lines changed

libevmasm/Assembly.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ void appendBigEndianUint16(bytes& _dest, ValueT _value)
924924
}
925925
}
926926

927-
std::tuple<bytes, std::vector<size_t>, size_t> Assembly::createEOFHeader(std::set<uint16_t> const& _referencedSubIds) const
927+
std::tuple<bytes, std::vector<size_t>, size_t> Assembly::createEOFHeader(std::set<ContainerID> const& _referencedSubIds) const
928928
{
929929
bytes retBytecode;
930930
std::vector<size_t> codeSectionSizePositions;
@@ -1330,9 +1330,9 @@ LinkerObject const& Assembly::assembleLegacy() const
13301330
return ret;
13311331
}
13321332

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

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

1347-
std::map<uint16_t, uint16_t> replacements;
1347+
std::map<ContainerID, ContainerID> replacements;
13481348
uint8_t nUnreferenced = 0;
1349-
for (uint8_t i = 0; i < static_cast<uint16_t>(m_subs.size()); ++i)
1349+
for (uint8_t i = 0; i < static_cast<ContainerID>(m_subs.size()); ++i)
13501350
{
13511351
if (referencedSubcontainersIds.count(i) > 0)
1352-
replacements[i] = static_cast<uint16_t>(i - nUnreferenced);
1352+
replacements[i] = static_cast<ContainerID>(i - nUnreferenced);
13531353
else
13541354
nUnreferenced++;
13551355
}
@@ -1441,13 +1441,17 @@ LinkerObject const& Assembly::assembleEOF() const
14411441
case EOFCreate:
14421442
{
14431443
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::EOFCREATE));
1444-
ret.bytecode.push_back(static_cast<uint8_t>(item.data()));
1444+
auto const containerID = static_cast<ContainerID>(item.data());
1445+
solAssert(subIdsReplacements.count(containerID) == 1);
1446+
ret.bytecode.push_back(subIdsReplacements.at(containerID));
14451447
break;
14461448
}
14471449
case ReturnContract:
14481450
{
14491451
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::RETURNCONTRACT));
1450-
ret.bytecode.push_back(static_cast<uint8_t>(item.data()));
1452+
auto const containerID = static_cast<ContainerID>(item.data());
1453+
solAssert(subIdsReplacements.count(containerID) == 1);
1454+
ret.bytecode.push_back(subIdsReplacements.at(containerID));
14511455
break;
14521456
}
14531457
case VerbatimBytecode:

libevmasm/Assembly.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@ class Assembly
247247
std::shared_ptr<std::string const> sharedSourceName(std::string const& _name) const;
248248

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

252252
LinkerObject const& assembleLegacy() const;
253253
LinkerObject const& assembleEOF() const;
254254

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
object "a" {
2+
code {
3+
mstore(0, eofcreate("c", 0, 0, 0, 0))
4+
return(0, 32)
5+
}
6+
7+
object "b" {
8+
code {
9+
mstore(0, 0x1122334455667788990011223344556677889900112233445566778899001122)
10+
mstore(32, 0x1122334455667788990011223344556677889900112233445566778899001122)
11+
}
12+
}
13+
14+
object "c" {
15+
code {
16+
mstore(0, 0x1122334455667788990011223344556677889900112233445566778899001122)
17+
mstore(32, 0x1122334455667788990011223344556677889900112233445566778899001122)
18+
}
19+
}
20+
}
21+
22+
// ====
23+
// EVMVersion: >=prague
24+
// bytecodeFormat: >=EOFv1
25+
// ----
26+
// Assembly:
27+
// /* "source":80:81 */
28+
// 0x00
29+
// /* "source":56:82 */
30+
// dup1
31+
// dup1
32+
// dup1
33+
// eofcreate{1}
34+
// /* "source":53:54 */
35+
// 0x00
36+
// /* "source":46:83 */
37+
// mstore
38+
// /* "source":106:108 */
39+
// 0x20
40+
// /* "source":103:104 */
41+
// 0x00
42+
// /* "source":96:109 */
43+
// return
44+
// stop
45+
//
46+
// sub_0: assembly {
47+
// /* "source":198:264 */
48+
// 0x1122334455667788990011223344556677889900112233445566778899001122
49+
// /* "source":195:196 */
50+
// 0x00
51+
// /* "source":188:265 */
52+
// mstore
53+
// /* "source":293:359 */
54+
// 0x1122334455667788990011223344556677889900112233445566778899001122
55+
// /* "source":289:291 */
56+
// 0x20
57+
// /* "source":282:360 */
58+
// mstore
59+
// /* "source":156:384 */
60+
// stop
61+
// }
62+
//
63+
// sub_1: assembly {
64+
// /* "source":463:529 */
65+
// 0x1122334455667788990011223344556677889900112233445566778899001122
66+
// /* "source":460:461 */
67+
// 0x00
68+
// /* "source":453:530 */
69+
// mstore
70+
// /* "source":558:624 */
71+
// 0x1122334455667788990011223344556677889900112233445566778899001122
72+
// /* "source":554:556 */
73+
// 0x20
74+
// /* "source":547:625 */
75+
// mstore
76+
// /* "source":421:649 */
77+
// stop
78+
// }
79+
// Bytecode: ef0001010004020001000c030001005b040000000080ffff5f808080ec005f5260205ff3ef00010100040200010048040000000080ffff7f11223344556677889900112233445566778899001122334455667788990011225f527f112233445566778899001122334455667788990011223344556677889900112260205200
80+
// 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
81+
// SourceMappings: 80:1:0:-:0;56:26;;;;53:1;46:37;106:2;103:1;96:13

0 commit comments

Comments
 (0)