This repository was archived by the owner on Apr 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathexample_vm.cpp
More file actions
393 lines (335 loc) · 12.4 KB
/
example_vm.cpp
File metadata and controls
393 lines (335 loc) · 12.4 KB
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
// EVMC: Ethereum Client-VM Connector API.
// Copyright 2016 The EVMC Authors.
// Licensed under the Apache License, Version 2.0.
/// @file
/// Example implementation of the EVMC VM interface.
///
/// This VM implements a subset of EVM instructions in simplistic, incorrect and unsafe way:
/// - memory bounds are not checked,
/// - stack bounds are not checked,
/// - most of the operations are done with 32-bit precision (instead of EVM 256-bit precision).
/// Yet, it is capable of coping with some example EVM bytecode inputs, which is very useful
/// in integration testing. The implementation is done in simple C++ for readability and uses
/// pure C API and some C helpers.
#include "example_vm.h"
#include <evmc/evmc.h>
#include <evmc/helpers.h>
#include <evmc/instructions.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
/// The Example VM methods, helper and types are contained in the anonymous namespace.
/// Technically, this limits the visibility of these elements (internal linkage).
/// This is not strictly required, but is good practice and promotes position independent code.
namespace
{
/// The example VM instance struct extending the evmc_vm.
struct ExampleVM : evmc_vm
{
int verbose = 0; ///< The verbosity level.
ExampleVM(); ///< Constructor to initialize the evmc_vm struct.
};
/// The implementation of the evmc_vm::destroy() method.
void destroy(evmc_vm* instance)
{
delete static_cast<ExampleVM*>(instance);
}
/// The example implementation of the evmc_vm::get_capabilities() method.
evmc_capabilities_flagset get_capabilities(evmc_vm* /*instance*/)
{
return EVMC_CAPABILITY_EVM1;
}
/// Example VM options.
///
/// The implementation of the evmc_vm::set_option() method.
/// VMs are allowed to omit this method implementation.
enum evmc_set_option_result set_option(evmc_vm* instance, const char* name, const char* value)
{
auto* vm = static_cast<ExampleVM*>(instance);
if (std::strcmp(name, "verbose") == 0)
{
if (value == nullptr)
return EVMC_SET_OPTION_INVALID_VALUE;
char* end = nullptr;
auto v = std::strtol(value, &end, 0);
if (end == value) // Parsing the value failed.
return EVMC_SET_OPTION_INVALID_VALUE;
if (v > 9 || v < -1) // Not in the valid range.
return EVMC_SET_OPTION_INVALID_VALUE;
vm->verbose = static_cast<int>(v);
return EVMC_SET_OPTION_SUCCESS;
}
return EVMC_SET_OPTION_INVALID_NAME;
}
/// The Example VM stack representation.
struct Stack
{
evmc_uint256be items[1024] = {}; ///< The array of stack items.
evmc_uint256be* pointer = items; ///< The pointer to the currently first empty stack slot.
/// Pops an item from the top of the stack.
evmc_uint256be pop() { return *--pointer; }
/// Pushes an item to the top of the stack.
void push(evmc_uint256be value) { *pointer++ = value; }
};
/// The Example VM memory representation.
struct Memory
{
uint32_t size = 0; ///< The current size of the memory.
uint8_t data[1024] = {}; ///< The fixed-size memory buffer.
/// Expands the "active" EVM memory by the given memory region defined by
/// @p offset and @p region_size. The region of size 0 also expands the memory
/// (what is different behavior than EVM specifies).
/// Returns pointer to the beginning of the region in the memory,
/// or nullptr if the memory cannot be expanded to the required size.
uint8_t* expand(uint32_t offset, uint32_t region_size)
{
uint32_t new_size = offset + region_size;
if (new_size > sizeof(data))
return nullptr; // Cannot expand more than fixed max memory size.
if (new_size > size)
size = new_size; // Update current memory size.
return &data[offset];
}
/// Stores the given value bytes in the memory at the given offset.
/// The Memory::size is updated accordingly.
/// Returns true if successful, false if the memory cannot be expanded to the required size.
bool store(uint32_t offset, const uint8_t* value_data, uint32_t value_size)
{
uint8_t* p = expand(offset, value_size);
if (p == nullptr)
return false;
std::memcpy(p, value_data, value_size);
return true;
}
};
/// Creates 256-bit value out of 32-bit input.
inline evmc_uint256be to_uint256(uint32_t x)
{
evmc_uint256be value = {};
value.bytes[31] = static_cast<uint8_t>(x);
value.bytes[30] = static_cast<uint8_t>(x >> 8);
value.bytes[29] = static_cast<uint8_t>(x >> 16);
value.bytes[28] = static_cast<uint8_t>(x >> 24);
return value;
}
/// Creates 256-bit value out of an 160-bit address.
inline evmc_uint256be to_uint256(evmc_address address)
{
evmc_uint256be value = {};
size_t offset = sizeof(value) - sizeof(address);
std::memcpy(&value.bytes[offset], address.bytes, sizeof(address.bytes));
return value;
}
/// Truncates 256-bit value to 32-bit value.
inline uint32_t to_uint32(evmc_uint256be value)
{
return (uint32_t{value.bytes[28]} << 24) | (uint32_t{value.bytes[29]} << 16) |
(uint32_t{value.bytes[30]} << 8) | (uint32_t{value.bytes[31]});
}
/// Truncates 256-bit value to 160-bit address.
inline evmc_address to_address(evmc_uint256be value)
{
evmc_address address = {};
size_t offset = sizeof(value) - sizeof(address);
std::memcpy(address.bytes, &value.bytes[offset], sizeof(address.bytes));
return address;
}
/// The example implementation of the evmc_vm::execute() method.
evmc_result execute(evmc_vm* instance,
const evmc_host_interface* host,
evmc_host_context* context,
enum evmc_revision rev,
const evmc_message* msg)
{
auto* vm = static_cast<ExampleVM*>(instance);
if (vm->verbose > 0)
std::puts("execution started\n");
int64_t gas_left = msg->gas;
Stack stack;
Memory memory;
for (size_t pc = 0; pc < msg->code_size; ++pc)
{
// Check remaining gas, assume each instruction costs 1.
gas_left -= 1;
if (gas_left < 0)
return evmc_make_result(EVMC_OUT_OF_GAS, 0, 0, nullptr, 0);
switch (msg->code[pc])
{
default:
return evmc_make_result(EVMC_UNDEFINED_INSTRUCTION, 0, 0, nullptr, 0);
case OP_STOP:
return evmc_make_result(EVMC_SUCCESS, gas_left, 0, nullptr, 0);
case OP_ADD:
{
uint32_t a = to_uint32(stack.pop());
uint32_t b = to_uint32(stack.pop());
uint32_t sum = a + b;
stack.push(to_uint256(sum));
break;
}
case OP_ADDRESS:
{
evmc_uint256be value = to_uint256(msg->recipient);
stack.push(value);
break;
}
case OP_CALLDATALOAD:
{
uint32_t offset = to_uint32(stack.pop());
evmc_uint256be value = {};
if (offset < msg->input_size)
{
size_t copy_size = std::min(msg->input_size - offset, sizeof(value));
std::memcpy(value.bytes, &msg->input_data[offset], copy_size);
}
stack.push(value);
break;
}
case OP_NUMBER:
{
evmc_uint256be value =
to_uint256(static_cast<uint32_t>(host->get_tx_context(context).block_number));
stack.push(value);
break;
}
case OP_MSTORE:
{
uint32_t index = to_uint32(stack.pop());
evmc_uint256be value = stack.pop();
if (!memory.store(index, value.bytes, sizeof(value)))
return evmc_make_result(EVMC_FAILURE, 0, 0, nullptr, 0);
break;
}
case OP_SLOAD:
{
evmc_uint256be index = stack.pop();
evmc_uint256be value = host->get_storage(context, &msg->recipient, &index);
stack.push(value);
break;
}
case OP_SSTORE:
{
evmc_uint256be index = stack.pop();
evmc_uint256be value = stack.pop();
host->set_storage(context, &msg->recipient, &index, &value);
break;
}
case OP_MSIZE:
{
evmc_uint256be value = to_uint256(memory.size);
stack.push(value);
break;
}
case OP_PUSH1:
case OP_PUSH2:
case OP_PUSH3:
case OP_PUSH4:
case OP_PUSH5:
case OP_PUSH6:
case OP_PUSH7:
case OP_PUSH8:
case OP_PUSH9:
case OP_PUSH10:
case OP_PUSH11:
case OP_PUSH12:
case OP_PUSH13:
case OP_PUSH14:
case OP_PUSH15:
case OP_PUSH16:
case OP_PUSH17:
case OP_PUSH18:
case OP_PUSH19:
case OP_PUSH20:
case OP_PUSH21:
case OP_PUSH22:
case OP_PUSH23:
case OP_PUSH24:
case OP_PUSH25:
case OP_PUSH26:
case OP_PUSH27:
case OP_PUSH28:
case OP_PUSH29:
case OP_PUSH30:
case OP_PUSH31:
case OP_PUSH32:
{
evmc_uint256be value = {};
size_t num_push_bytes = size_t{msg->code[pc]} - OP_PUSH1 + 1;
size_t offset = sizeof(value) - num_push_bytes;
std::memcpy(&value.bytes[offset], &msg->code[pc + 1], num_push_bytes);
pc += num_push_bytes;
stack.push(value);
break;
}
case OP_DUP1:
{
evmc_uint256be value = stack.pop();
stack.push(value);
stack.push(value);
break;
}
case OP_CALL:
{
evmc_message call_msg = {};
call_msg.gas = to_uint32(stack.pop());
call_msg.recipient = to_address(stack.pop());
call_msg.value = stack.pop();
uint32_t call_input_offset = to_uint32(stack.pop());
uint32_t call_input_size = to_uint32(stack.pop());
call_msg.input_data = memory.expand(call_input_offset, call_input_size);
call_msg.input_size = call_input_size;
uint32_t call_output_offset = to_uint32(stack.pop());
uint32_t call_output_size = to_uint32(stack.pop());
uint8_t* call_output_ptr = memory.expand(call_output_offset, call_output_size);
if (call_msg.input_data == nullptr || call_output_ptr == nullptr)
return evmc_make_result(EVMC_FAILURE, 0, 0, nullptr, 0);
evmc_result call_result = host->call(context, &call_msg);
evmc_uint256be value = to_uint256(call_result.status_code == EVMC_SUCCESS ? 1 : 0);
stack.push(value);
if (call_output_size > call_result.output_size)
call_output_size = static_cast<uint32_t>(call_result.output_size);
memory.store(call_output_offset, call_result.output_data, call_output_size);
if (call_result.release != nullptr)
call_result.release(&call_result);
break;
}
case OP_RETURN:
{
uint32_t output_offset = to_uint32(stack.pop());
uint32_t output_size = to_uint32(stack.pop());
uint8_t* output_ptr = memory.expand(output_offset, output_size);
if (output_ptr == nullptr)
return evmc_make_result(EVMC_FAILURE, 0, 0, nullptr, 0);
return evmc_make_result(EVMC_SUCCESS, gas_left, 0, output_ptr, output_size);
}
case OP_REVERT:
{
if (rev < EVMC_BYZANTIUM)
return evmc_make_result(EVMC_UNDEFINED_INSTRUCTION, 0, 0, nullptr, 0);
uint32_t output_offset = to_uint32(stack.pop());
uint32_t output_size = to_uint32(stack.pop());
uint8_t* output_ptr = memory.expand(output_offset, output_size);
if (output_ptr == nullptr)
return evmc_make_result(EVMC_FAILURE, 0, 0, nullptr, 0);
return evmc_make_result(EVMC_REVERT, gas_left, 0, output_ptr, output_size);
}
}
}
return evmc_make_result(EVMC_SUCCESS, gas_left, 0, nullptr, 0);
}
/// @cond internal
#if !defined(PROJECT_VERSION)
/// The dummy project version if not provided by the build system.
#define PROJECT_VERSION "0.0.0"
#endif
/// @endcond
ExampleVM::ExampleVM()
: evmc_vm{EVMC_ABI_VERSION, "example_vm", PROJECT_VERSION, ::destroy,
::execute, ::get_capabilities, ::set_option}
{}
} // namespace
extern "C" evmc_vm* evmc_create_example_vm()
{
return new ExampleVM;
}