Skip to content

Commit

Permalink
chore: Small UX enhancements (#122)
Browse files Browse the repository at this point in the history
* When error occurs, print the full current machine status to stdout
* Add a new option to disable overlapping detection between stack and
heap. By default, ckb-vm will have stack living at higher addresses,
but this is not a hard requirements. Many contracts can be executed
perfectly fine when we move stack to lower addresses compared to heap.
  • Loading branch information
xxuejie authored Dec 6, 2023
1 parent 72bfa02 commit 2b9408c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
14 changes: 13 additions & 1 deletion ckb-debugger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.takes_value(true)
.conflicts_with_all(&["bin", "tx-file"]),
)
.arg(
Arg::with_name("disable-overlapping-detection")
.long("disable-overlapping-detection")
.required(false)
.takes_value(false)
.help("Set to true to disable overlapping detection between stack and heap"),
)
.get_matches();

let matches_decode = matches.value_of_lossy("decode");
Expand Down Expand Up @@ -371,7 +378,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
};

if matches_mode == "full" {
let mut machine = PProfMachine::new(machine_init(), Profile::new(&verifier_program)?);
let mut machine = PProfMachine::new(
machine_init(),
Profile::new(&verifier_program)?
.set_disable_overlapping_detection(matches.is_present("disable-overlapping-detection")),
);
let bytes = machine.load_program(&verifier_program, &verifier_args_byte)?;
let transferred_cycles = transferred_byte_cycles(bytes);
machine.machine.add_cycles(transferred_cycles)?;
Expand Down Expand Up @@ -405,6 +416,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
machine.profile.display_stacktrace(" ", &mut std::io::stdout());
println!("Error:");
println!(" {:?}", err);
println!("Machine: {}", machine.machine);
}
}
return Ok(());
Expand Down
21 changes: 15 additions & 6 deletions ckb-vm-pprof/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pub struct Profile {
cache_fun: HashMap<u64, String>,
sbrk_addr: u64,
sbrk_heap: u64,
disable_overlapping_detection: bool,
}

impl Profile {
Expand All @@ -146,9 +147,15 @@ impl Profile {
cache_fun: goblin_fun(&elf),
sbrk_addr: goblin_get_sym(&elf, "_sbrk"),
sbrk_heap: goblin_get_sym(&elf, "_end"),
disable_overlapping_detection: false,
})
}

pub fn set_disable_overlapping_detection(mut self, disable_detection: bool) -> Self {
self.disable_overlapping_detection = disable_detection;
self
}

pub fn get_tag(&mut self, addr: u64) -> Tags {
if let Some(data) = self.cache_tag.get(&addr) {
return data.clone();
Expand Down Expand Up @@ -205,12 +212,14 @@ impl Profile {
decoder: &mut Decoder,
) -> Result<(), Error> {
let pc = machine.pc().to_u64();
let sp = machine.registers()[SP].to_u64();
if sp < self.sbrk_heap {
return Err(Error::External(format!(
"Heap and stack overlapping sp={} heap={}",
sp, self.sbrk_heap
)));
if !self.disable_overlapping_detection {
let sp = machine.registers()[SP].to_u64();
if sp < self.sbrk_heap {
return Err(Error::External(format!(
"Heap and stack overlapping sp={} heap={}",
sp, self.sbrk_heap
)));
}
}
let inst = decoder.decode(machine.memory_mut(), pc)?;
let opcode = ckb_vm::instructions::extract_opcode(inst);
Expand Down

0 comments on commit 2b9408c

Please sign in to comment.