Skip to content

Commit

Permalink
Fix instruction length setting in asm's step() (#181)
Browse files Browse the repository at this point in the history
* Fix instruction length setting in asm's step()

* Make decoder mutable in test_asm_step()
  • Loading branch information
mohanson authored Jul 27, 2021
1 parent 56ece0c commit 11f3c00
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/machine/asm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,8 @@ impl<'a> AsmMachine<'a> {
let pc = *self.machine.pc();
let slot = calculate_slot(pc);
let mut trace = Trace::default();
let mut instruction = decoder.decode(self.machine.memory_mut(), pc)?;
let instruction = decoder.decode(self.machine.memory_mut(), pc)?;
let len = instruction_length(instruction) as u8;
instruction |= u64::from(len) << 24;
trace.instructions[0] = instruction;
trace.cycles += self
.machine
Expand Down
23 changes: 23 additions & 0 deletions tests/test_asm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(has_asm)]

use ckb_vm::{
decoder::build_decoder,
machine::{
asm::{AsmCoreMachine, AsmMachine},
CoreMachine, VERSION0, VERSION1,
Expand Down Expand Up @@ -390,3 +391,25 @@ pub fn test_decoder_instructions_cache_pc_out_of_bound_timeout() {
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::OutOfBound);
}

pub fn test_asm_step() {
let buffer = fs::read("tests/programs/simple64").unwrap().into();
let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value());
let core = DefaultMachineBuilder::new(asm_core).build();
let mut machine = AsmMachine::new(core, None);
machine
.load_program(&buffer, &vec!["simple64".into()])
.unwrap();

let result = || -> Result<i8, Error> {
let mut decoder = build_decoder::<u64>(ISA_IMC);
machine.machine.set_running(true);
while machine.machine.running() {
machine.step(&mut decoder)?;
}
Ok(machine.machine.exit_code())
}();

assert!(result.is_ok());
assert_eq!(result.unwrap(), 0);
}

0 comments on commit 11f3c00

Please sign in to comment.