-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathanalysis_test.cpp
116 lines (94 loc) · 4.01 KB
/
analysis_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2018-2019 The evmone Authors.
// Licensed under the Apache License, Version 2.0.
#include <evmc/instructions.h>
#include <evmone/analysis.hpp>
#include <gtest/gtest.h>
#include <test/utils/bytecode.hpp>
#include <test/utils/utils.hpp>
using namespace evmone;
constexpr auto rev = EVMC_BYZANTIUM;
const auto fake_fn_table = []() noexcept
{
evmone::exec_fn_table fns;
for (size_t i = 0; i < fns.size(); ++i)
fns[i] = (evmone::exec_fn)i;
return fns;
}
();
TEST(analysis, example1)
{
const auto code = push(0x2a) + push(0x1e) + OP_MSTORE8 + OP_MSIZE + push(0) + OP_SSTORE;
const auto analysis = analyze(fake_fn_table, rev, &code[0], code.size());
ASSERT_EQ(analysis.instrs.size(), 8);
EXPECT_EQ(analysis.instrs[0].fn, fake_fn_table[OPX_BEGINBLOCK]);
EXPECT_EQ(analysis.instrs[1].fn, fake_fn_table[OP_PUSH1]);
EXPECT_EQ(analysis.instrs[2].fn, fake_fn_table[OP_PUSH1]);
EXPECT_EQ(analysis.instrs[3].fn, fake_fn_table[OP_MSTORE8]);
EXPECT_EQ(analysis.instrs[4].fn, fake_fn_table[OP_MSIZE]);
EXPECT_EQ(analysis.instrs[5].fn, fake_fn_table[OP_PUSH1]);
EXPECT_EQ(analysis.instrs[6].fn, fake_fn_table[OP_SSTORE]);
EXPECT_EQ(analysis.instrs[7].fn, fake_fn_table[OP_STOP]);
EXPECT_EQ(analysis.instrs[0].arg.block.gas_cost, 14);
EXPECT_EQ(analysis.instrs[0].arg.block.stack_req, 0);
EXPECT_EQ(analysis.instrs[0].arg.block.stack_max_growth, 2);
}
TEST(analysis, stack_up_and_down)
{
const auto code = OP_DUP2 + 6 * OP_DUP1 + 10 * OP_POP + push(0);
const auto analysis = analyze(fake_fn_table, rev, &code[0], code.size());
ASSERT_EQ(analysis.instrs.size(), 20);
EXPECT_EQ(analysis.instrs[0].fn, fake_fn_table[OPX_BEGINBLOCK]);
EXPECT_EQ(analysis.instrs[1].fn, fake_fn_table[OP_DUP2]);
EXPECT_EQ(analysis.instrs[2].fn, fake_fn_table[OP_DUP1]);
EXPECT_EQ(analysis.instrs[8].fn, fake_fn_table[OP_POP]);
EXPECT_EQ(analysis.instrs[18].fn, fake_fn_table[OP_PUSH1]);
EXPECT_EQ(analysis.instrs[0].arg.block.gas_cost, 7 * 3 + 10 * 2 + 3);
EXPECT_EQ(analysis.instrs[0].arg.block.stack_req, 3);
EXPECT_EQ(analysis.instrs[0].arg.block.stack_max_growth, 7);
}
TEST(analysis, push)
{
const auto code = push(0x0807060504030201) + "7f00ee";
const auto analysis = analyze(fake_fn_table, rev, &code[0], code.size());
ASSERT_EQ(analysis.instrs.size(), 4);
ASSERT_EQ(analysis.push_values.size(), 1);
EXPECT_EQ(analysis.instrs[0].fn, fake_fn_table[OPX_BEGINBLOCK]);
EXPECT_EQ(analysis.instrs[1].arg.small_push_value, 0x0807060504030201);
EXPECT_EQ(analysis.instrs[2].arg.push_value, &analysis.push_values[0]);
EXPECT_EQ(analysis.push_values[0], intx::uint256{0xee} << 240);
}
TEST(analysis, jump1)
{
const auto code = jump(add(4, 2)) + OP_JUMPDEST + mstore(0, 3) + ret(0, 0x20) + jump(6);
const auto analysis = analyze(fake_fn_table, rev, &code[0], code.size());
ASSERT_EQ(analysis.jumpdest_offsets.size(), 1);
ASSERT_EQ(analysis.jumpdest_targets.size(), 1);
EXPECT_EQ(analysis.jumpdest_offsets[0], 6);
EXPECT_EQ(analysis.jumpdest_targets[0], 5);
EXPECT_EQ(find_jumpdest(analysis, 6), 5);
EXPECT_EQ(find_jumpdest(analysis, 0), -1);
EXPECT_EQ(find_jumpdest(analysis, 7), -1);
}
TEST(analysis, empty)
{
bytes code;
auto analysis = evmone::analyze(fake_fn_table, rev, &code[0], code.size());
EXPECT_EQ(analysis.instrs.size(), 1);
EXPECT_EQ(analysis.instrs.back().fn, fake_fn_table[OP_STOP]);
}
TEST(analysis, only_jumpdest)
{
auto code = from_hex("5b");
auto analysis = evmone::analyze(fake_fn_table, rev, &code[0], code.size());
ASSERT_EQ(analysis.jumpdest_offsets.size(), 1);
ASSERT_EQ(analysis.jumpdest_targets.size(), 1);
EXPECT_EQ(analysis.jumpdest_offsets[0], 0);
EXPECT_EQ(analysis.jumpdest_targets[0], 0);
}
TEST(analysis, jumpi_at_the_end)
{
auto code = from_hex("57");
auto analysis = evmone::analyze(fake_fn_table, rev, &code[0], code.size());
EXPECT_EQ(analysis.instrs.back().fn, fake_fn_table[OP_STOP]);
}