Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Debugging: PC in a frame at a callsite should be the return address, not the call. (#12750)

Chris Fallin9593a3a1c970
diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rs
index 22849dbb16..5eccf9732d 100644
--- a/crates/cranelift/src/func_environ.rs
+++ b/crates/cranelift/src/func_environ.rs
@@ -223,6 +223,12 @@ pub struct FuncEnvironment<'module_environment> {
/// The stack-slot used for exposing Wasm state via debug
/// instrumentation, if any, and the builder containing its metadata.
pub(crate) state_slot: Option<(ir::StackSlot, FrameStateSlotBuilder)>,
+
+ /// The next-srcloc: the location of the operator *after* this one
+ /// (in original bytecode order, i.e., not accounting for
+ /// nonlinear control flow). This is useful in cases where we need
+ /// to e.g. record the return-address of a callsite for debuginfo.
+ pub(crate) next_srcloc: ir::SourceLoc,
}
impl<'module_environment> FuncEnvironment<'module_environment> {
@@ -284,6 +290,7 @@ impl<'module_environment> FuncEnvironment<'module_environment> {
stack_switching_values_buffer: None,
state_slot: None,
+ next_srcloc: ir::SourceLoc::default(),
}
}
diff --git a/crates/cranelift/src/translate/code_translator.rs b/crates/cranelift/src/translate/code_translator.rs
index 79d8edf56c..4648647ef9 100644
--- a/crates/cranelift/src/translate/code_translator.rs
+++ b/crates/cranelift/src/translate/code_translator.rs
@@ -664,8 +664,13 @@ pub fn translate_operator(
let mut args = environ.stacks.peekn(num_args).to_vec();
bitcast_wasm_params(environ, sig_ref, &mut args, builder);
- let inst_results =
- environ.translate_call(builder, srcloc, function_index, sig_ref, &args)?;
+ let inst_results = environ.translate_call(
+ builder,
+ environ.next_srcloc,
+ function_index,
+ sig_ref,
+ &args,
+ )?;
debug_assert_eq!(
inst_results.len(),
@@ -693,7 +698,7 @@ pub fn translate_operator(
let inst_results = environ.translate_call_indirect(
builder,
- srcloc,
+ environ.next_srcloc,
validator.features(),
TableIndex::from_u32(*table_index),
type_index,
@@ -2632,7 +2637,7 @@ pub fn translate_operator(
bitcast_wasm_params(environ, sigref, &mut args, builder);
let inst_results =
- environ.translate_call_ref(builder, srcloc, sigref, callee, &args)?;
+ environ.translate_call_ref(builder, environ.next_srcloc, sigref, callee, &args)?;
debug_assert_eq!(
inst_results.len(),
diff --git a/crates/cranelift/src/translate/func_translator.rs b/crates/cranelift/src/translate/func_translator.rs
index bda7dd4deb..b687574550 100644
--- a/crates/cranelift/src/translate/func_translator.rs
+++ b/crates/cranelift/src/translate/func_translator.rs
@@ -257,6 +257,7 @@ fn parse_function_body(
builder.set_srcloc(cur_srcloc(&reader.get_binary_reader()));
let op = reader.read()?;
+ environ.next_srcloc = cur_srcloc(&reader.get_binary_reader());
let operand_types =
validate_op_and_get_operand_types(validator, environ, &mut operand_types, &op, pos)?;
diff --git a/tests/all/debug.rs b/tests/all/debug.rs
index e8f5cacdce..4614313ec1 100644
--- a/tests/all/debug.rs
+++ b/tests/all/debug.rs
@@ -109,7 +109,7 @@ fn stack_values_two_frames() -> wasmtime::Result<()> {
);
assert_eq!(
stack.wasm_function_index_and_pc(&mut caller)?.unwrap().1,
- 65
+ 67
);
assert_eq!(stack.num_locals(&mut caller)?, 2);
@@ -130,7 +130,7 @@ fn stack_values_two_frames() -> wasmtime::Result<()> {
);
assert_eq!(
stack.wasm_function_index_and_pc(&mut caller)?.unwrap().1,
- 55
+ 57
);
let stack = stack.parent(&mut caller)?;
@@ -290,7 +290,7 @@ fn stack_values_two_activations() -> wasmtime::Result<()> {
);
assert_eq!(
stack.wasm_function_index_and_pc(&mut caller)?.unwrap().1,
- 56
+ 58
);
assert!(Module::same(
stack.module(&mut caller)?.unwrap(),
@@ -318,7 +318,7 @@ fn stack_values_two_activations() -> wasmtime::Result<()> {
);
assert_eq!(
stack.wasm_function_index_and_pc(&mut caller)?.unwrap().1,
- 56
+ 58
);
assert!(Module::same(
stack.module(&mut caller)?.unwrap(),
diff --git a/tests/all/pulley.rs b/tests/all/pulley.rs
index 151cebb24c..3298e9a9e4 100644
--- a/tests/all/pulley.rs
+++ b/tests/all/pulley.rs
@@ -147,7 +147,7 @@ fn pulley_provenance_test() -> Result<()> {
.unwrap()
.unwrap();
assert_eq!(caller_func.as_u32(), 3);
- assert_eq!(pc, 416);
+ assert_eq!(pc, 418);
let parent_frame = caller_frame.parent(&mut caller).unwrap();
assert!(parent_frame.is_none());
diff --git a/tests/disas/debug-exceptions.wat b/tests/disas/debug-exceptions.wat
index b21f539483..ad911f7302 100644
--- a/tests/disas/debug-exceptions.wat
+++ b/tests/disas/debug-exceptions.wat
@@ -116,7 +116,7 @@
;; ldr x2, [x1, #0x40]
;; ldur x3, [sp, #0x10]
;; blr x0
-;; ╰─╼ debug frame state (after previous inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 72, slot at FP-0xc0, locals , stack I32 @ slot+0x8
+;; ╰─╼ debug frame state (after previous inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 74, slot at FP-0xc0, locals , stack I32 @ slot+0x8
;; 158: ldur x0, [sp, #0x10]
;; nop
;; ├─╼ debug frame state (after previous inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 74, slot at FP-0xc0, locals , stack I32 @ slot+0x8