This repository was archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathVM.h
More file actions
181 lines (147 loc) · 5.29 KB
/
VM.h
File metadata and controls
181 lines (147 loc) · 5.29 KB
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
/*
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/>.
*/
#pragma once
#include "VMConfig.h"
#include <libevm/VMFace.h>
#include <evmc/evmc.h>
#include <evmc/instructions.h>
#include <boost/optional.hpp>
namespace dev
{
namespace eth
{
struct VMSchedule
{
static constexpr int64_t stackLimit = 1024;
static constexpr int64_t stepGas0 = 0;
static constexpr int64_t stepGas1 = 2;
static constexpr int64_t stepGas2 = 3;
static constexpr int64_t stepGas3 = 5;
static constexpr int64_t stepGas4 = 8;
static constexpr int64_t stepGas5 = 10;
static constexpr int64_t stepGas6 = 20;
static constexpr int64_t sha3Gas = 30;
static constexpr int64_t sha3WordGas = 6;
static constexpr int64_t sloadGas = 50;
static constexpr int64_t sstoreSetGas = 20000;
static constexpr int64_t sstoreResetGas = 5000;
static constexpr int64_t sstoreUnchangedGas = 200;
static constexpr int64_t jumpdestGas = 1;
static constexpr int64_t logGas = 375;
static constexpr int64_t logDataGas = 8;
static constexpr int64_t logTopicGas = 375;
static constexpr int64_t createGas = 32000;
static constexpr int64_t memoryGas = 3;
static constexpr int64_t quadCoeffDiv = 512;
static constexpr int64_t copyGas = 3;
static constexpr int64_t valueTransferGas = 9000;
static constexpr int64_t callStipend = 2300;
static constexpr int64_t callNewAccount = 25000;
};
class VM
{
public:
VM() = default;
owning_bytes_ref exec(evmc_context* _context, evmc_revision _rev, const evmc_message* _msg,
uint8_t const* _code, size_t _codeSize);
uint64_t m_io_gas = 0;
private:
evmc_context* m_context = nullptr;
evmc_revision m_rev = EVMC_FRONTIER;
evmc_message const* m_message = nullptr;
boost::optional<evmc_tx_context> m_tx_context;
static std::array<evmc_instruction_metrics, 256> c_metrics;
static void initMetrics();
static u256 exp256(u256 _base, u256 _exponent);
void copyCode(int);
typedef void (VM::*MemFnPtr)();
MemFnPtr m_bounce = nullptr;
uint64_t m_nSteps = 0;
// return bytes
owning_bytes_ref m_output;
// space for memory
bytes m_mem;
uint8_t const* m_pCode = nullptr;
size_t m_codeSize = 0;
// space for code
bytes m_code;
/// RETURNDATA buffer for memory returned from direct subcalls.
bytes m_returnData;
// space for data stack, grows towards smaller addresses from the end
u256 m_stack[VMSchedule::stackLimit];
u256 *m_stackEnd = &m_stack[VMSchedule::stackLimit];
size_t stackSize() { return m_stackEnd - m_SP; }
// constant pool
std::vector<u256> m_pool;
// interpreter state
Instruction m_OP; // current operation
uint64_t m_PC = 0; // program counter
u256* m_SP = m_stackEnd; // stack pointer
u256* m_SPP = m_SP; // stack pointer prime (next SP)
// metering and memory state
uint64_t m_runGas = 0;
uint64_t m_newMemSize = 0;
uint64_t m_copyMemSize = 0;
// initialize interpreter
void initEntry();
void optimize();
// interpreter loop & switch
void interpretCases();
// interpreter cases that call out
void caseCreate();
bool caseCallSetup(evmc_message& _msg, bytesRef& o_output);
void caseCall();
void copyDataToMemory(bytesConstRef _data, u256*_sp);
uint64_t memNeed(u256 const& _offset, u256 const& _size);
const evmc_tx_context& getTxContext();
void throwOutOfGas();
void throwInvalidInstruction();
void throwBadInstruction();
void throwBadJumpDestination();
void throwBadStack(int _removed, int _added);
void throwRevertInstruction(owning_bytes_ref&& _output);
void throwDisallowedStateChange();
void throwBufferOverrun(bigint const& _enfOfAccess);
std::vector<uint64_t> m_beginSubs;
std::vector<uint64_t> m_jumpDests;
int64_t verifyJumpDest(u256 const& _dest, bool _throw = true);
void onOperation() {}
void adjustStack(int _removed, int _added);
uint64_t gasForMem(u512 const& _size);
void updateIOGas();
void updateGas();
void updateMem(uint64_t _newMem);
void logGasMem();
void fetchInstruction();
uint64_t decodeJumpDest(const byte* const _code, uint64_t& _pc);
uint64_t decodeJumpvDest(const byte* const _code, uint64_t& _pc, byte _voff);
template<class T> uint64_t toInt63(T v)
{
// check for overflow
if (v > 0x7FFFFFFFFFFFFFFF)
throwOutOfGas();
uint64_t w = uint64_t(v);
return w;
}
template<class T> uint64_t toInt15(T v)
{
// check for overflow
if (v > 0x7FFF)
throwOutOfGas();
uint64_t w = uint64_t(v);
return w;
}
};
}
}