Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
7ea084a
eof
rakita Feb 13, 2024
cd523c3
Merge remote-tracking branch 'origin/main' into eof
rakita Feb 14, 2024
237068f
feat(EOF): Header decoder
rakita Feb 14, 2024
fabdc1b
EofBody decode
rakita Feb 15, 2024
4dd7d3a
Merge remote-tracking branch 'origin/main' into eof
rakita Feb 18, 2024
2ff0f28
disable eof deprecated opcodes
rakita Feb 18, 2024
20fcdaf
add eof instructions
rakita Feb 20, 2024
22bf551
temp tests
rakita Feb 20, 2024
318d775
Merge remote-tracking branch 'origin/main' into eof
rakita Feb 22, 2024
33974b3
rjump instructions
rakita Feb 22, 2024
a638385
eof rjump with tests
rakita Feb 23, 2024
2d14e04
Merge remote-tracking branch 'origin/main' into eof
rakita Feb 23, 2024
ad45264
EOF bytecode
rakita Feb 25, 2024
2c29f1b
callf, retf, jumpf
rakita Feb 26, 2024
ecc4467
tests for callf,retf,jumpf
rakita Feb 27, 2024
3979a12
small rename
rakita Feb 27, 2024
a440503
add dataload, dataloadn and datacopy opcodes
rakita Feb 28, 2024
cb3d93a
refactor calls
rakita Feb 29, 2024
1dabca4
blueprint for creates
rakita Mar 1, 2024
788182f
Merge remote-tracking branch 'origin/main' into eof
rakita Mar 1, 2024
4354b6d
eof create inputs
rakita Mar 2, 2024
207ad21
some wip
rakita Mar 4, 2024
dc07460
Merge remote-tracking branch 'origin/main' into eof
rakita Mar 4, 2024
2e48462
add eofcreate structs and exccall flow
rakita Mar 4, 2024
48abbe6
wip eofcreate code flow and handlers
rakita Mar 4, 2024
9d8f612
fix tests
rakita Mar 4, 2024
0fc345a
eof creates
rakita Mar 5, 2024
862d85b
Merge remote-tracking branch 'origin/main' into eof
rakita Mar 7, 2024
fab521a
refactor eofcreate a little
rakita Mar 7, 2024
e5abe46
some work on extcall
rakita Mar 8, 2024
d3dd1a9
Merge remote-tracking branch 'origin/main' into eof
rakita Mar 14, 2024
4aa166a
feat: refactor simplify CallInput, eof extcalls
rakita Mar 15, 2024
449f243
feat: restructure OpCode and add stack input/output num
rakita Mar 18, 2024
183adaf
add flags for stack_io and not_eof
rakita Mar 19, 2024
07c721a
wip eof verification
rakita Mar 19, 2024
42a04c1
Merge remote-tracking branch 'origin/main' into eof
rakita Mar 19, 2024
91d98c3
wip validation
rakita Mar 21, 2024
d28122e
EOF Bytecode validity
rakita Mar 23, 2024
77939ac
insturction and jump validation seems good
rakita Mar 23, 2024
28d1f74
merged eof validate function
rakita Mar 26, 2024
2db230a
EOP test runner, fex fixes
rakita Mar 26, 2024
f219ed6
RETURNDATALOAD, fix call bugs, refactor gas warm/cold calc
rakita Mar 27, 2024
e996fac
Merge remote-tracking branch 'origin/main' into eof
rakita Mar 27, 2024
8a4d9a2
debug session, rjumpv imm fix
rakita Mar 28, 2024
54d3d23
fixing validation bugs, bytecode decoder for EOF in revme
rakita Mar 31, 2024
c1c31ec
pass most of validation tests
rakita Apr 2, 2024
b8f7fad
bounds check moved to decode
rakita Apr 2, 2024
fedbd70
Merge remote-tracking branch 'origin/main' into eof
rakita Apr 2, 2024
605431c
Merge remote-tracking branch 'origin/main' into eof
rakita Apr 3, 2024
1ae9563
Fix merge compilation, fmt
rakita Apr 3, 2024
7b73779
TXCREATE work
rakita Apr 7, 2024
761aa2b
remove training wheels, panic on eof
rakita Apr 7, 2024
85d062b
Merge remote-tracking branch 'origin/main' into eof
rakita Apr 7, 2024
efa7327
test fix and std
rakita Apr 7, 2024
691ad44
std
rakita Apr 7, 2024
8faa75a
fix test
rakita Apr 7, 2024
4af1988
fix valgrind
rakita Apr 7, 2024
c337cd9
fix tests
rakita Apr 7, 2024
a19865f
clippy
rakita Apr 7, 2024
647a8d2
removed checked logic
rakita Apr 7, 2024
98745cc
small change
rakita Apr 7, 2024
65cf05c
no std revm-test
rakita Apr 7, 2024
6294f0f
Merge remote-tracking branch 'origin/main' into eof
rakita Apr 7, 2024
8162f32
check pending TODOs
rakita Apr 7, 2024
63ffb17
Merge remote-tracking branch 'origin/main' into eof
rakita Apr 7, 2024
0222cc0
build check no_std
rakita Apr 7, 2024
eeeb798
doc
rakita Apr 7, 2024
4c60857
chore: move some files. cleanup comments
rakita Apr 9, 2024
55c19ad
Merge remote-tracking branch 'origin/main' into eof
rakita Apr 9, 2024
d24dcbb
fix fmt,clippy and compile error
rakita Apr 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
eof rjump with tests
  • Loading branch information
