-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathhera.cpp
More file actions
567 lines (478 loc) · 16.5 KB
/
hera.cpp
File metadata and controls
567 lines (478 loc) · 16.5 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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
* Copyright 2016-2018 Alex Beregszaszi et al.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <hera/hera.h>
#include <limits>
#include <cstring>
#include <unistd.h>
#include <iostream>
#include <map>
#include <memory>
#include <evmc/evmc.h>
#include <evmc/helpers.hpp>
#include "debugging.h"
#include "eei.h"
#include "exceptions.h"
#include "helpers.h"
#if HERA_BINARYEN
#include "binaryen.h"
#endif
#if HERA_WAVM
#include "wavm.h"
#endif
#if HERA_WABT
#include "wabt.h"
#endif
#include <hera/buildinfo.h>
using namespace std;
using namespace hera;
namespace {
enum class hera_evm1mode {
reject,
fallback,
evm2wasm_contract,
runevm_contract,
};
const map<string, hera_evm1mode> evm1mode_options {
{ "reject", hera_evm1mode::reject },
{ "fallback", hera_evm1mode::fallback },
{ "evm2wasm", hera_evm1mode::evm2wasm_contract },
{ "runevm", hera_evm1mode::runevm_contract },
};
using WasmEngineCreateFn = unique_ptr<WasmEngine>(*)();
const map<string, WasmEngineCreateFn> wasm_engine_map {
#if HERA_BINARYEN
{ "binaryen", BinaryenEngine::create },
#endif
#if HERA_WAVM
{ "wavm", WavmEngine::create },
#endif
#if HERA_WABT
{ "wabt", WabtEngine::create },
#endif
};
WasmEngineCreateFn wasmEngineCreateFn =
// This is the order of preference.
#if HERA_BINARYEN
BinaryenEngine::create
#elif HERA_WABT
WabtEngine::create
#elif HERA_WAVM
WavmEngine::create
#else
#error "No engine requested."
#endif
;
struct hera_instance : evmc_instance {
unique_ptr<WasmEngine> engine = wasmEngineCreateFn();
hera_evm1mode evm1mode = hera_evm1mode::reject;
bool metering = false;
map<evmc_address, bytes> contract_preload_list;
// hera_instance() noexcept : evmc_instance({EVMC_ABI_VERSION, "hera", hera_get_buildinfo()->project_version, nullptr, nullptr, nullptr, nullptr, nullptr}) {}
// fix error with geth+evmc: /tmp/go-build940146972/b001/runtime.test: symbol lookup error: /tmp/go-build940146972/b001/runtime.test: undefined symbol: hera_get_buildinfo
hera_instance() noexcept : evmc_instance({EVMC_ABI_VERSION, "hera", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr}) {}
};
const evmc_address sentinelAddress = { .bytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xa } };
const evmc_address evm2wasmAddress = { .bytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xb } };
const evmc_address runevmAddress = { .bytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xc } };
// Calls a system contract at @address with input data @input.
// It is a "staticcall" with sender 000...000 and no value.
// @returns output data from the contract and update the @gas variable with the gas left.
pair<evmc_status_code, bytes> callSystemContract(
evmc_context* context,
evmc_address const& address,
int64_t & gas,
bytes_view input
) {
evmc_message message = {
.kind = EVMC_CALL,
.flags = EVMC_STATIC,
.depth = 0,
.gas = gas,
.destination = address,
.sender = {},
.input_data = input.data(),
.input_size = input.size(),
.value = {},
.create2_salt = {},
};
evmc_result result = context->host->call(context, &message);
bytes ret;
if (result.status_code == EVMC_SUCCESS && result.output_data)
ret.assign(result.output_data, result.output_data + result.output_size);
gas = result.gas_left;
if (result.release)
result.release(&result);
return {result.status_code, ret};
}
pair<evmc_status_code, bytes> locallyExecuteSystemContract(
evmc_context* context,
evmc_address const& address,
int64_t & gas,
bytes_view input,
bytes_view code,
bytes_view state_code
) {
const evmc_message message = {
.kind = EVMC_CALL,
.flags = EVMC_STATIC,
.depth = 0,
.gas = gas,
.destination = address,
.sender = {},
.input_data = input.data(),
.input_size = input.size(),
.value = {},
.create2_salt = {},
};
unique_ptr<WasmEngine> engine = wasmEngineCreateFn();
// TODO: should we catch exceptions here?
ExecutionResult result = engine->execute(context, code, state_code, message, false);
bytes ret;
evmc_status_code status = result.isRevert ? EVMC_REVERT : EVMC_SUCCESS;
if (status == EVMC_SUCCESS && result.returnValue.size() > 0)
ret = move(result.returnValue);
return {status, move(ret)};
}
// Calls the Sentinel contract with input data @input.
// @returns the validated and metered output or empty output otherwise.
bytes sentinel(evmc_context* context, bytes_view input)
{
HERA_DEBUG << "Metering (input " << input.size() << " bytes)...\n";
int64_t startgas = numeric_limits<int64_t>::max(); // do not charge for metering yet (give unlimited gas)
int64_t gas = startgas;
evmc_status_code status;
bytes ret;
tie(status, ret) = callSystemContract(
context,
sentinelAddress,
gas,
input
);
HERA_DEBUG << "Metering done (output " << ret.size() << " bytes, used " << (startgas - gas) << " gas) with code=" << status << "\n";
ensureCondition(
status == EVMC_SUCCESS,
ContractValidationFailure,
"Sentinel has failed on contract. It is invalid."
);
return ret;
}
// Calls the evm2wasm contract with input data @input.
// @returns the compiled output or empty output otherwise.
bytes evm2wasm(evmc_context* context, bytes_view input) {
HERA_DEBUG << "Calling evm2wasm (input " << input.size() << " bytes)...\n";
int64_t startgas = numeric_limits<int64_t>::max(); // do not charge for metering yet (give unlimited gas)
int64_t gas = startgas;
evmc_status_code status;
bytes ret;
tie(status, ret) = callSystemContract(
context,
evm2wasmAddress,
gas,
input
);
HERA_DEBUG << "evm2wasm done (output " << ret.size() << " bytes, used " << (startgas - gas) << " gas) with status=" << status << "\n";
ensureCondition(
status == EVMC_SUCCESS,
ContractValidationFailure,
"evm2wasm has failed."
);
return ret;
}
// Calls the runevm contract.
// @returns a wasm-based evm interpreter.
bytes runevm(evmc_context* context, bytes code) {
HERA_DEBUG << "Calling runevm (code " << code.size() << " bytes)...\n";
int64_t gas = numeric_limits<int64_t>::max(); // do not charge for metering yet (give unlimited gas)
evmc_status_code status;
bytes ret;
tie(status, ret) = locallyExecuteSystemContract(
context,
runevmAddress,
gas,
{},
code,
code
);
HERA_DEBUG << "runevm done (output " << ret.size() << " bytes) with status=" << status << "\n";
ensureCondition(
status == EVMC_SUCCESS,
ContractValidationFailure,
"runevm has failed."
);
ensureCondition(
ret.size() > 0,
ContractValidationFailure,
"Runevm returned empty."
);
ensureCondition(
hasWasmPreamble(ret),
ContractValidationFailure,
"Runevm result has no wasm preamble."
);
return ret;
}
void hera_destroy_result(evmc_result const* result) noexcept
{
delete[] result->output_data;
}
evmc_result hera_execute(
evmc_instance *instance,
evmc_context *context,
enum evmc_revision rev,
const evmc_message *msg,
const uint8_t *code,
size_t code_size
) noexcept {
hera_instance* hera = static_cast<hera_instance*>(instance);
HERA_DEBUG << "Executing message in Hera\n";
evmc_result ret;
memset(&ret, 0, sizeof(evmc_result));
try {
heraAssert(rev == EVMC_BYZANTIUM, "Only Byzantium supported.");
heraAssert(msg->gas >= 0, "EVMC supplied negative startgas");
bool meterInterfaceGas = true;
// the bytecode residing in the state - this will be used by interface methods (i.e. codecopy)
bytes_view state_code{code, code_size};
// the actual executable code - this can be modified (metered or evm2wasm compiled)
bytes run_code{state_code};
// replace executable code if replacement is supplied
auto preload = hera->contract_preload_list.find(msg->destination);
if (preload != hera->contract_preload_list.end()) {
HERA_DEBUG << "Overriding contract.\n";
run_code = preload->second;
}
// ensure we can only handle WebAssembly version 1
bool isWasm = hasWasmPreamble(run_code);
if (!isWasm) {
switch (hera->evm1mode) {
case hera_evm1mode::evm2wasm_contract:
run_code = evm2wasm(context, run_code);
ensureCondition(run_code.size() > 8, ContractValidationFailure, "Transcompiling via evm2wasm failed");
// TODO: enable this once evm2wasm does metering of interfaces
// meterInterfaceGas = false;
break;
case hera_evm1mode::fallback:
HERA_DEBUG << "Non-WebAssembly input, but fallback mode enabled, asking client to deal with it.\n";
ret.status_code = EVMC_REJECTED;
return ret;
case hera_evm1mode::reject:
HERA_DEBUG << "Non-WebAssembly input, failure.\n";
ret.status_code = EVMC_FAILURE;
return ret;
case hera_evm1mode::runevm_contract:
run_code = runevm(context, hera->contract_preload_list[runevmAddress]);
ensureCondition(run_code.size() > 8, ContractValidationFailure, "Interpreting via runevm failed");
// Runevm does interface metering on its own
meterInterfaceGas = false;
break;
}
}
ensureCondition(
hasWasmVersion(run_code, 1),
ContractValidationFailure,
"Contract has an invalid WebAssembly version."
);
// Avoid this in case of evm2wasm translated code
if (msg->kind == EVMC_CREATE && isWasm) {
// Meter the deployment (constructor) code if it is WebAssembly
if (hera->metering)
run_code = sentinel(context, run_code);
ensureCondition(
hasWasmPreamble(run_code) && hasWasmVersion(run_code, 1),
ContractValidationFailure,
"Invalid contract or metering failed."
);
}
heraAssert(hera->engine, "Wasm engine not set.");
WasmEngine& engine = *hera->engine;
ExecutionResult result = engine.execute(context, run_code, state_code, *msg, meterInterfaceGas);
heraAssert(result.gasLeft >= 0, "Negative gas left after execution.");
// copy call result
if (result.returnValue.size() > 0) {
bytes returnValue;
if (msg->kind == EVMC_CREATE && !result.isRevert && hasWasmPreamble(result.returnValue)) {
ensureCondition(
hasWasmVersion(result.returnValue, 1),
ContractValidationFailure,
"Contract has an invalid WebAssembly version."
);
// Meter the deployed code if it is WebAssembly
returnValue = hera->metering ? sentinel(context, result.returnValue) : move(result.returnValue);
ensureCondition(
hasWasmPreamble(returnValue) && hasWasmVersion(returnValue, 1),
ContractValidationFailure,
"Invalid contract or metering failed."
);
// FIXME: this should be done by the sentinel
engine.verifyContract({returnValue.data(), returnValue.size()});
} else {
returnValue = move(result.returnValue);
}
uint8_t* output_data = new uint8_t[returnValue.size()];
copy(returnValue.begin(), returnValue.end(), output_data);
ret.output_size = returnValue.size();
ret.output_data = output_data;
ret.release = hera_destroy_result;
}
ret.status_code = result.isRevert ? EVMC_REVERT : EVMC_SUCCESS;
ret.gas_left = result.gasLeft;
} catch (EndExecution const&) {
ret.status_code = EVMC_INTERNAL_ERROR;
HERA_DEBUG << "EndExecution exception has leaked through.\n";
} catch (VMTrap const& e) {
// TODO: use specific error code? EVMC_INVALID_INSTRUCTION or EVMC_TRAP_INSTRUCTION?
ret.status_code = EVMC_FAILURE;
HERA_DEBUG << e.what() << "\n";
} catch (ArgumentOutOfRange const& e) {
ret.status_code = EVMC_ARGUMENT_OUT_OF_RANGE;
HERA_DEBUG << e.what() << "\n";
} catch (OutOfGas const& e) {
ret.status_code = EVMC_OUT_OF_GAS;
HERA_DEBUG << e.what() << "\n";
} catch (ContractValidationFailure const& e) {
ret.status_code = EVMC_CONTRACT_VALIDATION_FAILURE;
HERA_DEBUG << e.what() << "\n";
} catch (InvalidMemoryAccess const& e) {
ret.status_code = EVMC_INVALID_MEMORY_ACCESS;
HERA_DEBUG << e.what() << "\n";
} catch (StaticModeViolation const& e) {
ret.status_code = EVMC_STATIC_MODE_VIOLATION;
HERA_DEBUG << e.what() << "\n";
} catch (InternalErrorException const& e) {
ret.status_code = EVMC_INTERNAL_ERROR;
HERA_DEBUG << "InternalError: " << e.what() << "\n";
} catch (exception const& e) {
ret.status_code = EVMC_INTERNAL_ERROR;
HERA_DEBUG << "Unknown exception: " << e.what() << "\n";
} catch (...) {
ret.status_code = EVMC_INTERNAL_ERROR;
HERA_DEBUG << "Totally unknown exception\n";
}
return ret;
}
bool hera_parse_sys_option(hera_instance *hera, string const& _name, string const& value)
{
heraAssert(_name.find("sys:") == 0, "");
string name = _name.substr(4, string::npos);
evmc_address address{};
if (name.find("0x") == 0) {
// hex address
bytes ret = parseHexString(name.substr(2, string::npos));
if (ret.empty()) {
HERA_DEBUG << "Failed to parse hex address: " << name << "\n";
return false;
}
if (ret.size() != 20) {
HERA_DEBUG << "Invalid address: " << name << "\n";
return false;
}
copy(ret.begin(), ret.end(), address.bytes);
} else {
// alias
const map<string, evmc_address> aliases = {
{ string("sentinel"), sentinelAddress },
{ string("evm2wasm"), evm2wasmAddress },
{ string("runevm"), runevmAddress },
};
if (aliases.count(name) == 0) {
HERA_DEBUG << "Failed to resolve system contract alias: " << name << "\n";
return false;
}
address = aliases.at(name);
}
bytes contents = loadFileContents(value);
if (contents.size() == 0) {
HERA_DEBUG << "Failed to load contract source (or empty): " << value << "\n";
return false;
}
HERA_DEBUG << "Loaded contract for " << name << " from " << value << " (" << contents.size() << " bytes)\n";
hera->contract_preload_list[address] = move(contents);
return true;
}
evmc_set_option_result hera_set_option(
evmc_instance *instance,
char const *name,
char const *value
) noexcept {
hera_instance* hera = static_cast<hera_instance*>(instance);
if (strcmp(name, "evm1mode") == 0) {
if (evm1mode_options.count(value)) {
hera->evm1mode = evm1mode_options.at(value);
return EVMC_SET_OPTION_SUCCESS;
}
return EVMC_SET_OPTION_INVALID_VALUE;
}
if (strcmp(name, "metering") == 0) {
if (strcmp(value, "true") == 0)
hera->metering = true;
else if (strcmp(value, "false") != 0)
return EVMC_SET_OPTION_INVALID_VALUE;
return EVMC_SET_OPTION_SUCCESS;
}
if (strcmp(name, "benchmark") == 0) {
if (strcmp(value, "true") == 0) {
WasmEngine::enableBenchmarking();
return EVMC_SET_OPTION_SUCCESS;
}
return EVMC_SET_OPTION_INVALID_VALUE;
}
if (strcmp(name, "engine") == 0) {
auto it = wasm_engine_map.find(value);
if (it != wasm_engine_map.end()) {
wasmEngineCreateFn = it->second;
hera->engine = wasmEngineCreateFn();
return EVMC_SET_OPTION_SUCCESS;
}
return EVMC_SET_OPTION_INVALID_VALUE;
}
if (strncmp(name, "sys:", 4) == 0) {
if (hera_parse_sys_option(hera, string(name), string(value)))
return EVMC_SET_OPTION_SUCCESS;
return EVMC_SET_OPTION_INVALID_VALUE;
}
return EVMC_SET_OPTION_INVALID_NAME;
}
void hera_destroy(evmc_instance* instance) noexcept
{
hera_instance* hera = static_cast<hera_instance*>(instance);
delete hera;
}
evmc_capabilities_flagset hera_get_capabilities(evmc_instance* instance)
{
evmc_capabilities_flagset caps = EVMC_CAPABILITY_EWASM;
if (static_cast<hera_instance*>(instance)->evm1mode != hera_evm1mode::reject)
caps |= EVMC_CAPABILITY_EVM1;
return caps;
}
} // anonymous namespace
extern "C" {
evmc_instance* evmc_create_hera() noexcept
{
hera_instance* instance = new hera_instance;
instance->destroy = hera_destroy;
instance->execute = hera_execute;
instance->get_capabilities = hera_get_capabilities;
instance->set_option = hera_set_option;
return instance;
}
#if hera_EXPORTS
// If compiled as shared library, also export this symbol.
EVMC_EXPORT evmc_instance* evmc_create() noexcept
{
return evmc_create_hera();
}
#endif
}