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
wip eof verification
  • Loading branch information
rakita committed Mar 19, 2024
commit 07c721a4e9c4387d84caa10cdf4c834ce512380c
116 changes: 97 additions & 19 deletions crates/interpreter/src/instructions/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ pub struct OpCodeInfo {
pub name: &'static str,
pub inputs: u8,
pub outputs: u8,
// TODO make this a bitfield
pub is_eof: bool,
// If the opcode is return from execution. aka STOP,RETURN, ..
pub is_terminating_opcode: bool,
pub size: u8,
}

Expand All @@ -245,6 +248,7 @@ impl OpCodeInfo {
inputs: 0,
outputs: 0,
is_eof: true,
is_terminating_opcode: false,
size: 1,
}
}
Expand Down Expand Up @@ -295,7 +299,7 @@ macro_rules! opcodes {
}

pub const fn not_eof(mut opcode: OpCodeInfo) -> OpCodeInfo {
opcode.is_eof = true;
opcode.is_eof = false;
opcode
}

Expand All @@ -304,6 +308,11 @@ pub const fn size<const N: u8>(mut opcode: OpCodeInfo) -> OpCodeInfo {
opcode
}

pub const fn terminating(mut opcode: OpCodeInfo) -> OpCodeInfo {
opcode.is_terminating_opcode = true;
opcode
}

