Skip to content

Commit 8af9531

Browse files
authored
feat(interpreter): test Host object-safety, allow dyn Host in instructions (#1245)
1 parent 223f6cd commit 8af9531

File tree

13 files changed

+140
-107
lines changed

13 files changed

+140
-107
lines changed

crates/interpreter/src/host.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,16 @@ pub struct SStoreResult {
6565
/// Is storage slot loaded from database
6666
pub is_cold: bool,
6767
}
68+
69+
#[cfg(test)]
70+
mod tests {
71+
use super::*;
72+
73+
fn assert_host<H: Host + ?Sized>() {}
74+
75+
#[test]
76+
fn object_safety() {
77+
assert_host::<DummyHost>();
78+
assert_host::<dyn Host>();
79+
}
80+
}

crates/interpreter/src/instructions/arithmetic.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,65 @@ use crate::{
55
Host, Interpreter,
66
};
77

8-
pub fn wrapping_add<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
8+
pub fn wrapping_add<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
99
gas!(interpreter, gas::VERYLOW);
1010
pop_top!(interpreter, op1, op2);
1111
*op2 = op1.wrapping_add(*op2);
1212
}
1313

14-
pub fn wrapping_mul<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
14+
pub fn wrapping_mul<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
1515
gas!(interpreter, gas::LOW);
1616
pop_top!(interpreter, op1, op2);
1717
*op2 = op1.wrapping_mul(*op2);
1818
}
1919

20-
pub fn wrapping_sub<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
20+
pub fn wrapping_sub<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
2121
gas!(interpreter, gas::VERYLOW);
2222
pop_top!(interpreter, op1, op2);
2323
*op2 = op1.wrapping_sub(*op2);
2424
}
2525

26-
pub fn div<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
26+
pub fn div<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
2727
gas!(interpreter, gas::LOW);
2828
pop_top!(interpreter, op1, op2);
2929
if *op2 != U256::ZERO {
3030
*op2 = op1.wrapping_div(*op2);
3131
}
3232
}
3333

34-
pub fn sdiv<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
34+
pub fn sdiv<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
3535
gas!(interpreter, gas::LOW);
3636
pop_top!(interpreter, op1, op2);
3737
*op2 = i256_div(op1, *op2);
3838
}
3939

40-
pub fn rem<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
40+
pub fn rem<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
4141
gas!(interpreter, gas::LOW);
4242
pop_top!(interpreter, op1, op2);
4343
if *op2 != U256::ZERO {
4444
*op2 = op1.wrapping_rem(*op2);
4545
}
4646
}
4747

48-
pub fn smod<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
48+
pub fn smod<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
4949
gas!(interpreter, gas::LOW);
5050
pop_top!(interpreter, op1, op2);
5151
*op2 = i256_mod(op1, *op2)
5252
}
5353

54-
pub fn addmod<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
54+
pub fn addmod<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
5555
gas!(interpreter, gas::MID);
5656
pop_top!(interpreter, op1, op2, op3);
5757
*op3 = op1.add_mod(op2, *op3)
5858
}
5959

60-
pub fn mulmod<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
60+
pub fn mulmod<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
6161
gas!(interpreter, gas::MID);
6262
pop_top!(interpreter, op1, op2, op3);
6363
*op3 = op1.mul_mod(op2, *op3)
6464
}
6565

66-
pub fn exp<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
66+
pub fn exp<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
6767
pop_top!(interpreter, op1, op2);
6868
gas_or_fail!(interpreter, gas::exp_cost::<SPEC>(*op2));
6969
*op2 = op1.pow(*op2);
@@ -84,7 +84,7 @@ pub fn exp<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
8484
/// `y | !mask` where `|` is the bitwise `OR` and `!` is bitwise negation. Similarly, if
8585
/// `b == 0` then the yellow paper says the output should start with all zeros, then end with
8686
/// bits from `b`; this is equal to `y & mask` where `&` is bitwise `AND`.
87-
pub fn signextend<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
87+
pub fn signextend<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
8888
gas!(interpreter, gas::LOW);
8989
pop_top!(interpreter, ext, x);
9090
// For 31 we also don't need to do anything.

crates/interpreter/src/instructions/bitwise.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,67 @@ use crate::{
77
use core::cmp::Ordering;
88
use revm_primitives::uint;
99

10-
pub fn lt<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
10+
pub fn lt<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
1111
gas!(interpreter, gas::VERYLOW);
1212
pop_top!(interpreter, op1, op2);
1313
*op2 = U256::from(op1 < *op2);
1414
}
1515

16-
pub fn gt<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
16+
pub fn gt<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
1717
gas!(interpreter, gas::VERYLOW);
1818
pop_top!(interpreter, op1, op2);
1919
*op2 = U256::from(op1 > *op2);
2020
}
2121

22-
pub fn slt<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
22+
pub fn slt<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
2323
gas!(interpreter, gas::VERYLOW);
2424
pop_top!(interpreter, op1, op2);
2525
*op2 = U256::from(i256_cmp(&op1, op2) == Ordering::Less);
2626
}
2727

28-
pub fn sgt<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
28+
pub fn sgt<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
2929
gas!(interpreter, gas::VERYLOW);
3030
pop_top!(interpreter, op1, op2);
3131
*op2 = U256::from(i256_cmp(&op1, op2) == Ordering::Greater);
3232
}
3333

