Skip to content

Commit

Permalink
Process subAssemblies ensuring that there are no gaps between the keys
Browse files Browse the repository at this point in the history
  • Loading branch information
r0qs committed Oct 23, 2023
1 parent b32d5fd commit 840e7e3
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 56 deletions.
19 changes: 15 additions & 4 deletions libevmasm/Assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <range/v3/algorithm/any_of.hpp>
#include <range/v3/view/drop_exactly.hpp>
#include <range/v3/view/enumerate.hpp>
#include <range/v3/view/transform.hpp>

#include <fstream>
#include <limits>
Expand Down Expand Up @@ -565,7 +566,7 @@ std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSO
{
solRequire(_json[".data"].isObject(), AssemblyImportException, "Optional member '.data' is not an object.");
Json::Value const& data = _json[".data"];
size_t subId{};
std::map<size_t, std::shared_ptr<Assembly>> subAssemblies{};
for (Json::ValueConstIterator dataIter = data.begin(); dataIter != data.end(); dataIter++)
{
solAssert(dataIter.key().isString());
Expand Down Expand Up @@ -599,17 +600,27 @@ std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSO
{
solThrow(AssemblyImportException, "The key '" + dataItemID + "' inside '.data' is out of the supported integer range.");
}
solRequire(subId == index, AssemblyImportException, "Invalid order of keys in '.data'. Expect key '" + std::to_string(subId) + "' but got '" + dataItemID + "'.");

auto [subassembly, emptySourceList] = Assembly::fromJSON(dataItem, _level == 0 ? parsedSourceList : _sourceList, _level + 1);
solAssert(subassembly);
solAssert(emptySourceList.empty());
result->m_subs.emplace_back(subassembly);
subId++;
subAssemblies[index] = subassembly;
}
else
solThrow(AssemblyImportException, "The value of key '" + dataItemID + "' inside '.data' is neither a hex string nor an object.");
}

size_t subId{};
result->m_subs = subAssemblies
| ranges::views::transform([&](auto const& _sub) {
solRequire(
subId == _sub.first,
AssemblyImportException,
"Invalid order of keys in '.data'. Expect key '" + std::to_string(subId) + "' but got '" + std::to_string(_sub.first) + "'.");
subId++;
return _sub.second;
})
| ranges::to<std::vector<std::shared_ptr<Assembly>>>;
}

if (_level == 0)
Expand Down
23 changes: 11 additions & 12 deletions test/cmdlineTests/asm_json_import_missing_subobjects_indices/stdin
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
}
],
".data": {
"2": {
".code": []
},
"0": {
".code": []
},
"4": {
".code": []
},
"1": {
".code": []
}
"2": { ".code": [] },
"0": { ".code": [] },
"4": { ".code": [] },
"5": { ".code": [] },
"7": { ".code": [] },
"6": { ".code": [] },
"9": { ".code": [] },
"10": { ".code": [] },
"1": { ".code": [] },
"8": { ".code": [] },
"11": { ".code": [] }
}
}
18 changes: 0 additions & 18 deletions test/cmdlineTests/asm_json_import_random_index_ordering/output

This file was deleted.

22 changes: 0 additions & 22 deletions test/cmdlineTests/asm_json_import_random_index_ordering/stdin

This file was deleted.

35 changes: 35 additions & 0 deletions test/cmdlineTests/asm_json_import_random_order_data_index/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Opcodes:
PUSH0 INVALID
EVM assembly:
/* */
0x00
stop

sub_0: assembly {
}

sub_1: assembly {
stop

sub_0: assembly {
}

sub_1: assembly {
}

sub_2: assembly {
}
}

sub_2: assembly {
stop

sub_0: assembly {
}

sub_1: assembly {
}
}

sub_3: assembly {
}
27 changes: 27 additions & 0 deletions test/cmdlineTests/asm_json_import_random_order_data_index/stdin
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
".code": [
{
"name": "PUSH",
"value": "0"
}
],
".data": {
"2": {
".code": [],
".data": {
"1": { ".code": [] },
"0": { ".code": [] }
}
},
"0": { ".code": [] },
"1": {
".code": [],
".data": {
"1": { ".code": [] },
"2": { ".code": [] },
"0": { ".code": [] }
}
},
"3": { ".code": [] }
}
}

0 comments on commit 840e7e3

Please sign in to comment.