-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy patheei.cpp
857 lines (660 loc) · 29.2 KB
/
eei.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
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/*
* 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 <array>
#include <fstream>
#include <iostream>
#include "debugging.h"
#include "eei.h"
#include "exceptions.h"
#include "helpers.h"
#include <evmc/instructions.h>
using namespace std;
namespace hera {
namespace
{
/* Checks if host supplied 256 bit value exceeds UINT128_MAX */
bool exceedsUint128(evmc::uint256be const& value) noexcept
{
for (unsigned i = 0; i < 16; i++)
{
if (value.bytes[i])
return true;
}
return false;
}
} // namespace
bool WasmEngine::benchmarkingEnabled = false;
void WasmEngine::collectBenchmarkingData()
{
// Convert duration to string with microsecond units.
constexpr auto to_us_str = [](clock::duration d) {
return std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(d).count());
};
const auto now = clock::now();
const auto instantiationDuration = executionStartTime - instantiationStartTime;
const auto executionDuration = now - executionStartTime;
const auto log = "Time [us]: " + to_us_str(instantiationDuration + executionDuration) +
" (instantiation: " + to_us_str(instantiationDuration) +
", execution: " + to_us_str(executionDuration) + ")\n";
std::cerr << log;
std::ofstream{"hera_benchmarks.log", std::ios::out | std::ios::app} << log;
}
#if HERA_DEBUGGING
void EthereumInterface::debugPrint32(uint32_t value)
{
cerr << depthToString() << " DEBUG print32: " << value << " " << hex << "0x" << value << dec << endl;
}
void EthereumInterface::debugPrint64(uint64_t value)
{
cerr << depthToString() << " DEBUG print64: " << value << " " << hex << "0x" << value << dec << endl;
}
void EthereumInterface::debugPrintMem(bool useHex, uint32_t offset, uint32_t length)
{
heraAssert((offset + length) > offset, "Overflow.");
heraAssert(memorySize() >= (offset + length), "Out of memory bounds.");
cerr << depthToString() << " DEBUG printMem" << (useHex ? "Hex(" : "(") << hex << "0x" << offset << ":0x" << length << "): " << dec;
if (useHex)
{
cerr << hex;
for (uint32_t i = offset; i < (offset + length); i++) {
cerr << static_cast<int>(memoryGet(i)) << " ";
}
cerr << dec;
}
else
{
for (uint32_t i = offset; i < (offset + length); i++) {
cerr << memoryGet(i) << " ";
}
}
cerr << endl;
}
void EthereumInterface::debugPrintStorage(bool useHex, uint32_t pathOffset)
{
evmc::uint256be path = loadBytes32(pathOffset);
HERA_DEBUG << depthToString() << " DEBUG printStorage" << (useHex ? "Hex" : "") << "(0x" << hex;
// Print out the path
for (uint8_t b: path.bytes)
cerr << static_cast<int>(b);
HERA_DEBUG << "): " << dec;
evmc::bytes32 result = m_host.get_storage(m_msg.recipient, path);
if (useHex)
{
cerr << hex;
for (uint8_t b: result.bytes)
cerr << static_cast<int>(b) << " ";
cerr << dec;
}
else
{
for (uint8_t b: result.bytes)
cerr << b << " ";
}
cerr << endl;
}
void EthereumInterface::debugEvmTrace(uint32_t pc, int32_t opcode, uint32_t cost, int32_t sp)
{
HERA_DEBUG << depthToString() << " evmTrace\n";
static constexpr int stackItemSize = sizeof(evmc::uint256be);
heraAssert(sp <= (1024 * stackItemSize), "EVM stack pointer out of bounds.");
heraAssert(opcode >= 0x00 && opcode <= 0xff, "Invalid EVM instruction.");
const char* const* const opNamesTable = evmc_get_instruction_names_table(EVMC_BYZANTIUM);
const char* opName = opNamesTable[static_cast<uint8_t>(opcode)];
if (opName == nullptr)
opName = "UNDEFINED";
cout << "{\"depth\":" << dec << m_msg.depth
<< ",\"gas\":" << m_result.gasLeft
<< ",\"gasCost\":" << cost
<< ",\"op\":" << opName
<< ",\"pc\":" << pc
<< ",\"stack\":[";
for (int32_t i = 0; i <= sp; i += stackItemSize) {
evmc::uint256be x = loadUint256(static_cast<uint32_t>(i));
cout << '"' << toHex(x) << '"';
if (i != sp)
cout << ',';
}
cout << "]}" << endl;
}
#endif
void EthereumInterface::eeiUseGas(int64_t gas)
{
HERA_DEBUG << depthToString() << " useGas " << gas << "\n";
ensureCondition(gas >= 0, ArgumentOutOfRange, "Negative gas supplied.");
takeGas(gas);
}
int64_t EthereumInterface::eeiGetGasLeft()
{
HERA_DEBUG << depthToString() << " getGasLeft\n";
static_assert(is_same<decltype(m_result.gasLeft), int64_t>::value, "int64_t type expected");
takeInterfaceGas(GasSchedule::base);
return m_result.gasLeft;
}
void EthereumInterface::eeiGetAddress(uint32_t resultOffset)
{
HERA_DEBUG << depthToString() << " getAddress " << hex << resultOffset << dec << "\n";
takeInterfaceGas(GasSchedule::base);
storeAddress(m_msg.recipient, resultOffset);
}
void EthereumInterface::eeiGetExternalBalance(uint32_t addressOffset, uint32_t resultOffset)
{
HERA_DEBUG << depthToString() << " getExternalBalance " << hex << addressOffset << " " << resultOffset << dec << "\n";
takeInterfaceGas(GasSchedule::balance);
evmc::address address = loadAddress(addressOffset);
evmc::uint256be balance = m_host.get_balance(address);
storeUint128(balance, resultOffset);
}
uint32_t EthereumInterface::eeiGetBlockHash(uint64_t number, uint32_t resultOffset)
{
HERA_DEBUG << depthToString() << " getBlockHash " << hex << number << " " << resultOffset << dec << "\n";
takeInterfaceGas(GasSchedule::blockhash);
const auto blockhash = m_host.get_block_hash(static_cast<int64_t>(number));
if (is_zero(blockhash))
return 1;
storeBytes32(blockhash, resultOffset);
return 0;
}
uint32_t EthereumInterface::eeiGetCallDataSize()
{
HERA_DEBUG << depthToString() << " getCallDataSize\n";
takeInterfaceGas(GasSchedule::base);
return static_cast<uint32_t>(m_msg.input_size);
}
void EthereumInterface::eeiCallDataCopy(uint32_t resultOffset, uint32_t dataOffset, uint32_t length)
{
HERA_DEBUG << depthToString() << " callDataCopy " << hex << resultOffset << " " << dataOffset << " " << length << dec << "\n";
safeChargeDataCopy(length, GasSchedule::verylow);
storeMemory({m_msg.input_data, m_msg.input_size}, dataOffset, resultOffset, length);
}
void EthereumInterface::eeiGetCaller(uint32_t resultOffset)
{
HERA_DEBUG << depthToString() << " getCaller " << hex << resultOffset << dec << "\n";
takeInterfaceGas(GasSchedule::base);
storeAddress(m_msg.sender, resultOffset);
}
void EthereumInterface::eeiGetCallValue(uint32_t resultOffset)
{
HERA_DEBUG << depthToString() << " getCallValue " << hex << resultOffset << dec << "\n";
takeInterfaceGas(GasSchedule::base);
storeUint128(m_msg.value, resultOffset);
}
void EthereumInterface::eeiCodeCopy(uint32_t resultOffset, uint32_t codeOffset, uint32_t length)
{
HERA_DEBUG << depthToString() << " codeCopy " << hex << resultOffset << " " << codeOffset << " " << length << dec << "\n";
safeChargeDataCopy(length, GasSchedule::verylow);
storeMemory(m_code, codeOffset, resultOffset, length);
}
uint32_t EthereumInterface::eeiGetCodeSize()
{
HERA_DEBUG << depthToString() << " getCodeSize\n";
takeInterfaceGas(GasSchedule::base);
return static_cast<uint32_t>(m_code.size());
}
void EthereumInterface::eeiExternalCodeCopy(uint32_t addressOffset, uint32_t resultOffset, uint32_t codeOffset, uint32_t length)
{
HERA_DEBUG << depthToString() << " externalCodeCopy " << hex << addressOffset << " " << resultOffset << " " << codeOffset << " " << length << dec << "\n";
safeChargeDataCopy(length, GasSchedule::extcode);
evmc::address address = loadAddress(addressOffset);
// TODO: optimise this so no copy needs to be created
bytes codeBuffer(length, '\0');
size_t numCopied = m_host.copy_code(address, codeOffset, codeBuffer.data(), codeBuffer.size());
ensureCondition(numCopied == length, InvalidMemoryAccess, "Out of bounds (source) memory copy");
storeMemory(codeBuffer, 0, resultOffset, length);
}
uint32_t EthereumInterface::eeiGetExternalCodeSize(uint32_t addressOffset)
{
HERA_DEBUG << depthToString() << " getExternalCodeSize " << hex << addressOffset << dec << "\n";
takeInterfaceGas(GasSchedule::extcode);
evmc::address address = loadAddress(addressOffset);
size_t code_size = m_host.get_code_size(address);
return static_cast<uint32_t>(code_size);
}
void EthereumInterface::eeiGetBlockCoinbase(uint32_t resultOffset)
{
HERA_DEBUG << depthToString() << " getBlockCoinbase " << hex << resultOffset << dec << "\n";
takeInterfaceGas(GasSchedule::base);
storeAddress(m_host.get_tx_context().block_coinbase, resultOffset);
}
void EthereumInterface::eeiGetBlockDifficulty(uint32_t offset)
{
HERA_DEBUG << depthToString() << " getBlockDifficulty " << hex << offset << dec << "\n";
takeInterfaceGas(GasSchedule::base);
storeUint256(m_host.get_tx_context().block_prev_randao, offset);
}
int64_t EthereumInterface::eeiGetBlockGasLimit()
{
HERA_DEBUG << depthToString() << " getBlockGasLimit\n";
takeInterfaceGas(GasSchedule::base);
static_assert(is_same<decltype(m_host.get_tx_context().block_gas_limit), int64_t>::value, "int64_t type expected");
return m_host.get_tx_context().block_gas_limit;
}
void EthereumInterface::eeiGetTxGasPrice(uint32_t valueOffset)
{
HERA_DEBUG << depthToString() << " getTxGasPrice " << hex << valueOffset << dec << "\n";
takeInterfaceGas(GasSchedule::base);
storeUint128(m_host.get_tx_context().tx_gas_price, valueOffset);
}
void EthereumInterface::eeiLog(uint32_t dataOffset, uint32_t length, uint32_t numberOfTopics, uint32_t topic1, uint32_t topic2, uint32_t topic3, uint32_t topic4)
{
HERA_DEBUG << depthToString() << " log " << hex << dataOffset << " " << length << " " << numberOfTopics << dec << "\n";
static_assert(GasSchedule::log <= 65536, "Gas cost of log could lead to overflow");
static_assert(GasSchedule::logTopic <= 65536, "Gas cost of logTopic could lead to overflow");
static_assert(GasSchedule::logData <= 65536, "Gas cost of logData could lead to overflow");
// Using uint64_t to force a type issue if the underlying API changes.
takeInterfaceGas(GasSchedule::log + (GasSchedule::logTopic * numberOfTopics) + (GasSchedule::logData * int64_t(length)));
ensureCondition(!(m_msg.flags & EVMC_STATIC), StaticModeViolation, "log");
ensureCondition(numberOfTopics <= 4, ContractValidationFailure, "Too many topics specified");
// TODO: should this assert that unused topic offsets must be 0?
array<evmc::uint256be, 4> topics;
topics[0] = (numberOfTopics >= 1) ? loadBytes32(topic1) : evmc::uint256be{};
topics[1] = (numberOfTopics >= 2) ? loadBytes32(topic2) : evmc::uint256be{};
topics[2] = (numberOfTopics >= 3) ? loadBytes32(topic3) : evmc::uint256be{};
topics[3] = (numberOfTopics == 4) ? loadBytes32(topic4) : evmc::uint256be{};
ensureSourceMemoryBounds(dataOffset, length);
bytes data(length, '\0');
loadMemory(dataOffset, data, length);
m_host.emit_log(m_msg.recipient, data.data(), length, topics.data(), numberOfTopics);
}
int64_t EthereumInterface::eeiGetBlockNumber()
{
HERA_DEBUG << depthToString() << " getBlockNumber\n";
takeInterfaceGas(GasSchedule::base);
static_assert(is_same<decltype(m_host.get_tx_context().block_number), int64_t>::value, "int64_t type expected");
return m_host.get_tx_context().block_number;
}
int64_t EthereumInterface::eeiGetBlockTimestamp()
{
HERA_DEBUG << depthToString() << " getBlockTimestamp\n";
takeInterfaceGas(GasSchedule::base);
static_assert(is_same<decltype(m_host.get_tx_context().block_timestamp), int64_t>::value, "int64_t type expected");
return m_host.get_tx_context().block_timestamp;
}
void EthereumInterface::eeiGetTxOrigin(uint32_t resultOffset)
{
HERA_DEBUG << depthToString() << " getTxOrigin " << hex << resultOffset << dec << "\n";
takeInterfaceGas(GasSchedule::base);
storeAddress(m_host.get_tx_context().tx_origin, resultOffset);
}
void EthereumInterface::eeiStorageStore(uint32_t pathOffset, uint32_t valueOffset)
{
HERA_DEBUG << depthToString() << " storageStore " << hex << pathOffset << " " << valueOffset << dec << "\n";
static_assert(
GasSchedule::storageStoreCreate >= GasSchedule::storageStoreChange,
"storageStoreChange costs more than storageStoreCreate"
);
// Charge this here as it is the minimum cost.
takeInterfaceGas(GasSchedule::storageStoreChange);
ensureCondition(!(m_msg.flags & EVMC_STATIC), StaticModeViolation, "storageStore");
const auto path = loadBytes32(pathOffset);
const auto value = loadBytes32(valueOffset);
const auto current = m_host.get_storage(m_msg.recipient, path);
// Charge the right amount in case of the create case.
if (is_zero(current) && !is_zero(value))
takeInterfaceGas(GasSchedule::storageStoreCreate - GasSchedule::storageStoreChange);
// We do not need to take care about the delete case (gas refund), the client does it.
m_host.set_storage(m_msg.recipient, path, value);
}
void EthereumInterface::eeiStorageLoad(uint32_t pathOffset, uint32_t resultOffset)
{
HERA_DEBUG << depthToString() << " storageLoad " << hex << pathOffset << " " << resultOffset << dec << "\n";
takeInterfaceGas(GasSchedule::storageLoad);
evmc::bytes32 path = loadBytes32(pathOffset);
evmc::bytes32 result = m_host.get_storage(m_msg.recipient, path);
storeBytes32(result, resultOffset);
}
void EthereumInterface::eeiRevertOrFinish(bool revert, uint32_t offset, uint32_t size)
{
HERA_DEBUG << depthToString() << " " << (revert ? "revert " : "finish ") << hex << offset << " " << size << dec << "\n";
ensureSourceMemoryBounds(offset, size);
m_result.returnValue = bytes(size, '\0');
loadMemory(offset, m_result.returnValue, size);
m_result.isRevert = revert;
throw EndExecution{};
}
uint32_t EthereumInterface::eeiGetReturnDataSize()
{
HERA_DEBUG << depthToString() << " getReturnDataSize\n";
takeInterfaceGas(GasSchedule::base);
return static_cast<uint32_t>(m_lastReturnData.size());
}
void EthereumInterface::eeiReturnDataCopy(uint32_t dataOffset, uint32_t offset, uint32_t size)
{
HERA_DEBUG << depthToString() << " returnDataCopy " << hex << dataOffset << " " << offset << " " << size << dec << "\n";
safeChargeDataCopy(size, GasSchedule::verylow);
storeMemory(m_lastReturnData, offset, dataOffset, size);
}
uint32_t EthereumInterface::eeiCall(EEICallKind kind, int64_t gas, uint32_t addressOffset, uint32_t valueOffset, uint32_t dataOffset, uint32_t dataLength)
{
ensureCondition(gas >= 0, ArgumentOutOfRange, "Negative gas supplied.");
evmc_message call_message;
call_message.recipient = loadAddress(addressOffset);
call_message.flags = m_msg.flags & EVMC_STATIC;
call_message.depth = m_msg.depth + 1;
switch (kind) {
case EEICallKind::Call:
case EEICallKind::CallCode:
call_message.kind = (kind == EEICallKind::CallCode) ? EVMC_CALLCODE : EVMC_CALL;
call_message.sender = m_msg.recipient;
call_message.value = loadUint128(valueOffset);
if ((kind == EEICallKind::Call) && !evmc::is_zero(call_message.value)) {
ensureCondition(!(m_msg.flags & EVMC_STATIC), StaticModeViolation, "call");
}
break;
case EEICallKind::CallDelegate:
call_message.kind = EVMC_DELEGATECALL;
call_message.sender = m_msg.sender;
call_message.value = m_msg.value;
break;
case EEICallKind::CallStatic:
call_message.kind = EVMC_CALL;
call_message.flags |= EVMC_STATIC;
call_message.sender = m_msg.recipient;
call_message.value = {};
break;
}
#if HERA_DEBUGGING
string methodName;
switch (kind) {
case EEICallKind::Call: methodName = "call"; break;
case EEICallKind::CallCode: methodName = "callCode"; break;
case EEICallKind::CallDelegate: methodName = "callDelegate"; break;
case EEICallKind::CallStatic: methodName = "callStatic"; break;
}
HERA_DEBUG <<
depthToString() << " " <<
methodName << " " << hex <<
gas << " " <<
addressOffset << " " <<
valueOffset << " " <<
dataOffset << " " <<
dataLength << dec << "\n";
#endif
// NOTE: this must be declared outside the condition to ensure the memory doesn't go out of scope
bytes input_data;
if (dataLength) {
ensureSourceMemoryBounds(dataOffset, dataLength);
input_data.resize(dataLength);
loadMemory(dataOffset, input_data, dataLength);
call_message.input_data = input_data.data();
call_message.input_size = dataLength;
} else {
call_message.input_data = nullptr;
call_message.input_size = 0;
}
// Start with base call gas
takeInterfaceGas(GasSchedule::call);
if (m_msg.depth >= 1024)
return 1;
// These checks are in EIP150 but not in the YellowPaper
// Charge valuetransfer gas if value is being transferred.
if ((kind == EEICallKind::Call || kind == EEICallKind::CallCode) && !evmc::is_zero(call_message.value)) {
takeInterfaceGas(GasSchedule::valuetransfer);
if (!enoughSenderBalanceFor(call_message.value))
return 1;
// Only charge callNewAccount gas if the account is new and non-zero value is being transferred per EIP161.
if ((kind == EEICallKind::Call) && !m_host.account_exists(call_message.recipient))
takeInterfaceGas(GasSchedule::callNewAccount);
}
// This is the gas we are forwarding to the callee.
// Retain one 64th of it as per EIP150
gas = min(gas, maxCallGas(m_result.gasLeft));
takeInterfaceGas(gas);
// Add gas stipend for value transfers
if (!evmc::is_zero(call_message.value))
gas += GasSchedule::valueStipend;
call_message.gas = gas;
auto call_result = m_host.call(call_message);
if (call_result.output_data) {
m_lastReturnData.assign(call_result.output_data, call_result.output_data + call_result.output_size);
} else {
m_lastReturnData.clear();
}
/* Return unspent gas */
heraAssert(call_result.gas_left >= 0, "EVMC returned negative gas left");
m_result.gasLeft += call_result.gas_left;
switch (call_result.status_code) {
case EVMC_SUCCESS:
return 0;
case EVMC_REVERT:
return 2;
default:
return 1;
}
}
uint32_t EthereumInterface::eeiCreate(uint32_t valueOffset, uint32_t dataOffset, uint32_t length, uint32_t resultOffset)
{
HERA_DEBUG << depthToString() << " create " << hex << valueOffset << " " << dataOffset << " " << length << dec << " " << resultOffset << dec << "\n";
takeInterfaceGas(GasSchedule::create);
ensureCondition(!(m_msg.flags & EVMC_STATIC), StaticModeViolation, "create");
evmc_message create_message;
create_message.recipient = {};
create_message.sender = m_msg.recipient;
create_message.value = loadUint128(valueOffset);
if (m_msg.depth >= 1024)
return 1;
if (!enoughSenderBalanceFor(create_message.value))
return 1;
// NOTE: this must be declared outside the condition to ensure the memory doesn't go out of scope
bytes contract_code;
if (length) {
ensureSourceMemoryBounds(dataOffset, length);
contract_code.resize(length);
loadMemory(dataOffset, contract_code, length);
create_message.input_data = contract_code.data();
create_message.input_size = length;
} else {
create_message.input_data = nullptr;
create_message.input_size = 0;
}
create_message.depth = m_msg.depth + 1;
create_message.kind = EVMC_CREATE;
create_message.flags = 0;
int64_t gas = maxCallGas(m_result.gasLeft);
create_message.gas = gas;
takeInterfaceGas(gas);
auto create_result = m_host.call(create_message);
/* Return unspent gas */
heraAssert(create_result.gas_left >= 0, "EVMC returned negative gas left");
m_result.gasLeft += create_result.gas_left;
if (create_result.status_code == EVMC_SUCCESS) {
storeAddress(create_result.create_address, resultOffset);
m_lastReturnData.clear();
} else if (create_result.output_data) {
m_lastReturnData.assign(create_result.output_data, create_result.output_data + create_result.output_size);
} else {
m_lastReturnData.clear();
}
switch (create_result.status_code) {
case EVMC_SUCCESS:
return 0;
case EVMC_REVERT:
return 2;
default:
return 1;
}
}
void EthereumInterface::eeiSelfDestruct(uint32_t addressOffset)
{
HERA_DEBUG << depthToString() << " selfDestruct " << hex << addressOffset << dec << "\n";
takeInterfaceGas(GasSchedule::selfdestruct);
ensureCondition(!(m_msg.flags & EVMC_STATIC), StaticModeViolation, "selfDestruct");
evmc::address address = loadAddress(addressOffset);
if (!m_host.account_exists(address))
takeInterfaceGas(GasSchedule::callNewAccount);
m_host.selfdestruct(m_msg.recipient, address);
throw EndExecution{};
}
void EthereumInterface::takeGas(int64_t gas)
{
// NOTE: gas >= 0 is validated by the callers of this method
ensureCondition(gas <= m_result.gasLeft, OutOfGas, "Out of gas.");
m_result.gasLeft -= gas;
}
void EthereumInterface::takeInterfaceGas(int64_t gas)
{
if (!m_meterGas)
return;
heraAssert(gas >= 0, "Trying to take negative gas.");
takeGas(gas);
}
/*
* Memory Operations
*/
void EthereumInterface::ensureSourceMemoryBounds(uint32_t offset, uint32_t length) {
ensureCondition((offset + length) >= offset, InvalidMemoryAccess, "Out of bounds (source) memory copy.");
ensureCondition(memorySize() >= (offset + length), InvalidMemoryAccess, "Out of bounds (source) memory copy.");
}
void EthereumInterface::loadMemoryReverse(uint32_t srcOffset, uint8_t *dst, size_t length)
{
// NOTE: the source bound check is not needed as the caller already ensures it
ensureCondition((srcOffset + length) >= srcOffset, InvalidMemoryAccess, "Out of bounds (source) memory copy.");
ensureCondition(memorySize() >= (srcOffset + length), InvalidMemoryAccess, "Out of bounds (source) memory copy.");
if (!length)
HERA_DEBUG << "Zero-length memory load from offset 0x" << hex << srcOffset << dec << "\n";
for (uint32_t i = 0; i < length; ++i) {
dst[length - (i + 1)] = memoryGet(srcOffset + i);
}
}
void EthereumInterface::loadMemory(uint32_t srcOffset, uint8_t *dst, size_t length)
{
// NOTE: the source bound check is not needed as the caller already ensures it
ensureCondition((srcOffset + length) >= srcOffset, InvalidMemoryAccess, "Out of bounds (source) memory copy.");
ensureCondition(memorySize() >= (srcOffset + length), InvalidMemoryAccess, "Out of bounds (source) memory copy.");
if (!length)
HERA_DEBUG << "Zero-length memory load from offset 0x" << hex << srcOffset << dec << "\n";
for (uint32_t i = 0; i < length; ++i) {
dst[i] = memoryGet(srcOffset + i);
}
}
void EthereumInterface::loadMemory(uint32_t srcOffset, bytes& dst, size_t length)
{
// NOTE: the source bound check is not needed as the caller already ensures it
ensureCondition((srcOffset + length) >= srcOffset, InvalidMemoryAccess, "Out of bounds (source) memory copy.");
ensureCondition(memorySize() >= (srcOffset + length), InvalidMemoryAccess, "Out of bounds (source) memory copy.");
ensureCondition(dst.size() >= length, InvalidMemoryAccess, "Out of bounds (destination) memory copy.");
if (!length)
HERA_DEBUG << "Zero-length memory load from offset 0x" << hex << srcOffset << dec <<"\n";
for (uint32_t i = 0; i < length; ++i) {
dst[i] = memoryGet(srcOffset + i);
}
}
void EthereumInterface::storeMemoryReverse(const uint8_t *src, uint32_t dstOffset, uint32_t length)
{
ensureCondition((dstOffset + length) >= dstOffset, InvalidMemoryAccess, "Out of bounds (destination) memory copy.");
ensureCondition(memorySize() >= (dstOffset + length), InvalidMemoryAccess, "Out of bounds (destination) memory copy.");
if (!length)
HERA_DEBUG << "Zero-length memory store to offset 0x" << hex << dstOffset << dec << "\n";
for (uint32_t i = 0; i < length; ++i) {
memorySet(dstOffset + length - (i + 1), src[i]);
}
}
void EthereumInterface::storeMemory(const uint8_t *src, uint32_t dstOffset, uint32_t length)
{
ensureCondition((dstOffset + length) >= dstOffset, InvalidMemoryAccess, "Out of bounds (destination) memory copy.");
ensureCondition(memorySize() >= (dstOffset + length), InvalidMemoryAccess, "Out of bounds (destination) memory copy.");
if (!length)
HERA_DEBUG << "Zero-length memory store to offset 0x" << hex << dstOffset << dec << "\n";
for (uint32_t i = 0; i < length; ++i) {
memorySet(dstOffset + i, src[i]);
}
}
void EthereumInterface::storeMemory(bytes_view src, uint32_t srcOffset, uint32_t dstOffset, uint32_t length)
{
ensureCondition((srcOffset + length) >= srcOffset, InvalidMemoryAccess, "Out of bounds (source) memory copy.");
ensureCondition(src.size() >= (srcOffset + length), InvalidMemoryAccess, "Out of bounds (source) memory copy.");
ensureCondition((dstOffset + length) >= dstOffset, InvalidMemoryAccess, "Out of bounds (destination) memory copy.");
ensureCondition(memorySize() >= (dstOffset + length), InvalidMemoryAccess, "Out of bounds (destination) memory copy.");
if (!length)
HERA_DEBUG << "Zero-length memory store to offset 0x" << hex << dstOffset << dec << "\n";
for (uint32_t i = 0; i < length; i++) {
memorySet(dstOffset + i, src[srcOffset + i]);
}
}
/*
* Memory Op Wrapper Functions
*/
evmc::bytes32 EthereumInterface::loadBytes32(uint32_t srcOffset)
{
evmc::bytes32 dst;
loadMemory(srcOffset, dst.bytes, 32);
return dst;
}
void EthereumInterface::storeBytes32(evmc::uint256be const& src, uint32_t dstOffset)
{
storeMemory(src.bytes, dstOffset, 32);
}
evmc::uint256be EthereumInterface::loadUint256(uint32_t srcOffset)
{
evmc::uint256be dst;
loadMemoryReverse(srcOffset, dst.bytes, 32);
return dst;
}
void EthereumInterface::storeUint256(evmc::uint256be const& src, uint32_t dstOffset)
{
storeMemoryReverse(src.bytes, dstOffset, 32);
}
evmc::address EthereumInterface::loadAddress(uint32_t srcOffset)
{
evmc::address dst;
loadMemory(srcOffset, dst.bytes, 20);
return dst;
}
void EthereumInterface::storeAddress(evmc::address const& src, uint32_t dstOffset)
{
storeMemory(src.bytes, dstOffset, 20);
}
evmc::uint256be EthereumInterface::loadUint128(uint32_t srcOffset)
{
evmc::uint256be dst;
loadMemoryReverse(srcOffset, dst.bytes + 16, 16);
return dst;
}
void EthereumInterface::storeUint128(evmc::uint256be const& src, uint32_t dstOffset)
{
ensureCondition(!exceedsUint128(src), ArgumentOutOfRange, "Account balance (or transaction value) exceeds 128 bits.");
storeMemoryReverse(src.bytes + 16, dstOffset, 16);
}
/*
* Utilities
*/
void EthereumInterface::safeChargeDataCopy(uint32_t length, unsigned baseCost) {
takeInterfaceGas(baseCost);
// Since length here is 32 bits divided by 32 (aka shifted right by 5 bits), we
// can assume the upper bound for values is 27 bits.
//
// Since `gas` is 63 bits wide, that means we have an extra 36 bits of headroom.
//
// Allow 16 bits here.
static_assert(GasSchedule::copy <= 65536, "Gas cost of copy could lead to overflow");
// Using uint64_t to force a type issue if the underlying API changes.
takeInterfaceGas(GasSchedule::copy * ((int64_t(length) + 31) / 32));
}
bool EthereumInterface::enoughSenderBalanceFor(evmc::uint256be const& value)
{
evmc::uint256be balance = m_host.get_balance(m_msg.recipient);
return safeLoadUint128(balance) >= safeLoadUint128(value);
}
unsigned __int128 EthereumInterface::safeLoadUint128(evmc::uint256be const& value)
{
ensureCondition(!exceedsUint128(value), ArgumentOutOfRange, "Account balance (or transaction value) exceeds 128 bits.");
unsigned __int128 ret = 0;
for (unsigned i = 16; i < 32; i++) {
ret <<= 8;
ret |= value.bytes[i];
}
return ret;
}
}