rakita committed Feb 23, 2024
commit a638385db24b05908ecf879d93a9fab2b554ebaa
2 changes: 1 addition & 1 deletion crates/interpreter/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#[macro_use]
pub mod macros;

pub mod arithmetic;
pub mod bitwise;
pub mod contract;
Expand All @@ -15,4 +14,5 @@ pub mod memory;
pub mod opcode;
pub mod stack;
pub mod system;

pub use opcode::{Instruction, OpCode, OPCODE_JUMPMAP};
137 changes: 109 additions & 28 deletions crates/interpreter/src/instructions/control.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,54 @@
use revm_primitives::Bytes;

use crate::{
gas,
primitives::{Spec, U256},
primitives::{Bytes, Spec, U256},
Host, InstructionResult, Interpreter, InterpreterResult,
};

fn read_i16(ptr: *const u8) -> i16 {
unsafe { i16::from_be_bytes(core::slice::from_raw_parts(ptr, 2).try_into().unwrap()) }
}

pub fn rjump<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
error_on_disabled_eof!(interpreter);
gas!(interpreter, gas::BASE);
let offset = unsafe { *interpreter.instruction_pointer.cast::<i16>() } as isize;

let offset = read_i16(interpreter.instruction_pointer) as isize;
// In spec it is +3 but pointer is already incremented in
// `Interpreter::step` so for revm is +2.
interpreter.instruction_pointer = unsafe { interpreter.instruction_pointer.offset(offset + 2) };
}

pub fn rjumpi<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
error_on_disabled_eof!(interpreter);
gas!(interpreter, gas::CONDITION_JUMP_GAS);
pop!(interpreter, condition);
// In spec it is +3 but pointer is already incremented in
// `Interpreter::step` so for revm is +2.
let mut offset = 2;
if !condition.is_zero() {
offset += unsafe { *interpreter.instruction_pointer.cast::<i16>() } as isize;
offset += read_i16(interpreter.instruction_pointer) as isize;
}

interpreter.instruction_pointer = unsafe { interpreter.instruction_pointer.offset(offset) };
}

pub fn rjumpv<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
error_on_disabled_eof!(interpreter);
gas!(interpreter, gas::CONDITION_JUMP_GAS);
pop!(interpreter, case);
let case = as_isize_saturated!(case);

let max_index = interpreter.instruction_pointer.cast::<u8>() as isize;

// In spec it is (max_index+1)*2 but pointer is already incremented in
// `Interpreter::step` so for revm it is max_index*2 for destinations and +1 for max_index.
let mut offset = max_index * 2 + 1;
let max_index = unsafe { *interpreter.instruction_pointer } as isize;
// for number of items we are adding 1 to max_index, multiply by 2 as each offset is 2 bytes
// and add 1 for max_index itself. Note that revm already incremented the instruction pointer
let mut offset = (max_index + 1) * 2 + 1;

if case <= max_index {
offset += unsafe {
offset += read_i16(unsafe {
interpreter
.instruction_pointer
// offset for max_index that is one byte
.offset(1)
.cast::<i16>()
.offset(case)
} as isize;
.offset(1 + case * 2)
}) as isize;
}

interpreter.instruction_pointer = unsafe { interpreter.instruction_pointer.offset(offset) };
Expand Down Expand Up @@ -89,11 +90,17 @@ pub fn jumpdest_or_nop<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::JUMPDEST);
}

pub fn callf<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {}
pub fn callf<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
error_on_disabled_eof!(interpreter);
}

pub fn retf<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {}
pub fn retf<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
error_on_disabled_eof!(interpreter);
}

pub fn jumpf<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {}
pub fn jumpf<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
error_on_disabled_eof!(interpreter);
}

