diff --git a/crates/chunk-js/README.md b/crates/chunk-js/README.md index 108c6491..a93a044e 100644 --- a/crates/chunk-js/README.md +++ b/crates/chunk-js/README.md @@ -40,8 +40,9 @@ methods, whose output (at most 32 messages and 16 KiB per call) the backend logs also when called on a subclass. An error's `stack` is always a string: `Error.prepareStackTrace` is ignored, so code never receives call-site objects. Not available: filesystem, network, process, timers, Node or Deno globals, `import()` (every call rejects, including a module importing itself), `Intl` and locale methods, `performance`, weak references, -WebAssembly, `SharedArrayBuffer` and resizable `ArrayBuffer`s. A promise that can never settle fails the call, and so -does an unhandled rejection. +WebAssembly, `SharedArrayBuffer` and resizable `ArrayBuffer`s. A promise that can never settle fails the call, and so do +an unhandled rejection and an error thrown from a `queueMicrotask` callback. A failed call reports V8's message for the +thrown value and the stack V8 captured with it, at most 16 KiB, without running code on the value. ## Limits @@ -60,8 +61,8 @@ bound. ## Safety and tests -`unsafe` is denied in this crate, except in `src/isolate.rs`, which enters parked isolates, and `src/allocator.rs`, -which implements V8's `ArrayBuffer` allocator with the crate's memory accounting. +`unsafe` is denied in this crate, except in `src/isolate.rs`, which enters parked isolates and interrupts running ones, +and `src/allocator.rs`, which implements V8's `ArrayBuffer` allocator with the crate's memory accounting. `cargo test -p chunk-js` runs the tests. `cargo bench -p chunk-js --bench engines -- ` compares this engine with a plain V8 context on a compiled bundle, such as one [`chunk-bench`](../chunk-bench/README.md) writes. diff --git a/crates/chunk-js/src/bootstrap.js b/crates/chunk-js/src/bootstrap.js index c5d4a673..e5b5f961 100644 --- a/crates/chunk-js/src/bootstrap.js +++ b/crates/chunk-js/src/bootstrap.js @@ -11,6 +11,14 @@ const write = Deno.core.ops.op_chunk_write; const now = Deno.core.ops.op_chunk_now; const random = Deno.core.ops.op_chunk_random; + const report = Deno.core.ops.op_chunk_report; + // Every application error reaches the host through report, which formats it without running + // application code; deno_core's own conversion reads properties the thrown value controls. + Deno.core.setReportExceptionCallback((error) => report(error, false)); + Deno.core.setUnhandledPromiseRejectionHandler((_, reason) => { + report(reason, true); + return true; + }); const freeze = Object.freeze; const parse = JSON.parse; const stringify = JSON.stringify; @@ -94,7 +102,7 @@ } return (actionEnv = freeze(values)); }; - return async (handler, callerJson, generation, argsJson) => { + const invoke = async (handler, callerJson, generation, argsJson) => { const invocationId = actionId(); let caller; let callerRead = false; @@ -158,4 +166,11 @@ }); return serialize(await handler(context, parse(argsJson))); }; + return async (handler, callerJson, generation, argsJson) => { + try { + return await invoke(handler, callerJson, generation, argsJson); + } catch (error) { + report(error, false); + } + }; })(); diff --git a/crates/chunk-js/src/deadline.rs b/crates/chunk-js/src/deadline.rs index 45158916..1b8681c7 100644 --- a/crates/chunk-js/src/deadline.rs +++ b/crates/chunk-js/src/deadline.rs @@ -58,16 +58,16 @@ impl Deadline { // Hold the lock through termination so disarming cannot race with // a stale timeout that interrupts the next call or recycled isolate. let active = watch.active.lock().unwrap(); - if let Some(active) = active.as_ref() - && (active.cancellation.is_cancelled() || watch.now() >= watch.at.load(Ordering::Acquire)) - { - active.termination.record(if active.cancellation.is_cancelled() { - Reason::Cancelled - } else { - Reason::Deadline - }); - active.handle.terminate_execution(); - watch.at.store(0, Ordering::Release); + let Some(active) = active.as_ref() else { continue }; + if active.cancellation.is_cancelled() { + active.termination.record(Reason::Cancelled); + } else if watch.now() >= watch.at.load(Ordering::Acquire) { + active.termination.record(Reason::Deadline); + } + // deno_core cancels termination while it converts an exception, and may + // run JavaScript afterwards, so a terminated run is interrupted until it ends. + if active.termination.is_recorded() { + crate::isolate::interrupt(&active.handle); } } })?; diff --git a/crates/chunk-js/src/exception.rs b/crates/chunk-js/src/exception.rs new file mode 100644 index 00000000..a5770ec2 --- /dev/null +++ b/crates/chunk-js/src/exception.rs @@ -0,0 +1,74 @@ +use deno_core::{JsRuntime, op2, v8}; + +const MESSAGE_BYTES: usize = 16 * 1024; +const FRAMES: usize = 16; + +/// The first application error of a run, which fails it. +#[derive(Default)] +pub(crate) struct Reported(pub Option); + +/// The report text, which copies at most [`MESSAGE_BYTES`] out of V8 however long its strings are. +struct Text(String); + +impl Text { + fn remaining(&self) -> usize { + MESSAGE_BYTES - self.0.len() + } + + fn push(&mut self, scope: &mut v8::PinScope, string: v8::Local) { + let mut buffer = vec![0; self.remaining()]; + let written = string.write_utf8_v2(scope, &mut buffer, v8::WriteFlags::kReplaceInvalidUtf8, None); + self.0.push_str(&String::from_utf8_lossy(&buffer[..written])); + } + + fn push_str(&mut self, text: &str) { + self.0.push_str(&text[..text.floor_char_boundary(self.remaining())]); + } +} + +/// Formats a thrown value from V8's message and the frames captured with it. Neither runs +/// JavaScript, so getters, proxies and hooks on the value can't stall or enlarge the result. +pub(crate) fn describe(scope: &mut v8::PinScope, exception: v8::Local) -> String { + let mut text = Text(String::with_capacity(MESSAGE_BYTES)); + let message = v8::Exception::create_message(scope, exception).get(scope); + text.push(scope, message); + if text.0.starts_with("Uncaught ") { + text.0.drain(.."Uncaught ".len()); + } + let Some(trace) = v8::Exception::get_stack_trace(scope, exception) else { return text.0 }; + for index in 0..trace.get_frame_count().min(FRAMES) { + let Some(frame) = trace.get_frame(scope, index) else { break }; + text.push_str("\n at "); + let function = frame.get_function_name(scope).filter(|name| name.length() > 0); + if let Some(function) = function { + text.push(scope, function); + text.push_str(" ("); + } + match frame.get_script_name_or_source_url(scope) { + Some(script) => text.push(scope, script), + None => text.push_str(""), + } + text.push_str(&format!(":{}:{}", frame.get_line_number(), frame.get_column())); + if function.is_some() { + text.push_str(")"); + } + if text.remaining() == 0 { + break; + } + } + text.0 +} + +/// Fails the run with `error` and ends it. The exception `deno_core` then converts is the +/// dispatched `undefined`, never the application's value. +#[op2(fast)] +fn op_chunk_report(scope: &mut v8::PinScope, error: v8::Local, promise: bool) { + let message = describe(scope, error); + if let Some(reported) = JsRuntime::op_state_from(scope).borrow_mut().try_borrow_mut::() { + reported.0.get_or_insert(message); + } + let undefined = v8::undefined(scope); + deno_core::error::dispatch_exception(scope, undefined.into(), promise); +} + +deno_core::extension!(chunk_exceptions, ops = [op_chunk_report]); diff --git a/crates/chunk-js/src/isolate.rs b/crates/chunk-js/src/isolate.rs index 87e809f7..78792c8b 100644 --- a/crates/chunk-js/src/isolate.rs +++ b/crates/chunk-js/src/isolate.rs @@ -1,11 +1,42 @@ #![allow(unsafe_code)] +use deno_core::v8; + use crate::{ Cancellation, Error, Execution, Limits, deadline::Deadline, runtime::{Prepared, State}, }; +struct MainContext(v8::Global); + +/// Lets [`interrupt`] enter the runtime's context. +pub(crate) fn prepare_interrupts(runtime: &mut deno_core::JsRuntime) { + let context = runtime.main_context(); + runtime.v8_isolate().set_slot(MainContext(context)); +} + +/// Ends the isolate's JavaScript from any thread. Dispatching `undefined` before terminating +/// keeps `deno_core`, which converts the dispatched exception, from building its own error, +/// whose prototype application code controls. +pub(crate) fn interrupt(handle: &v8::IsolateHandle) { + handle.request_interrupt(dispatch, std::ptr::null_mut()); +} + +extern "C" fn dispatch(mut isolate: v8::UnsafeRawIsolatePtr, _: *mut std::ffi::c_void) { + // SAFETY: V8 runs interrupts on the isolate's thread while it executes JavaScript, + // like a function callback. + let isolate = unsafe { v8::Isolate::ref_from_raw_isolate_ptr_mut(&mut isolate) }; + let Some(MainContext(context)) = isolate.get_slot::() else { return }; + let context = context.clone(); + v8::callback_scope!(unsafe scope, isolate); + v8::scope!(let scope, scope); + let context = v8::Local::new(scope, context); + let scope = &mut v8::ContextScope::new(scope, context); + let undefined = v8::undefined(scope); + deno_core::error::dispatch_exception(scope, undefined.into(), false); +} + /// A runtime is exited while parked. All access and destruction enter it on its /// owning thread. State (and therefore this wrapper) is neither Send nor Sync. pub(crate) struct Runtime(Option); diff --git a/crates/chunk-js/src/lib.rs b/crates/chunk-js/src/lib.rs index cd3c9fda..c90a9996 100644 --- a/crates/chunk-js/src/lib.rs +++ b/crates/chunk-js/src/lib.rs @@ -7,6 +7,7 @@ mod allocator; mod capabilities; mod deadline; mod engine; +mod exception; mod extensions; mod http; mod isolate; diff --git a/crates/chunk-js/src/runtime.rs b/crates/chunk-js/src/runtime.rs index 0ab971ce..4f02fb7d 100644 --- a/crates/chunk-js/src/runtime.rs +++ b/crates/chunk-js/src/runtime.rs @@ -4,6 +4,7 @@ use crate::{ Cancellation, Error, Execution, Json, Limits, Write, capabilities::{Capabilities, chunk_capabilities}, deadline::Deadline, + exception::Reported, model::bounds, termination::{Reason, Termination}, }; @@ -58,11 +59,16 @@ impl State { self.runtime.op_state().borrow_mut().put(action); self.runtime.op_state().borrow_mut().put(crate::actions::Env(env)); let result = self.guarded(deadline, limits, cancellation, |engine| { - let result = executor.block_on(engine.invoke(&export, caller.as_str(), arguments.as_str(), json_bytes)); - if !is_action { - executor.block_on(engine.drain())?; + let output = executor.block_on(engine.invoke(&export, caller.as_str(), arguments.as_str(), json_bytes)); + // A failed call still settles its microtasks and rejections here, under the guard, + // so none reach the next call on this runtime. A terminated runtime is discarded instead. + if !is_action && (output.is_ok() || !engine.termination.is_recorded()) { + let drained = executor.block_on(engine.drain()); + if output.is_ok() { + drained?; + } } - result + output }); let logs = crate::profile::end(&mut self.runtime); let capabilities = self.runtime.op_state().borrow_mut().borrow_mut::>().take(); @@ -82,6 +88,7 @@ impl State { let mut extensions = crate::extensions::web(); extensions.extend([ chunk_capabilities::init(), + crate::exception::chunk_exceptions::init(), crate::profile::chunk_profile::init(), crate::actions::chunk_actions::init(), crate::jobs::chunk_scheduler::init(), @@ -110,12 +117,14 @@ impl State { runtime.op_state().borrow_mut().put(None::); runtime.op_state().borrow_mut().put(None::); runtime.op_state().borrow_mut().put(crate::actions::Env(Json::empty())); + runtime.op_state().borrow_mut().put(Reported::default()); crate::profile::initialize(&mut runtime); + crate::isolate::prepare_interrupts(&mut runtime); let heap_signal = termination.clone(); let handle = runtime.v8_isolate().thread_safe_handle(); runtime.add_near_heap_limit_callback(move |limit, _| { heap_signal.record(Reason::Heap); - handle.terminate_execution(); + crate::isolate::interrupt(&handle); limit + bounds::EMERGENCY_HEAP_BYTES }); Self { run: None, namespace: None, runtime, termination, calls: 0 } @@ -140,11 +149,17 @@ impl State { cancellation: &Cancellation, run: impl FnOnce(&mut Self) -> Result, ) -> Result { + self.runtime.op_state().borrow_mut().put(Reported::default()); let handle = self.runtime.v8_isolate().thread_safe_handle(); let guard = deadline.arm(handle, cancellation.clone(), limits.execution, self.termination.clone()); let result = run(self); drop(guard); - self.termination.take().map_or(result, Err) + let reported = self.runtime.op_state().borrow_mut().borrow_mut::().0.take(); + match (self.termination.take(), reported) { + (Some(error), _) => Err(error), + (None, Some(message)) => Err(Error::JavaScript(message)), + (None, None) => result, + } } async fn initialize(&mut self, specifier: &str, source: &str) -> Result<(), Error> { diff --git a/crates/chunk-js/src/termination.rs b/crates/chunk-js/src/termination.rs index 991757d3..680ea581 100644 --- a/crates/chunk-js/src/termination.rs +++ b/crates/chunk-js/src/termination.rs @@ -14,6 +14,10 @@ impl Termination { let _ = self.0.compare_exchange(0, reason as u8, Ordering::AcqRel, Ordering::Acquire); } + pub fn is_recorded(&self) -> bool { + self.0.load(Ordering::Acquire) != 0 + } + pub fn take(&self) -> Option { match self.0.swap(0, Ordering::AcqRel) { 1 => Some(Error::Cancelled), diff --git a/crates/chunk-js/src/tests.rs b/crates/chunk-js/src/tests.rs index 3e648cb7..2120da17 100644 --- a/crates/chunk-js/src/tests.rs +++ b/crates/chunk-js/src/tests.rs @@ -593,3 +593,139 @@ fn imports_at_runtime_reject_without_aborting() { "; assert!(matches!(Deployment::new("build-a".into(), source.into(), Limits::default()), Err(Error::JavaScript(_)))); } + +#[test] +fn thrown_values_cannot_abort_or_stall_error_reporting() { + let limits = Limits { execution: Duration::from_millis(200), ..Limits::default() }; + let values = [ + "Object.assign(new Error('keys'), { [Symbol.for('errorAdditionalPropertyKeys')]: new Array(2 ** 32 - 1) })", + "Object.defineProperty(new Error('stalled'), 'message', { get() { for (;;); } })", + "new Proxy({}, { get() { for (;;); } })", + ]; + for thrown in values { + for failure in [ + format!("throw {thrown};"), + format!("queueMicrotask(() => {{ throw {thrown}; }});"), + format!("Promise.reject({thrown});"), + ] { + let mut engine = deployment(&format!("if (args.fail) {{ {failure} await 0; }} return 42;"), limits); + let mut fail = invocation(); + fail.arguments = json!({"fail": true}).into(); + let result = engine.execute(fail, Box::new(Snapshot), &Cancellation::default()); + assert!(matches!(result, Err(Error::JavaScript(_))), "{failure}: {result:?}"); + assert_eq!(value(&call(&mut engine).unwrap()), json!(42), "{failure}"); + let source = format!("{failure} export default () => 1;"); + let result = Deployment::new("build-a".into(), source, limits).map(|_| ()); + assert!(matches!(result, Err(Error::JavaScript(_))), "top-level {failure}: {result:?}"); + } + } + let Err(Error::JavaScript(thrown)) = run(&format!("throw {};", values[0])) else { + panic!("expected a JavaScript error"); + }; + assert!(thrown.starts_with("Error: keys\n at default (chunk:deployment/build-a:1:"), "{thrown}"); +} + +#[test] +fn termination_survives_error_prototypes_that_poison_conversion() { + let poison = r" + const keys = Symbol.for('errorAdditionalPropertyKeys'); + Error.prototype[keys] = new Array(2 ** 32 - 1); + Error.prototype.cause = Object.assign(new Error('cause'), { [keys]: new Array(2 ** 32 - 1) }); + Object.defineProperty(Error.prototype, 'name', { get() { for (;;); } }); + "; + let time = Limits { execution: Duration::from_millis(200), ..Limits::default() }; + let heap = Limits { execution: Duration::from_secs(5), heap_bytes: 16 * 1024 * 1024 }; + for (body, limits, expected) in [ + ("for (;;);", time, Error::Deadline), + ("for (;;) await 0;", time, Error::Deadline), + ("const a = []; for (;;) a.push(new Array(10000).fill('x'));", heap, Error::Heap), + ] { + let kind = |result: Result<(), Error>| std::mem::discriminant(&result.unwrap_err()); + let expected = std::mem::discriminant(&expected); + let result = call(&mut deployment(&format!("{poison} {body}"), limits)).map(|_| ()); + assert_eq!(kind(result), expected, "{body}"); + let source = format!("{poison} {body} export default () => 1;"); + let result = Deployment::new("build-a".into(), source, limits).map(|_| ()); + assert_eq!(kind(result), expected, "top-level {body}"); + } +} + +#[test] +fn failed_invocation_leaves_no_rejection_for_the_next_call() { + let mut engine = deployment( + r#"if (args.fail) { Promise.reject(new Error("leftover")); throw new Error("primary"); } return 42;"#, + Limits::default(), + ); + let mut fail = invocation(); + fail.arguments = json!({"fail": true}).into(); + let Err(Error::JavaScript(message)) = engine.execute(fail, Box::new(Snapshot), &Cancellation::default()) else { + panic!("expected a JavaScript error"); + }; + assert!(message.starts_with("Error: primary"), "{message}"); + let mut pass = invocation(); + pass.arguments = json!({"fail": false}).into(); + let result = engine.execute(pass, Box::new(Snapshot), &Cancellation::default()); + assert_eq!(value(&result.unwrap()), json!(42)); + + let mut engine = deployment( + r#" + globalThis.calls = (globalThis.calls ?? 0) + 1; + if (args.fail) { + queueMicrotask(() => { Promise.reject(new Error("leftover")); throw new Error("primary"); }); + } + return calls; + "#, + Limits::default(), + ); + let mut fail = invocation(); + fail.arguments = json!({"fail": true}).into(); + let Err(Error::JavaScript(message)) = engine.execute(fail, Box::new(Snapshot), &Cancellation::default()) else { + panic!("expected a JavaScript error"); + }; + assert!(message.starts_with("Error: primary"), "{message}"); + for expected in [2, 3] { + let mut pass = invocation(); + pass.arguments = json!({"fail": false}).into(); + let result = engine.execute(pass, Box::new(Snapshot), &Cancellation::default()); + assert_eq!(value(&result.unwrap()), json!(expected)); + } +} + +#[test] +fn error_reports_are_bounded_before_formatting() { + let started = std::time::Instant::now(); + let Err(Error::JavaScript(message)) = run(r#" + throw new Error("€".repeat(4 * 1024 * 1024)); + "#) else { + panic!("expected a JavaScript error"); + }; + assert!(message.len() <= 16 * 1024 && message.capacity() <= 16 * 1024, "{} {}", message.len(), message.capacity()); + assert!(message.starts_with("Error: €€€"), "{}", &message[..20]); + assert!(started.elapsed() < Duration::from_secs(2), "{:?}", started.elapsed()); +} + +#[test] +fn reading_stack_in_a_function_that_hits_its_deadline_stays_bounded() { + let limits = Limits { execution: Duration::from_millis(200), ..Limits::default() }; + let mut engine = deployment( + r" + if (args.fail) { + const error = Object.defineProperty(new Error('x'), 'message', { get() { for (;;); } }); + error.stack; + for (;;); + } + return 42; + ", + limits, + ); + let mut fail = invocation(); + fail.arguments = json!({"fail": true}).into(); + let started = std::time::Instant::now(); + let result = engine.execute(fail, Box::new(Snapshot), &Cancellation::default()); + assert!(matches!(result, Err(Error::Deadline)), "{result:?}"); + assert!(started.elapsed() < Duration::from_secs(2), "{:?}", started.elapsed()); + let mut pass = invocation(); + pass.arguments = json!({"fail": false}).into(); + let result = engine.execute(pass, Box::new(Snapshot), &Cancellation::default()); + assert_eq!(value(&result.unwrap()), json!(42)); +}