-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalysis.hpp
456 lines (416 loc) · 13.3 KB
/
analysis.hpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2019-2020 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "execution_state.hpp"
#include "limits.hpp"
#include "evmc/evmc.hpp"
#include "evmc/instructions.h"
#include "intx/intx.hpp"
#include <array>
#include <cstdint>
template <typename T>
class static_vector {
private:
mutable T array[400];
size_t index = 0;
public:
static_vector() : index(0) { }
template <typename... Us>
void emplace_back(Us... params) {
array[index++] = T(params...);
}
const T* begin() const {
return &(array[0]);
}
const T* end() const {
return &(array[index]);
}
void emplace_back(){
// increase len and do nothing
index++;
}
void reserve(int /*size*/){ } // reserve max, anyway
T& operator[](int id) const { // later "const" allow passing obj as "const this"
return array[id];
}
T& back() {
return array[index - 1];
}
size_t size() const{
return index;
}
};
const uint32_t max_instruction_size = 2048;
/// Compressed information about instruction basic block.
struct block_info
{
/// The total base gas cost of all instructions in the block.
/// This cannot overflow, see the static_assert() below.
uint32_t gas_cost = 0;
// static_assert(
// max_code_size * max_instruction_base_cost < std::numeric_limits<decltype(gas_cost)>::max(),
// "Potential block_info::gas_cost overflow");
/// The stack height required to execute the block.
/// This MAY overflow.
int16_t stack_req = 0;
/// The maximum stack height growth relative to the stack height at block start.
/// This cannot overflow, see the static_assert() below.
int16_t stack_max_growth = 0;
// static_assert(max_code_size * max_instruction_stack_increase <
// std::numeric_limits<decltype(stack_max_growth)>::max(),
// "Potential block_info::stack_max_growth overflow");
constexpr explicit block_info(): gas_cost(0), stack_req(0), stack_max_growth(0) {}
constexpr explicit block_info(uint32_t gas, int16_t sr, int16_t sg): gas_cost(gas), stack_req(sr), stack_max_growth(sg) {}
};
union instruction_argument
{
int64_t number;
intx::uint256 push_value;
uint64_t small_push_value;
block_info block{};
};
struct instruction
{
uint8_t opcode;
instruction_argument arg;
explicit constexpr instruction() noexcept : opcode(0), arg{} {}
explicit constexpr instruction(uint8_t c) noexcept : opcode(c), arg{} {}
};
//struct AdvancedCodeAnalysis;
struct AdvancedCodeAnalysis
{
static_vector<instruction> instrs;
/// Storage for large push values.
static_vector<intx::uint256> push_values;
/// The offsets of JUMPDESTs in the original code.
/// These are values that JUMP/JUMPI receives as an argument.
/// The elements are sorted.
static_vector<int32_t> jumpdest_offsets;
/// The indexes of the instructions in the generated instruction table
/// matching the elements from jumdest_offsets.
/// This is value to which the next instruction pointer must be set in JUMP/JUMPI.
static_vector<int32_t> jumpdest_targets;
};
/// The execution state specialized for the Advanced interpreter.
struct AdvancedExecutionState : ExecutionState
{
/// The gas cost of the current block.
///
/// This is only needed to correctly calculate the "current gas left" value.
uint32_t current_block_cost = 0;
/// Pointer to code analysis.
// AdvancedCodeAnalysis analysis; //= nullptr;
using ExecutionState::ExecutionState;
AdvancedExecutionState(): current_block_cost(0) {}
/// Terminates the execution with the given status code.
const instruction* exit(evmc_status_code status_code) noexcept
{
status = status_code;
return nullptr;
}
/// Resets the contents of the execution_state so that it could be reused.
void reset(const evmc_message& message, evmc_revision revision,
const evmc_host_interface& host_interface, evmc_host_context* host_ctx,
const uint8_t* code_ptr, size_t code_size) noexcept
{
ExecutionState::reset(message, revision, host_interface, host_ctx, code_ptr, code_size);
current_block_cost = 0;
}
};
// no need static check as push_value is not pointer anymore
//static_assert(
// sizeof(instruction_argument) == sizeof(uint64_t), "Incorrect size of instruction_argument");
/// The pointer to function implementing an instruction execution.
//using instruction_exec_fn = const instruction* (*)(const instruction*, AdvancedExecutionState&);
/// The evmone intrinsic opcodes.
///
/// These intrinsic instructions may be injected to the code in the analysis phase.
/// They contain additional and required logic to be executed by the interpreter.
enum intrinsic_opcodes
{
/// The BEGINBLOCK instruction.
///
/// This instruction is defined as alias for JUMPDEST and replaces all JUMPDEST instructions.
/// It is also injected at beginning of basic blocks not being the valid jump destination.
/// It checks basic block execution requirements and terminates execution if they are not met.
OPX_BEGINBLOCK = OP_JUMPDEST
};
struct op_table_entry
{
int16_t gas_cost;
int8_t stack_req;
int8_t stack_change;
constexpr op_table_entry(int16_t gas_cost, int8_t stack_req, int8_t stack_change): gas_cost(gas_cost), stack_req(stack_req), stack_change(stack_change) {};
};
using op_table = ::std::array<op_table_entry, 256>;
// default op_table using EVMC_LONDON - latest version
constexpr op_table default_op_table = {{
op_table_entry(0, 0, 0),
op_table_entry(3, 2, -1),
op_table_entry(5, 2, -1),
op_table_entry(3, 2, -1),
op_table_entry(5, 2, -1),
op_table_entry(5, 2, -1),
op_table_entry(5, 2, -1),
op_table_entry(5, 2, -1),
op_table_entry(8, 3, -2),
op_table_entry(8, 3, -2),
op_table_entry(10, 2, -1),
op_table_entry(5, 2, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(3, 2, -1),
op_table_entry(3, 2, -1),
op_table_entry(3, 2, -1),
op_table_entry(3, 2, -1),
op_table_entry(3, 2, -1),
op_table_entry(3, 1, 0),
op_table_entry(3, 2, -1),
op_table_entry(3, 2, -1),
op_table_entry(3, 2, -1),
op_table_entry(3, 1, 0),
op_table_entry(3, 2, -1),
op_table_entry(3, 2, -1),
op_table_entry(3, 2, -1),
op_table_entry(3, 2, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(30, 2, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(2, 0, 1),
op_table_entry(100, 1, 0),
op_table_entry(2, 0, 1),
op_table_entry(2, 0, 1),
op_table_entry(2, 0, 1),
op_table_entry(3, 1, 0),
op_table_entry(2, 0, 1),
op_table_entry(3, 3, -3),
op_table_entry(2, 0, 1),
op_table_entry(3, 3, -3),
op_table_entry(2, 0, 1),
op_table_entry(100, 1, 0),
op_table_entry(100, 4, -4),
op_table_entry(2, 0, 1),
op_table_entry(3, 3, -3),
op_table_entry(100, 1, 0),
op_table_entry(20, 1, 0),
op_table_entry(2, 0, 1),
op_table_entry(2, 0, 1),
op_table_entry(2, 0, 1),
op_table_entry(2, 0, 1),
op_table_entry(2, 0, 1),
op_table_entry(2, 0, 1),
op_table_entry(5, 0, 1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(2, 1, -1),
op_table_entry(3, 1, 0),
op_table_entry(3, 2, -2),
op_table_entry(3, 2, -2),
op_table_entry(100, 1, 0),
op_table_entry(0, 2, -2),
op_table_entry(8, 1, -1),
op_table_entry(10, 2, -2),
op_table_entry(2, 0, 1),
op_table_entry(2, 0, 1),
op_table_entry(2, 0, 1),
op_table_entry(1, 0, 0),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 0, 1),
op_table_entry(3, 1, 1),
op_table_entry(3, 2, 1),
op_table_entry(3, 3, 1),
op_table_entry(3, 4, 1),
op_table_entry(3, 5, 1),
op_table_entry(3, 6, 1),
op_table_entry(3, 7, 1),
op_table_entry(3, 8, 1),
op_table_entry(3, 9, 1),
op_table_entry(3, 10, 1),
op_table_entry(3, 11, 1),
op_table_entry(3, 12, 1),
op_table_entry(3, 13, 1),
op_table_entry(3, 14, 1),
op_table_entry(3, 15, 1),
op_table_entry(3, 16, 1),
op_table_entry(3, 2, 0),
op_table_entry(3, 3, 0),
op_table_entry(3, 4, 0),
op_table_entry(3, 5, 0),
op_table_entry(3, 6, 0),
op_table_entry(3, 7, 0),
op_table_entry(3, 8, 0),
op_table_entry(3, 9, 0),
op_table_entry(3, 10, 0),
op_table_entry(3, 11, 0),
op_table_entry(3, 12, 0),
op_table_entry(3, 13, 0),
op_table_entry(3, 14, 0),
op_table_entry(3, 15, 0),
op_table_entry(3, 16, 0),
op_table_entry(3, 17, 0),
op_table_entry(375, 2, -2),
op_table_entry(750, 3, -3),
op_table_entry(1125, 4, -4),
op_table_entry(1500, 5, -5),
op_table_entry(1875, 6, -6),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(32000, 3, -2),
op_table_entry(100, 7, -6),
op_table_entry(100, 7, -6),
op_table_entry(0, 2, -2),
op_table_entry(100, 6, -5),
op_table_entry(32000, 4, -3),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(100, 6, -5),
op_table_entry(-1, -1, -1),
op_table_entry(-1, -1, -1),
op_table_entry(0, 2, -2),
op_table_entry(0, 0, 0),
op_table_entry(5000, 1, -1),
}};
inline int find_jumpdest(const AdvancedCodeAnalysis& analysis, int offset) noexcept
{
const auto begin = analysis.jumpdest_offsets.begin();// std::begin(analysis.jumpdest_offsets);
const auto end = analysis.jumpdest_offsets.end(); // std::end(analysis.jumpdest_offsets);
const auto it = ::std::lower_bound(begin, end, offset);
// printf("Jump to instruction %d\n", static_cast<size_t>(it - begin));
return (it != end && *it == offset) ?
analysis.jumpdest_targets[static_cast<size_t>(it - begin)] :
-1;
}
AdvancedCodeAnalysis analyze(const uint8_t* code, size_t code_size) noexcept;