pub fn pc<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
panic_on_eof!(interpreter);
Expand Down Expand Up @@ -153,20 +160,94 @@ pub fn unknown<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {

#[cfg(test)]
mod test {
use revm_primitives::PragueSpec;

use super::*;
use crate::{opcode::RJUMP, Interpreter};
use crate::{
opcode::{make_instruction_table, NOP, RJUMP, RJUMPI, RJUMPV, STOP},
DummyHost, Gas, Interpreter,
};

#[test]
fn rjump() {
let table = make_instruction_table::<_, PragueSpec>();
let mut host = DummyHost::default();
let mut interp = Interpreter::new_bytecode(Bytes::from([RJUMP, 0x00, 0x02, STOP, STOP]));
interp.is_eof = true;
interp.gas = Gas::new(10000);

interp.step(&table, &mut host);
assert_eq!(interp.program_counter(), 5);
}

#[test]
fn sanity_rjump() {
let interp = Interpreter::new_bytecode(Bytes::from([RJUMP, 0x00, 0x00]));
fn rjumpi() {
let table = make_instruction_table::<_, PragueSpec>();
let mut host = DummyHost::default();
let mut interp = Interpreter::new_bytecode(Bytes::from([
RJUMPI, 0x00, 0x03, RJUMPI, 0x00, 0x01, STOP, STOP,
]));
interp.is_eof = true;
interp.stack.push(U256::from(1)).unwrap();
interp.stack.push(U256::from(0)).unwrap();
interp.gas = Gas::new(10000);

// dont jump
interp.step(&table, &mut host);
assert_eq!(interp.program_counter(), 3);
// jumps to last opcode
interp.step(&table, &mut host);
assert_eq!(interp.program_counter(), 7);
}

#[test]
fn ptr() {
let mut a = [1, 2, 3, 4, 5];
let ptr = a.as_ptr();
let ptr = ptr.cast::<u16>();
let slice = unsafe { core::slice::from_raw_parts(ptr, 2) };
assert_eq!(slice, [3, 4]);
fn rjumpv() {
let table = make_instruction_table::<_, PragueSpec>();
let mut host = DummyHost::default();
let mut interp = Interpreter::new_bytecode(Bytes::from([
RJUMPV,
0x01, // max index, 0 and 1
0x00, // first x0001
0x01,
0x00, // second 0x002
0x02,
NOP,
NOP,
NOP,
RJUMP,
0xFF,
(-12i8) as u8,
STOP,
]));
interp.is_eof = true;
interp.gas = Gas::new(1000);

// more then max_index
interp.stack.push(U256::from(10)).unwrap();
interp.step(&table, &mut host);
assert_eq!(interp.program_counter(), 6);

// cleanup
interp.step(&table, &mut host);
interp.step(&table, &mut host);
interp.step(&table, &mut host);
interp.step(&table, &mut host);
assert_eq!(interp.program_counter(), 0);

// jump to first index of vtable
interp.stack.push(U256::from(0)).unwrap();
interp.step(&table, &mut host);
assert_eq!(interp.program_counter(), 7);

// cleanup
interp.step(&table, &mut host);
interp.step(&table, &mut host);
interp.step(&table, &mut host);
assert_eq!(interp.program_counter(), 0);

// jump to second index of vtable
interp.stack.push(U256::from(1)).unwrap();
interp.step(&table, &mut host);
assert_eq!(interp.program_counter(), 8);
}
}
11 changes: 10 additions & 1 deletion crates/interpreter/src/instructions/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ macro_rules! error_on_static_call {
};
}

macro_rules! panic_on_eof {
macro_rules! error_on_disabled_eof {
($interp:expr) => {
if $interp.is_static {
$interp.instruction_result = InstructionResult::OpcodeDisabledInEof;
Expand All @@ -16,6 +16,15 @@ macro_rules! panic_on_eof {
};
}

macro_rules! panic_on_eof {
($interp:expr) => {
if $interp.is_eof {
$interp.instruction_result = InstructionResult::OpcodeDisabledInEof;
return;
}
};
}

macro_rules! check {
($interp:expr, $min:ident) => {
// TODO: Force const-eval on the condition with a `const {}` block once they are stable
Expand Down
2 changes: 2 additions & 0 deletions crates/interpreter/src/instructions/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ where
core::array::from_fn(|i| outer(table[i]))
}

pub const NOP: u8 = JUMPDEST;

// When adding new opcodes:
// 1. add the opcode to the list below; make sure it's sorted by opcode value
// 2. add its gas info in the `opcode_gas_info` function below
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ impl Interpreter {
///
/// Internally it will increment instruction pointer by one.
#[inline(always)]
fn step<FN, H: Host>(&mut self, instruction_table: &[FN; 256], host: &mut H)
pub(crate) fn step<FN, H: Host>(&mut self, instruction_table: &[FN; 256], host: &mut H)
where
FN: Fn(&mut Interpreter, &mut H),
{
Expand Down