forked from ethereum/aleth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExecutive.cpp
563 lines (495 loc) · 19.6 KB
/
Executive.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
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Executive.h"
#include "Block.h"
#include "BlockChain.h"
#include "ExtVM.h"
#include "Interface.h"
#include "State.h"
#include <libdevcore/CommonIO.h>
#include <libethcore/CommonJS.h>
#include <libevm/LegacyVM.h>
#include <libevm/VMFactory.h>
#include <json/json.h>
#include <boost/timer.hpp>
using namespace std;
using namespace dev;
using namespace dev::eth;
namespace
{
std::string dumpStackAndMemory(LegacyVM const& _vm)
{
ostringstream o;
o << "\n STACK\n";
for (auto i : _vm.stack())
o << (h256)i << "\n";
o << " MEMORY\n"
<< ((_vm.memory().size() > 1000) ? " mem size greater than 1000 bytes " :
memDump(_vm.memory()));
return o.str();
};
std::string dumpStorage(ExtVM const& _ext)
{
ostringstream o;
o << " STORAGE\n";
for (auto const& i : _ext.state().storage(_ext.myAddress))
o << showbase << hex << i.second.first << ": " << i.second.second << "\n";
return o.str();
};
} // namespace
StandardTrace::StandardTrace():
m_trace(Json::arrayValue)
{}
bool changesMemory(Instruction _inst)
{
return
_inst == Instruction::MSTORE ||
_inst == Instruction::MSTORE8 ||
_inst == Instruction::MLOAD ||
_inst == Instruction::CREATE ||
_inst == Instruction::CALL ||
_inst == Instruction::CALLCODE ||
_inst == Instruction::SHA3 ||
_inst == Instruction::CALLDATACOPY ||
_inst == Instruction::CODECOPY ||
_inst == Instruction::EXTCODECOPY ||
_inst == Instruction::DELEGATECALL;
}
bool changesStorage(Instruction _inst)
{
return _inst == Instruction::SSTORE;
}
void StandardTrace::operator()(uint64_t _steps, uint64_t PC, Instruction inst, bigint newMemSize,
bigint gasCost, bigint gas, VMFace const* _vm, ExtVMFace const* voidExt)
{
(void)_steps;
ExtVM const& ext = dynamic_cast<ExtVM const&>(*voidExt);
auto vm = dynamic_cast<LegacyVM const*>(_vm);
Json::Value r(Json::objectValue);
Json::Value stack(Json::arrayValue);
if (vm && !m_options.disableStack)
{
// Try extracting information about the stack from the VM is supported.
for (auto const& i : vm->stack())
stack.append(toCompactHexPrefixed(i, 1));
r["stack"] = stack;
}
bool newContext = false;
Instruction lastInst = Instruction::STOP;
if (m_lastInst.size() == ext.depth)
{
// starting a new context
assert(m_lastInst.size() == ext.depth);
m_lastInst.push_back(inst);
newContext = true;
}
else if (m_lastInst.size() == ext.depth + 2)
{
m_lastInst.pop_back();
lastInst = m_lastInst.back();
}
else if (m_lastInst.size() == ext.depth + 1)
{
// continuing in previous context
lastInst = m_lastInst.back();
m_lastInst.back() = inst;
}
else
{
cwarn << "GAA!!! Tracing VM and more than one new/deleted stack frame between steps!";
cwarn << "Attmepting naive recovery...";
m_lastInst.resize(ext.depth + 1);
}
Json::Value memJson(Json::arrayValue);
if (vm && !m_options.disableMemory && (changesMemory(lastInst) || newContext))
{
for (unsigned i = 0; i < vm->memory().size(); i += 32)
{
bytesConstRef memRef(vm->memory().data() + i, 32);
memJson.append(toHex(memRef));
}
r["memory"] = memJson;
}
if (!m_options.disableStorage && (m_options.fullStorage || changesStorage(lastInst) || newContext))
{
Json::Value storage(Json::objectValue);
for (auto const& i: ext.state().storage(ext.myAddress))
storage[toCompactHexPrefixed(i.second.first, 1)] = toCompactHexPrefixed(i.second.second, 1);
r["storage"] = storage;
}
if (m_showMnemonics)
r["op"] = instructionInfo(inst).name;
r["pc"] = toString(PC);
r["gas"] = toString(gas);
r["gasCost"] = toString(gasCost);
r["depth"] = toString(ext.depth);
if (!!newMemSize)
r["memexpand"] = toString(newMemSize);
m_trace.append(r);
}
string StandardTrace::json(bool _styled) const
{
return _styled ? Json::StyledWriter().write(m_trace) : Json::FastWriter().write(m_trace);
}
Executive::Executive(Block& _s, BlockChain const& _bc, unsigned _level):
m_s(_s.mutableState()),
m_envInfo(_s.info(), _bc.lastBlockHashes(), 0),
m_depth(_level),
m_sealEngine(*_bc.sealEngine())
{
}
Executive::Executive(Block& _s, LastBlockHashesFace const& _lh, unsigned _level):
m_s(_s.mutableState()),
m_envInfo(_s.info(), _lh, 0),
m_depth(_level),
m_sealEngine(*_s.sealEngine())
{
}
Executive::Executive(State& io_s, Block const& _block, unsigned _txIndex, BlockChain const& _bc, unsigned _level):
m_s(createIntermediateState(io_s, _block, _txIndex, _bc)),
m_envInfo(_block.info(), _bc.lastBlockHashes(), _txIndex ? _block.receipt(_txIndex - 1).cumulativeGasUsed() : 0),
m_depth(_level),
m_sealEngine(*_bc.sealEngine())
{
}
u256 Executive::gasUsed() const
{
return m_t.gas() - m_gas;
}
void Executive::accrueSubState(SubState& _parentContext)
{
if (m_ext)
_parentContext += m_ext->sub;
}
void Executive::initialize(Transaction const& _transaction)
{
m_t = _transaction;
m_baseGasRequired = m_t.baseGasRequired(m_sealEngine.evmSchedule(m_envInfo.number()));
try
{
m_sealEngine.verifyTransaction(ImportRequirements::Everything, m_t, m_envInfo.header(), m_envInfo.gasUsed());
}
catch (Exception const& ex)
{
m_excepted = toTransactionException(ex);
throw;
}
if (!m_t.hasZeroSignature())
{
// Avoid invalid transactions.
u256 nonceReq;
try
{
nonceReq = m_s.getNonce(m_t.sender());
}
catch (InvalidSignature const&)
{
LOG(m_execLogger) << "Invalid Signature";
m_excepted = TransactionException::InvalidSignature;
throw;
}
if (m_t.nonce() != nonceReq)
{
LOG(m_execLogger) << "Sender: " << m_t.sender().hex() << " Invalid Nonce: Require "
<< nonceReq << " Got " << m_t.nonce();
m_excepted = TransactionException::InvalidNonce;
BOOST_THROW_EXCEPTION(InvalidNonce() << RequirementError((bigint)nonceReq, (bigint)m_t.nonce()));
}
// Avoid unaffordable transactions.
bigint gasCost = (bigint)m_t.gas() * m_t.gasPrice();
bigint totalCost = m_t.value() + gasCost;
if (m_s.balance(m_t.sender()) < totalCost)
{
LOG(m_execLogger) << "Not enough cash: Require > " << totalCost << " = " << m_t.gas()
<< " * " << m_t.gasPrice() << " + " << m_t.value() << " Got"
<< m_s.balance(m_t.sender()) << " for sender: " << m_t.sender();
m_excepted = TransactionException::NotEnoughCash;
m_excepted = TransactionException::NotEnoughCash;
BOOST_THROW_EXCEPTION(NotEnoughCash() << RequirementError(totalCost, (bigint)m_s.balance(m_t.sender())) << errinfo_comment(m_t.sender().hex()));
}
m_gasCost = (u256)gasCost; // Convert back to 256-bit, safe now.
}
}
bool Executive::execute()
{
// Entry point for a user-executed transaction.
// Pay...
LOG(m_detailsLogger) << "Paying " << formatBalance(m_gasCost) << " from sender for gas ("
<< m_t.gas() << " gas at " << formatBalance(m_t.gasPrice()) << ")";
m_s.subBalance(m_t.sender(), m_gasCost);
assert(m_t.gas() >= (u256)m_baseGasRequired);
if (m_t.isCreation())
return create(m_t.sender(), m_t.value(), m_t.gasPrice(), m_t.gas() - (u256)m_baseGasRequired, &m_t.data(), m_t.sender());
else
return call(m_t.receiveAddress(), m_t.sender(), m_t.value(), m_t.gasPrice(), bytesConstRef(&m_t.data()), m_t.gas() - (u256)m_baseGasRequired);
}
bool Executive::call(Address const& _receiveAddress, Address const& _senderAddress, u256 const& _value, u256 const& _gasPrice, bytesConstRef _data, u256 const& _gas)
{
CallParameters params{_senderAddress, _receiveAddress, _receiveAddress, _value, _value, _gas, _data, {}};
return call(params, _gasPrice, _senderAddress);
}
bool Executive::call(CallParameters const& _p, u256 const& _gasPrice, Address const& _origin)
{
// If external transaction.
if (m_t)
{
// FIXME: changelog contains unrevertable balance change that paid
// for the transaction.
// Increment associated nonce for sender.
if (_p.senderAddress != MaxAddress || m_envInfo.number() < m_sealEngine.chainParams().constantinopleForkBlock) // EIP86
m_s.incNonce(_p.senderAddress);
}
m_savepoint = m_s.savepoint();
if (m_sealEngine.isPrecompiled(_p.codeAddress, m_envInfo.number()))
{
bigint g = m_sealEngine.costOfPrecompiled(_p.codeAddress, _p.data, m_envInfo.number());
if (_p.gas < g)
{
m_excepted = TransactionException::OutOfGasBase;
// Bail from exception.
// Empty precompiled contracts need to be deleted even in case of OOG
// because the bug in both Geth and Parity led to deleting RIPEMD precompiled in this case
// see https://github.com/ethereum/go-ethereum/pull/3341/files#diff-2433aa143ee4772026454b8abd76b9dd
// We mark the account as touched here, so that is can be removed among other touched empty accounts (after tx finalization)
if (m_envInfo.number() >= m_sealEngine.chainParams().EIP158ForkBlock)
m_s.addBalance(_p.codeAddress, 0);
return true; // true actually means "all finished - nothing more to be done regarding go().
}
else
{
m_gas = (u256)(_p.gas - g);
bytes output;
bool success;
tie(success, output) = m_sealEngine.executePrecompiled(_p.codeAddress, _p.data, m_envInfo.number());
size_t outputSize = output.size();
m_output = owning_bytes_ref{std::move(output), 0, outputSize};
if (!success)
{
m_gas = 0;
m_excepted = TransactionException::OutOfGas;
return true; // true means no need to run go().
}
}
}
else
{
m_gas = _p.gas;
if (m_s.addressHasCode(_p.codeAddress))
{
bytes const& c = m_s.code(_p.codeAddress);
h256 codeHash = m_s.codeHash(_p.codeAddress);
m_ext = make_shared<ExtVM>(m_s, m_envInfo, m_sealEngine, _p.receiveAddress,
_p.senderAddress, _origin, _p.apparentValue, _gasPrice, _p.data, &c, codeHash,
m_depth, false, _p.staticCall);
}
}
// Transfer ether.
m_s.transferBalance(_p.senderAddress, _p.receiveAddress, _p.valueTransfer);
return !m_ext;
}
bool Executive::create(Address const& _txSender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _init, Address const& _origin)
{
// Contract creation by an external account is the same as CREATE opcode
return createOpcode(_txSender, _endowment, _gasPrice, _gas, _init, _origin);
}
bool Executive::createOpcode(Address const& _sender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _init, Address const& _origin)
{
u256 nonce = m_s.getNonce(_sender);
m_newAddress = right160(sha3(rlpList(_sender, nonce)));
return executeCreate(_sender, _endowment, _gasPrice, _gas, _init, _origin);
}
bool Executive::create2Opcode(Address const& _sender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _init, Address const& _origin, u256 const& _salt)
{
m_newAddress = right160(sha3(_sender.asBytes() + toBigEndian(_salt) + sha3(_init).asBytes()));
return executeCreate(_sender, _endowment, _gasPrice, _gas, _init, _origin);
}
bool Executive::executeCreate(Address const& _sender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _init, Address const& _origin)
{
if (_sender != MaxAddress || m_envInfo.number() < m_sealEngine.chainParams().constantinopleForkBlock) // EIP86
m_s.incNonce(_sender);
m_savepoint = m_s.savepoint();
m_isCreation = true;
// We can allow for the reverted state (i.e. that with which m_ext is constructed) to contain the m_orig.address, since
// we delete it explicitly if we decide we need to revert.
m_gas = _gas;
bool accountAlreadyExist = (m_s.addressHasCode(m_newAddress) || m_s.getNonce(m_newAddress) > 0);
if (accountAlreadyExist)
{
LOG(m_detailsLogger) << "Address already used: " << m_newAddress;
m_gas = 0;
m_excepted = TransactionException::AddressAlreadyUsed;
revert();
m_ext = {}; // cancel the _init execution if there are any scheduled.
return !m_ext;
}
// Transfer ether before deploying the code. This will also create new
// account if it does not exist yet.
m_s.transferBalance(_sender, m_newAddress, _endowment);
u256 newNonce = m_s.requireAccountStartNonce();
if (m_envInfo.number() >= m_sealEngine.chainParams().EIP158ForkBlock)
newNonce += 1;
m_s.setNonce(m_newAddress, newNonce);
// Schedule _init execution if not empty.
if (!_init.empty())
m_ext = make_shared<ExtVM>(m_s, m_envInfo, m_sealEngine, m_newAddress, _sender, _origin,
_endowment, _gasPrice, bytesConstRef(), _init, sha3(_init), m_depth, true, false);
return !m_ext;
}
OnOpFunc Executive::simpleTrace()
{
Logger& traceLogger = m_vmTraceLogger;
return [&traceLogger](uint64_t steps, uint64_t PC, Instruction inst, bigint newMemSize,
bigint gasCost, bigint gas, VMFace const* _vm, ExtVMFace const* voidExt) {
ExtVM const& ext = *static_cast<ExtVM const*>(voidExt);
auto vm = dynamic_cast<LegacyVM const*>(_vm);
ostringstream o;
if (vm)
LOG(traceLogger) << dumpStackAndMemory(*vm);
LOG(traceLogger) << dumpStorage(ext);
LOG(traceLogger) << " < " << dec << ext.depth << " : " << ext.myAddress << " : #" << steps
<< " : " << hex << setw(4) << setfill('0') << PC << " : "
<< instructionInfo(inst).name << " : " << dec << gas << " : -" << dec
<< gasCost << " : " << newMemSize << "x32"
<< " >";
};
}
bool Executive::go(OnOpFunc const& _onOp)
{
if (m_ext)
{
#if ETH_TIMED_EXECUTIONS
Timer t;
#endif
try
{
// Create VM instance. Force Interpreter if tracing requested.
auto vm = VMFactory::create();
if (m_isCreation)
{
m_s.clearStorage(m_ext->myAddress);
auto out = vm->exec(m_gas, *m_ext, _onOp);
if (m_res)
{
m_res->gasForDeposit = m_gas;
m_res->depositSize = out.size();
}
if (out.size() > m_ext->evmSchedule().maxCodeSize)
BOOST_THROW_EXCEPTION(OutOfGas());
else if (out.size() * m_ext->evmSchedule().createDataGas <= m_gas)
{
if (m_res)
m_res->codeDeposit = CodeDeposit::Success;
m_gas -= out.size() * m_ext->evmSchedule().createDataGas;
}
else
{
if (m_ext->evmSchedule().exceptionalFailedCodeDeposit)
BOOST_THROW_EXCEPTION(OutOfGas());
else
{
if (m_res)
m_res->codeDeposit = CodeDeposit::Failed;
out = {};
}
}
if (m_res)
m_res->output = out.toVector(); // copy output to execution result
m_s.setCode(m_ext->myAddress, out.toVector());
}
else
m_output = vm->exec(m_gas, *m_ext, _onOp);
}
catch (RevertInstruction& _e)
{
revert();
m_output = _e.output();
m_excepted = TransactionException::RevertInstruction;
}
catch (VMException const& _e)
{
LOG(m_detailsLogger) << "Safe VM Exception. " << diagnostic_information(_e);
m_gas = 0;
m_excepted = toTransactionException(_e);
revert();
}
catch (InternalVMError const& _e)
{
cwarn << "Internal VM Error (" << *boost::get_error_info<errinfo_evmcStatusCode>(_e) << ")\n"
<< diagnostic_information(_e);
throw;
}
catch (Exception const& _e)
{
// TODO: AUDIT: check that this can never reasonably happen. Consider what to do if it does.
cwarn << "Unexpected exception in VM. There may be a bug in this implementation. " << diagnostic_information(_e);
exit(1);
// Another solution would be to reject this transaction, but that also
// has drawbacks. Essentially, the amount of ram has to be increased here.
}
catch (std::exception const& _e)
{
// TODO: AUDIT: check that this can never reasonably happen. Consider what to do if it does.
cwarn << "Unexpected std::exception in VM. Not enough RAM? " << _e.what();
exit(1);
// Another solution would be to reject this transaction, but that also
// has drawbacks. Essentially, the amount of ram has to be increased here.
}
if (m_res && m_output)
// Copy full output:
m_res->output = m_output.toVector();
#if ETH_TIMED_EXECUTIONS
cnote << "VM took:" << t.elapsed() << "; gas used: " << (sgas - m_endGas);
#endif
}
return true;
}
bool Executive::finalize()
{
// Accumulate refunds for suicides.
if (m_ext)
m_ext->sub.refunds += m_ext->evmSchedule().suicideRefundGas * m_ext->sub.suicides.size();
// SSTORE refunds...
// must be done before the miner gets the fees.
m_refunded = m_ext ? min((m_t.gas() - m_gas) / 2, m_ext->sub.refunds) : 0;
m_gas += m_refunded;
if (m_t)
{
m_s.addBalance(m_t.sender(), m_gas * m_t.gasPrice());
u256 feesEarned = (m_t.gas() - m_gas) * m_t.gasPrice();
m_s.addBalance(m_envInfo.author(), feesEarned);
}
// Suicides...
if (m_ext)
for (auto a: m_ext->sub.suicides)
m_s.kill(a);
// Logs..
if (m_ext)
m_logs = m_ext->sub.logs;
if (m_res) // Collect results
{
m_res->gasUsed = gasUsed();
m_res->excepted = m_excepted; // TODO: m_except is used only in ExtVM::call
m_res->newAddress = m_newAddress;
m_res->gasRefunded = m_ext ? m_ext->sub.refunds : 0;
}
return (m_excepted == TransactionException::None);
}
void Executive::revert()
{
if (m_ext)
m_ext->sub.clear();
// Set result address to the null one.
m_newAddress = {};
m_s.rollback(m_savepoint);
}