34-
pub fn eq<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
34+
pub fn eq<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
3535
gas!(interpreter, gas::VERYLOW);
3636
pop_top!(interpreter, op1, op2);
3737
*op2 = U256::from(op1 == *op2);
3838
}
3939

40-
pub fn iszero<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
40+
pub fn iszero<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
4141
gas!(interpreter, gas::VERYLOW);
4242
pop_top!(interpreter, op1);
4343
*op1 = U256::from(*op1 == U256::ZERO);
4444
}
4545

46-
pub fn bitand<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
46+
pub fn bitand<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
4747
gas!(interpreter, gas::VERYLOW);
4848
pop_top!(interpreter, op1, op2);
4949
*op2 = op1 & *op2;
5050
}
5151

52-
pub fn bitor<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
52+
pub fn bitor<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
5353
gas!(interpreter, gas::VERYLOW);
5454
pop_top!(interpreter, op1, op2);
5555
*op2 = op1 | *op2;
5656
}
5757

58-
pub fn bitxor<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
58+
pub fn bitxor<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
5959
gas!(interpreter, gas::VERYLOW);
6060
pop_top!(interpreter, op1, op2);
6161
*op2 = op1 ^ *op2;
6262
}
6363

64-
pub fn not<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
64+
pub fn not<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
6565
gas!(interpreter, gas::VERYLOW);
6666
pop_top!(interpreter, op1);
6767
*op1 = !*op1;
6868
}
6969

70-
pub fn byte<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
70+
pub fn byte<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
7171
gas!(interpreter, gas::VERYLOW);
7272
pop_top!(interpreter, op1, op2);
7373

@@ -81,23 +81,23 @@ pub fn byte<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
8181
}
8282

8383
/// EIP-145: Bitwise shifting instructions in EVM
84-
pub fn shl<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
84+
pub fn shl<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
8585
check!(interpreter, CONSTANTINOPLE);
8686
gas!(interpreter, gas::VERYLOW);
8787
pop_top!(interpreter, op1, op2);
8888
*op2 <<= as_usize_saturated!(op1);
8989
}
9090

9191
/// EIP-145: Bitwise shifting instructions in EVM
92-
pub fn shr<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
92+
pub fn shr<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
9393
check!(interpreter, CONSTANTINOPLE);
9494
gas!(interpreter, gas::VERYLOW);
9595
pop_top!(interpreter, op1, op2);
9696
*op2 >>= as_usize_saturated!(op1);
9797
}
9898

9999
/// EIP-145: Bitwise shifting instructions in EVM
100-
pub fn sar<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
100+
pub fn sar<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
101101
check!(interpreter, CONSTANTINOPLE);
102102
gas!(interpreter, gas::VERYLOW);
103103
pop_top!(interpreter, op1, op2);

crates/interpreter/src/instructions/control.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use crate::{
44
Host, InstructionResult, Interpreter, InterpreterResult,
55
};
66

7-
pub fn jump<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
7+
pub fn jump<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
88
gas!(interpreter, gas::MID);
99
pop!(interpreter, dest);
1010
jump_inner(interpreter, dest);
1111
}
1212

13-
pub fn jumpi<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
13+
pub fn jumpi<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
1414
gas!(interpreter, gas::HIGH);
1515
pop!(interpreter, dest, value);
1616
if value != U256::ZERO {
@@ -29,11 +29,11 @@ fn jump_inner(interpreter: &mut Interpreter, dest: U256) {
2929
interpreter.instruction_pointer = unsafe { interpreter.contract.bytecode.as_ptr().add(dest) };
3030
}
3131

32-
pub fn jumpdest<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
32+
pub fn jumpdest<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
3333
gas!(interpreter, gas::JUMPDEST);
3434
}
3535

36-
pub fn pc<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
36+
pub fn pc<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
3737
gas!(interpreter, gas::BASE);
3838
// - 1 because we have already advanced the instruction pointer in `Interpreter::step`
3939
push!(interpreter, U256::from(interpreter.program_counter() - 1));
@@ -63,27 +63,27 @@ fn return_inner(interpreter: &mut Interpreter, instruction_result: InstructionRe
6363
};
6464
}
6565

66-
pub fn ret<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
66+
pub fn ret<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
6767
return_inner(interpreter, InstructionResult::Return);
6868
}
6969

7070
/// EIP-140: REVERT instruction
71-
pub fn revert<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
71+
pub fn revert<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
7272
check!(interpreter, BYZANTIUM);
7373
return_inner(interpreter, InstructionResult::Revert);
7474
}
7575

7676
/// Stop opcode. This opcode halts the execution.
77-
pub fn stop<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
77+
pub fn stop<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
7878
interpreter.instruction_result = InstructionResult::Stop;
7979
}
8080

8181
/// Invalid opcode. This opcode halts the execution.
82-
pub fn invalid<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
82+
pub fn invalid<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
8383
interpreter.instruction_result = InstructionResult::InvalidFEOpcode;
8484
}
8585

8686
/// Unknown opcode. This opcode halts the execution.
87-
pub fn unknown<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
87+
pub fn unknown<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
8888
interpreter.instruction_result = InstructionResult::OpcodeNotFound;
8989
}

0 commit comments

Comments
 (0)