Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Block info optimization #144

Merged
merged 3 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 44 additions & 25 deletions lib/evmone/analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@

namespace evmone
{
struct block_analysis
{
int gas_cost = 0;

int stack_req = 0;
int stack_max_growth = 0;
int stack_change = 0;

/// The index of the beginblock instruction that starts the block.
/// This is the place where the analysis data is going to be dumped.
size_t begin_block_index = 0;

explicit block_analysis(size_t index) noexcept : begin_block_index{index} {}
};

inline constexpr uint64_t load64be(const unsigned char* data) noexcept
{
return uint64_t{data[7]} | (uint64_t{data[6]} << 8) | (uint64_t{data[5]} << 16) |
Expand All @@ -31,13 +46,9 @@ code_analysis analyze(evmc_revision rev, const uint8_t* code, size_t code_size)

const auto* instr_table = evmc_get_instruction_metrics_table(rev);

// Create new block.
auto block = &analysis.blocks.emplace_back();
int block_stack_change = 0;
{
auto& beginblock_instr = analysis.instrs.emplace_back(opx_beginblock_fn);
beginblock_instr.arg.number = static_cast<int>(analysis.blocks.size() - 1);
}
// Create first block.
analysis.instrs.emplace_back(opx_beginblock_fn);
auto block = block_analysis{0};

const auto code_end = code + code_size;
auto code_pos = code;
Expand All @@ -50,19 +61,12 @@ code_analysis analyze(evmc_revision rev, const uint8_t* code, size_t code_size)
const auto instr_stack_req = metrics.num_stack_arguments;
const auto instr_stack_change = metrics.num_stack_returned_items - instr_stack_req;

// TODO: Define a block_analysis struct with regular ints for analysis.
// Compress it when block is closed.
auto stack_req = instr_stack_req - block_stack_change;
if (stack_req > std::numeric_limits<decltype(block->stack_req)>::max())
stack_req = std::numeric_limits<decltype(block->stack_req)>::max();

block->stack_req = std::max(block->stack_req, static_cast<int16_t>(stack_req));
block_stack_change += instr_stack_change;
block->stack_max_growth =
static_cast<int16_t>(std::max(int{block->stack_max_growth}, block_stack_change));
block.stack_req = std::max(block.stack_req, instr_stack_req - block.stack_change);
block.stack_change += instr_stack_change;
block.stack_max_growth = std::max(block.stack_max_growth, block.stack_change);

if (metrics.gas_cost > 0) // can be -1 for undefined instruction
block->gas_cost += metrics.gas_cost;
block.gas_cost += metrics.gas_cost;

if (opcode == OP_JUMPDEST)
{
Expand Down Expand Up @@ -137,25 +141,40 @@ code_analysis analyze(evmc_revision rev, const uint8_t* code, size_t code_size)
case OP_STATICCALL:
case OP_CREATE:
case OP_CREATE2:
instr.arg.number = block->gas_cost;
instr.arg.number = block.gas_cost;
break;

case OP_PC:
instr.arg.number = static_cast<int>(code_pos - code - 1);
break;
}

// If this is a terminating instruction or the next instruction is a JUMPDEST.
if (is_terminator || (code_pos != code_end && *code_pos == OP_JUMPDEST))
{
// Create new basic block if
// this is a terminating instruction or the next instruction is a JUMPDEST.
block = &analysis.blocks.emplace_back();
block_stack_change = 0;
auto& beginblock_instr = analysis.instrs.emplace_back(opx_beginblock_fn);
beginblock_instr.arg.number = static_cast<int>(analysis.blocks.size() - 1);
// Save current block.
const auto stack_req = block.stack_req <= std::numeric_limits<int16_t>::max() ?
static_cast<int16_t>(block.stack_req) :
std::numeric_limits<int16_t>::max();
const auto stack_max_growth = static_cast<int16_t>(block.stack_max_growth);
analysis.instrs[block.begin_block_index].arg.block = {
block.gas_cost, stack_req, stack_max_growth};


// Create new block.
analysis.instrs.emplace_back(opx_beginblock_fn);
block = block_analysis{analysis.instrs.size() - 1};
}
}

// Save current block.
const auto stack_req = block.stack_req <= std::numeric_limits<int16_t>::max() ?
static_cast<int16_t>(block.stack_req) :
std::numeric_limits<int16_t>::max();
const auto stack_max_growth = static_cast<int16_t>(block.stack_max_growth);
analysis.instrs[block.begin_block_index].arg.block = {
block.gas_cost, stack_req, stack_max_growth};

// Make sure the last block is terminated.
// TODO: This is not needed if the last instruction is a terminating one.
analysis.instrs.emplace_back(fns[OP_STOP]);
Expand Down
1 change: 0 additions & 1 deletion lib/evmone/analysis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ static_assert(sizeof(block_info) == 8);
struct code_analysis
{
std::vector<instr_info> instrs;
std::vector<block_info> blocks;

/// Storage for large push values.
std::vector<intx::uint256> push_values;
Expand Down
2 changes: 1 addition & 1 deletion lib/evmone/instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ const instr_info* op_selfdestruct(const instr_info*, execution_state& state) noe

const instr_info* opx_beginblock(const instr_info* instr, execution_state& state) noexcept
{
auto& block = state.analysis->blocks[static_cast<size_t>(instr->arg.number)];
auto& block = instr->arg.block;

if ((state.gas_left -= block.gas_cost) < 0)
return state.exit(EVMC_OUT_OF_GAS);
Expand Down
25 changes: 8 additions & 17 deletions test/unittests/analysis_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ TEST(analysis, example1)
ASSERT_EQ(analysis.instrs.size(), 8);

EXPECT_EQ(analysis.instrs[0].fn, op_table[OPX_BEGINBLOCK]);
EXPECT_EQ(analysis.instrs[0].arg.number, 0);
EXPECT_EQ(analysis.instrs[1].fn, op_table[OP_PUSH1]);
EXPECT_EQ(analysis.instrs[2].fn, op_table[OP_PUSH1]);
EXPECT_EQ(analysis.instrs[3].fn, op_table[OP_MSTORE8]);
Expand All @@ -30,10 +29,10 @@ TEST(analysis, example1)
EXPECT_EQ(analysis.instrs[6].fn, op_table[OP_SSTORE]);
EXPECT_EQ(analysis.instrs[7].fn, op_table[OP_STOP]);

ASSERT_EQ(analysis.blocks.size(), 1);
EXPECT_EQ(analysis.blocks[0].gas_cost, 14);
EXPECT_EQ(analysis.blocks[0].stack_req, 0);
EXPECT_EQ(analysis.blocks[0].stack_max_growth, 2);
const auto& block = analysis.instrs[0].arg.block;
EXPECT_EQ(block.gas_cost, 14);
EXPECT_EQ(block.stack_req, 0);
EXPECT_EQ(block.stack_max_growth, 2);
}

TEST(analysis, stack_up_and_down)
Expand All @@ -43,16 +42,15 @@ TEST(analysis, stack_up_and_down)

ASSERT_EQ(analysis.instrs.size(), 20);
EXPECT_EQ(analysis.instrs[0].fn, op_table[OPX_BEGINBLOCK]);
EXPECT_EQ(analysis.instrs[0].arg.number, 0);
EXPECT_EQ(analysis.instrs[1].fn, op_table[OP_DUP2]);
EXPECT_EQ(analysis.instrs[2].fn, op_table[OP_DUP1]);
EXPECT_EQ(analysis.instrs[8].fn, op_table[OP_POP]);
EXPECT_EQ(analysis.instrs[18].fn, op_table[OP_PUSH1]);

ASSERT_EQ(analysis.blocks.size(), 1);
EXPECT_EQ(analysis.blocks[0].gas_cost, 7 * 3 + 10 * 2 + 3);
EXPECT_EQ(analysis.blocks[0].stack_req, 3);
EXPECT_EQ(analysis.blocks[0].stack_max_growth, 7);
const auto& block = analysis.instrs[0].arg.block;
EXPECT_EQ(block.gas_cost, 7 * 3 + 10 * 2 + 3);
EXPECT_EQ(block.stack_req, 3);
EXPECT_EQ(block.stack_max_growth, 7);
}

TEST(analysis, push)
Expand All @@ -77,7 +75,6 @@ TEST(analysis, jumpdest_skip)
const auto code = bytecode{} + OP_STOP + OP_JUMPDEST;
auto analysis = evmone::analyze(rev, &code[0], code.size());

EXPECT_EQ(analysis.blocks.size(), 2);
ASSERT_EQ(analysis.instrs.size(), 4);
EXPECT_EQ(analysis.instrs[0].fn, op_table[OPX_BEGINBLOCK]);
EXPECT_EQ(analysis.instrs[1].fn, op_table[OP_STOP]);
Expand All @@ -90,7 +87,6 @@ TEST(analysis, jump1)
const auto code = jump(add(4, 2)) + OP_JUMPDEST + mstore(0, 3) + ret(0, 0x20) + jump(6);
const auto analysis = analyze(rev, &code[0], code.size());

ASSERT_EQ(analysis.blocks.size(), 4);
ASSERT_EQ(analysis.jumpdest_offsets.size(), 1);
ASSERT_EQ(analysis.jumpdest_targets.size(), 1);
EXPECT_EQ(analysis.jumpdest_offsets[0], 6);
Expand All @@ -105,7 +101,6 @@ TEST(analysis, empty)
bytes code;
auto analysis = evmone::analyze(rev, &code[0], code.size());

EXPECT_EQ(analysis.blocks.size(), 1);
ASSERT_EQ(analysis.instrs.size(), 2);
EXPECT_EQ(analysis.instrs[0].fn, op_table[OPX_BEGINBLOCK]);
EXPECT_EQ(analysis.instrs[1].fn, op_table[OP_STOP]);
Expand All @@ -116,7 +111,6 @@ TEST(analysis, only_jumpdest)
const auto code = bytecode{OP_JUMPDEST};
auto analysis = evmone::analyze(rev, &code[0], code.size());

ASSERT_EQ(analysis.blocks.size(), 1);
ASSERT_EQ(analysis.jumpdest_offsets.size(), 1);
ASSERT_EQ(analysis.jumpdest_targets.size(), 1);
EXPECT_EQ(analysis.jumpdest_offsets[0], 0);
Expand All @@ -128,7 +122,6 @@ TEST(analysis, jumpi_at_the_end)
const auto code = bytecode{OP_JUMPI};
auto analysis = evmone::analyze(rev, &code[0], code.size());

EXPECT_EQ(analysis.blocks.size(), 2);
ASSERT_EQ(analysis.instrs.size(), 4);
EXPECT_EQ(analysis.instrs[0].fn, op_table[OPX_BEGINBLOCK]);
EXPECT_EQ(analysis.instrs[1].fn, op_table[OP_JUMPI]);
Expand All @@ -143,7 +136,6 @@ TEST(analysis, terminated_last_block)
const auto code = ret(0, 0);
auto analysis = evmone::analyze(rev, &code[0], code.size());

EXPECT_EQ(analysis.blocks.size(), 2);
ASSERT_EQ(analysis.instrs.size(), 6);
EXPECT_EQ(analysis.instrs[0].fn, op_table[OPX_BEGINBLOCK]);
EXPECT_EQ(analysis.instrs[3].fn, op_table[OP_RETURN]);
Expand All @@ -156,7 +148,6 @@ TEST(analysis, jumpdests_groups)
const auto code = 3 * OP_JUMPDEST + push(1) + 3 * OP_JUMPDEST + push(2) + OP_JUMPI;
auto analysis = evmone::analyze(rev, &code[0], code.size());

EXPECT_EQ(analysis.blocks.size(), 7);
ASSERT_EQ(analysis.instrs.size(), 11);
EXPECT_EQ(analysis.instrs[0].fn, op_table[OP_JUMPDEST]);
EXPECT_EQ(analysis.instrs[1].fn, op_table[OP_JUMPDEST]);
Expand Down
2 changes: 1 addition & 1 deletion test/utils/dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void dump(const evmone::code_analysis& analysis)

if (c == OPX_BEGINBLOCK)
{
block = &analysis.blocks[size_t(instr.arg.number)];
block = &instr.arg.block;

const auto get_jumpdest_offset = [&analysis](size_t index) noexcept
{
Expand Down