Skip to content

Truly independent web browser

LibJS: Root the functions of stack traces

Shannon Boothd4165d80c26e
diff --git a/Libraries/LibJS/Rust/src/console.rs b/Libraries/LibJS/Rust/src/console.rs
index c9c73d2e1c6..00197eb3435 100644
--- a/Libraries/LibJS/Rust/src/console.rs
+++ b/Libraries/LibJS/Rust/src/console.rs
@@ -359,7 +359,7 @@ impl Console {
let stack_trace = vm.stack_trace();
// NOTE: Skip the first frame (console.trace() itself)
- for element in stack_trace.iter().skip(1) {
+ for element in stack_trace.values().iter().skip(1) {
let function_name = element
.function
.map(|function| function.name_for_call_stack())
diff --git a/Libraries/LibJS/Rust/src/debugger.rs b/Libraries/LibJS/Rust/src/debugger.rs
index 54176e0d38a..a5443c4bada 100644
--- a/Libraries/LibJS/Rust/src/debugger.rs
+++ b/Libraries/LibJS/Rust/src/debugger.rs
@@ -68,12 +68,12 @@ pub enum ResumeMode {
/// What the debugger tells its pause callback about where execution paused. It only lives on the stack while the
/// callback runs, where the collector finds the cells it holds.
-pub struct PauseInfo {
+pub struct PauseInfo<'a> {
pub executable: Gc<Executable>,
pub bytecode_offset: u32,
pub source_range: Option<SourceRange>,
/// The frames from the paused one down, the first of which is where execution paused.
- pub stack_trace: Vec<StackTraceElement>,
+ pub stack_trace: &'a [StackTraceElement],
pub breakpoint_ids: Vec<BreakpointID>,
pub exception: Option<Value>,
pub exception_will_be_caught: bool,
@@ -89,7 +89,7 @@ pub struct FrameBinding {
/// The callback must call continue_execution() before it returns. It may evaluate JavaScript, in which case any pause
/// triggered by that evaluation is ignored.
-pub type PauseCallback = Rc<dyn Fn(&Vm, &PauseInfo)>;
+pub type PauseCallback = Rc<dyn Fn(&Vm, &PauseInfo<'_>)>;
#[derive(Clone, Copy, PartialEq, Eq)]
enum BindingStorage {
@@ -158,7 +158,7 @@ impl Debugger {
}
}
- pub fn set_pause_callback(&self, callback: impl Fn(&Vm, &PauseInfo) + 'static) {
+ pub fn set_pause_callback(&self, callback: impl Fn(&Vm, &PauseInfo<'_>) + 'static) {
*self.pause_callback.borrow_mut() = Some(Rc::new(callback));
}
@@ -195,16 +195,22 @@ impl Debugger {
}
self.is_paused.set(true);
- let mut stack_trace = vm.stack_trace();
- assert!(!stack_trace.is_empty());
+ let stack_trace = vm.stack_trace();
+ let paused_frame = stack_trace.get(0).expect("execution pauses in a frame");
let source_range = executable.source_range_at(bytecode_offset);
- stack_trace[0].source_range = source_range.clone();
+ stack_trace.set(
+ 0,
+ StackTraceElement {
+ source_range: source_range.clone(),
+ ..paused_frame
+ },
+ );
// NB: Frames that JIT code creates get their ids when the debugger first needs them, which is when it steps
// from a paused frame.
// SAFETY: The frames of the stack trace are live while execution is paused.
- vm.frame_id_of(unsafe { stack_trace[0].execution_context.as_ref() });
+ vm.frame_id_of(unsafe { paused_frame.execution_context.as_ref() });
self.paused_execution_context
- .set(stack_trace[0].execution_context.as_ptr().cast_const());
+ .set(paused_frame.execution_context.as_ptr().cast_const());
*self.paused_source_range.borrow_mut() = source_range.clone();
pause_callback(
vm,
@@ -212,7 +218,7 @@ impl Debugger {
executable,
bytecode_offset,
source_range,
- stack_trace,
+ stack_trace: &stack_trace.values(),
breakpoint_ids: executable.debugger_breakpoints_at(bytecode_offset),
exception,
exception_will_be_caught,
diff --git a/Libraries/LibJS/Rust/src/embedding/vm.rs b/Libraries/LibJS/Rust/src/embedding/vm.rs
index 247bcd21346..88296611cab 100644
--- a/Libraries/LibJS/Rust/src/embedding/vm.rs
+++ b/Libraries/LibJS/Rust/src/embedding/vm.rs
@@ -153,8 +153,9 @@ pub unsafe extern "C" fn js_vm_stack_trace(vm: *mut JSVM, sink: *const JSStackTr
// SAFETY: The caller passes a live VM and a valid sink.
let (vm, sink) = unsafe { (vm_from_abi(vm), &*sink) };
let append = sink.append.expect("a stack trace sink has an append function");
- for element in vm.stack_trace() {
- let element = stack_frame_into_abi(&element);
+ let stack_trace = vm.stack_trace();
+ for element in stack_trace.values().iter() {
+ let element = stack_frame_into_abi(element);
// SAFETY: The embedder's sink takes elements with the context it came with, and only reads them during the
// call, while the trace's contexts and their executables are alive.
unsafe { append(sink.context, &raw const element) };
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rs
index 1be74e30f94..4c1ec3f69bc 100644
--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs
@@ -1291,8 +1291,8 @@ impl Vm {
}
/// The frames of every execution context, from the running one down, with where each is in its executable.
- pub fn stack_trace(&self) -> Vec<StackTraceElement> {
- let mut stack_trace = Vec::new();
+ pub fn stack_trace(&self) -> MarkedVec<'_, StackTraceElement> {
+ let stack_trace = MarkedVec::new(self);
self.for_each_stack_frame_top_to_bottom(|frame| {
// NB: Builtins written in JavaScript show up like other builtins, without a position in their source.
let runs_builtin = frame
@@ -1335,7 +1335,10 @@ impl Vm {
let caller = unsafe { &*caller };
let site = return_pc & !INLINED_CALL_SITE_BIT;
let frame_pointer = crate::jit::translate::frame_pointer_from_low_half(context.caller_dst_raw.get());
- for frame in crate::jit::translate::call_site_frames(self, caller, site, frame_pointer) {
+ for frame in crate::jit::translate::call_site_frames(self, caller, site, frame_pointer)
+ .values()
+ .iter()
+ {
callback(StackFrame {
execution_context: caller,
function: Some(frame.function),
@@ -2055,9 +2058,12 @@ pub struct TypeErrorRealmOverride {
/// An element of VM::stack_trace(): a frame's execution context, its function and where it is in its executable, if it
/// runs one. A frame of a call that JIT code inlined has the execution context of the function it is inlined in.
+#[derive(Clone, Trace)]
pub struct StackTraceElement {
+ #[gc(untraced)]
pub execution_context: NonNull<ExecutionContext>,
pub function: Option<Gc<FunctionObject>>,
+ #[gc(untraced)]
pub source_range: Option<SourceRange>,
}
diff --git a/Libraries/LibJS/Rust/src/jit/disabled.rs b/Libraries/LibJS/Rust/src/jit/disabled.rs
index 8f838627dff..8ea50b762ce 100644
--- a/Libraries/LibJS/Rust/src/jit/disabled.rs
+++ b/Libraries/LibJS/Rust/src/jit/disabled.rs
@@ -172,10 +172,13 @@ pub mod translate {
use crate::layout::function_object::FunctionObject;
use crate::layout::value::Value;
+ #[derive(Clone, libjs_runtime_macros::Trace)]
pub struct CallSiteFrame {
pub function: Gc<FunctionObject>,
pub executable: Gc<Executable>,
+ #[gc(untraced)]
pub program_counter: u32,
+ #[gc(untraced)]
pub frame_state: usize,
}
@@ -183,13 +186,13 @@ pub mod translate {
u64::from(low_half)
}
- pub fn call_site_frames(
- _vm: &Vm,
+ pub fn call_site_frames<'vm>(
+ vm: &'vm Vm,
_frame: &ExecutionContext,
_index: u32,
_frame_pointer: u64,
- ) -> Vec<CallSiteFrame> {
- Vec::new()
+ ) -> crate::gc::root::MarkedVec<'vm, CallSiteFrame> {
+ crate::gc::root::MarkedVec::new(vm)
}
pub fn call_site_frame_arguments<'d>(
diff --git a/Libraries/LibJS/Rust/src/jit/translate.rs b/Libraries/LibJS/Rust/src/jit/translate.rs
index 5187d458be4..c5abd2ecff9 100644
--- a/Libraries/LibJS/Rust/src/jit/translate.rs
+++ b/Libraries/LibJS/Rust/src/jit/translate.rs
@@ -25,6 +25,7 @@ use std::collections::HashMap;
use libjs_abi::register;
use libjs_jit::code::{CALLEE_SLOT, FrameState, Repr, ResumeMode, Site, SiteKind, ValueLocation};
+use libjs_runtime_macros::Trace;
use super::code::{CompileState, EXIT_COUNT_BEFORE_DISCARD, InlinedFunction, JitCode};
use super::{executable_of, frame_of, frames_above};
@@ -33,6 +34,7 @@ use crate::bytecode::instruction::{OpCode, instruction_length_from_bytes};
use crate::bytecode::instruction_at;
use crate::bytecode::op;
use crate::gc::defer_gc::{DeferGc, Deferred};
+use crate::gc::root::MarkedVec;
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::execution_context::ExecutionContext;
@@ -846,12 +848,15 @@ pub(super) fn materialize_call_site_frames(
/// A frame of an inlined call that a call at a call site of the code running a frame is in, as stack walks see it while
/// the call runs without it (View translation).
+#[derive(Clone, Trace)]
pub struct CallSiteFrame {
pub function: Gc<FunctionObject>,
pub executable: Gc<Executable>,
/// The pc of the call the frame makes.
+ #[gc(untraced)]
pub program_counter: u32,
/// The index of its frame state in the site.
+ #[gc(untraced)]
pub frame_state: usize,
}
@@ -871,7 +876,12 @@ pub fn frame_pointer_from_low_half(low_half: u32) -> u64 {
/// View translation: the frames of the inlined calls that a call at the call site `index` of the code running `frame`
/// is in, innermost first, while it runs without them, with the JIT frame at `frame_pointer`.
-pub fn call_site_frames(vm: &Vm, frame: &ExecutionContext, index: u32, frame_pointer: u64) -> Vec<CallSiteFrame> {
+pub fn call_site_frames<'vm>(
+ vm: &'vm Vm,
+ frame: &ExecutionContext,
+ index: u32,
+ frame_pointer: u64,
+) -> MarkedVec<'vm, CallSiteFrame> {
let compiled_executable = executable_of(frame);
let code = compiled_executable
.jit_code(vm)
@@ -880,41 +890,39 @@ pub fn call_site_frames(vm: &Vm, frame: &ExecutionContext, index: u32, frame_poi
assert_eq!(site.kind, SiteKind::Call);
let dump = RegisterDump::of_stack_slots(frame_pointer);
let (_, inlined) = site.frames.split_last().expect("a call site has frames");
- inlined
- .iter()
- .enumerate()
- .map(|(index, frame_state)| {
- let snapshot_executable = code.snapshot_executable(frame_state.executable);
- // NB: Frames of inlined closures run the closure that was called. Walks create no objects, and the closure
- // never is one the code did not create.
- let closure = frame_state
- .values
- .iter()
- .find(|(slot, _)| *slot == CALLEE_SLOT)
- .and_then(|(_, location)| match location {
- ValueLocation::Stack(..) | ValueLocation::Constant(_) => Some(read_location(*location, &dump)),
- _ => None,
- });
- let function = match closure {
- Some(closure) => as_ecmascript_function_object(closure.as_object())
- .expect("an inlined closure is a function")
- .as_function_object_gc(),
- None => match snapshot_executable
- .function
- .expect("an inlined executable has a function")
- {
- InlinedFunction::Ecmascript(function) => function.as_function_object_gc(),
- InlinedFunction::Builtin(function) => function.as_function_object_gc(),
- },
- };
- CallSiteFrame {
- function,
- executable: snapshot_executable.executable,
- program_counter: frame_state.pc,
- frame_state: index,
- }
- })
- .collect()
+ let frames = MarkedVec::with_capacity(vm, inlined.len());
+ for (index, frame_state) in inlined.iter().enumerate() {
+ let snapshot_executable = code.snapshot_executable(frame_state.executable);
+ // NB: Frames of inlined closures run the closure that was called. Walks create no objects, and the closure
+ // never is one the code did not create.
+ let closure = frame_state
+ .values
+ .iter()
+ .find(|(slot, _)| *slot == CALLEE_SLOT)
+ .and_then(|(_, location)| match location {
+ ValueLocation::Stack(..) | ValueLocation::Constant(_) => Some(read_location(*location, &dump)),
+ _ => None,
+ });
+ let function = match closure {
+ Some(closure) => as_ecmascript_function_object(closure.as_object())
+ .expect("an inlined closure is a function")
+ .as_function_object_gc(),
+ None => match snapshot_executable
+ .function
+ .expect("an inlined executable has a function")
+ {
+ InlinedFunction::Ecmascript(function) => function.as_function_object_gc(),
+ InlinedFunction::Builtin(function) => function.as_function_object_gc(),
+ },
+ };
+ frames.push(CallSiteFrame {
+ function,
+ executable: snapshot_executable.executable,
+ program_counter: frame_state.pc,
+ frame_state: index,
+ });
+ }
+ frames
}
/// View translation: the passed arguments of the frame of an inlined call that a call at the call site `index` of the
diff --git a/Libraries/LibJS/Rust/src/runtime/error_data.rs b/Libraries/LibJS/Rust/src/runtime/error_data.rs
index d1c12f3d28b..837d6be308d 100644
--- a/Libraries/LibJS/Rust/src/runtime/error_data.rs
+++ b/Libraries/LibJS/Rust/src/runtime/error_data.rs
@@ -101,13 +101,13 @@ impl ErrorData {
fn populate_stack(vm: &Vm, skipped_innermost_frames: usize) -> Vec<TracebackFrame> {
let stack_trace = vm.stack_trace();
let mut traceback = Vec::with_capacity(stack_trace.len().saturating_sub(skipped_innermost_frames));
- for element in stack_trace.into_iter().skip(skipped_innermost_frames) {
+ for element in stack_trace.values().iter().skip(skipped_innermost_frames) {
traceback.push(TracebackFrame {
function_name: element
.function
.map(|function| function.name_for_call_stack())
.unwrap_or_default(),
- cached_source_range: element.source_range,
+ cached_source_range: element.source_range.clone(),
});
}
traceback