VM.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file VM.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #include <libethereum/ExtVM.h>
  19. #include "VMConfig.h"
  20. #include "VM.h"
  21. using namespace std;
  22. using namespace dev;
  23. using namespace dev::eth;
  24. uint64_t VM::memNeed(u256 _offset, u256 _size)
  25. {
  26. return toUint64(_size ? u512(_offset) + _size : u512(0));
  27. }
  28. template <class S> S divWorkaround(S const& _a, S const& _b)
  29. {
  30. return (S)(s512(_a) / s512(_b));
  31. }
  32. template <class S> S modWorkaround(S const& _a, S const& _b)
  33. {
  34. return (S)(s512(_a) % s512(_b));
  35. }
  36. //
  37. // for tracing, checking, metering, measuring ...
  38. //
  39. void VM::onOperation()
  40. {
  41. if (m_onOp)
  42. (m_onOp)(++m_nSteps, m_pc, m_op,
  43. m_newMemSize > m_mem.size() ? (m_newMemSize - m_mem.size()) / 32 : uint64_t(0),
  44. m_runGas, m_io_gas, this, m_ext);
  45. }
  46. void VM::checkStack(unsigned _removed, unsigned _added)
  47. {
  48. int const size = 1 + m_sp - m_stack;
  49. int const usedSize = size - _removed;
  50. if (usedSize < 0 || usedSize + _added > 1024)
  51. throwBadStack(size, _removed, _added);
  52. }
  53. uint64_t VM::gasForMem(u512 _size)
  54. {
  55. u512 s = _size / 32;
  56. return toUint64((u512)m_schedule->memoryGas * s + s * s / m_schedule->quadCoeffDiv);
  57. }
  58. void VM::updateIOGas()
  59. {
  60. if (m_io_gas < m_runGas)
  61. throwOutOfGas();
  62. m_io_gas -= m_runGas;
  63. }
  64. void VM::updateGas()
  65. {
  66. if (m_newMemSize > m_mem.size())
  67. m_runGas += toUint64(gasForMem(m_newMemSize) - gasForMem(m_mem.size()));
  68. m_runGas += (m_schedule->copyGas * ((m_copyMemSize + 31) / 32));
  69. if (m_io_gas < m_runGas)
  70. throwOutOfGas();
  71. }
  72. void VM::updateMem()
  73. {
  74. m_newMemSize = (m_newMemSize + 31) / 32 * 32;
  75. updateGas();
  76. if (m_newMemSize > m_mem.size())
  77. m_mem.resize(m_newMemSize);
  78. }
  79. void VM::logGasMem()
  80. {
  81. unsigned n = (unsigned)m_op - (unsigned)Instruction::LOG0;
  82. m_runGas = toUint64(m_schedule->logGas + m_schedule->logTopicGas * n + u512(m_schedule->logDataGas) * *(m_sp - 1));
  83. m_newMemSize = memNeed(*m_sp, *(m_sp - 1));
  84. updateMem();
  85. }
  86. void VM::fetchInstruction()
  87. {
  88. m_op = Instruction(m_code[m_pc]);
  89. const InstructionMetric& metric = c_metrics[static_cast<size_t>(m_op)];
  90. checkStack(metric.args, metric.ret);
  91. // FEES...
  92. m_runGas = toUint64(m_schedule->tierStepGas[metric.gasPriceTier]);
  93. m_newMemSize = m_mem.size();
  94. m_copyMemSize = 0;
  95. }
  96. ///////////////////////////////////////////////////////////////////////////////
  97. //
  98. // interpreter entry point
  99. bytesConstRef VM::execImpl(u256& _io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp)
  100. {
  101. io_gas = &_io_gas;
  102. m_io_gas = uint64_t(_io_gas);
  103. m_ext = &_ext;
  104. m_schedule = &m_ext->evmSchedule();
  105. m_onOp = _onOp;
  106. m_onFail = &VM::onOperation;
  107. try
  108. {
  109. // trampoline to minimize depth of call stack when calling out
  110. m_bounce = &VM::initEntry;
  111. do
  112. (this->*m_bounce)();
  113. while (m_bounce);
  114. }
  115. catch (...)
  116. {
  117. *io_gas = m_io_gas;
  118. throw;
  119. }
  120. *io_gas = m_io_gas;
  121. return m_bytes;
  122. }
  123. //
  124. // main interpreter loop and switch
  125. //
  126. void VM::interpretCases()
  127. {
  128. INIT_CASES
  129. DO_CASES
  130. //
  131. // Call-related instructions
  132. //
  133. CASE_BEGIN(CREATE)
  134. m_bounce = &VM::caseCreate;
  135. CASE_RETURN;
  136. CASE_BEGIN(DELEGATECALL)
  137. // Pre-homestead
  138. if (!m_schedule->haveDelegateCall)
  139. throwBadInstruction();
  140. CASE_BEGIN(CALL)
  141. CASE_BEGIN(CALLCODE)
  142. m_bounce = &VM::caseCall;
  143. CASE_RETURN
  144. CASE_BEGIN(RETURN)
  145. {
  146. m_newMemSize = memNeed(*m_sp, *(m_sp - 1));
  147. updateMem();
  148. ON_OP();
  149. updateIOGas();
  150. uint64_t b = (uint64_t)*m_sp--;
  151. uint64_t s = (uint64_t)*m_sp--;
  152. m_bytes = bytesConstRef(m_mem.data() + b, s);
  153. m_bounce = 0;
  154. }
  155. CASE_RETURN
  156. CASE_BEGIN(SUICIDE)
  157. {
  158. m_runGas = toUint64(m_schedule->suicideGas);
  159. Address dest = asAddress(*m_sp);
  160. // After EIP158 zero-value suicides do not have to pay account creation gas.
  161. if (m_ext->balance(m_ext->myAddress) > 0 || m_schedule->zeroValueTransferChargesNewAccountGas())
  162. // After EIP150 hard fork charge additional cost of sending
  163. // ethers to non-existing account.
  164. if (m_schedule->suicideChargesNewAccountGas() && !m_ext->exists(dest))
  165. m_runGas += m_schedule->callNewAccountGas;
  166. ON_OP();
  167. updateIOGas();
  168. m_ext->suicide(dest);
  169. m_bounce = 0;
  170. }
  171. CASE_RETURN
  172. CASE_BEGIN(STOP)
  173. ON_OP();
  174. updateIOGas();
  175. m_bounce = 0;
  176. CASE_RETURN;
  177. //
  178. // instructions potentially expanding memory
  179. //
  180. CASE_BEGIN(MLOAD)
  181. {
  182. m_newMemSize = toUint64(*m_sp) + 32;
  183. updateMem();
  184. ON_OP();
  185. updateIOGas();
  186. *m_sp = (u256)*(h256 const*)(m_mem.data() + (unsigned)*m_sp);
  187. ++m_pc;
  188. }
  189. CASE_END
  190. CASE_BEGIN(MSTORE)
  191. {
  192. m_newMemSize = toUint64(*m_sp) + 32;
  193. updateMem();
  194. ON_OP();
  195. updateIOGas();
  196. *(h256*)&m_mem[(unsigned)*m_sp] = (h256)*(m_sp - 1);
  197. m_sp -= 2;
  198. ++m_pc;
  199. }
  200. CASE_END
  201. CASE_BEGIN(MSTORE8)
  202. {
  203. m_newMemSize = toUint64(*m_sp) + 1;
  204. updateMem();
  205. ON_OP();
  206. updateIOGas();
  207. m_mem[(unsigned)*m_sp] = (byte)(*(m_sp - 1) & 0xff);
  208. m_sp -= 2;
  209. ++m_pc;
  210. }
  211. CASE_END
  212. CASE_BEGIN(SHA3)
  213. {
  214. m_runGas = toUint64(m_schedule->sha3Gas + (u512(*(m_sp - 1)) + 31) / 32 * m_schedule->sha3WordGas);
  215. m_newMemSize = memNeed(*m_sp, *(m_sp - 1));
  216. updateMem();
  217. ON_OP();
  218. updateIOGas();
  219. uint64_t inOff = (uint64_t)*m_sp--;
  220. uint64_t inSize = (uint64_t)*m_sp--;
  221. *++m_sp = (u256)sha3(bytesConstRef(m_mem.data() + inOff, inSize));
  222. ++m_pc;
  223. }
  224. CASE_END
  225. CASE_BEGIN(LOG0)
  226. logGasMem();
  227. ON_OP();
  228. updateIOGas();
  229. m_ext->log({}, bytesConstRef(m_mem.data() + (uint64_t)*m_sp, (uint64_t)*(m_sp - 1)));
  230. m_sp -= 2;
  231. ++m_pc;
  232. CASE_END
  233. CASE_BEGIN(LOG1)
  234. logGasMem();
  235. ON_OP();
  236. updateIOGas();
  237. m_ext->log({*(m_sp - 2)}, bytesConstRef(m_mem.data() + (uint64_t)*m_sp, (uint64_t)*(m_sp - 1)));
  238. m_sp -= 3;
  239. ++m_pc;
  240. CASE_END
  241. CASE_BEGIN(LOG2)
  242. logGasMem();
  243. ON_OP();
  244. updateIOGas();
  245. m_ext->log({*(m_sp - 2), *(m_sp-3)}, bytesConstRef(m_mem.data() + (uint64_t)*m_sp, (uint64_t)*(m_sp - 1)));
  246. m_sp -= 4;
  247. ++m_pc;
  248. CASE_END
  249. CASE_BEGIN(LOG3)
  250. logGasMem();
  251. ON_OP();
  252. updateIOGas();
  253. m_ext->log({*(m_sp - 2), *(m_sp-3), *(m_sp-4)}, bytesConstRef(m_mem.data() + (uint64_t)*m_sp, (uint64_t)*(m_sp - 1)));
  254. m_sp -= 5;
  255. ++m_pc;
  256. CASE_END
  257. CASE_BEGIN(LOG4)
  258. logGasMem();
  259. ON_OP();
  260. updateIOGas();
  261. m_ext->log({*(m_sp - 2), *(m_sp-3), *(m_sp-4), *(m_sp-5)}, bytesConstRef(m_mem.data() + (uint64_t)*m_sp, (uint64_t)*(m_sp - 1)));
  262. m_sp -= 6;
  263. ++m_pc;
  264. CASE_END
  265. CASE_BEGIN(EXP)
  266. {
  267. u256 expon = *(m_sp - 1);
  268. m_runGas = toUint64(m_schedule->expGas + m_schedule->expByteGas * (32 - (h256(expon).firstBitSet() / 8)));
  269. ON_OP();
  270. updateIOGas();
  271. u256 base = *m_sp--;
  272. *m_sp = exp256(base, expon);
  273. ++m_pc;
  274. }
  275. CASE_END
  276. //
  277. // ordinary instructions
  278. //
  279. CASE_BEGIN(ADD)
  280. ON_OP();
  281. updateIOGas();
  282. //pops two items and pushes S[-1] + S[-2] mod 2^256.
  283. *(m_sp - 1) += *m_sp;
  284. --m_sp;
  285. ++m_pc;
  286. CASE_END
  287. CASE_BEGIN(MUL)
  288. ON_OP();
  289. updateIOGas();
  290. //pops two items and pushes S[-1] * S[-2] mod 2^256.
  291. *(m_sp - 1) *= *m_sp;
  292. --m_sp;
  293. ++m_pc;
  294. CASE_END
  295. CASE_BEGIN(SUB)
  296. ON_OP();
  297. updateIOGas();
  298. *(m_sp - 1) = *m_sp - *(m_sp - 1);
  299. --m_sp;
  300. ++m_pc;
  301. CASE_END
  302. CASE_BEGIN(DIV)
  303. ON_OP();
  304. updateIOGas();
  305. *(m_sp - 1) = *(m_sp - 1) ? divWorkaround(*m_sp, *(m_sp - 1)) : 0;
  306. --m_sp;
  307. ++m_pc;
  308. CASE_END
  309. CASE_BEGIN(SDIV)
  310. ON_OP();
  311. updateIOGas();
  312. *(m_sp - 1) = *(m_sp - 1) ? s2u(divWorkaround(u2s(*m_sp), u2s(*(m_sp - 1)))) : 0;
  313. --m_sp;
  314. ++m_pc;
  315. CASE_END
  316. CASE_BEGIN(MOD)
  317. ON_OP();
  318. updateIOGas();
  319. *(m_sp - 1) = *(m_sp - 1) ? modWorkaround(*m_sp, *(m_sp - 1)) : 0;
  320. --m_sp;
  321. ++m_pc;
  322. CASE_END
  323. CASE_BEGIN(SMOD)
  324. ON_OP();
  325. updateIOGas();
  326. *(m_sp - 1) = *(m_sp - 1) ? s2u(modWorkaround(u2s(*m_sp), u2s(*(m_sp - 1)))) : 0;
  327. --m_sp;
  328. ++m_pc;
  329. CASE_END
  330. CASE_BEGIN(NOT)
  331. ON_OP();
  332. updateIOGas();
  333. *m_sp = ~*m_sp;
  334. ++m_pc;
  335. CASE_END
  336. CASE_BEGIN(LT)
  337. ON_OP();
  338. updateIOGas();
  339. *(m_sp - 1) = *m_sp < *(m_sp - 1) ? 1 : 0;
  340. --m_sp;
  341. ++m_pc;
  342. CASE_END
  343. CASE_BEGIN(GT)
  344. ON_OP();
  345. updateIOGas();
  346. *(m_sp - 1) = *m_sp > *(m_sp - 1) ? 1 : 0;
  347. --m_sp;
  348. ++m_pc;
  349. CASE_END
  350. CASE_BEGIN(SLT)
  351. ON_OP();
  352. updateIOGas();
  353. *(m_sp - 1) = u2s(*m_sp) < u2s(*(m_sp - 1)) ? 1 : 0;
  354. --m_sp;
  355. ++m_pc;
  356. CASE_END
  357. CASE_BEGIN(SGT)
  358. ON_OP();
  359. updateIOGas();
  360. *(m_sp - 1) = u2s(*m_sp) > u2s(*(m_sp - 1)) ? 1 : 0;
  361. --m_sp;
  362. ++m_pc;
  363. CASE_END
  364. CASE_BEGIN(EQ)
  365. ON_OP();
  366. updateIOGas();
  367. *(m_sp - 1) = *m_sp == *(m_sp - 1) ? 1 : 0;
  368. --m_sp;
  369. ++m_pc;
  370. CASE_END
  371. CASE_BEGIN(ISZERO)
  372. ON_OP();
  373. updateIOGas();
  374. *m_sp = *m_sp ? 0 : 1;
  375. ++m_pc;
  376. CASE_END
  377. CASE_BEGIN(AND)
  378. ON_OP();
  379. updateIOGas();
  380. *(m_sp - 1) = *m_sp & *(m_sp - 1);
  381. --m_sp;
  382. ++m_pc;
  383. CASE_END
  384. CASE_BEGIN(OR)
  385. ON_OP();
  386. updateIOGas();
  387. *(m_sp - 1) = *m_sp | *(m_sp - 1);
  388. --m_sp;
  389. ++m_pc;
  390. CASE_END
  391. CASE_BEGIN(XOR)
  392. ON_OP();
  393. updateIOGas();
  394. *(m_sp - 1) = *m_sp ^ *(m_sp - 1);
  395. --m_sp;
  396. ++m_pc;
  397. CASE_END
  398. CASE_BEGIN(BYTE)
  399. ON_OP();
  400. updateIOGas();
  401. *(m_sp - 1) = *m_sp < 32 ? (*(m_sp - 1) >> (unsigned)(8 * (31 - *m_sp))) & 0xff : 0;
  402. --m_sp;
  403. ++m_pc;
  404. CASE_END
  405. CASE_BEGIN(ADDMOD)
  406. ON_OP();
  407. updateIOGas();
  408. *(m_sp - 2) = *(m_sp - 2) ? u256((u512(*m_sp) + u512(*(m_sp - 1))) % *(m_sp - 2)) : 0;
  409. m_sp -= 2;
  410. ++m_pc;
  411. CASE_END
  412. CASE_BEGIN(MULMOD)
  413. ON_OP();
  414. updateIOGas();
  415. *(m_sp - 2) = *(m_sp - 2) ? u256((u512(*m_sp) * u512(*(m_sp - 1))) % *(m_sp - 2)) : 0;
  416. m_sp -= 2;
  417. ++m_pc;
  418. CASE_END
  419. CASE_BEGIN(SIGNEXTEND)
  420. ON_OP();
  421. updateIOGas();
  422. if (*m_sp < 31)
  423. {
  424. unsigned testBit = static_cast<unsigned>(*m_sp) * 8 + 7;
  425. u256& number = *(m_sp - 1);
  426. u256 mask = ((u256(1) << testBit) - 1);
  427. if (boost::multiprecision::bit_test(number, testBit))
  428. number |= ~mask;
  429. else
  430. number &= mask;
  431. }
  432. --m_sp;
  433. ++m_pc;
  434. CASE_END
  435. CASE_BEGIN(ADDRESS)
  436. ON_OP();
  437. updateIOGas();
  438. *++m_sp = fromAddress(m_ext->myAddress);
  439. ++m_pc;
  440. CASE_END
  441. CASE_BEGIN(ORIGIN)
  442. ON_OP();
  443. updateIOGas();
  444. *++m_sp = fromAddress(m_ext->origin);
  445. ++m_pc;
  446. CASE_END
  447. CASE_BEGIN(BALANCE)
  448. {
  449. m_runGas = toUint64(m_schedule->balanceGas);
  450. ON_OP();
  451. updateIOGas();
  452. *m_sp = m_ext->balance(asAddress(*m_sp));
  453. ++m_pc;
  454. }
  455. CASE_END
  456. CASE_BEGIN(CALLER)
  457. ON_OP();
  458. updateIOGas();
  459. *++m_sp = fromAddress(m_ext->caller);
  460. ++m_pc;
  461. CASE_END
  462. CASE_BEGIN(CALLVALUE)
  463. ON_OP();
  464. updateIOGas();
  465. *++m_sp = m_ext->value;
  466. ++m_pc;
  467. CASE_END
  468. CASE_BEGIN(CALLDATALOAD)
  469. {
  470. ON_OP();
  471. updateIOGas();
  472. if (u512(*m_sp) + 31 < m_ext->data.size())
  473. *m_sp = (u256)*(h256 const*)(m_ext->data.data() + (size_t)*m_sp);
  474. else if (*m_sp >= m_ext->data.size())
  475. *m_sp = u256(0);
  476. else
  477. {
  478. h256 r;
  479. for (uint64_t i = (uint64_t)*m_sp, e = (uint64_t)*m_sp + (uint64_t)32, j = 0; i < e; ++i, ++j)
  480. r[j] = i < m_ext->data.size() ? m_ext->data[i] : 0;
  481. *m_sp = (u256)r;
  482. }
  483. ++m_pc;
  484. }
  485. CASE_END
  486. CASE_BEGIN(CALLDATASIZE)
  487. ON_OP();
  488. updateIOGas();
  489. *++m_sp = m_ext->data.size();
  490. ++m_pc;
  491. CASE_END
  492. CASE_BEGIN(CODESIZE)
  493. ON_OP();
  494. updateIOGas();
  495. *++m_sp = m_ext->code.size();
  496. ++m_pc;
  497. CASE_END
  498. CASE_BEGIN(EXTCODESIZE)
  499. m_runGas = toUint64(m_schedule->extcodesizeGas);
  500. ON_OP();
  501. updateIOGas();
  502. *m_sp = m_ext->codeSizeAt(asAddress(*m_sp));
  503. ++m_pc;
  504. CASE_END
  505. CASE_BEGIN(CALLDATACOPY)
  506. m_copyMemSize = toUint64(*(m_sp - 2));
  507. m_newMemSize = memNeed(*m_sp, *(m_sp - 2));
  508. updateMem();
  509. ON_OP();
  510. updateIOGas();
  511. copyDataToMemory(m_ext->data, m_sp);
  512. ++m_pc;
  513. CASE_END
  514. CASE_BEGIN(CODECOPY)
  515. m_copyMemSize = toUint64(*(m_sp - 2));
  516. m_newMemSize = memNeed(*m_sp, *(m_sp - 2));
  517. updateMem();
  518. ON_OP();
  519. updateIOGas();
  520. copyDataToMemory(&m_ext->code, m_sp);
  521. ++m_pc;
  522. CASE_END
  523. CASE_BEGIN(EXTCODECOPY)
  524. {
  525. m_runGas = toUint64(m_schedule->extcodecopyGas);
  526. m_copyMemSize = toUint64(*(m_sp - 3));
  527. m_newMemSize = memNeed(*(m_sp - 1), *(m_sp - 3));
  528. updateMem();
  529. ON_OP();
  530. updateIOGas();
  531. Address a = asAddress(*m_sp);
  532. --m_sp;
  533. copyDataToMemory(&m_ext->codeAt(a), m_sp);
  534. ++m_pc;
  535. }
  536. CASE_END
  537. CASE_BEGIN(GASPRICE)
  538. ON_OP();
  539. updateIOGas();
  540. *++m_sp = m_ext->gasPrice;
  541. ++m_pc;
  542. CASE_END
  543. CASE_BEGIN(BLOCKHASH)
  544. ON_OP();
  545. updateIOGas();
  546. *m_sp = (u256)m_ext->blockHash(*m_sp);
  547. ++m_pc;
  548. CASE_END
  549. CASE_BEGIN(COINBASE)
  550. ON_OP();
  551. updateIOGas();
  552. *++m_sp = (u160)m_ext->envInfo().author();
  553. ++m_pc;
  554. CASE_END
  555. CASE_BEGIN(TIMESTAMP)
  556. ON_OP();
  557. updateIOGas();
  558. *++m_sp = m_ext->envInfo().timestamp();
  559. ++m_pc;
  560. CASE_END
  561. CASE_BEGIN(NUMBER)
  562. ON_OP();
  563. updateIOGas();
  564. *++m_sp = m_ext->envInfo().number();
  565. ++m_pc;
  566. CASE_END
  567. CASE_BEGIN(DIFFICULTY)
  568. ON_OP();
  569. updateIOGas();
  570. *++m_sp = m_ext->envInfo().difficulty();
  571. ++m_pc;
  572. CASE_END
  573. CASE_BEGIN(GASLIMIT)
  574. ON_OP();
  575. updateIOGas();
  576. *++m_sp = m_ext->envInfo().gasLimit();
  577. ++m_pc;
  578. CASE_END
  579. CASE_BEGIN(POP)
  580. ON_OP();
  581. updateIOGas();
  582. --m_sp;
  583. ++m_pc;
  584. CASE_END
  585. CASE_BEGIN(PUSHC)
  586. #ifdef EVM_USE_CONSTANT_POOL
  587. {
  588. ON_OP();
  589. updateIOGas();
  590. ++m_pc;
  591. *++m_sp = m_pool[m_code[m_pc]];
  592. ++m_pc;
  593. m_pc += m_code[m_pc];
  594. }
  595. #else
  596. throwBadInstruction();
  597. #endif
  598. CASE_END
  599. CASE_BEGIN(PUSH1)
  600. ON_OP();
  601. updateIOGas();
  602. *++m_sp = m_code[++m_pc];
  603. ++m_pc;
  604. CASE_END
  605. CASE_BEGIN(PUSH2)
  606. CASE_BEGIN(PUSH3)
  607. CASE_BEGIN(PUSH4)
  608. CASE_BEGIN(PUSH5)
  609. CASE_BEGIN(PUSH6)
  610. CASE_BEGIN(PUSH7)
  611. CASE_BEGIN(PUSH8)
  612. CASE_BEGIN(PUSH9)
  613. CASE_BEGIN(PUSH10)
  614. CASE_BEGIN(PUSH11)
  615. CASE_BEGIN(PUSH12)
  616. CASE_BEGIN(PUSH13)
  617. CASE_BEGIN(PUSH14)
  618. CASE_BEGIN(PUSH15)
  619. CASE_BEGIN(PUSH16)
  620. CASE_BEGIN(PUSH17)
  621. CASE_BEGIN(PUSH18)
  622. CASE_BEGIN(PUSH19)
  623. CASE_BEGIN(PUSH20)
  624. CASE_BEGIN(PUSH21)
  625. CASE_BEGIN(PUSH22)
  626. CASE_BEGIN(PUSH23)
  627. CASE_BEGIN(PUSH24)
  628. CASE_BEGIN(PUSH25)
  629. CASE_BEGIN(PUSH26)
  630. CASE_BEGIN(PUSH27)
  631. CASE_BEGIN(PUSH28)
  632. CASE_BEGIN(PUSH29)
  633. CASE_BEGIN(PUSH30)
  634. CASE_BEGIN(PUSH31)
  635. CASE_BEGIN(PUSH32)
  636. {
  637. ON_OP();
  638. updateIOGas();
  639. int numBytes = (int)m_op - (int)Instruction::PUSH1 + 1;
  640. *++m_sp = 0;
  641. // Construct a number out of PUSH bytes.
  642. // This requires the code has been copied and extended by 32 zero
  643. // bytes to handle "out of code" push data here.
  644. for (++m_pc; numBytes--; ++m_pc)
  645. *m_sp = (*m_sp << 8) | m_code[m_pc];
  646. }
  647. CASE_END
  648. CASE_BEGIN(JUMP)
  649. ON_OP();
  650. updateIOGas();
  651. m_pc = verifyJumpDest(*m_sp);
  652. --m_sp;
  653. CASE_END
  654. CASE_BEGIN(JUMPI)
  655. ON_OP();
  656. updateIOGas();
  657. if (*(m_sp - 1))
  658. m_pc = verifyJumpDest(*m_sp);
  659. else
  660. ++m_pc;
  661. m_sp -= 2;
  662. CASE_END
  663. CASE_BEGIN(JUMPV)
  664. #ifdef EVM_REPLACE_CONST_JUMP
  665. ON_OP();
  666. updateIOGas();
  667. m_pc = uint64_t(*m_sp);
  668. --m_sp;
  669. #else
  670. throwBadInstruction();
  671. #endif
  672. CASE_END
  673. CASE_BEGIN(JUMPVI)
  674. #ifdef EVM_REPLACE_CONST_JUMP
  675. ON_OP();
  676. updateIOGas();
  677. if (*(m_sp - 1))
  678. m_pc = uint64_t(*m_sp);
  679. else
  680. ++m_pc;
  681. m_sp -= 2;
  682. #else
  683. throwBadInstruction();
  684. #endif
  685. CASE_END
  686. CASE_BEGIN(DUP1)
  687. CASE_BEGIN(DUP2)
  688. CASE_BEGIN(DUP3)
  689. CASE_BEGIN(DUP4)
  690. CASE_BEGIN(DUP5)
  691. CASE_BEGIN(DUP6)
  692. CASE_BEGIN(DUP7)
  693. CASE_BEGIN(DUP8)
  694. CASE_BEGIN(DUP9)
  695. CASE_BEGIN(DUP10)
  696. CASE_BEGIN(DUP11)
  697. CASE_BEGIN(DUP12)
  698. CASE_BEGIN(DUP13)
  699. CASE_BEGIN(DUP14)
  700. CASE_BEGIN(DUP15)
  701. CASE_BEGIN(DUP16)
  702. {
  703. ON_OP();
  704. updateIOGas();
  705. unsigned n = 1 + (unsigned)m_op - (unsigned)Instruction::DUP1;
  706. *(m_sp+1) = m_stack[(1 + m_sp - m_stack) - n];
  707. ++m_sp;
  708. ++m_pc;
  709. }
  710. CASE_END
  711. CASE_BEGIN(SWAP1)
  712. CASE_BEGIN(SWAP2)
  713. CASE_BEGIN(SWAP3)
  714. CASE_BEGIN(SWAP4)
  715. CASE_BEGIN(SWAP5)
  716. CASE_BEGIN(SWAP6)
  717. CASE_BEGIN(SWAP7)
  718. CASE_BEGIN(SWAP8)
  719. CASE_BEGIN(SWAP9)
  720. CASE_BEGIN(SWAP10)
  721. CASE_BEGIN(SWAP11)
  722. CASE_BEGIN(SWAP12)
  723. CASE_BEGIN(SWAP13)
  724. CASE_BEGIN(SWAP14)
  725. CASE_BEGIN(SWAP15)
  726. CASE_BEGIN(SWAP16)
  727. {
  728. ON_OP();
  729. updateIOGas();
  730. unsigned n = (unsigned)m_op - (unsigned)Instruction::SWAP1 + 2;
  731. u256 d = *m_sp;
  732. *m_sp = m_stack[(1 + m_sp - m_stack) - n];
  733. m_stack[(1 + m_sp - m_stack) - n] = d;
  734. ++m_pc;
  735. }
  736. CASE_END
  737. CASE_BEGIN(SLOAD)
  738. m_runGas = toUint64(m_schedule->sloadGas);
  739. ON_OP();
  740. updateIOGas();
  741. *m_sp = m_ext->store(*m_sp);
  742. ++m_pc;
  743. CASE_END
  744. CASE_BEGIN(SSTORE)
  745. if (!m_ext->store(*m_sp) && *(m_sp - 1))
  746. m_runGas = toUint64(m_schedule->sstoreSetGas);
  747. else if (m_ext->store(*m_sp) && !*(m_sp - 1))
  748. {
  749. m_runGas = toUint64(m_schedule->sstoreResetGas);
  750. m_ext->sub.refunds += m_schedule->sstoreRefundGas;
  751. }
  752. else
  753. m_runGas = toUint64(m_schedule->sstoreResetGas);
  754. ON_OP();
  755. updateIOGas();
  756. m_ext->setStore(*m_sp, *(m_sp - 1));
  757. m_sp -= 2;
  758. ++m_pc;
  759. CASE_END
  760. CASE_BEGIN(PC)
  761. ON_OP();
  762. updateIOGas();
  763. *++m_sp = m_pc;
  764. ++m_pc;
  765. CASE_END
  766. CASE_BEGIN(MSIZE)
  767. ON_OP();
  768. updateIOGas();
  769. *++m_sp = m_mem.size();
  770. ++m_pc;
  771. CASE_END
  772. CASE_BEGIN(GAS)
  773. ON_OP();
  774. updateIOGas();
  775. *++m_sp = m_io_gas;
  776. ++m_pc;
  777. CASE_END
  778. CASE_BEGIN(JUMPDEST)
  779. m_runGas = 1;
  780. ON_OP();
  781. updateIOGas();
  782. ++m_pc;
  783. CASE_END
  784. CASE_BEGIN(BAD)
  785. throwBadInstruction();
  786. CASE_END
  787. CASE_DEFAULT
  788. throwBadInstruction();
  789. CASE_END
  790. END_CASES
  791. }