forked from ethereum/aleth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJitVM.cpp
More file actions
375 lines (325 loc) · 10.3 KB
/
Copy pathJitVM.cpp
File metadata and controls
375 lines (325 loc) · 10.3 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
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
#include "JitVM.h"
#include <libdevcore/Log.h>
#include <libevm/VM.h>
#include <libevm/VMFactory.h>
namespace dev
{
namespace eth
{
namespace
{
static_assert(sizeof(Address) == sizeof(evm_uint160be),
"Address types size mismatch");
static_assert(alignof(Address) == alignof(evm_uint160be),
"Address types alignment mismatch");
inline evm_uint160be toEvmC(Address const& _addr)
{
return reinterpret_cast<evm_uint160be const&>(_addr);
}
inline Address fromEvmC(evm_uint160be const& _addr)
{
return reinterpret_cast<Address const&>(_addr);
}
static_assert(sizeof(h256) == sizeof(evm_uint256be), "Hash types size mismatch");
static_assert(alignof(h256) == alignof(evm_uint256be), "Hash types alignment mismatch");
inline evm_uint256be toEvmC(h256 const& _h)
{
return reinterpret_cast<evm_uint256be const&>(_h);
}
inline u256 fromEvmC(evm_uint256be const& _n)
{
return fromBigEndian<u256>(_n.bytes);
}
void queryState(
evm_variant* o_result,
evm_env* _opaqueEnv,
evm_query_key _key,
evm_uint160be const* _addr,
evm_uint256be const* _storageKey
) noexcept
{
auto &env = reinterpret_cast<ExtVMFace&>(*_opaqueEnv);
Address addr = fromEvmC(*_addr);
switch (_key)
{
case EVM_CODE_BY_ADDRESS:
{
auto &code = env.codeAt(addr);
o_result->data = code.data();
o_result->data_size = code.size();
break;
}
case EVM_CODE_SIZE:
o_result->int64 = env.codeSizeAt(addr);
break;
case EVM_BALANCE:
o_result->uint256be = toEvmC(env.balance(addr));
break;
case EVM_SLOAD:
{
auto storageKey = fromEvmC(*_storageKey);
o_result->uint256be = toEvmC(env.store(storageKey));
break;
}
case EVM_ACCOUNT_EXISTS:
o_result->int64 = env.exists(addr);
break;
}
}
void updateState(
evm_env* _opaqueEnv,
evm_update_key _key,
evm_uint160be const* _addr,
evm_variant const* _arg1,
evm_variant const* _arg2
) noexcept
{
(void) _addr;
auto &env = reinterpret_cast<ExtVMFace&>(*_opaqueEnv);
assert(fromEvmC(*_addr) == env.myAddress);
switch (_key)
{
case EVM_SSTORE:
{
u256 index = fromEvmC(_arg1->uint256be);
u256 value = fromEvmC(_arg2->uint256be);
if (value == 0 && env.store(index) != 0) // If delete
env.sub.refunds += env.evmSchedule().sstoreRefundGas; // Increase refund counter
env.setStore(index, value); // Interface uses native endianness
break;
}
case EVM_LOG:
{
size_t numTopics = _arg2->data_size / sizeof(h256);
h256 const* pTopics = reinterpret_cast<h256 const*>(_arg2->data);
env.log({pTopics, pTopics + numTopics}, {_arg1->data, _arg1->data_size});
break;
}
case EVM_SELFDESTRUCT:
// Register selfdestruction beneficiary.
env.suicide(fromEvmC(_arg1->address));
break;
}
}
void getTxContext(evm_tx_context* result, evm_env* _opaqueEnv) noexcept
{
auto &env = reinterpret_cast<ExtVMFace&>(*_opaqueEnv);
result->tx_gas_price = toEvmC(env.gasPrice);
result->tx_origin = toEvmC(env.origin);
result->block_coinbase = toEvmC(env.envInfo().author());
result->block_number = static_cast<int64_t>(env.envInfo().number());
result->block_timestamp = static_cast<int64_t>(env.envInfo().timestamp());
result->block_gas_limit = static_cast<int64_t>(env.envInfo().gasLimit());
result->block_difficulty = toEvmC(env.envInfo().difficulty());
}
void getBlockHash(evm_uint256be* o_hash, evm_env* _envPtr, int64_t _number)
{
auto &env = reinterpret_cast<ExtVMFace&>(*_envPtr);
*o_hash = toEvmC(env.blockHash(_number));
}
void create(evm_result* o_result, ExtVMFace& _env, evm_message const* _msg) noexcept
{
u256 gas = _msg->gas;
u256 value = fromEvmC(_msg->value);
bytesConstRef init = {_msg->input, _msg->input_size};
// ExtVM::create takes the sender address from .myAddress.
assert(fromEvmC(_msg->sender) == _env.myAddress);
// TODO: EVMJIT does not support RETURNDATA at the moment, so
// the output is ignored here.
h160 addr;
std::tie(addr, std::ignore) = _env.create(value, gas, init, {});
o_result->gas_left = static_cast<int64_t>(gas);
o_result->release = nullptr;
if (addr)
{
o_result->code = EVM_SUCCESS;
// Use reserved data to store the address.
static_assert(sizeof(o_result->reserved.data) >= addr.size,
"Not enough space to store an address");
std::copy(addr.begin(), addr.end(), o_result->reserved.data);
o_result->output_data = o_result->reserved.data;
o_result->output_size = addr.size;
}
else
{
o_result->code = EVM_FAILURE;
o_result->output_data = nullptr;
o_result->output_size = 0;
}
}
void call(evm_result* o_result, evm_env* _opaqueEnv, evm_message const* _msg) noexcept
{
assert(_msg->gas >= 0 && "Invalid gas value");
auto &env = reinterpret_cast<ExtVMFace&>(*_opaqueEnv);
// Handle CREATE separately.
if (_msg->kind == EVM_CREATE)
return create(o_result, env, _msg);
CallParameters params;
params.gas = _msg->gas;
params.apparentValue = fromEvmC(_msg->value);
params.valueTransfer = _msg->kind == EVM_DELEGATECALL ? 0 : params.apparentValue;
params.senderAddress = fromEvmC(_msg->sender);
params.codeAddress = fromEvmC(_msg->address);
params.receiveAddress = _msg->kind == EVM_CALL ? params.codeAddress : env.myAddress;
params.data = {_msg->input, _msg->input_size};
params.onOp = {};
bool success = false;
owning_bytes_ref output;
std::tie(success, output) = env.call(params);
// FIXME: We have a mess here. It is hard to distinguish reverts from failures.
// In first case we want to keep the output, in the second one the output
// is optional and should not be passed to the contract, but can be useful
// for EVM in general.
o_result->code = success ? EVM_SUCCESS : EVM_REVERT;
o_result->gas_left = static_cast<int64_t>(params.gas);
// Pass the output to the EVM without a copy. The EVM will delete it
// when finished with it.
// First assign reference. References are not invalidated when vector
// of bytes is moved. See `.takeBytes()` below.
o_result->output_data = output.data();
o_result->output_size = output.size();
// Place a new vector of bytes containing output in result's reserved memory.
static_assert(sizeof(bytes) <= sizeof(o_result->reserved), "Vector is too big");
new(&o_result->reserved) bytes(output.takeBytes());
// Set the destructor to delete the vector.
o_result->release = [](evm_result const* _result)
{
auto& output = reinterpret_cast<bytes const&>(_result->reserved);
// Explicitly call vector's destructor to release its data.
// This is normal pattern when placement new operator is used.
output.~bytes();
};
}
/// RAII wrapper for an evm instance.
class EVM
{
public:
EVM(evm_query_state_fn _queryFn, evm_update_state_fn _updateFn, evm_call_fn _callFn,
evm_get_tx_context_fn _getTxContextFn,
evm_get_block_hash_fn _getBlockHashFn
)
{
auto factory = evmjit_get_factory();
m_instance = factory.create(
_queryFn, _updateFn, _callFn, _getTxContextFn, _getBlockHashFn
);
}
~EVM()
{
m_instance->destroy(m_instance);
}
EVM(EVM const&) = delete;
EVM& operator=(EVM) = delete;
class Result
{
public:
explicit Result(evm_result const& _result):
m_result(_result)
{}
~Result()
{
if (m_result.release)
m_result.release(&m_result);
}
Result(Result&& _other):
m_result(_other.m_result)
{
// Disable releaser of the rvalue object.
_other.m_result.release = nullptr;
}
Result(Result const&) = delete;
Result& operator=(Result const&) = delete;
evm_result_code code() const
{
return m_result.code;
}
int64_t gasLeft() const
{
return m_result.gas_left;
}
bytesConstRef output() const
{
return {m_result.output_data, m_result.output_size};
}
private:
evm_result m_result;
};
/// Handy wrapper for evm_execute().
Result execute(ExtVMFace& _ext, int64_t gas)
{
auto env = reinterpret_cast<evm_env*>(&_ext);
auto mode = JitVM::scheduleToMode(_ext.evmSchedule());
evm_message msg = {toEvmC(_ext.myAddress), toEvmC(_ext.caller),
toEvmC(_ext.value), _ext.data.data(),
_ext.data.size(), toEvmC(_ext.codeHash), gas,
static_cast<int32_t>(_ext.depth), EVM_CALL};
return Result{m_instance->execute(
m_instance, env, mode, &msg, _ext.code.data(), _ext.code.size()
)};
}
bool isCodeReady(evm_mode _mode, h256 _codeHash)
{
return m_instance->get_code_status(m_instance, _mode, toEvmC(_codeHash)) == EVM_READY;
}
void compile(evm_mode _mode, bytesConstRef _code, h256 _codeHash)
{
m_instance->prepare_code(
m_instance, _mode, toEvmC(_codeHash), _code.data(), _code.size()
);
}
private:
/// The VM instance created with m_interface.create().
evm_instance* m_instance = nullptr;
};
EVM& getJit()
{
// Create EVM JIT instance by using EVM-C interface.
static EVM jit(queryState, updateState, call, getTxContext, getBlockHash);
return jit;
}
} // End of anonymous namespace.
owning_bytes_ref JitVM::exec(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp)
{
bool rejected = false;
// TODO: Rejecting transactions with gas limit > 2^63 can be used by attacker to take JIT out of scope
rejected |= io_gas > std::numeric_limits<int64_t>::max(); // Do not accept requests with gas > 2^63 (int64 max)
rejected |= _ext.envInfo().number() > std::numeric_limits<int64_t>::max();
rejected |= _ext.envInfo().timestamp() > std::numeric_limits<int64_t>::max();
rejected |= _ext.envInfo().gasLimit() > std::numeric_limits<int64_t>::max();
if (rejected)
{
cwarn << "Execution rejected by EVM JIT (gas limit: " << io_gas << "), executing with interpreter";
return VMFactory::create(VMKind::Interpreter)->exec(io_gas, _ext, _onOp);
}
auto gas = static_cast<int64_t>(io_gas);
auto r = getJit().execute(_ext, gas);
// TODO: Add EVM-C result codes mapping with exception types.
if (r.code() == EVM_FAILURE)
BOOST_THROW_EXCEPTION(OutOfGas());
io_gas = r.gasLeft();
// FIXME: Copy the output for now, but copyless version possible.
owning_bytes_ref output{r.output().toVector(), 0, r.output().size()};
if (r.code() == EVM_REVERT)
throw RevertInstruction(std::move(output));
return output;
}
evm_mode JitVM::scheduleToMode(EVMSchedule const& _schedule)
{
if (_schedule.haveRevert)
return EVM_METROPOLIS;
if (_schedule.eip158Mode)
return EVM_CLEARING;
if (_schedule.eip150Mode)
return EVM_ANTI_DOS;
return _schedule.haveDelegateCall ? EVM_HOMESTEAD : EVM_FRONTIER;
}
bool JitVM::isCodeReady(evm_mode _mode, h256 _codeHash)
{
return getJit().isCodeReady(_mode, _codeHash);
}
void JitVM::compile(evm_mode _mode, bytesConstRef _code, h256 _codeHash)
{
getJit().compile(_mode, _code, _codeHash);
}
}
}