diff --git a/libevmasm/Assembly.cpp b/libevmasm/Assembly.cpp index 6ac8eb95a..07030448f 100644 --- a/libevmasm/Assembly.cpp +++ b/libevmasm/Assembly.cpp @@ -578,17 +578,49 @@ std::pair, std::vector> Assembly::fromJSO { shared_ptr subassembly(Assembly::fromJSON(code, sourceList, _level + 1).first); solAssert(subassembly); - result->m_subs.emplace_back(make_shared(*subassembly)); - // TODO: this shouldn't be enough for the general case. - result->encodeSubPath({0, 0}); + result->m_subs.emplace_back(subassembly); } else solThrow(AssemblyImportException, "Key inside '.data' '" + dataItemID + "' can only be a valid hex-string or an object."); } } + if (_level == 0) + result->updatePaths(); return std::make_pair(result, sourceList); } +void Assembly::updatePaths(std::vector const& _parents, std::vector const& _absolutePathFromRoot) +{ + size_t subId = 0; + for (auto& assembly: this->m_subs) + { + std::vector parents{_parents}; + parents.push_back(this); + + std::vector absolutePathFromRoot{_absolutePathFromRoot}; + absolutePathFromRoot.emplace_back(subId); + + int pindex = 0; + for (auto& parent: parents) + { + if (pindex == 0) + parent->encodeSubPath(absolutePathFromRoot); + else + { + std::vector relativePath{absolutePathFromRoot}; + for (int i = 0; i < pindex; ++i) + relativePath.erase(relativePath.begin()); + parent->encodeSubPath(relativePath); + } + ++pindex; + } + + assembly->updatePaths(parents, absolutePathFromRoot); + + ++subId; + } +} + AssemblyItem Assembly::namedTag(string const& _name, size_t _params, size_t _returns, optional _sourceID) { assertThrow(!_name.empty(), AssemblyException, "Empty named tag."); diff --git a/libevmasm/Assembly.h b/libevmasm/Assembly.h index 9bbe7a7a3..12459b7da 100644 --- a/libevmasm/Assembly.h +++ b/libevmasm/Assembly.h @@ -196,6 +196,8 @@ private: Assembly const* subAssemblyById(size_t _subId) const; + void updatePaths(std::vector const& _parents = {}, std::vector const& _absolutePathFromRoot = {}); + protected: /// 0 is reserved for exception unsigned m_usedTags = 1; diff --git a/test/cmdlineTests/asm_json_export_yul_more_subobjects/args b/test/cmdlineTests/asm_json_export_yul_more_subobjects/args new file mode 100644 index 000000000..aa82dbacc --- /dev/null +++ b/test/cmdlineTests/asm_json_export_yul_more_subobjects/args @@ -0,0 +1 @@ +--strict-assembly - --asm-json --pretty-json diff --git a/test/cmdlineTests/asm_json_export_yul_more_subobjects/output b/test/cmdlineTests/asm_json_export_yul_more_subobjects/output new file mode 100644 index 000000000..31e381275 --- /dev/null +++ b/test/cmdlineTests/asm_json_export_yul_more_subobjects/output @@ -0,0 +1,365 @@ + +======= (EVM) ======= +{ + ".code": + [ + { + "begin": 37, + "end": 51, + "name": "PUSHSIZE", + "source": 0 + }, + { + "begin": 34, + "end": 35, + "name": "PUSH", + "source": 0, + "value": "0" + }, + { + "begin": 27, + "end": 52, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 67, + "end": 81, + "name": "PUSH #[$]", + "source": 0, + "value": "0000000000000000000000000000000000000000000000000000000000000000" + }, + { + "begin": 64, + "end": 65, + "name": "PUSH", + "source": 0, + "value": "1" + }, + { + "begin": 57, + "end": 82, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 97, + "end": 114, + "name": "PUSH #[$]", + "source": 0, + "value": "000000000000000000000000000000000000000000000000ffffffffffffffff" + }, + { + "begin": 94, + "end": 95, + "name": "PUSH", + "source": 0, + "value": "2" + }, + { + "begin": 87, + "end": 115, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 130, + "end": 152, + "name": "PUSH #[$]", + "source": 0, + "value": "000000000000000000000000000000000000000000000000fffffffffffffffe" + }, + { + "begin": 127, + "end": 128, + "name": "PUSH", + "source": 0, + "value": "3" + }, + { + "begin": 120, + "end": 153, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 168, + "end": 190, + "name": "PUSH #[$]", + "source": 0, + "value": "000000000000000000000000000000000000000000000000fffffffffffffffd" + }, + { + "begin": 165, + "end": 166, + "name": "PUSH", + "source": 0, + "value": "4" + }, + { + "begin": 158, + "end": 191, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 206, + "end": 228, + "name": "PUSH #[$]", + "source": 0, + "value": "000000000000000000000000000000000000000000000000fffffffffffffffc" + }, + { + "begin": 203, + "end": 204, + "name": "PUSH", + "source": 0, + "value": "5" + }, + { + "begin": 196, + "end": 229, + "name": "SSTORE", + "source": 0 + } + ], + ".data": + { + "0": + { + ".code": + [ + { + "begin": 278, + "end": 292, + "name": "PUSHSIZE", + "source": 0 + }, + { + "begin": 275, + "end": 276, + "name": "PUSH", + "source": 0, + "value": "6" + }, + { + "begin": 268, + "end": 293, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 310, + "end": 324, + "name": "PUSH #[$]", + "source": 0, + "value": "0000000000000000000000000000000000000000000000000000000000000000" + }, + { + "begin": 307, + "end": 308, + "name": "PUSH", + "source": 0, + "value": "7" + }, + { + "begin": 300, + "end": 325, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 342, + "end": 361, + "name": "PUSH #[$]", + "source": 0, + "value": "000000000000000000000000000000000000000000000000ffffffffffffffff" + }, + { + "begin": 339, + "end": 340, + "name": "PUSH", + "source": 0, + "value": "8" + }, + { + "begin": 332, + "end": 362, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 379, + "end": 398, + "name": "PUSH #[$]", + "source": 0, + "value": "000000000000000000000000000000000000000000000000fffffffffffffffe" + }, + { + "begin": 376, + "end": 377, + "name": "PUSH", + "source": 0, + "value": "9" + }, + { + "begin": 369, + "end": 399, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 417, + "end": 436, + "name": "PUSH #[$]", + "source": 0, + "value": "000000000000000000000000000000000000000000000000fffffffffffffffd" + }, + { + "begin": 413, + "end": 415, + "name": "PUSH", + "source": 0, + "value": "A" + }, + { + "begin": 406, + "end": 437, + "name": "SSTORE", + "source": 0 + } + ], + ".data": + { + "0": + { + ".code": + [ + { + "begin": 494, + "end": 508, + "name": "PUSHSIZE", + "source": 0 + }, + { + "begin": 490, + "end": 492, + "name": "PUSH", + "source": 0, + "value": "B" + }, + { + "begin": 483, + "end": 509, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 529, + "end": 545, + "name": "PUSH #[$]", + "source": 0, + "value": "0000000000000000000000000000000000000000000000000000000000000000" + }, + { + "begin": 525, + "end": 527, + "name": "PUSH", + "source": 0, + "value": "C" + }, + { + "begin": 518, + "end": 546, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 566, + "end": 582, + "name": "PUSH #[$]", + "source": 0, + "value": "0000000000000000000000000000000000000000000000000000000000000001" + }, + { + "begin": 562, + "end": 564, + "name": "PUSH", + "source": 0, + "value": "D" + }, + { + "begin": 555, + "end": 583, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 603, + "end": 619, + "name": "PUSH #[$]", + "source": 0, + "value": "0000000000000000000000000000000000000000000000000000000000000002" + }, + { + "begin": 599, + "end": 601, + "name": "PUSH", + "source": 0, + "value": "E" + }, + { + "begin": 592, + "end": 620, + "name": "SSTORE", + "source": 0 + } + ], + ".data": + { + "0": + { + ".code": + [ + { + "begin": 676, + "end": 685, + "name": "INVALID", + "source": 0 + } + ] + }, + "1": + { + ".code": + [ + { + "begin": 751, + "end": 760, + "name": "INVALID", + "source": 0 + } + ] + }, + "2": + { + ".code": + [ + { + "begin": 826, + "end": 835, + "name": "INVALID", + "source": 0 + } + ] + } + } + } + } + } + }, + "sourceList": + [ + "" + ] +} diff --git a/test/cmdlineTests/asm_json_export_yul_more_subobjects/stdin b/test/cmdlineTests/asm_json_export_yul_more_subobjects/stdin new file mode 100644 index 000000000..1b08be852 --- /dev/null +++ b/test/cmdlineTests/asm_json_export_yul_more_subobjects/stdin @@ -0,0 +1,43 @@ +object "L0" { + code { + sstore(0, datasize("L0")) + sstore(1, datasize("L1")) + sstore(2, datasize("L1.L2")) + sstore(3, datasize("L1.L2.L2_0")) + sstore(4, datasize("L1.L2.L2_1")) + sstore(5, datasize("L1.L2.L2_2")) + } + + object "L1" { + code { + sstore(6, datasize("L1")) + sstore(7, datasize("L2")) + sstore(8, datasize("L2.L2_0")) + sstore(9, datasize("L2.L2_1")) + sstore(10, datasize("L2.L2_2")) + } + object "L2" { + code { + sstore(11, datasize("L2")) + sstore(12, datasize("L2_0")) + sstore(13, datasize("L2_1")) + sstore(14, datasize("L2_2")) + } + object "L2_0" { + code { + invalid() + } + } + object "L2_1" { + code { + invalid() + } + } + object "L2_2" { + code { + invalid() + } + } + } + } +} diff --git a/test/cmdlineTests/asm_json_import_all_valid_flags/output b/test/cmdlineTests/asm_json_import_all_valid_flags/output index 48b7b3e21..7b8aa0297 100644 --- a/test/cmdlineTests/asm_json_import_all_valid_flags/output +++ b/test/cmdlineTests/asm_json_import_all_valid_flags/output @@ -37,9 +37,9 @@ "#utility.yul" ] }, - "bin": "6000fe", + "bin": "5ffe", "bin-runtime": "6001", - "opcodes": "PUSH1 0x0 INVALID ", + "opcodes": "PUSH0 INVALID ", "srcmap": "0:0::-:0", "srcmap-runtime": "0:0::-:0" } @@ -52,7 +52,7 @@ "version": "" } Binary: -6000fe +5ffe Binary of the runtime part: 6001 EVM assembly: diff --git a/test/cmdlineTests/asm_json_import_verbatim/output b/test/cmdlineTests/asm_json_import_verbatim/output index b2cea96a6..cadab413f 100644 --- a/test/cmdlineTests/asm_json_import_verbatim/output +++ b/test/cmdlineTests/asm_json_import_verbatim/output @@ -1,5 +1,5 @@ Opcodes: -PUSH1 0x0 CALLDATALOAD PUSH1 0x14 DUP1 DUP3 SSTORE PUSH1 0x20 CALLDATALOAD ISZERO PUSH1 0x20 JUMPI DUP1 DUP3 SSTORE PUSH1 0x0 DUP1 PUSH1 0x0 DUP1 PUSH1 0x0 DUP1 STATICCALL POP PUSH25 0x797A5B80825550500000000000000000000000000000000000 +PUSH0 CALLDATALOAD PUSH1 0x14 DUP1 DUP3 SSTORE PUSH1 0x20 CALLDATALOAD ISZERO PUSH1 0x1C JUMPI DUP1 DUP3 SSTORE PUSH0 DUP1 PUSH0 DUP1 PUSH0 DUP1 STATICCALL POP PUSH25 0x797A5B80825550500000000000000000000000000000000000 EVM assembly: /* "":28:29 b */ 0x00 diff --git a/test/cmdlineTests/asm_json_import_yul_more_subobjects/args b/test/cmdlineTests/asm_json_import_yul_more_subobjects/args new file mode 100644 index 000000000..58dfc8cc7 --- /dev/null +++ b/test/cmdlineTests/asm_json_import_yul_more_subobjects/args @@ -0,0 +1 @@ +--import-asm-json - --opcodes diff --git a/test/cmdlineTests/asm_json_import_yul_more_subobjects/output b/test/cmdlineTests/asm_json_import_yul_more_subobjects/output new file mode 100644 index 000000000..81aa5b938 --- /dev/null +++ b/test/cmdlineTests/asm_json_import_yul_more_subobjects/output @@ -0,0 +1,2 @@ +Opcodes: +PUSH1 0x1E PUSH0 SSTORE PUSH1 0x1A PUSH1 0x1 SSTORE PUSH1 0x15 PUSH1 0x2 SSTORE PUSH1 0x1 PUSH1 0x3 SSTORE PUSH1 0x1 PUSH1 0x4 SSTORE PUSH1 0x1 PUSH1 0x5 SSTORE INVALID diff --git a/test/cmdlineTests/asm_json_import_yul_more_subobjects/stdin b/test/cmdlineTests/asm_json_import_yul_more_subobjects/stdin new file mode 100644 index 000000000..7b5b92bce --- /dev/null +++ b/test/cmdlineTests/asm_json_import_yul_more_subobjects/stdin @@ -0,0 +1,363 @@ +{ + ".code": + [ + { + "begin": 37, + "end": 51, + "name": "PUSHSIZE", + "source": 0 + }, + { + "begin": 34, + "end": 35, + "name": "PUSH", + "source": 0, + "value": "0" + }, + { + "begin": 27, + "end": 52, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 67, + "end": 81, + "name": "PUSH #[$]", + "source": 0, + "value": "0000000000000000000000000000000000000000000000000000000000000000" + }, + { + "begin": 64, + "end": 65, + "name": "PUSH", + "source": 0, + "value": "1" + }, + { + "begin": 57, + "end": 82, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 97, + "end": 114, + "name": "PUSH #[$]", + "source": 0, + "value": "000000000000000000000000000000000000000000000000ffffffffffffffff" + }, + { + "begin": 94, + "end": 95, + "name": "PUSH", + "source": 0, + "value": "2" + }, + { + "begin": 87, + "end": 115, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 130, + "end": 152, + "name": "PUSH #[$]", + "source": 0, + "value": "000000000000000000000000000000000000000000000000fffffffffffffffe" + }, + { + "begin": 127, + "end": 128, + "name": "PUSH", + "source": 0, + "value": "3" + }, + { + "begin": 120, + "end": 153, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 168, + "end": 190, + "name": "PUSH #[$]", + "source": 0, + "value": "000000000000000000000000000000000000000000000000fffffffffffffffd" + }, + { + "begin": 165, + "end": 166, + "name": "PUSH", + "source": 0, + "value": "4" + }, + { + "begin": 158, + "end": 191, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 206, + "end": 228, + "name": "PUSH #[$]", + "source": 0, + "value": "000000000000000000000000000000000000000000000000fffffffffffffffc" + }, + { + "begin": 203, + "end": 204, + "name": "PUSH", + "source": 0, + "value": "5" + }, + { + "begin": 196, + "end": 229, + "name": "SSTORE", + "source": 0 + } + ], + ".data": + { + "0": + { + ".code": + [ + { + "begin": 278, + "end": 292, + "name": "PUSHSIZE", + "source": 0 + }, + { + "begin": 275, + "end": 276, + "name": "PUSH", + "source": 0, + "value": "6" + }, + { + "begin": 268, + "end": 293, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 310, + "end": 324, + "name": "PUSH #[$]", + "source": 0, + "value": "0000000000000000000000000000000000000000000000000000000000000000" + }, + { + "begin": 307, + "end": 308, + "name": "PUSH", + "source": 0, + "value": "7" + }, + { + "begin": 300, + "end": 325, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 342, + "end": 361, + "name": "PUSH #[$]", + "source": 0, + "value": "000000000000000000000000000000000000000000000000ffffffffffffffff" + }, + { + "begin": 339, + "end": 340, + "name": "PUSH", + "source": 0, + "value": "8" + }, + { + "begin": 332, + "end": 362, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 379, + "end": 398, + "name": "PUSH #[$]", + "source": 0, + "value": "000000000000000000000000000000000000000000000000fffffffffffffffe" + }, + { + "begin": 376, + "end": 377, + "name": "PUSH", + "source": 0, + "value": "9" + }, + { + "begin": 369, + "end": 399, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 417, + "end": 436, + "name": "PUSH #[$]", + "source": 0, + "value": "000000000000000000000000000000000000000000000000fffffffffffffffd" + }, + { + "begin": 413, + "end": 415, + "name": "PUSH", + "source": 0, + "value": "A" + }, + { + "begin": 406, + "end": 437, + "name": "SSTORE", + "source": 0 + } + ], + ".data": + { + "0": + { + ".code": + [ + { + "begin": 494, + "end": 508, + "name": "PUSHSIZE", + "source": 0 + }, + { + "begin": 490, + "end": 492, + "name": "PUSH", + "source": 0, + "value": "B" + }, + { + "begin": 483, + "end": 509, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 529, + "end": 545, + "name": "PUSH #[$]", + "source": 0, + "value": "0000000000000000000000000000000000000000000000000000000000000000" + }, + { + "begin": 525, + "end": 527, + "name": "PUSH", + "source": 0, + "value": "C" + }, + { + "begin": 518, + "end": 546, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 566, + "end": 582, + "name": "PUSH #[$]", + "source": 0, + "value": "0000000000000000000000000000000000000000000000000000000000000001" + }, + { + "begin": 562, + "end": 564, + "name": "PUSH", + "source": 0, + "value": "D" + }, + { + "begin": 555, + "end": 583, + "name": "SSTORE", + "source": 0 + }, + { + "begin": 603, + "end": 619, + "name": "PUSH #[$]", + "source": 0, + "value": "0000000000000000000000000000000000000000000000000000000000000002" + }, + { + "begin": 599, + "end": 601, + "name": "PUSH", + "source": 0, + "value": "E" + }, + { + "begin": 592, + "end": 620, + "name": "SSTORE", + "source": 0 + } + ], + ".data": + { + "0": + { + ".code": + [ + { + "begin": 676, + "end": 685, + "name": "INVALID", + "source": 0 + } + ] + }, + "1": + { + ".code": + [ + { + "begin": 751, + "end": 760, + "name": "INVALID", + "source": 0 + } + ] + }, + "2": + { + ".code": + [ + { + "begin": 826, + "end": 835, + "name": "INVALID", + "source": 0 + } + ] + } + } + } + } + } + }, + "sourceList": + [ + "" + ] +} diff --git a/test/cmdlineTests/asm_json_import_yul_subobjects/args b/test/cmdlineTests/asm_json_import_yul_subobjects/args index 2b4b3a153..58dfc8cc7 100644 --- a/test/cmdlineTests/asm_json_import_yul_subobjects/args +++ b/test/cmdlineTests/asm_json_import_yul_subobjects/args @@ -1 +1 @@ ---import-asm-json - --opcodes --asm +--import-asm-json - --opcodes diff --git a/test/cmdlineTests/asm_json_import_yul_subobjects/err b/test/cmdlineTests/asm_json_import_yul_subobjects/err deleted file mode 100644 index 6bce200fe..000000000 --- a/test/cmdlineTests/asm_json_import_yul_subobjects/err +++ /dev/null @@ -1,5 +0,0 @@ -Uncaught exception: -/solidity/libevmasm/Assembly.cpp(1070): Throw in function vector solidity::evmasm::Assembly::decodeSubPath(size_t) const -Dynamic exception type: -std::exception::what: Assertion failed -[solidity::util::tag_comment*] = Assertion failed diff --git a/test/cmdlineTests/asm_json_import_yul_subobjects/exit b/test/cmdlineTests/asm_json_import_yul_subobjects/exit deleted file mode 100644 index 0cfbf0888..000000000 --- a/test/cmdlineTests/asm_json_import_yul_subobjects/exit +++ /dev/null @@ -1 +0,0 @@ -2 diff --git a/test/cmdlineTests/asm_json_import_yul_subobjects/output b/test/cmdlineTests/asm_json_import_yul_subobjects/output new file mode 100644 index 000000000..6aa2d0f64 --- /dev/null +++ b/test/cmdlineTests/asm_json_import_yul_subobjects/output @@ -0,0 +1,2 @@ +Opcodes: +PUSH0 DUP1 SSTORE PUSH1 0x6A PUSH1 0x1 SSTORE PUSH1 0x31 PUSH1 0x2 SSTORE PUSH1 0x2C PUSH1 0x3 SSTORE PUSH1 0x5E PUSH1 0x4 SSTORE PUSH1 0xC PUSH1 0x5 SSTORE PUSH1 0x5D PUSH1 0x6 SSTORE PUSH1 0x1 PUSH1 0x7 SSTORE PUSH1 0x5D PUSH1 0x8 SSTORE PUSH1 0x1 PUSH1 0x9 SSTORE INVALID PUSH1 0x1F PUSH1 0xA SSTORE PUSH1 0xC PUSH1 0xB SSTORE PUSH1 0x2B PUSH1 0xC SSTORE PUSH1 0x1 PUSH1 0xD SSTORE PUSH1 0x2B PUSH1 0xE SSTORE PUSH1 0x1 PUSH1 0xF SSTORE INVALID PUSH1 0xB PUSH1 0x10 SSTORE PUSH1 0x1 PUSH1 0x11 SSTORE INVALID INVALID INVALID INVALID PUSH1 0xB PUSH1 0x10 SSTORE PUSH1 0x1 PUSH1 0x11 SSTORE INVALID INVALID