Skip to content

Commit 9ca8739

Browse files
committed
Split jumpdest map into 2 vectors of int16_t
1 parent 06d3b86 commit 9ca8739

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

lib/evmone/analysis.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ code_analysis analyze(
6565
beginblock_instr.arg.p.number = static_cast<int>(analysis.blocks.size() - 1);
6666

6767
if (jumpdest) // Add the jumpdest to the map.
68-
analysis.jumpdest_map.emplace_back(static_cast<int>(i), instr_index);
68+
{
69+
analysis.jumpdest_offsets.emplace_back(static_cast<int16_t>(i));
70+
analysis.jumpdest_targets.emplace_back(static_cast<int16_t>(instr_index));
71+
}
6972
else // Increase instruction count because additional BEGINBLOCK was injected.
7073
++instr_index;
7174
}

lib/evmone/analysis.hpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,23 @@ struct code_analysis
152152
/// invalidated when the container grows.
153153
std::deque<bytes32> args_storage;
154154

155-
std::vector<std::pair<int, int>> jumpdest_map;
155+
/// The offsets of JUMPDESTs in the original code.
156+
/// These are values that JUMP/JUMPI receives as an argument.
157+
/// The elements are sorted.
158+
std::vector<int16_t> jumpdest_offsets;
159+
160+
/// The indexes of the instructions in the generated instruction table
161+
/// matching the elements from jumdest_offsets.
162+
/// This is value to which the next instruction pointer must be set in JUMP/JUMPI.
163+
std::vector<int16_t> jumpdest_targets;
156164
};
157165

158166
inline int find_jumpdest(const code_analysis& analysis, int offset) noexcept
159167
{
160-
const auto& m = analysis.jumpdest_map;
161-
const auto it = std::lower_bound(std::begin(m), std::end(m), offset,
162-
[](std::pair<int, int> p, int v) noexcept { return p.first < v; });
163-
return (it != std::end(m) && it->first == offset) ? it->second : -1;
168+
const auto begin = std::begin(analysis.jumpdest_offsets);
169+
const auto end = std::end(analysis.jumpdest_offsets);
170+
const auto it = std::lower_bound(begin, end, offset);
171+
return (it != end && *it == offset) ? analysis.jumpdest_targets[it - begin] : -1;
164172
}
165173

166174
EVMC_EXPORT code_analysis analyze(

test/unittests/analysis_test.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ TEST(analysis, jump1)
8686
const auto analysis = analyze(fake_fn_table, rev, &code[0], code.size());
8787

8888
ASSERT_EQ(analysis.blocks.size(), 3);
89-
ASSERT_EQ(analysis.jumpdest_map.size(), 1);
90-
EXPECT_EQ(analysis.jumpdest_map[0], std::pair(6, 5));
89+
ASSERT_EQ(analysis.jumpdest_offsets.size(), 1);
90+
ASSERT_EQ(analysis.jumpdest_targets.size(), 1);
91+
EXPECT_EQ(analysis.jumpdest_offsets[0], 6);
92+
EXPECT_EQ(analysis.jumpdest_targets[0], 5);
9193
EXPECT_EQ(find_jumpdest(analysis, 6), 5);
9294
EXPECT_EQ(find_jumpdest(analysis, 0), -1);
9395
EXPECT_EQ(find_jumpdest(analysis, 7), -1);
@@ -109,8 +111,10 @@ TEST(analysis, only_jumpdest)
109111
auto analysis = evmone::analyze(fake_fn_table, rev, &code[0], code.size());
110112

111113
ASSERT_EQ(analysis.blocks.size(), 1);
112-
ASSERT_EQ(analysis.jumpdest_map.size(), 1);
113-
EXPECT_EQ(analysis.jumpdest_map[0], std::pair(0, 0));
114+
ASSERT_EQ(analysis.jumpdest_offsets.size(), 1);
115+
ASSERT_EQ(analysis.jumpdest_targets.size(), 1);
116+
EXPECT_EQ(analysis.jumpdest_offsets[0], 0);
117+
EXPECT_EQ(analysis.jumpdest_targets[0], 0);
114118
}
115119

116120
TEST(analysis, jumpi_at_the_end)

test/utils/dump.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ void dump_analysis(const evmone::code_analysis& analysis)
3232

3333
auto get_jumpdest_offset = [&analysis](size_t index) noexcept
3434
{
35-
for (const auto& d : analysis.jumpdest_map)
35+
for (auto it = analysis.jumpdest_targets.begin();
36+
it != analysis.jumpdest_targets.end(); ++it)
3637
{
37-
if (d.second == static_cast<int>(index))
38-
return d.first;
38+
if (*it == static_cast<int>(index))
39+
return analysis.jumpdest_offsets[it - analysis.jumpdest_targets.begin()];
3940
}
40-
return -1;
41+
return int16_t{-1};
4142
};
4243

4344
std::cout << "";

0 commit comments

Comments
 (0)