VM.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <unordered_map>
  20. #include <libdevcore/Exceptions.h>
  21. #include <libethcore/Common.h>
  22. #include <libevmcore/Instruction.h>
  23. #include <libdevcore/SHA3.h>
  24. #include <libethcore/BlockHeader.h>
  25. #include "VMFace.h"
  26. namespace dev
  27. {
  28. namespace eth
  29. {
  30. // Convert from a 256-bit integer stack/memory entry into a 160-bit Address hash.
  31. // Currently we just pull out the right (low-order in BE) 160-bits.
  32. inline Address asAddress(u256 _item)
  33. {
  34. return right160(h256(_item));
  35. }
  36. inline u256 fromAddress(Address _a)
  37. {
  38. return (u160)_a;
  39. }
  40. struct InstructionMetric
  41. {
  42. int gasPriceTier;
  43. int args;
  44. int ret;
  45. };
  46. /**
  47. */
  48. class VM: public VMFace
  49. {
  50. public:
  51. virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp) override final;
  52. bytes const& memory() const { return m_mem; }
  53. u256s stack() const { assert(m_stack <= m_sp + 1); return u256s(m_stack, m_sp + 1); };
  54. VM(): m_stackSpace(1025), m_stack(m_stackSpace.data() + 1), m_pool(256) {};
  55. private:
  56. u256* io_gas = 0;
  57. uint64_t m_io_gas = 0;
  58. ExtVMFace* m_ext = 0;
  59. OnOpFunc m_onOp;
  60. static std::array<InstructionMetric, 256> c_metrics;
  61. static void initMetrics();
  62. static u256 exp256(u256 _base, u256 _exponent);
  63. void copyCode();
  64. const void* const* c_jumpTable = 0;
  65. bool m_caseInit = false;
  66. typedef void (VM::*MemFnPtr)();
  67. MemFnPtr m_bounce = 0;
  68. MemFnPtr m_onFail = 0;
  69. uint64_t m_nSteps = 0;
  70. EVMSchedule const* m_schedule = nullptr;
  71. // return bytes
  72. bytesConstRef m_bytes = bytesConstRef();
  73. // space for memory
  74. bytes m_mem;
  75. // space for code and pointer to data
  76. bytes m_codeSpace;
  77. byte* m_code;
  78. // space for stack and pointer to data
  79. u256s m_stackSpace;
  80. u256* m_stack;
  81. // constant pool
  82. u256s m_pool;
  83. // interpreter state
  84. uint64_t m_pc = 0;
  85. u256* m_sp = m_stack - 1;
  86. Instruction m_op;
  87. // metering and memory state
  88. uint64_t m_runGas = 0;
  89. uint64_t m_newMemSize = 0;
  90. uint64_t m_copyMemSize = 0;
  91. // initialize interpreter
  92. void initEntry();
  93. void optimize();
  94. // interpreter loop & switch
  95. void interpretCases();
  96. // interpreter cases that call out
  97. void caseCreate();
  98. bool caseCallSetup(CallParameters*);
  99. void caseCall();
  100. void copyDataToMemory(bytesConstRef _data, u256*& m_sp);
  101. uint64_t memNeed(u256 _offset, u256 _size);
  102. void throwOutOfGas();
  103. void throwBadInstruction();
  104. void throwBadJumpDestination();
  105. void throwBadStack(unsigned _size, unsigned _n, unsigned _d);
  106. void reportStackUse();
  107. std::vector<uint64_t> m_jumpDests;
  108. int64_t verifyJumpDest(u256 const& _dest, bool _throw = true);
  109. int poolConstant(const u256&);
  110. void onOperation();
  111. void checkStack(unsigned _n, unsigned _d);
  112. uint64_t gasForMem(u512 _size);
  113. void updateIOGas();
  114. void updateGas();
  115. void updateMem();
  116. void logGasMem();
  117. void fetchInstruction();
  118. template<class T> uint64_t toUint64(T v)
  119. {
  120. // check for overflow
  121. if (v > 0x7FFFFFFFFFFFFFFF)
  122. throwOutOfGas();
  123. uint64_t w = uint64_t(v);
  124. return w;
  125. }
  126. };
  127. }
  128. }