Skip to content

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

Guest debugging: handle functions with no program points properly. (#14677)

Chris Fallin4b80791e0278
diff --git a/crates/cranelift/src/compiler.rs b/crates/cranelift/src/compiler.rs
index fc33677345..cf1ea2b264 100644
--- a/crates/cranelift/src/compiler.rs
+++ b/crates/cranelift/src/compiler.rs
@@ -762,6 +762,9 @@ impl wasmtime_environ::Compiler for Compiler {
range.clone(),
func.buffer.call_sites(),
)?;
+ if self.tunables.debug_guest {
+ frame_tables.add_func_start(u32::try_from(range.start).unwrap());
+ }
if self.tunables.debug_guest
&& let Some(frame_layout) = func.buffer.frame_layout()
{
diff --git a/crates/environ/src/compile/frame_table.rs b/crates/environ/src/compile/frame_table.rs
index f3288f08f1..38f591d42b 100644
--- a/crates/environ/src/compile/frame_table.rs
+++ b/crates/environ/src/compile/frame_table.rs
@@ -197,6 +197,7 @@ impl FrameStateSlotBuilder {
/// - `num_slot_descriptors`: u32
/// - `num_progpoints`: u32
/// - `num_breakpoints`: u32
+/// - `num_funcs`: u32
/// - `frame_descriptor_pool_length`: u32
/// - `progpoint_descriptor_pool_length`: u32
/// - `breakpoint_patch_pool_length`: u32
@@ -218,6 +219,8 @@ impl FrameStateSlotBuilder {
/// - end of breakpoint patch data in pool: u32
/// (find the start by end of previous; patches are in the
/// pool in order and this saves storing redundant start/end values)
+/// - `num_funcs` times:
+/// - function start offset in text: u32 (sorted order)
/// - frame descriptors (format described above; `frame_descriptor_pool_length` bytes)
/// - progpoint descriptors (`progpoint_descriptor_pool_length` bytes)
/// - each descriptor: sequence of frames
@@ -245,6 +248,9 @@ pub struct FrameTableBuilder {
breakpoint_patch_data_ends: Vec<U32<LittleEndian>>,
breakpoint_patch_data: Vec<u8>,
+
+ /// Start offsets of every function in the text section.
+ func_starts: Vec<U32<LittleEndian>>,
}
impl FrameTableBuilder {
@@ -273,6 +279,21 @@ impl FrameTableBuilder {
index
}
+ /// Record the start of a function in the text section.
+ ///
+ /// This must be called for every function in the text section
+ /// (whether or not it has any program points), in ascending
+ /// order of offset, so that program-point lookups do not fall
+ /// through into a preceding function's metadata.
+ pub fn add_func_start(&mut self, native_pc: u32) {
+ debug_assert!(
+ self.func_starts
+ .last()
+ .map_or(true, |last| last.get(LittleEndian) <= native_pc)
+ );
+ self.func_starts.push(U32::new(LittleEndian, native_pc));
+ }
+
/// Add one program point.
pub fn add_program_point(
&mut self,
@@ -345,6 +366,8 @@ impl FrameTableBuilder {
f(&num_prog_points.to_le_bytes());
let num_breakpoints = u32::try_from(self.breakpoint_pcs.len()).unwrap();
f(&num_breakpoints.to_le_bytes());
+ let num_funcs = u32::try_from(self.func_starts.len()).unwrap();
+ f(&num_funcs.to_le_bytes());
let frame_descriptor_pool_length = u32::try_from(self.frame_descriptor_data.len()).unwrap();
f(&frame_descriptor_pool_length.to_le_bytes());
@@ -361,6 +384,7 @@ impl FrameTableBuilder {
f(object::bytes_of_slice(&self.breakpoint_pcs));
f(object::bytes_of_slice(&self.breakpoint_patch_offsets));
f(object::bytes_of_slice(&self.breakpoint_patch_data_ends));
+ f(object::bytes_of_slice(&self.func_starts));
f(&self.frame_descriptor_data);
f(object::bytes_of_slice(&self.progpoint_descriptor_data));
f(&self.breakpoint_patch_data);
diff --git a/crates/environ/src/frame_table.rs b/crates/environ/src/frame_table.rs
index c23a746b26..f5ac5ffeb5 100644
--- a/crates/environ/src/frame_table.rs
+++ b/crates/environ/src/frame_table.rs
@@ -58,6 +58,8 @@ pub struct FrameTable<'a> {
breakpoint_patch_data_ends: &'a [U32<LittleEndian>],
breakpoint_patch_data: &'a [u8],
+ func_starts: &'a [U32<LittleEndian>],
+
original_text: &'a [u8],
}
@@ -79,6 +81,10 @@ impl<'a> FrameTable<'a> {
.read::<U32<LittleEndian>>()
.map_err(|_| anyhow::anyhow!("Unable to read breakpoint count prefix"))?;
let num_breakpoints = usize::try_from(num_breakpoints.get(LittleEndian))?;
+ let num_funcs = data
+ .read::<U32<LittleEndian>>()
+ .map_err(|_| anyhow::anyhow!("Unable to read function count prefix"))?;
+ let num_funcs = usize::try_from(num_funcs.get(LittleEndian))?;
let frame_descriptor_pool_length = data
.read::<U32<LittleEndian>>()
@@ -118,6 +124,8 @@ impl<'a> FrameTable<'a> {
let (breakpoint_patch_data_ends, data) =
object::slice_from_bytes::<U32<LittleEndian>>(data, num_breakpoints)
.map_err(|_| anyhow::anyhow!("Unable to read breakpoint patch data ends slice"))?;
+ let (func_starts, data) = object::slice_from_bytes::<U32<LittleEndian>>(data, num_funcs)
+ .map_err(|_| anyhow::anyhow!("Unable to read function starts slice"))?;
let (frame_descriptor_data, data) = data
.split_at_checked(frame_descriptor_pool_length)
@@ -142,6 +150,7 @@ impl<'a> FrameTable<'a> {
breakpoint_patch_offsets,
breakpoint_patch_data_ends,
breakpoint_patch_data,
+ func_starts,
original_text,
})
}
@@ -175,6 +184,11 @@ impl<'a> FrameTable<'a> {
/// Get frames for the program point at the PC upper-bounded by a
/// given search PC (offset in text section).
+ ///
+ /// The search is bounded by the start of the function containing
+ /// the search PC: if that function has no program point at or
+ /// before the search PC, `None` is returned, rather than a
+ /// program point from a preceding function.
pub fn find_program_point(
&self,
search_pc: u32,
@@ -190,6 +204,22 @@ impl<'a> FrameTable<'a> {
Err(_) => return None,
};
+ // Find the start of the function containing the search
+ // point. If the result of the search above falls before the
+ // start of the function, that means that the function has no
+ // program points; so return `None`.
+ let func_start = match self.func_starts.binary_search_by_key(&key, |entry| {
+ FrameInstPos::encode(entry.get(LittleEndian), FrameInstPos::Pre)
+ }) {
+ Ok(idx) => self.func_starts[idx].get(LittleEndian),
+ Err(idx) if idx > 0 => self.func_starts[idx - 1].get(LittleEndian),
+ Err(_) => return None,
+ };
+ let func_start_key = FrameInstPos::encode(func_start, FrameInstPos::Pre);
+ if self.progpoint_pcs[index].get(LittleEndian) < func_start_key {
+ return None;
+ }
+
Some(self.program_point_frame_iter(index))
}
diff --git a/crates/wasmtime/src/runtime/debug.rs b/crates/wasmtime/src/runtime/debug.rs
index c5a8fbd7cf..85da2462dc 100644
--- a/crates/wasmtime/src/runtime/debug.rs
+++ b/crates/wasmtime/src/runtime/debug.rs
@@ -681,19 +681,20 @@ impl VirtualFrame {
.expect("Wasm frame PC does not correspond to a module");
let table = store_code.code_memory().frame_table().unwrap();
let pc = u32::try_from(pc).expect("PC offset too large");
- let program_points = table.find_program_point(pc, FrameInstPos::Post)
- .expect("There must be a program point record in every frame when debug instrumentation is enabled");
-
- (
- store_code,
- program_points
- .map(|(wasm_pc, frame_descriptor, stack_shape)| VirtualFrame {
- wasm_pc,
- frame_descriptor,
- stack_shape,
- })
- .collect(),
- )
+ // Functions without any program points (e.g. the module
+ // start function, or trampolines) have no virtual frames.
+ let frames = table
+ .find_program_point(pc, FrameInstPos::Post)
+ .into_iter()
+ .flatten()
+ .map(|(wasm_pc, frame_descriptor, stack_shape)| VirtualFrame {
+ wasm_pc,
+ frame_descriptor,
+ stack_shape,
+ })
+ .collect();
+
+ (store_code, frames)
}
}
diff --git a/tests/all/debug.rs b/tests/all/debug.rs
index d31131cd38..0b3be505e0 100644
--- a/tests/all/debug.rs
+++ b/tests/all/debug.rs
@@ -3,8 +3,8 @@
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use wasmtime::{
- AsContextMut, Caller, Config, DebugEvent, DebugHandler, Engine, Extern, FrameHandle, Func,
- Global, GlobalType, Inlining, Instance, Module, ModulePC, Mutability, Result, Store,
+ AsContextMut, Caller, Collector, Config, DebugEvent, DebugHandler, Engine, Extern, FrameHandle,
+ Func, Global, GlobalType, Inlining, Instance, Module, ModulePC, Mutability, Result, Store,
StoreContextMut, Val, ValType,
};
@@ -1760,3 +1760,35 @@ async fn take_exception_in_debug_handler() -> Result<()> {
}
}
}
+
+#[test]
+#[cfg_attr(miri, ignore)]
+fn gc_in_func_without_program_points() -> wasmtime::Result<()> {
+ for collector in [Collector::Copying, Collector::DeferredReferenceCounting] {
+ let mut config = Config::default();
+ config.guest_debug(true).wasm_gc(true).collector(collector);
+ let engine = Engine::new(&config)?;
+ let module = Module::new(
+ &engine,
+ r#"
+ (module
+ (type $a (array (mut i8)))
+ (func (export "f")
+ (local externref externref externref externref
+ externref externref externref externref
+ externref externref externref externref
+ externref externref externref externref
+ externref externref externref externref
+ externref externref externref externref
+ externref externref externref externref
+ externref externref externref externref))
+ (global (ref $a) (array.new_default $a (i32.const 100000)))
+ (global (ref $a) (array.new_default $a (i32.const 100000)))
+ (global (ref $a) (array.new_default $a (i32.const 100000))))
+ "#,
+ )?;
+ let mut store = Store::new(&engine, ());
+ Instance::new(&mut store, &module, &[])?;
+ }
+ Ok(())
+}