Fabcoin Core  0.16.2
P2P Digital Currency
VM.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include <libethereum/ExtVM.h>
23 #include "VMConfig.h"
24 #include "VM.h"
25 using namespace std;
26 using namespace dev;
27 using namespace dev::eth;
28 
29 
30 uint64_t VM::memNeed(u256 _offset, u256 _size)
31 {
32  return toInt63(_size ? u512(_offset) + _size : u512(0));
33 }
34 
35 template <class S> S divWorkaround(S const& _a, S const& _b)
36 {
37  return (S)(s512(_a) / s512(_b));
38 }
39 
40 template <class S> S modWorkaround(S const& _a, S const& _b)
41 {
42  return (S)(s512(_a) % s512(_b));
43 }
44 
45 
46 //
47 // for decoding destinations of JUMPTO, JUMPV, JUMPSUB and JUMPSUBV
48 //
49 
50 uint64_t VM::decodeJumpDest(const byte* const _code, uint64_t& _pc)
51 {
52  // turn 4 MSB-first bytes in the code into a native-order integer
53  uint64_t dest = _code[_pc++];
54  dest = (dest << 8) | _code[_pc++];
55  dest = (dest << 8) | _code[_pc++];
56  dest = (dest << 8) | _code[_pc++];
57  return dest;
58 }
59 
60 uint64_t VM::decodeJumpvDest(const byte* const _code, uint64_t& _pc, u256*& _sp)
61 {
62  // Layout of jump table in bytecode...
63  // byte opcode
64  // byte n_jumps
65  // byte table[n_jumps][4]
66  //
67  uint64_t i = uint64_t(*_sp--); // byte on stack indexes into jump table
68  uint64_t pc = _pc;
69  byte n = _code[++pc]; // byte after opcode is number of jumps
70  if (i >= n) i = n - 1; // if index overflow use default jump
71  pc += i * 4; // adjust pc to index destination in table
72 
73  uint64_t dest = decodeJumpDest(_code, pc);
74 
75  _pc += 1 + n * 4; // adust input _pc to opcode after table
76  return dest;
77 }
78 
79 
80 //
81 // for tracing, checking, metering, measuring ...
82 //
83 void VM::onOperation()
84 {
85  if (m_onOp)
86  (m_onOp)(++m_nSteps, m_PC, m_OP,
87  m_newMemSize > m_mem.size() ? (m_newMemSize - m_mem.size()) / 32 : uint64_t(0),
88  m_runGas, m_io_gas, this, m_ext);
89 }
90 
91 void VM::checkStack(unsigned _removed, unsigned _added)
92 {
93  int const size = 1 + m_SP - m_stack;
94  int const usedSize = size - _removed;
95  if (usedSize < 0 || usedSize + _added > 1024)
96  throwBadStack(size, _removed, _added);
97 }
98 
99 uint64_t VM::gasForMem(u512 _size)
100 {
101  u512 s = _size / 32;
102  return toInt63((u512)m_schedule->memoryGas * s + s * s / m_schedule->quadCoeffDiv);
103 }
104 
105 void VM::updateIOGas()
106 {
107  if (m_io_gas < m_runGas)
108  throwOutOfGas();
109  m_io_gas -= m_runGas;
110 }
111 
112 void VM::updateGas()
113 {
114  if (m_newMemSize > m_mem.size())
115  m_runGas += toInt63(gasForMem(m_newMemSize) - gasForMem(m_mem.size()));
116  m_runGas += (m_schedule->copyGas * ((m_copyMemSize + 31) / 32));
117  if (m_io_gas < m_runGas)
118  throwOutOfGas();
119 }
120 
121 void VM::updateMem()
122 {
123  m_newMemSize = (m_newMemSize + 31) / 32 * 32;
124  updateGas();
125  if (m_newMemSize > m_mem.size())
126  m_mem.resize(m_newMemSize);
127 }
128 
129 void VM::logGasMem()
130 {
131  unsigned n = (unsigned)m_OP - (unsigned)Instruction::LOG0;
132  m_runGas = toInt63(m_schedule->logGas + m_schedule->logTopicGas * n + u512(m_schedule->logDataGas) * *(m_SP - 1));
133  m_newMemSize = memNeed(*m_SP, *(m_SP - 1));
134  updateMem();
135 }
136 
137 void VM::fetchInstruction()
138 {
139  m_OP = Instruction(m_code[m_PC]);
140  const InstructionMetric& metric = c_metrics[static_cast<size_t>(m_OP)];
141  checkStack(metric.args, metric.ret);
142 
143  // FEES...
144  m_runGas = toInt63(m_schedule->tierStepGas[static_cast<unsigned>(metric.gasPriceTier)]);
145  m_newMemSize = m_mem.size();
146  m_copyMemSize = 0;
147 }
148 
149 #if EVM_HACK_ON_OPERATION
150  #define onOperation()
151 #endif
152 #if EVM_HACK_UPDATE_IO_GAS
153  #define updateIOGas()
154 #endif
155 #if EVM_HACK_STACK
156  #define checkStack(r,a)
157 #endif
158 
160 //
161 // interpreter entry point
162 
163 owning_bytes_ref VM::exec(u256& _io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp)
164 {
165  io_gas = &_io_gas;
166  m_io_gas = uint64_t(_io_gas);
167  m_ext = &_ext;
168  m_schedule = &m_ext->evmSchedule();
169  m_onOp = _onOp;
170  m_onFail = &VM::onOperation;
171 
172  try
173  {
174  // trampoline to minimize depth of call stack when calling out
175  m_bounce = &VM::initEntry;
176  do
177  (this->*m_bounce)();
178  while (m_bounce);
179 
180  }
181  catch (...)
182  {
183  *io_gas = m_io_gas;
184  throw;
185  }
186 
187  *io_gas = m_io_gas;
188  return std::move(m_output);
189 }
190 
191 //
192 // main interpreter loop and switch
193 //
194 void VM::interpretCases()
195 {
196  INIT_CASES
197  DO_CASES
198  {
199  //
200  // Call-related instructions
201  //
202 
203  CASE(CREATE)
204  {
205  m_bounce = &VM::caseCreate;
206  }
207  BREAK;
208 
209  CASE(DELEGATECALL)
210 
211  // Pre-homestead
212  if (!m_schedule->haveDelegateCall)
213  throwBadInstruction();
214 
215  CASE(CALL)
216  CASE(CALLCODE)
217  {
218  m_bounce = &VM::caseCall;
219  }
220  BREAK
221 
222  CASE(RETURN)
223  {
224  m_newMemSize = memNeed(*m_SP, *(m_SP - 1));
225  updateMem();
226  ON_OP();
227  updateIOGas();
228 
229  size_t b = (size_t)*m_SP--;
230  size_t s = (size_t)*m_SP--;
231  m_output = owning_bytes_ref{std::move(m_mem), b, s};
232  m_bounce = 0;
233  }
234  BREAK
235 
236  CASE(SUICIDE)
237  {
238  m_runGas = toInt63(m_schedule->suicideGas);
239  Address dest = asAddress(*m_SP);
240 
241  // After EIP158 zero-value suicides do not have to pay account creation gas.
242  if (m_ext->balance(m_ext->myAddress) > 0 || m_schedule->zeroValueTransferChargesNewAccountGas())
243  // After EIP150 hard fork charge additional cost of sending
244  // ethers to non-existing account.
245  if (m_schedule->suicideChargesNewAccountGas() && !m_ext->exists(dest))
246  m_runGas += m_schedule->callNewAccountGas;
247 
248  ON_OP();
249  updateIOGas();
250  m_ext->suicide(dest);
251  m_bounce = 0;
252  }
253  BREAK
254 
255  CASE(STOP)
256  {
257  ON_OP();
258  updateIOGas();
259  m_bounce = 0;
260  }
261  BREAK;
262 
263 
264  //
265  // instructions potentially expanding memory
266  //
267 
268  CASE(MLOAD)
269  {
270  m_newMemSize = toInt63(*m_SP) + 32;
271  updateMem();
272  ON_OP();
273  updateIOGas();
274 
275  *m_SP = (u256)*(h256 const*)(m_mem.data() + (unsigned)*m_SP);
276  }
277  NEXT
278 
279  CASE(MSTORE)
280  {
281  m_newMemSize = toInt63(*m_SP) + 32;
282  updateMem();
283  ON_OP();
284  updateIOGas();
285 
286  *(h256*)&m_mem[(unsigned)*m_SP] = (h256)*(m_SP - 1);
287  m_SP -= 2;
288  }
289  NEXT
290 
291  CASE(MSTORE8)
292  {
293  m_newMemSize = toInt63(*m_SP) + 1;
294  updateMem();
295  ON_OP();
296  updateIOGas();
297 
298  m_mem[(unsigned)*m_SP] = (byte)(*(m_SP - 1) & 0xff);
299  m_SP -= 2;
300  }
301  NEXT
302 
303  CASE(SHA3)
304  {
305  m_runGas = toInt63(m_schedule->sha3Gas + (u512(*(m_SP - 1)) + 31) / 32 * m_schedule->sha3WordGas);
306  m_newMemSize = memNeed(*m_SP, *(m_SP - 1));
307  updateMem();
308  ON_OP();
309  updateIOGas();
310 
311  uint64_t inOff = (uint64_t)*m_SP--;
312  uint64_t inSize = (uint64_t)*m_SP--;
313  *++m_SP = (u256)sha3(bytesConstRef(m_mem.data() + inOff, inSize));
314  }
315  NEXT
316 
317  CASE(LOG0)
318  {
319  logGasMem();
320  ON_OP();
321  updateIOGas();
322 
323  m_ext->log({}, bytesConstRef(m_mem.data() + (uint64_t)*m_SP, (uint64_t)*(m_SP - 1)));
324  m_SP -= 2;
325  }
326  NEXT
327 
328  CASE(LOG1)
329  {
330  logGasMem();
331  ON_OP();
332  updateIOGas();
333 
334  m_ext->log({*(m_SP - 2)}, bytesConstRef(m_mem.data() + (uint64_t)*m_SP, (uint64_t)*(m_SP - 1)));
335  m_SP -= 3;
336  }
337  NEXT
338 
339  CASE(LOG2)
340  {
341  logGasMem();
342  ON_OP();
343  updateIOGas();
344 
345  m_ext->log({*(m_SP - 2), *(m_SP-3)}, bytesConstRef(m_mem.data() + (uint64_t)*m_SP, (uint64_t)*(m_SP - 1)));
346  m_SP -= 4;
347  }
348  NEXT
349 
350  CASE(LOG3)
351  {
352  logGasMem();
353  ON_OP();
354  updateIOGas();
355 
356  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)));
357  m_SP -= 5;
358  }
359  NEXT
360 
361  CASE(LOG4)
362  {
363  logGasMem();
364  ON_OP();
365  updateIOGas();
366 
367  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)));
368  m_SP -= 6;
369  }
370  NEXT
371 
372  CASE(EXP)
373  {
374  u256 expon = *(m_SP - 1);
375  m_runGas = toInt63(m_schedule->expGas + m_schedule->expByteGas * (32 - (h256(expon).firstBitSet() / 8)));
376  ON_OP();
377  updateIOGas();
378 
379  u256 base = *m_SP--;
380  *m_SP = exp256(base, expon);
381  }
382  NEXT
383 
384  //
385  // ordinary instructions
386  //
387 
388  CASE(ADD)
389  {
390  ON_OP();
391  updateIOGas();
392 
393  //pops two items and pushes S[-1] + S[-2] mod 2^256.
394  *(m_SP - 1) += *m_SP;
395  --m_SP;
396  }
397  NEXT
398 
399  CASE(MUL)
400  {
401  ON_OP();
402  updateIOGas();
403 
404  //pops two items and pushes S[-1] * S[-2] mod 2^256.
405 #if EVM_HACK_MUL_64
406  *(uint64_t*)(m_SP - 1) *= *(uint64_t*)m_SP;
407 #else
408  *(m_SP - 1) *= *m_SP;
409 #endif
410  --m_SP;
411  }
412  NEXT
413 
414  CASE(SUB)
415  {
416  ON_OP();
417  updateIOGas();
418 
419  *(m_SP - 1) = *m_SP - *(m_SP - 1);
420  --m_SP;
421  }
422  NEXT
423 
424  CASE(DIV)
425  {
426  ON_OP();
427  updateIOGas();
428 
429  *(m_SP - 1) = *(m_SP - 1) ? divWorkaround(*m_SP, *(m_SP - 1)) : 0;
430  --m_SP;
431  }
432  NEXT
433 
434  CASE(SDIV)
435  {
436  ON_OP();
437  updateIOGas();
438 
439  *(m_SP - 1) = *(m_SP - 1) ? s2u(divWorkaround(u2s(*m_SP), u2s(*(m_SP - 1)))) : 0;
440  --m_SP;
441  }
442  NEXT
443 
444  CASE(MOD)
445  {
446  ON_OP();
447  updateIOGas();
448 
449  *(m_SP - 1) = *(m_SP - 1) ? modWorkaround(*m_SP, *(m_SP - 1)) : 0;
450  --m_SP;
451  }
452  NEXT
453 
454  CASE(SMOD)
455  {
456  ON_OP();
457  updateIOGas();
458 
459  *(m_SP - 1) = *(m_SP - 1) ? s2u(modWorkaround(u2s(*m_SP), u2s(*(m_SP - 1)))) : 0;
460  --m_SP;
461  }
462  NEXT
463 
464  CASE(NOT)
465  {
466  ON_OP();
467  updateIOGas();
468 
469  *m_SP = ~*m_SP;
470  }
471  NEXT
472 
473  CASE(LT)
474  {
475  ON_OP();
476  updateIOGas();
477 
478  *(m_SP - 1) = *m_SP < *(m_SP - 1) ? 1 : 0;
479  --m_SP;
480  }
481  NEXT
482 
483  CASE(GT)
484  {
485  ON_OP();
486  updateIOGas();
487 
488  *(m_SP - 1) = *m_SP > *(m_SP - 1) ? 1 : 0;
489  --m_SP;
490  }
491  NEXT
492 
493  CASE(SLT)
494  {
495  ON_OP();
496  updateIOGas();
497 
498  *(m_SP - 1) = u2s(*m_SP) < u2s(*(m_SP - 1)) ? 1 : 0;
499  --m_SP;
500  }
501  NEXT
502 
503  CASE(SGT)
504  {
505  ON_OP();
506  updateIOGas();
507 
508  *(m_SP - 1) = u2s(*m_SP) > u2s(*(m_SP - 1)) ? 1 : 0;
509  --m_SP;
510  }
511  NEXT
512 
513  CASE(EQ)
514  {
515  ON_OP();
516  updateIOGas();
517 
518  *(m_SP - 1) = *m_SP == *(m_SP - 1) ? 1 : 0;
519  --m_SP;
520  }
521  NEXT
522 
523  CASE(ISZERO)
524  {
525  ON_OP();
526  updateIOGas();
527 
528  *m_SP = *m_SP ? 0 : 1;
529  }
530  NEXT
531 
532  CASE(AND)
533  {
534  ON_OP();
535  updateIOGas();
536 
537  *(m_SP - 1) = *m_SP & *(m_SP - 1);
538  --m_SP;
539  }
540  NEXT
541 
542  CASE(OR)
543  {
544  ON_OP();
545  updateIOGas();
546 
547  *(m_SP - 1) = *m_SP | *(m_SP - 1);
548  --m_SP;
549  }
550  NEXT
551 
552  CASE(XOR)
553  {
554  ON_OP();
555  updateIOGas();
556 
557  *(m_SP - 1) = *m_SP ^ *(m_SP - 1);
558  --m_SP;
559  }
560  NEXT
561 
562  CASE(BYTE)
563  {
564  ON_OP();
565  updateIOGas();
566 
567  *(m_SP - 1) = *m_SP < 32 ? (*(m_SP - 1) >> (unsigned)(8 * (31 - *m_SP))) & 0xff : 0;
568  --m_SP;
569  }
570  NEXT
571 
572  CASE(ADDMOD)
573  {
574  ON_OP();
575  updateIOGas();
576 
577  *(m_SP - 2) = *(m_SP - 2) ? u256((u512(*m_SP) + u512(*(m_SP - 1))) % *(m_SP - 2)) : 0;
578  m_SP -= 2;
579  }
580  NEXT
581 
582  CASE(MULMOD)
583  {
584  ON_OP();
585  updateIOGas();
586 
587  *(m_SP - 2) = *(m_SP - 2) ? u256((u512(*m_SP) * u512(*(m_SP - 1))) % *(m_SP - 2)) : 0;
588  m_SP -= 2;
589  }
590  NEXT
591 
592  CASE(SIGNEXTEND)
593  {
594  ON_OP();
595  updateIOGas();
596 
597  if (*m_SP < 31)
598  {
599  unsigned testBit = static_cast<unsigned>(*m_SP) * 8 + 7;
600  u256& number = *(m_SP - 1);
601  u256 mask = ((u256(1) << testBit) - 1);
602  if (boost::multiprecision::bit_test(number, testBit))
603  number |= ~mask;
604  else
605  number &= mask;
606  }
607  --m_SP;
608  }
609  NEXT
610 
611  CASE(ADDRESS)
612  {
613  ON_OP();
614  updateIOGas();
615 
616  *++m_SP = fromAddress(m_ext->myAddress);
617  }
618  NEXT
619 
620  CASE(ORIGIN)
621  {
622  ON_OP();
623  updateIOGas();
624 
625  *++m_SP = fromAddress(m_ext->origin);
626  }
627  NEXT
628 
629  CASE(BALANCE)
630  {
631  m_runGas = toInt63(m_schedule->balanceGas);
632  ON_OP();
633  updateIOGas();
634 
635  *m_SP = m_ext->balance(asAddress(*m_SP));
636  }
637  NEXT
638 
639 
640  CASE(CALLER)
641  {
642  ON_OP();
643  updateIOGas();
644 
645  *++m_SP = fromAddress(m_ext->caller);
646  }
647  NEXT
648 
649  CASE(CALLVALUE)
650  {
651  ON_OP();
652  updateIOGas();
653 
654  *++m_SP = m_ext->value;
655  }
656  NEXT
657 
658 
659  CASE(CALLDATALOAD)
660  {
661  ON_OP();
662  updateIOGas();
663 
664  if (u512(*m_SP) + 31 < m_ext->data.size())
665  *m_SP = (u256)*(h256 const*)(m_ext->data.data() + (size_t)*m_SP);
666  else if (*m_SP >= m_ext->data.size())
667  *m_SP = u256(0);
668  else
669  {
670  h256 r;
671  for (uint64_t i = (uint64_t)*m_SP, e = (uint64_t)*m_SP + (uint64_t)32, j = 0; i < e; ++i, ++j)
672  r[j] = i < m_ext->data.size() ? m_ext->data[i] : 0;
673  *m_SP = (u256)r;
674  }
675  }
676  NEXT
677 
678 
679  CASE(CALLDATASIZE)
680  {
681  ON_OP();
682  updateIOGas();
683 
684  *++m_SP = m_ext->data.size();
685  }
686  NEXT
687 
688  CASE(CODESIZE)
689  {
690  ON_OP();
691  updateIOGas();
692 
693  *++m_SP = m_ext->code.size();
694  }
695  NEXT
696 
697  CASE(EXTCODESIZE)
698  {
699  m_runGas = toInt63(m_schedule->extcodesizeGas);
700  ON_OP();
701  updateIOGas();
702 
703  *m_SP = m_ext->codeSizeAt(asAddress(*m_SP));
704  }
705  NEXT
706 
707  CASE(CALLDATACOPY)
708  {
709  m_copyMemSize = toInt63(*(m_SP - 2));
710  m_newMemSize = memNeed(*m_SP, *(m_SP - 2));
711  updateMem();
712  ON_OP();
713  updateIOGas();
714 
715  copyDataToMemory(m_ext->data, m_SP);
716  }
717  NEXT
718 
719  CASE(CODECOPY)
720  {
721  m_copyMemSize = toInt63(*(m_SP - 2));
722  m_newMemSize = memNeed(*m_SP, *(m_SP - 2));
723  updateMem();
724  ON_OP();
725  updateIOGas();
726 
727  copyDataToMemory(&m_ext->code, m_SP);
728  }
729  NEXT
730 
731  CASE(EXTCODECOPY)
732  {
733  m_runGas = toInt63(m_schedule->extcodecopyGas);
734  m_copyMemSize = toInt63(*(m_SP - 3));
735  m_newMemSize = memNeed(*(m_SP - 1), *(m_SP - 3));
736  updateMem();
737  ON_OP();
738  updateIOGas();
739 
740  Address a = asAddress(*m_SP);
741  --m_SP;
742  copyDataToMemory(&m_ext->codeAt(a), m_SP);
743  }
744  NEXT
745 
746 
747  CASE(GASPRICE)
748  {
749  ON_OP();
750  updateIOGas();
751 
752  *++m_SP = m_ext->gasPrice;
753  }
754  NEXT
755 
756  CASE(BLOCKHASH)
757  {
758  ON_OP();
759  updateIOGas();
760 
761  *m_SP = (u256)m_ext->blockHash(*m_SP);
762  }
763  NEXT
764 
765  CASE(COINBASE)
766  {
767  ON_OP();
768  updateIOGas();
769 
770  *++m_SP = (u160)m_ext->envInfo().author();
771  }
772  NEXT
773 
774  CASE(TIMESTAMP)
775  {
776  ON_OP();
777  updateIOGas();
778 
779  *++m_SP = m_ext->envInfo().timestamp();
780  }
781  NEXT
782 
783  CASE(NUMBER)
784  {
785  ON_OP();
786  updateIOGas();
787 
788  *++m_SP = m_ext->envInfo().number();
789  }
790  NEXT
791 
792  CASE(DIFFICULTY)
793  {
794  ON_OP();
795  updateIOGas();
796 
797  *++m_SP = m_ext->envInfo().difficulty();
798  }
799  NEXT
800 
801  CASE(GASLIMIT)
802  {
803  ON_OP();
804  updateIOGas();
805 
806  *++m_SP = m_ext->envInfo().gasLimit();
807  }
808  NEXT
809 
810  CASE(POP)
811  {
812  ON_OP();
813  updateIOGas();
814 
815  --m_SP;
816  }
817  NEXT
818 
819  CASE(PUSHC)
820  {
821 #ifdef EVM_USE_CONSTANT_POOL
822  ON_OP();
823  updateIOGas();
824 
825  ++m_PC;
826  *++m_SP = m_pool[m_code[m_PC]];
827  ++m_PC;
828  m_PC += m_code[m_PC];
829 #else
830  throwBadInstruction();
831 #endif
832  }
833  CONTINUE
834 
835  CASE(PUSH1)
836  {
837  ON_OP();
838  updateIOGas();
839  *++m_SP = m_code[++m_PC];
840  ++m_PC;
841  }
842  CONTINUE
843 
844  CASE(PUSH2)
845  CASE(PUSH3)
846  CASE(PUSH4)
847  CASE(PUSH5)
848  CASE(PUSH6)
849  CASE(PUSH7)
850  CASE(PUSH8)
851  CASE(PUSH9)
852  CASE(PUSH10)
853  CASE(PUSH11)
854  CASE(PUSH12)
855  CASE(PUSH13)
856  CASE(PUSH14)
857  CASE(PUSH15)
858  CASE(PUSH16)
859  CASE(PUSH17)
860  CASE(PUSH18)
861  CASE(PUSH19)
862  CASE(PUSH20)
863  CASE(PUSH21)
864  CASE(PUSH22)
865  CASE(PUSH23)
866  CASE(PUSH24)
867  CASE(PUSH25)
868  CASE(PUSH26)
869  CASE(PUSH27)
870  CASE(PUSH28)
871  CASE(PUSH29)
872  CASE(PUSH30)
873  CASE(PUSH31)
874  CASE(PUSH32)
875  {
876  ON_OP();
877  updateIOGas();
878 
879  int numBytes = (int)m_OP - (int)Instruction::PUSH1 + 1;
880  *++m_SP = 0;
881  // Construct a number out of PUSH bytes.
882  // This requires the code has been copied and extended by 32 zero
883  // bytes to handle "out of code" push data here.
884  for (++m_PC; numBytes--; ++m_PC)
885  *m_SP = (*m_SP << 8) | m_code[m_PC];
886  }
887  CONTINUE
888 
889  CASE(JUMP)
890  {
891  ON_OP();
892  updateIOGas();
893 
894  m_PC = verifyJumpDest(*m_SP);
895  --m_SP;
896  }
897  CONTINUE
898 
899  CASE(JUMPI)
900  {
901  ON_OP();
902  updateIOGas();
903  if (*(m_SP - 1))
904  m_PC = verifyJumpDest(*m_SP);
905  else
906  ++m_PC;
907  m_SP -= 2;
908  }
909  CONTINUE
910 
911 #if EVM_JUMPS_AND_SUBS
912  CASE(JUMPTO)
913  {
914  ON_OP();
915  updateIOGas();
916  m_PC = decodeJumpDest(m_code, m_PC);
917  }
918  CONTINUE
919 
920  CASE(JUMPIF)
921  {
922  ON_OP();
923  updateIOGas();
924  if (*m_SP)
925  m_PC = decodeJumpDest(m_code, m_PC);
926  else
927  ++m_PC;
928  --m_SP;
929  }
930  CONTINUE
931 
932  CASE(JUMPV)
933  {
934  ON_OP();
935  updateIOGas();
936  m_PC = decodeJumpvDest(m_code, m_PC, m_SP);
937  }
938  CONTINUE
939 
940  CASE(JUMPSUB)
941  {
942  ON_OP();
943  updateIOGas();
944  {
945  *++m_RP = m_PC;
946  m_PC = decodeJumpDest(m_code, m_PC);
947  }
948  }
949  CONTINUE
950 
951  CASE(JUMPSUBV)
952  {
953  ON_OP();
954  updateIOGas();
955  {
956  *++m_RP = m_PC;
957  m_PC = decodeJumpDest(m_code, m_PC);
958  }
959  }
960  CONTINUE
961 
962  CASE(RETURNSUB)
963  {
964  ON_OP();
965  updateIOGas();
966 
967  m_PC = *m_RP--;
968  }
969  NEXT
970 #else
971  CASE(JUMPTO)
972  CASE(JUMPIF)
973  CASE(JUMPV)
974  CASE(JUMPSUB)
975  CASE(JUMPSUBV)
976  CASE(RETURNSUB)
977  {
978  throwBadInstruction();
979  }
980  CONTINUE
981 #endif
982 
983  CASE(JUMPC)
984  {
985 #ifdef EVM_REPLACE_CONST_JUMP
986  ON_OP();
987  updateIOGas();
988 
989  m_PC = uint64_t(*m_SP);
990  --m_SP;
991 #else
992  throwBadInstruction();
993 #endif
994  }
995  CONTINUE
996 
997  CASE(JUMPCI)
998  {
999 #ifdef EVM_REPLACE_CONST_JUMP
1000  ON_OP();
1001  updateIOGas();
1002 
1003  if (*(m_SP - 1))
1004  m_PC = uint64_t(*m_SP);
1005  else
1006  ++m_PC;
1007  m_SP -= 2;
1008 #else
1009  throwBadInstruction();
1010 #endif
1011  }
1012  CONTINUE
1013 
1014  CASE(DUP1)
1015  CASE(DUP2)
1016  CASE(DUP3)
1017  CASE(DUP4)
1018  CASE(DUP5)
1019  CASE(DUP6)
1020  CASE(DUP7)
1021  CASE(DUP8)
1022  CASE(DUP9)
1023  CASE(DUP10)
1024  CASE(DUP11)
1025  CASE(DUP12)
1026  CASE(DUP13)
1027  CASE(DUP14)
1028  CASE(DUP15)
1029  CASE(DUP16)
1030  {
1031  ON_OP();
1032  updateIOGas();
1033 
1034  unsigned n = 1 + (unsigned)m_OP - (unsigned)Instruction::DUP1;
1035 #if EVM_HACK_DUP_64
1036  *(uint64_t*)(m_SP+1) = *(uint64_t*)&m_stack[(1 + m_SP - m_stack) - n];
1037 #else
1038  *(m_SP+1) = m_stack[(1 + m_SP - m_stack) - n];
1039 #endif
1040  ++m_SP;
1041  }
1042  NEXT
1043 
1044 
1045  CASE(SWAP1)
1046  CASE(SWAP2)
1047  CASE(SWAP3)
1048  CASE(SWAP4)
1049  CASE(SWAP5)
1050  CASE(SWAP6)
1051  CASE(SWAP7)
1052  CASE(SWAP8)
1053  CASE(SWAP9)
1054  CASE(SWAP10)
1055  CASE(SWAP11)
1056  CASE(SWAP12)
1057  CASE(SWAP13)
1058  CASE(SWAP14)
1059  CASE(SWAP15)
1060  CASE(SWAP16)
1061  {
1062  ON_OP();
1063  updateIOGas();
1064 
1065  unsigned n = (unsigned)m_OP - (unsigned)Instruction::SWAP1 + 2;
1066  u256 d = *m_SP;
1067  *m_SP = m_stack[(1 + m_SP - m_stack) - n];
1068  m_stack[(1 + m_SP - m_stack) - n] = d;
1069  }
1070  NEXT
1071 
1072 
1073  CASE(SLOAD)
1074  {
1075  m_runGas = toInt63(m_schedule->sloadGas);
1076  ON_OP();
1077  updateIOGas();
1078 
1079  *m_SP = m_ext->store(*m_SP);
1080  }
1081  NEXT
1082 
1083  CASE(SSTORE)
1084  {
1085  if (!m_ext->store(*m_SP) && *(m_SP - 1))
1086  m_runGas = toInt63(m_schedule->sstoreSetGas);
1087  else if (m_ext->store(*m_SP) && !*(m_SP - 1))
1088  {
1089  m_runGas = toInt63(m_schedule->sstoreResetGas);
1090  m_ext->sub.refunds += m_schedule->sstoreRefundGas;
1091  }
1092  else
1093  m_runGas = toInt63(m_schedule->sstoreResetGas);
1094  ON_OP();
1095  updateIOGas();
1096 
1097  m_ext->setStore(*m_SP, *(m_SP - 1));
1098  m_SP -= 2;
1099  }
1100  NEXT
1101 
1102  CASE(PC)
1103  {
1104  ON_OP();
1105  updateIOGas();
1106 
1107  *++m_SP = m_PC;
1108  }
1109  NEXT
1110 
1111  CASE(MSIZE)
1112  {
1113  ON_OP();
1114  updateIOGas();
1115 
1116  *++m_SP = m_mem.size();
1117  }
1118  NEXT
1119 
1120  CASE(GAS)
1121  {
1122  ON_OP();
1123  updateIOGas();
1124 
1125  *++m_SP = m_io_gas;
1126  }
1127  NEXT
1128 
1129  CASE(JUMPDEST)
1130  {
1131  m_runGas = 1;
1132  ON_OP();
1133  updateIOGas();
1134  }
1135  NEXT
1136 
1137 #if EVM_JUMPS_AND_SUBS
1138  CASE(BEGINSUB)
1139  {
1140  m_runGas = 1;
1141  ON_OP();
1142  updateIOGas();
1143  }
1144  NEXT
1145 #else
1146  CASE(BEGINSUB)
1147 #endif
1148  CASE(BEGINDATA)
1149  CASE(BAD)
1150  DEFAULT
1151  throwBadInstruction();
1152  }
1153  WHILE_CASES
1154 }
#define CASE(name)
Definition: VMConfig.h:133
set a potential jumpsub destination
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
return to subroutine jumped from
uint8_t byte
Definition: Common.h:57
#define OR
Definition: lubyrack.h:61
#define MUL(a, b)
Definition: idea.cpp:154
SHA3 message digest base class.
Definition: sha3.h:28
alter the program counter to a beginsub
conditionally alter the program counter
S divWorkaround(S const &_a, S const &_b)
Definition: VM.cpp:35
std::hash for asio::adress
Definition: Common.h:323
Instruction
Virtual machine bytecode instruction.
Definition: Instruction.h:16
u256 fromAddress(Address _a)
Definition: VM.h:44
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 160, 160, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u160
Definition: Common.h:127
#define LT(i, a, b, c, d, e)
Definition: serpentp.h:6
#define CONTINUE
Definition: VMConfig.h:135
conditionally alter the program counter - pre-verified
#define a(i)
alter the program counter - pre-verified
#define ON_OP()
Definition: VMConfig.h:108
#define MSIZE
Definition: lockedpool.cpp:14
#define DEFAULT
Definition: VMConfig.h:137
vector_ref< byte const > bytesConstRef
Definition: Common.h:77
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 512, 512, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u512
Definition: Common.h:129
std::function< void(uint64_t, uint64_t, Instruction, bigint, bigint, bigint, VM *, ExtVMFace const *)> OnOpFunc
Definition: ExtVMFace.h:193
FixedHash< 32 > h256
Definition: FixedHash.h:340
#define DO_CASES
Definition: VMConfig.h:132
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
#define b(i, j)
#define NEXT
Definition: VMConfig.h:134
#define INIT_CASES
Definition: VMConfig.h:131
alter the program counter to a beginsub
#define WHILE_CASES
Definition: VMConfig.h:138
Interface and null implementation of the class for specifying VM externalities.
Definition: ExtVMFace.h:265
s256 u2s(u256 _u)
Interprets _u as a two&#39;s complement signed number and returns the resulting s256. ...
Definition: Common.h:159
S modWorkaround(S const &_a, S const &_b)
Definition: VM.cpp:40
#define BREAK
Definition: VMConfig.h:136
Reference to a slice of buffer that also owns the buffer.
Definition: ExtVMFace.h:56
uint8_t const size_t const size
Definition: sha3.h:20
u256 s2u(s256 _u)
Definition: Common.h:169
virtual EVMSchedule const & evmSchedule() const
Return the EVM gas-price schedule for this execution context.
Definition: ExtVMFace.h:316
Address asAddress(u256 _item)
Definition: VM.h:39
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
alter the program counter to a jumpdest
placed to force invalid instruction exception
#define e(i)
Definition: sha.cpp:733
#define d(i)
Definition: sha.cpp:732
#define S(a)
Definition: mars.cpp:50
alter the program counter to a jumpdest
push value from constant pool
begine the data section
#define EXP(x)
Definition: safer.cpp:50
uint8_t const * data
Definition: sha3.h:19
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 512, 512, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void >> s512
Definition: Common.h:130