Skip to content

Commit

Permalink
Optimize build_jumpdest_map()
Browse files Browse the repository at this point in the history
Optimize the build_jumpdest_map() procedure for JUMPDEST analysis.
- "is push opcode" conditions has been changed to (op & 0xE0) == 0x60.
- the op == JUMPDEST condition has been marked as unlikely.
  • Loading branch information
chfast committed Apr 22, 2021
1 parent c38ae16 commit b04b001
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/evmone/baseline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ namespace evmone
{
JumpdestMap build_jumpdest_map(const uint8_t* code, size_t code_size)
{
JumpdestMap m(code_size);
// Bitmask for PUSH instructions identification.
// It removes the lower 5 bits of a PUSH instruction with information about the data length.
// The remaining top 3 bits of every PUSH instruction is 0b011, i.e. 0x60, OP_PUSH1.
constexpr auto push_op_mask = 0b11100000;

JumpdestMap map(code_size);
for (size_t i = 0; i < code_size; ++i)
{
const auto op = code[i];
if (op == OP_JUMPDEST)
m[i] = true;
else if (op >= OP_PUSH1 && op <= OP_PUSH32)
i += op - size_t{OP_PUSH1 - 1};
if ((op & push_op_mask) == OP_PUSH1)
i += op - size_t{OP_PUSH1 - 1}; // Skip PUSH data.
else if (INTX_UNLIKELY(op == OP_JUMPDEST))
map[i] = true;
}
return m;
return map;
}

namespace
Expand Down

0 comments on commit b04b001

Please sign in to comment.