pub const fn stack_io<const I: u8, const O: u8>(mut opcode: OpCodeInfo) -> OpCodeInfo {
opcode.inputs = I;
opcode.outputs = O;
Expand All @@ -316,7 +325,7 @@ pub const fn stack_io<const I: u8, const O: u8>(mut opcode: OpCodeInfo) -> OpCod
// 3. implement the opcode in the corresponding module;
// the function signature must be the exact same as the others
opcodes! {
0x00 => STOP => control::stop => stack_io<0,0>;
0x00 => STOP => control::stop => stack_io<0,0>, terminating;

0x01 => ADD => arithmetic::wrapping_add => stack_io<2, 1>;
0x02 => MUL => arithmetic::wrapping_mul => stack_io<2, 1>;
Expand Down Expand Up @@ -404,8 +413,8 @@ opcodes! {
0x53 => MSTORE8 => memory::mstore8 => stack_io<2, 0>;
0x54 => SLOAD => host::sload::<H, SPEC> => stack_io<1, 1>;
0x55 => SSTORE => host::sstore::<H, SPEC> => stack_io<2, 0>;
0x56 => JUMP => control::jump => stack_io<1, 0>;
0x57 => JUMPI => control::jumpi => stack_io<2, 0>;
0x56 => JUMP => control::jump => stack_io<1, 0>, not_eof;
0x57 => JUMPI => control::jumpi => stack_io<2, 0>, not_eof;
0x58 => PC => control::pc => stack_io<0, 1>;
0x59 => MSIZE => memory::msize => stack_io<0, 1>;
0x5A => GAS => system::gas => stack_io<0, 1>, not_eof;
Expand Down Expand Up @@ -531,7 +540,7 @@ opcodes! {
// 0xCE
// 0xCF
0xD0 => DATALOAD => data::data_load => stack_io<1, 1>;
0xD1 => DATALOADN => data::data_loadn => stack_io<0, 1>;
0xD1 => DATALOADN => data::data_loadn => stack_io<0, 1>, size<3>;
0xD2 => DATASIZE => data::data_size => stack_io<0, 1>;
0xD3 => DATACOPY => data::data_copy => stack_io<3, 0>;
// 0xD4
Expand All @@ -546,26 +555,26 @@ opcodes! {
// 0xDD
// 0xDE
// 0xDF
0xE0 => RJUMP => control::rjump => stack_io<0, 0>;
0xE1 => RJUMPI => control::rjumpi => stack_io<1, 0>;
0xE2 => RJUMPV => control::rjumpv => stack_io<1, 0>;
0xE3 => CALLF => control::callf => stack_io<0, 0>;
0xE4 => RETF => control::retf => stack_io<0, 0>;
0xE5 => JUMPF => control::jumpf => stack_io<0, 0>;
0xE6 => DUPN => stack::dupn => stack_io<0, 0>;
0xE7 => SWAPN => stack::swapn => stack_io<0, 0>;
0xE8 => EXCHANGE => stack::exchange => stack_io<0, 0>;
0xE0 => RJUMP => control::rjump => stack_io<0, 0>, size<3>;
0xE1 => RJUMPI => control::rjumpi => stack_io<1, 0>, size<3>;
0xE2 => RJUMPV => control::rjumpv => stack_io<1, 0>, size<3>;
0xE3 => CALLF => control::callf => stack_io<0, 0>, size<3>;
0xE4 => RETF => control::retf => stack_io<0, 0>, terminating;
0xE5 => JUMPF => control::jumpf => stack_io<0, 0>, size<3>;
0xE6 => DUPN => stack::dupn => stack_io<0, 0>, size<2>;
0xE7 => SWAPN => stack::swapn => stack_io<0, 0>, size<2>;
0xE8 => EXCHANGE => stack::exchange => stack_io<0, 0>, size<2>;
// 0xE9
// 0xEA
// 0xEB
0xEC => EOFCREATE => contract::eofcreate::<H> => stack_io<4, 1>;
0xED => CREATE4 => contract::txcreate::<H> => stack_io<5, 1>;
0xEE => RETURNCONTRACT => contract::return_contract::<H> => stack_io<0, 0>; // TODO(EOF) input/output
0xEE => RETURNCONTRACT => contract::return_contract::<H> => stack_io<0, 0>, terminating; // TODO(EOF) input/output
// 0xEF
0xF0 => CREATE => contract::create::<false, H, SPEC> => stack_io<4, 1>;
0xF1 => CALL => contract::call::<H, SPEC> => stack_io<7, 1>, not_eof;
0xF2 => CALLCODE => contract::call_code::<H, SPEC> => stack_io<7, 1>, not_eof;
0xF3 => RETURN => control::ret => stack_io<0, 0>;
0xF3 => RETURN => control::ret => stack_io<0, 0>, terminating;
0xF4 => DELEGATECALL => contract::delegate_call::<H, SPEC> => stack_io<6, 1>, not_eof;
0xF5 => CREATE2 => contract::create::<true, H, SPEC> => stack_io<5, 1>;
// 0xF6
Expand All @@ -575,9 +584,9 @@ opcodes! {
0xFA => STATICCALL => contract::static_call::<H, SPEC> => stack_io<6, 1>, not_eof;
0xFB => EXTSCALL => contract::extscall::<H> => stack_io<3, 1>;
// 0xFC
0xFD => REVERT => control::revert::<H, SPEC> => stack_io<0, 0>;
0xFE => INVALID => control::invalid => stack_io<0, 0>;
0xFF => SELFDESTRUCT => host::selfdestruct::<H, SPEC> => stack_io<1, 0>, not_eof;
0xFD => REVERT => control::revert::<H, SPEC> => stack_io<0, 0>, terminating;
0xFE => INVALID => control::invalid => stack_io<0, 0>, not_eof, terminating;
0xFF => SELFDESTRUCT => host::selfdestruct::<H, SPEC> => stack_io<1, 0>, not_eof, terminating;
}

#[cfg(test)]
Expand All @@ -593,4 +602,73 @@ mod tests {
assert_eq!(opcode.as_str(), "STOP");
assert_eq!(opcode.get(), 0x00);
}

const REJECTED_IN_EOF: &[u8] = &[
0x38, 0x39, 0x3b, 0x3c, 0x3f, 0x5a, 0xf1, 0xf2, 0xf4, 0xfa, 0xff,
];

#[test]
fn test_eof_disable() {
for opcode in REJECTED_IN_EOF.iter() {
let opcode = OpCode::new(*opcode).unwrap();
assert_eq!(
opcode.info().is_eof,
false,
"Opcode {:?} is not EOF",
opcode
);
}
}

#[test]
fn test_opcode_size() {
let mut opcodes = [0u8; 256];
// PUSH opcodes
for push in PUSH1..PUSH32 {
opcodes[push as usize] = push - PUSH1 + 1;
}
opcodes[DATALOADN as usize] = 3;
opcodes[RJUMP as usize] = 3;
opcodes[RJUMPI as usize] = 3;
opcodes[RJUMPV as usize] = 3;
opcodes[CALLF as usize] = 3;
opcodes[JUMPF as usize] = 3;
opcodes[DUPN as usize] = 2;
opcodes[SWAPN as usize] = 2;
opcodes[EXCHANGE as usize] = 2;
}

#[test]
fn test_enabled_opcodes() {
// List obtained from https://eips.ethereum.org/EIPS/eip-3670
let opcodes = [
0x10..=0x1d,
0x20..=0x20,
0x30..=0x3f,
0x40..=0x48,
0x50..=0x5b,
0x5f..=0x54,
0x60..=0x6f,
0x70..=0x7f,
0x80..=0x8f,
0x90..=0x9f,
0xa0..=0xa4,
0xf0..=0xf5,
0xfa..=0xfa,
0xfd..=0xfd,
//0xfe,
0xff..=0xff,
];
for i in opcodes {
for opcode in i {
OpCode::new(opcode).expect("Opcode should be valid and enabled");
}
}
}

#[test]
pub fn test_terminating_opcodes() {
// TODO check JUMPF
let terminating = [RETF, JUMPF];
}
}
64 changes: 61 additions & 3 deletions crates/interpreter/src/interpreter/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,45 @@ pub fn validate_eof_bytecode(code: &[u8], types: &TypesSection) -> Result<(), ()
let mut iter = code.as_ptr();
let end = code.as_ptr().wrapping_add(code.len());

let mut eof_table = [0; 256];
let is_returning = false;

// all bytes that are intermediate.
let jumptable = vec![false; code.len()];

// We can check validity and jump destinations in one pass.
while iter < end {
let opcode = unsafe { *iter };
let opcode_info = &OPCODE_INFO_JUMPTABLE[opcode as usize];
let op = unsafe { *iter };
let opcode_info = &OPCODE_INFO_JUMPTABLE[op as usize];

// Unknown opcode
let Some(opcode) = opcode_info else {
return Err(());
};

// check if the size of the opcode is within the bounds of the code
if unsafe { iter.add(opcode.size as usize - 1) } > end {
return Err(());
}

match op {
opcode::RJUMPV | opcode::RJUMP | opcode::RJUMPI => {
// check jump destination with bytecode size.

// check if jump destination is valid
}
opcode::CALLF => {
// check codes size.
// targeted code needs to have zero outputs (be non returning).
}
opcode::RETF => {
// check if it is returning. TODO here
}
_ => {}
}

// if let Some(jump) = opcode_info.jump {
// eof_table[jump as usize] += 1;
// }

// if opcode::JUMPDEST == opcode {
// // SAFETY: jumps are max length of the code
Expand Down Expand Up @@ -133,3 +167,27 @@ pub fn validate_eof_bytecode(code: &[u8], types: &TypesSection) -> Result<(), ()

Ok(())
}

/// Validate stack requirements and if all codes sections are used.
pub fn validate_stack_requirement(codes: &[u8]) -> Result<(), ()> {
#[derive(Copy, Clone)]
struct StackInfo {
min: u16,
max: u16,
}
let mut code_stack_access: Vec<Option<StackInfo>> = vec![None; codes.len()];
let mut worklist: Vec<u16> = Vec::new();

while let Some(workitem) = worklist.pop() {}

// Iterate over accessed code, error on not accessed opcode and return max stack requirement.
let mut max_stack_requirement = 0;
for opcode in code_stack_access {
if let Some(opcode) = opcode {
max_stack_requirement = core::cmp::max(opcode.max, max_stack_requirement);
} else {
return Err(());
}
}
Ok(())
}