Skip to content

Commit 0708d50

Browse files
committed
fix trace_offset calculation
1 parent 94631d4 commit 0708d50

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

crates/wasmi/src/engine/translator/func/encoder.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{Reset, ReusableAllocations};
22
use crate::{
3+
ir::OpCode,
34
core::FuelCostsProvider,
45
engine::{
56
executor::op_code_to_handler,
@@ -779,7 +780,14 @@ fn trace_branch_offset(src: BytePos, dst: Pos<Op>) -> Result<BranchOffset, Error
779780
fn trace_offset_or_none(src: BytePos, dst: BytePos) -> Option<BranchOffset> {
780781
let src = isize::try_from(usize::from(src)).ok()?;
781782
let dst = isize::try_from(usize::from(dst)).ok()?;
782-
let offset = dst.checked_sub(src)?;
783+
// Offset branch-offset by op-code encoded size since
784+
// execution handlers for operations act on `ip` pointing
785+
// to the first operand instead of the op-code.
786+
let op_code = match cfg!(feature = "compact") {
787+
true => mem::size_of::<OpCode>(),
788+
false => mem::size_of::<usize>(), // direct-threading uses fn-ptr sized op-codes
789+
};
790+
let offset = dst.checked_sub(src)?.checked_sub_unsigned(op_code)?;
783791
i32::try_from(offset).map(BranchOffset::from).ok()
784792
}
785793
let Some(offset) = trace_offset_or_none(src, BytePos::from(dst)) else {

0 commit comments

Comments
 (0)