Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions crates/chunk-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 -- <bundle directory>` compares this
engine with a plain V8 context on a compiled bundle, such as one [`chunk-bench`](../chunk-bench/README.md) writes.
17 changes: 16 additions & 1 deletion crates/chunk-js/src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
};
})();
20 changes: 10 additions & 10 deletions crates/chunk-js/src/deadline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
})?;
Expand Down
74 changes: 74 additions & 0 deletions crates/chunk-js/src/exception.rs
Original file line number Diff line number Diff line change
@@ -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<String>);

/// 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<v8::String>) {
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<v8::Value>) -> 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("<anonymous>"),
}
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<v8::Value>, promise: bool) {
let message = describe(scope, error);
if let Some(reported) = JsRuntime::op_state_from(scope).borrow_mut().try_borrow_mut::<Reported>() {
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]);
31 changes: 31 additions & 0 deletions crates/chunk-js/src/isolate.rs
Original file line number Diff line number Diff line change
@@ -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<v8::Context>);

/// 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::<MainContext>() 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<State>);
Expand Down
1 change: 1 addition & 0 deletions crates/chunk-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod allocator;
mod capabilities;
mod deadline;
mod engine;
mod exception;
mod extensions;
mod http;
mod isolate;
Expand Down
27 changes: 21 additions & 6 deletions crates/chunk-js/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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::<Option<Capabilities>>().take();
Expand All @@ -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(),
Expand Down Expand Up @@ -110,12 +117,14 @@ impl State {
runtime.op_state().borrow_mut().put(None::<Capabilities>);
runtime.op_state().borrow_mut().put(None::<crate::actions::ActionCapabilities>);
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 }
Expand All @@ -140,11 +149,17 @@ impl State {
cancellation: &Cancellation,
run: impl FnOnce(&mut Self) -> Result<T, Error>,
) -> Result<T, Error> {
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::<Reported>().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> {
Expand Down
4 changes: 4 additions & 0 deletions crates/chunk-js/src/termination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Error> {
match self.0.swap(0, Ordering::AcqRel) {
1 => Some(Error::Cancelled),
Expand Down
Loading