forked from bluealloy/revm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.rs
522 lines (458 loc) · 16.8 KB
/
host.rs
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
mod call_helpers;
pub use call_helpers::{calc_call_gas, get_memory_input_and_out_ranges};
use crate::{
gas::{self, COLD_ACCOUNT_ACCESS_COST, WARM_STORAGE_READ_COST},
interpreter::{Interpreter, InterpreterAction},
primitives::{Bytes, Log, LogData, Spec, SpecId::*, B256, U256},
CallContext, CallInputs, CallScheme, CreateInputs, CreateScheme, Host, InstructionResult,
SStoreResult, Transfer, MAX_INITCODE_SIZE,
};
use core::cmp::min;
use revm_primitives::BLOCK_HASH_HISTORY;
use std::{boxed::Box, vec::Vec};
pub fn balance<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
pop_address!(interpreter, address);
let Some((balance, is_cold)) = host.balance(address) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
gas!(
interpreter,
if SPEC::enabled(ISTANBUL) {
// EIP-1884: Repricing for trie-size-dependent opcodes
gas::account_access_gas::<SPEC>(is_cold)
} else if SPEC::enabled(TANGERINE) {
400
} else {
20
}
);
push!(interpreter, balance);
}
/// EIP-1884: Repricing for trie-size-dependent opcodes
pub fn selfbalance<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
check!(interpreter, ISTANBUL);
gas!(interpreter, gas::LOW);
let Some((balance, _)) = host.balance(interpreter.contract.address) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
push!(interpreter, balance);
}
pub fn extcodesize<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
pop_address!(interpreter, address);
let Some((code, is_cold)) = host.code(address) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
if SPEC::enabled(BERLIN) {
gas!(
interpreter,
if is_cold {
COLD_ACCOUNT_ACCESS_COST
} else {
WARM_STORAGE_READ_COST
}
);
} else if SPEC::enabled(TANGERINE) {
gas!(interpreter, 700);
} else {
gas!(interpreter, 20);
}
push!(interpreter, U256::from(code.len()));
}
/// EIP-1052: EXTCODEHASH opcode
pub fn extcodehash<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
check!(interpreter, CONSTANTINOPLE);
pop_address!(interpreter, address);
let Some((code_hash, is_cold)) = host.code_hash(address) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
if SPEC::enabled(BERLIN) {
gas!(
interpreter,
if is_cold {
COLD_ACCOUNT_ACCESS_COST
} else {
WARM_STORAGE_READ_COST
}
);
} else if SPEC::enabled(ISTANBUL) {
gas!(interpreter, 700);
} else {
gas!(interpreter, 400);
}
push_b256!(interpreter, code_hash);
}
pub fn extcodecopy<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
pop_address!(interpreter, address);
pop!(interpreter, memory_offset, code_offset, len_u256);
let Some((code, is_cold)) = host.code(address) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
let len = as_usize_or_fail!(interpreter, len_u256);
gas_or_fail!(
interpreter,
gas::extcodecopy_cost::<SPEC>(len as u64, is_cold)
);
if len == 0 {
return;
}
let memory_offset = as_usize_or_fail!(interpreter, memory_offset);
let code_offset = min(as_usize_saturated!(code_offset), code.len());
resize_memory!(interpreter, memory_offset, len);
// Note: this can't panic because we resized memory to fit.
interpreter
.shared_memory
.set_data(memory_offset, code_offset, len, code.bytes());
}
pub fn blockhash<H: Host>(interpreter: &mut Interpreter, host: &mut H) {
gas!(interpreter, gas::BLOCKHASH);
pop_top!(interpreter, number);
if let Some(diff) = host.env().block.number.checked_sub(*number) {
let diff = as_usize_saturated!(diff);
// blockhash should push zero if number is same as current block number.
if diff <= BLOCK_HASH_HISTORY && diff != 0 {
let Some(hash) = host.block_hash(*number) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
*number = U256::from_be_bytes(hash.0);
return;
}
}
*number = U256::ZERO;
}
pub fn sload<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
pop!(interpreter, index);
let Some((value, is_cold)) = host.sload(interpreter.contract.address, index) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
gas!(interpreter, gas::sload_cost::<SPEC>(is_cold));
push!(interpreter, value);
}
pub fn sstore<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
check_staticcall!(interpreter);
pop!(interpreter, index, value);
let Some(SStoreResult {
original_value: original,
present_value: old,
new_value: new,
is_cold,
}) = host.sstore(interpreter.contract.address, index, value)
else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
gas_or_fail!(interpreter, {
let remaining_gas = interpreter.gas.remaining();
gas::sstore_cost::<SPEC>(original, old, new, remaining_gas, is_cold)
});
refund!(interpreter, gas::sstore_refund::<SPEC>(original, old, new));
}
/// EIP-1153: Transient storage opcodes
/// Store value to transient storage
pub fn tstore<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
check!(interpreter, CANCUN);
check_staticcall!(interpreter);
gas!(interpreter, gas::WARM_STORAGE_READ_COST);
pop!(interpreter, index, value);
host.tstore(interpreter.contract.address, index, value);
}
/// EIP-1153: Transient storage opcodes
/// Load value from transient storage
pub fn tload<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
check!(interpreter, CANCUN);
gas!(interpreter, gas::WARM_STORAGE_READ_COST);
pop_top!(interpreter, index);
*index = host.tload(interpreter.contract.address, *index);
}
pub fn log<const N: usize, H: Host>(interpreter: &mut Interpreter, host: &mut H) {
check_staticcall!(interpreter);
pop!(interpreter, offset, len);
let len = as_usize_or_fail!(interpreter, len);
gas_or_fail!(interpreter, gas::log_cost(N as u8, len as u64));
let data = if len == 0 {
Bytes::new()
} else {
let offset = as_usize_or_fail!(interpreter, offset);
resize_memory!(interpreter, offset, len);
Bytes::copy_from_slice(interpreter.shared_memory.slice(offset, len))
};
if interpreter.stack.len() < N {
interpreter.instruction_result = InstructionResult::StackUnderflow;
return;
}
let mut topics = Vec::with_capacity(N);
for _ in 0..N {
// SAFETY: stack bounds already checked few lines above
topics.push(B256::from(unsafe { interpreter.stack.pop_unsafe() }));
}
let log = Log {
address: interpreter.contract.address,
data: LogData::new(topics, data).expect("LogData should have <=4 topics"),
};
host.log(log);
}
pub fn selfdestruct<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
check_staticcall!(interpreter);
pop_address!(interpreter, target);
let Some(res) = host.selfdestruct(interpreter.contract.address, target) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
// EIP-3529: Reduction in refunds
if !SPEC::enabled(LONDON) && !res.previously_destroyed {
refund!(interpreter, gas::SELFDESTRUCT)
}
gas!(interpreter, gas::selfdestruct_cost::<SPEC>(res));
interpreter.instruction_result = InstructionResult::SelfDestruct;
}
pub fn create<const IS_CREATE2: bool, H: Host, SPEC: Spec>(
interpreter: &mut Interpreter,
host: &mut H,
) {
check_staticcall!(interpreter);
// EIP-1014: Skinny CREATE2
if IS_CREATE2 {
check!(interpreter, PETERSBURG);
}
pop!(interpreter, value, code_offset, len);
let len = as_usize_or_fail!(interpreter, len);
let mut code = Bytes::new();
if len != 0 {
// EIP-3860: Limit and meter initcode
if SPEC::enabled(SHANGHAI) {
// Limit is set as double of max contract bytecode size
let max_initcode_size = host
.env()
.cfg
.limit_contract_code_size
.map(|limit| limit.saturating_mul(2))
.unwrap_or(MAX_INITCODE_SIZE);
if len > max_initcode_size {
interpreter.instruction_result = InstructionResult::CreateInitCodeSizeLimit;
return;
}
gas!(interpreter, gas::initcode_cost(len as u64));
}
let code_offset = as_usize_or_fail!(interpreter, code_offset);
resize_memory!(interpreter, code_offset, len);
code = Bytes::copy_from_slice(interpreter.shared_memory.slice(code_offset, len));
}
// EIP-1014: Skinny CREATE2
let scheme = if IS_CREATE2 {
pop!(interpreter, salt);
gas_or_fail!(interpreter, gas::create2_cost(len));
CreateScheme::Create2 { salt }
} else {
gas!(interpreter, gas::CREATE);
CreateScheme::Create
};
let mut gas_limit = interpreter.gas().remaining();
// EIP-150: Gas cost changes for IO-heavy operations
if SPEC::enabled(TANGERINE) {
// take remaining gas and deduce l64 part of it.
gas_limit -= gas_limit / 64
}
gas!(interpreter, gas_limit);
// Call host to interact with target contract
interpreter.next_action = InterpreterAction::Create {
inputs: Box::new(CreateInputs {
caller: interpreter.contract.address,
scheme,
value,
init_code: code,
gas_limit,
}),
};
interpreter.instruction_result = InstructionResult::CallOrCreate;
}
pub fn call<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
pop!(interpreter, local_gas_limit);
pop_address!(interpreter, to);
// max gas limit is not possible in real ethereum situation.
let local_gas_limit = u64::try_from(local_gas_limit).unwrap_or(u64::MAX);
pop!(interpreter, value);
if interpreter.is_static && value != U256::ZERO {
interpreter.instruction_result = InstructionResult::CallNotAllowedInsideStatic;
return;
}
let Some((input, return_memory_offset)) = get_memory_input_and_out_ranges(interpreter) else {
return;
};
let Some(mut gas_limit) = calc_call_gas::<H, SPEC>(
interpreter,
host,
to,
value != U256::ZERO,
local_gas_limit,
true,
true,
) else {
return;
};
gas!(interpreter, gas_limit);
// add call stipend if there is value to be transferred.
if value != U256::ZERO {
gas_limit = gas_limit.saturating_add(gas::CALL_STIPEND);
}
// Call host to interact with target contract
interpreter.next_action = InterpreterAction::Call {
inputs: Box::new(CallInputs {
contract: to,
transfer: Transfer {
source: interpreter.contract.address,
target: to,
value,
},
input,
gas_limit,
context: CallContext {
address: to,
caller: interpreter.contract.address,
code_address: to,
apparent_value: value,
scheme: CallScheme::Call,
},
is_static: interpreter.is_static,
return_memory_offset,
}),
};
interpreter.instruction_result = InstructionResult::CallOrCreate;
}
pub fn call_code<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
pop!(interpreter, local_gas_limit);
pop_address!(interpreter, to);
// max gas limit is not possible in real ethereum situation.
let local_gas_limit = u64::try_from(local_gas_limit).unwrap_or(u64::MAX);
pop!(interpreter, value);
let Some((input, return_memory_offset)) = get_memory_input_and_out_ranges(interpreter) else {
return;
};
let Some(mut gas_limit) = calc_call_gas::<H, SPEC>(
interpreter,
host,
to,
value != U256::ZERO,
local_gas_limit,
true,
false,
) else {
return;
};
gas!(interpreter, gas_limit);
// add call stipend if there is value to be transferred.
if value != U256::ZERO {
gas_limit = gas_limit.saturating_add(gas::CALL_STIPEND);
}
// Call host to interact with target contract
interpreter.next_action = InterpreterAction::Call {
inputs: Box::new(CallInputs {
contract: to,
transfer: Transfer {
source: interpreter.contract.address,
target: interpreter.contract.address,
value,
},
input,
gas_limit,
context: CallContext {
address: interpreter.contract.address,
caller: interpreter.contract.address,
code_address: to,
apparent_value: value,
scheme: CallScheme::CallCode,
},
is_static: interpreter.is_static,
return_memory_offset,
}),
};
interpreter.instruction_result = InstructionResult::CallOrCreate;
}
pub fn delegate_call<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
check!(interpreter, HOMESTEAD);
pop!(interpreter, local_gas_limit);
pop_address!(interpreter, to);
// max gas limit is not possible in real ethereum situation.
let local_gas_limit = u64::try_from(local_gas_limit).unwrap_or(u64::MAX);
let Some((input, return_memory_offset)) = get_memory_input_and_out_ranges(interpreter) else {
return;
};
let Some(gas_limit) =
calc_call_gas::<H, SPEC>(interpreter, host, to, false, local_gas_limit, false, false)
else {
return;
};
gas!(interpreter, gas_limit);
// Call host to interact with target contract
interpreter.next_action = InterpreterAction::Call {
inputs: Box::new(CallInputs {
contract: to,
// This is dummy send for StaticCall and DelegateCall,
// it should do nothing and not touch anything.
transfer: Transfer {
source: interpreter.contract.address,
target: interpreter.contract.address,
value: U256::ZERO,
},
input,
gas_limit,
context: CallContext {
address: interpreter.contract.address,
caller: interpreter.contract.caller,
code_address: to,
apparent_value: interpreter.contract.value,
scheme: CallScheme::DelegateCall,
},
is_static: interpreter.is_static,
return_memory_offset,
}),
};
interpreter.instruction_result = InstructionResult::CallOrCreate;
}
pub fn static_call<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
check!(interpreter, BYZANTIUM);
pop!(interpreter, local_gas_limit);
pop_address!(interpreter, to);
// max gas limit is not possible in real ethereum situation.
let local_gas_limit = u64::try_from(local_gas_limit).unwrap_or(u64::MAX);
let value = U256::ZERO;
let Some((input, return_memory_offset)) = get_memory_input_and_out_ranges(interpreter) else {
return;
};
let Some(gas_limit) =
calc_call_gas::<H, SPEC>(interpreter, host, to, false, local_gas_limit, false, true)
else {
return;
};
gas!(interpreter, gas_limit);
// Call host to interact with target contract
interpreter.next_action = InterpreterAction::Call {
inputs: Box::new(CallInputs {
contract: to,
// This is dummy send for StaticCall and DelegateCall,
// it should do nothing and not touch anything.
transfer: Transfer {
source: interpreter.contract.address,
target: interpreter.contract.address,
value: U256::ZERO,
},
input,
gas_limit,
context: CallContext {
address: to,
caller: interpreter.contract.address,
code_address: to,
apparent_value: value,
scheme: CallScheme::StaticCall,
},
is_static: true,
return_memory_offset,
}),
};
interpreter.instruction_result = InstructionResult::CallOrCreate;
}