Skip to content

Commit

Permalink
Do not keep information about block stack change
Browse files Browse the repository at this point in the history
This information is only needed in analysis to compute other block stack requirements parameters.
  • Loading branch information
chfast committed Aug 14, 2019
1 parent 5bd0043 commit 207dee9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
8 changes: 5 additions & 3 deletions lib/evmone/analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ code_analysis analyze(

block_info* block = nullptr;

int block_stack_change = 0;
int instr_index = 0;

const auto code_end = code + code_size;
Expand All @@ -68,6 +69,7 @@ code_analysis analyze(
{
// Create new block.
block = &analysis.blocks.emplace_back();
block_stack_change = 0;

// Create BEGINBLOCK instruction which either replaces JUMPDEST or is injected
// in case there is no JUMPDEST.
Expand All @@ -89,9 +91,9 @@ code_analysis analyze(
const auto instr_stack_req = metrics.num_stack_arguments;
const auto instr_stack_change = metrics.num_stack_returned_items - instr_stack_req;

block->stack_req = std::max(block->stack_req, instr_stack_req - block->stack_change);
block->stack_change += instr_stack_change;
block->stack_max = std::max(block->stack_max, 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 = std::max(block->stack_max, block_stack_change);

if (metrics.gas_cost > 0) // can be -1 for undefined instruction
block->gas_cost += metrics.gas_cost;
Expand Down
10 changes: 7 additions & 3 deletions lib/evmone/analysis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,14 @@ struct instr_info

struct block_info
{
/// The total base gas cost of all instructions in the block.
int64_t gas_cost = 0;
int stack_req = 0; ///< The required stack height to execute the block.
int stack_max = 0; ///< The relative maximum stack height in the block.
int stack_change = 0; ///< The relative stack height change after the execution of the block.

/// The stack height required to execute the block.
int stack_req = 0;

/// The maximum stack height relative to the stack height at block start.
int stack_max = 0;
};

struct code_analysis
Expand Down
2 changes: 0 additions & 2 deletions test/unittests/analysis_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ TEST(analysis, example1)
EXPECT_EQ(analysis.blocks[0].gas_cost, 14);
EXPECT_EQ(analysis.blocks[0].stack_req, 0);
EXPECT_EQ(analysis.blocks[0].stack_max, 2);
EXPECT_EQ(analysis.blocks[0].stack_change, 0);
}

TEST(analysis, stack_up_and_down)
Expand All @@ -63,7 +62,6 @@ TEST(analysis, stack_up_and_down)
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, 7);
EXPECT_EQ(analysis.blocks[0].stack_change, -2);
}

TEST(analysis, push)
Expand Down
2 changes: 1 addition & 1 deletion test/utils/dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void dump_analysis(const evmone::code_analysis& analysis)
std::cout << " ";

std::cout << " " << std::setw(10) << block->gas_cost << " " << block->stack_req << " "
<< block->stack_max << " " << block->stack_change << "\n";
<< block->stack_max << "\n";
}

std::cout << "" << std::setw(9) << std::left << name << std::setw(4) << std::right
Expand Down

0 comments on commit 207dee9

Please sign in to comment.