Skip to content
Merged
Changes from 1 commit
Commits
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
jump stack check
  • Loading branch information
rakita committed May 13, 2024
commit 3510e2f20609eab74de261f4b1af31b689b5a60e
29 changes: 28 additions & 1 deletion crates/interpreter/src/instructions/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,17 @@ pub fn jumpf<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {

let idx = unsafe { read_u16(interpreter.instruction_pointer) } as usize;

// TODO(EOF) do types stack checks
// get target types
let Some(types) = interpreter.eof().unwrap().body.types_section.get(idx) else {
panic!("Invalid EOF in execution, expecting correct intermediate in jumpf")
};

// Check max stack height for target code section.
// safe to subtract as max_stack_height is always more than inputs.
if interpreter.stack.len() + (types.max_stack_size - types.inputs as u16) as usize > 1024 {
interpreter.instruction_result = InstructionResult::StackOverflow;
return;
}

interpreter.function_stack.set_current_code_idx(idx);
interpreter.load_eof_code(idx, 0)
Expand Down Expand Up @@ -409,4 +419,21 @@ mod test {
interp.step(&table, &mut host);
assert_eq!(interp.instruction_result, InstructionResult::Stop);
}

#[test]
fn jumpf_stack_overflow() {
let table = make_instruction_table::<_, PragueSpec>();
let mut host = DummyHost::default();

let bytes1 = Bytes::from([JUMPF, 0x00, 0x01]);
let bytes2 = Bytes::from([STOP]);
let mut interp =
eof_setup_with_types(bytes1, bytes2.clone(), TypesSection::new(0, 0, 1025));

// JUMPF
interp.step(&table, &mut host);

// stack overflow
assert_eq!(interp.instruction_result, InstructionResult::StackOverflow);
}
}