Skip to content
Closed
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
146 changes: 117 additions & 29 deletions src/js_parser_jsc/Macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ impl MacroContext {
hash,
) {
Ok(m) => m,
Err(crate::Error::MacroLoadStalled) => {
*macro_entry.value_ptr = Macro::disabled_sentinel();
log.add_range_error_fmt(
Some(source),
import_range,
format_args!(
"macro \"{}\" never finished loading: its top-level await is pending and nothing is keeping the event loop alive",
bstr::BStr::new(import_record_path)
),
);
return Err(crate::Error::MacroFailed);
}
Err(e) => {
*macro_entry.value_ptr = Macro::disabled_sentinel();
return Err(e);
Comment thread
robobun marked this conversation as resolved.
Resolved
Expand All @@ -208,7 +220,16 @@ impl MacroContext {
let macro_vm = macro_entry.value_ptr.vm;
let macro_: *const Macro = macro_entry.value_ptr;
if macro_disabled {
return Ok(caller);
// Its load failed earlier; returning `caller` would leave an unbound call in the output.
log.add_error_fmt(
Some(source),
caller.loc,
format_args!(
"macro \"{}\" failed to load",
bstr::BStr::new(import_record_path)
),
);
return Err(crate::Error::MacroFailed);
}
// SAFETY: `Some` for every non-disabled Macro; see `Macro` struct comment.
let vm = macro_vm
Expand Down Expand Up @@ -457,7 +478,7 @@ impl Macro {
}

// SAFETY: `vm` is the per-thread VM; uniquely accessed here.
let loaded_result = unsafe {
let (loaded_result, waited) = unsafe {
(*vm).load_macro_entry_point(input_specifier, function_name, specifier, hash)
}?;

Expand All @@ -466,13 +487,21 @@ impl Macro {
let unwrapped = unsafe {
(*loaded_result).unwrap(&*(*vm).jsc_vm, jsc::PromiseUnwrapMode::LeaveUnhandled)
};
if let jsc::PromiseResult::Rejected(result) = unwrapped {
// SAFETY: `vm.global` is the live per-thread global; `loaded_result`
// is a live promise cell.
unsafe {
(*vm).unhandled_rejection(&*(*vm).global, result, (*loaded_result).to_js());
match unwrapped {
jsc::PromiseResult::Rejected(result) => {
// SAFETY: `vm` is the live per-thread VM.
unsafe { (*vm).run_error_handler(result, None) };
return Err(crate::Error::MacroLoadError);
}
return Err(crate::Error::MacroLoadError);
// A `Macro` returned here would leave its first call in the output as an
// unbound reference.
Comment thread
robobun marked this conversation as resolved.
Resolved
jsc::PromiseResult::Pending => {
return Err(match waited {
Err(jsc::Unsettled::Idle) => crate::Error::MacroLoadStalled,
_ => crate::Error::JSError,
});
}
jsc::PromiseResult::Fulfilled(_) => {}
}

Ok(Macro {
Expand Down Expand Up @@ -544,6 +573,8 @@ pub(crate) struct Run<'a> {
pub(crate) source: &'a Source,
pub(crate) visited: VisitMap,
pub(crate) is_top_level: bool,
/// `run` recurses once per nesting level of the returned value, on a bundler pool thread's stack.
pub(crate) stack_check: bun_core::StackCheck,
}

impl<'a> Run<'a> {
Expand All @@ -563,11 +594,17 @@ impl<'a> Run<'a> {
};

let global = vm.global();
let result = vm.run_with_api_lock(|| {
macro_callback
.call(global, JSValue::ZERO, args)
.unwrap_or_else(|_| global.try_take_exception().unwrap_or_default())
});
let result = match vm.run_with_api_lock(|| macro_callback.call(global, JSValue::ZERO, args))
{
Ok(result) => result,
// Printed, not `uncaught_exception`: in a `require()`ing program's VM that path is fatal.
Err(JsError::Thrown) => {
let err = global.take_exception(JsError::Thrown);
vm.as_mut().run_error_handler(err, None);
return Err(MacroError::MacroFailed);
}
Err(e) => return Err(e.into()),
};

let mut runner = Run {
caller,
Expand All @@ -578,15 +615,32 @@ impl<'a> Run<'a> {
source,
visited: VisitMap::default(),
is_top_level: false,
stack_check: bun_core::StackCheck::init(),
};

// `runner.visited` dropped at scope exit (was `defer runner.visited.deinit(allocator)`)

runner.run(result)
match runner.run(result) {
// A throw while converting the result (a getter) is reported like a throw from the macro.
Err(MacroError::Js(JsError::Thrown)) => {
let err = global.take_exception(JsError::Thrown);
vm.as_mut().run_error_handler(err, None);
Err(MacroError::MacroFailed)
}
other => other,
}
}

pub(crate) fn run(&mut self, value: JSValue) -> Result<Expr, MacroError> {
use ConsoleObject::formatter::Tag as T;
if !self.stack_check.is_safe_to_recurse() {
self.log.add_error_fmt(
Some(self.source),
self.caller.loc,
format_args!("macro return value is too deeply nested"),
);
return Err(MacroError::MacroFailed);
}
// `Tag::get` returns `TagResult { tag: TagPayload, .. }`;
// collapse the payload to its discriminant via `.tag()`.
match T::get(value, self.global)?.tag.tag() {
Expand Down Expand Up @@ -629,10 +683,10 @@ impl<'a> Run<'a> {
use ConsoleObject::formatter::Tag as T;
match tag {
T::Error => {
// Returning `self.caller` would leave the call in the output with its import gone.
// SAFETY: `vm()` is the per-thread VM; uniquely accessed here.
let _ =
unsafe { (*self.macro_.vm()).uncaught_exception(self.global, value, false) };
return Ok(self.caller);
unsafe { (*self.macro_.vm()).run_error_handler(value, None) };
return Err(MacroError::MacroFailed);
}
T::Undefined => {
if self.is_top_level {
Expand Down Expand Up @@ -666,9 +720,7 @@ impl<'a> Run<'a> {
|| value.as_::<BuildMessage>().is_some()
{
// SAFETY: `vm()` is the per-thread VM; uniquely accessed here.
let _ = unsafe {
(*self.macro_.vm()).uncaught_exception(self.global, value, false)
};
unsafe { (*self.macro_.vm()).run_error_handler(value, None) };
return Err(MacroError::MacroFailed);
}
}
Expand Down Expand Up @@ -703,6 +755,30 @@ impl<'a> Run<'a> {

let mut iter = JSArrayIterator::init(value, self.global)?;

// Every index below `length` becomes an element (holes as `undefined`), so
// `[,,1]` with `length = 1e9` would be a billion `undefined`s.
Comment thread
robobun marked this conversation as resolved.
Resolved
if let Err(stored) = value.indexed_storage_covers(iter.len) {
self.log.add_range_error_fmt_with_notes(
Some(self.source),
Range {
loc: self.caller.loc,
..Default::default()
},
Box::new([bun_ast::range_data(
None,
Range::NONE,
b"return a dense array",
)]),
format_args!(
"cannot coerce a sparse array to Bun's AST: its length is {} but it holds {} element{}",
iter.len,
stored,
if stored == 1 { "" } else { "s" },
),
Comment thread
robobun marked this conversation as resolved.
Resolved
);
return Err(MacroError::MacroFailed);
}

// Process all array items
let mut array = ExprNodeList::init_capacity(iter.len as usize);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// (errdefer free deleted — drops on `?`)
Expand Down Expand Up @@ -841,10 +917,26 @@ impl<'a> Run<'a> {

let _ = self.macro_.vm();
let vm = VirtualMachine::get();
// The VM stopped before the macro's promise settled: throw its termination and unwind.
vm.as_mut()
.wait_for_promise(promise)
.map_err(|stopped| MacroError::Js(stopped.throw(self.global)))?;
// This wait handles the promise: the rejection tracker must not report it too.
promise.set_handled(vm.jsc_vm());
match vm.as_mut().wait_for_promise_until_idle(promise) {
Ok(()) => {}
// The VM stopped before the macro's promise settled: throw its termination and unwind.
Err(jsc::Unsettled::Stopped(stopped)) => {
return Err(MacroError::Js(stopped.throw(self.global)));
}
// Node's exit-13 condition: the wait would never end.
Err(jsc::Unsettled::Idle) => {
self.log.add_error_fmt(
Some(self.source),
self.caller.loc,
format_args!(
"macro returned a promise that never settles: no timer, I/O, or task is keeping the event loop alive"
),
);
return Err(MacroError::MacroFailed);
}
}

let promise_result = promise.result(vm.jsc_vm());
let rejected = promise.status() == jsc::js_promise::Status::Rejected;
Expand All @@ -863,11 +955,7 @@ impl<'a> Run<'a> {
|| promise_result
.is_exception(std::ptr::from_ref::<jsc::VM>(self.global.vm()).cast_mut())
{
vm.as_mut().unhandled_rejection(
self.global,
promise_result,
promise.as_value(),
);
vm.as_mut().run_error_handler(promise_result, None);
return Err(MacroError::MacroFailed);
}
self.is_top_level = false;
Expand Down
4 changes: 4 additions & 0 deletions src/js_parser_jsc/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ pub enum Error {
MacroNotFound,
#[error("MacroLoadError")]
MacroLoadError,
/// The macro module's top-level await has nothing left that could settle it.
#[error("MacroLoadStalled")]
MacroLoadStalled,
#[error("MacroFailed")]
MacroFailed,
#[error("JSError")]
Expand All @@ -31,6 +34,7 @@ impl Error {
Self::ModuleNotFound => "ModuleNotFound",
Self::MacroNotFound => "MacroNotFound",
Self::MacroLoadError => "MacroLoadError",
Self::MacroLoadStalled => "MacroLoadStalled",
Self::MacroFailed => "MacroFailed",
Self::JSError => "JSError",
Self::Alloc(_) => "OutOfMemory",
Expand Down
19 changes: 19 additions & 0 deletions src/jsc/JSValue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2615,6 +2615,25 @@ impl JSValue {
index => Some(index as u32),
}
}
/// `Ok(())` when `self` (an object) backs `length` indexed slots with storage (a dense array,
/// holes included). `Err(stored)` when `length` runs past that storage (a sparse array, or a
/// `length` property written past the elements), with the number of elements it does hold.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub fn indexed_storage_covers(self, length: u32) -> Result<(), u32> {
debug_assert!(self.is_object());
unsafe extern "C" {
safe fn Bun__JSObject__indexedStorageCovers(
this: JSValue,
length: u32,
out_stored_count: &mut u32,
) -> bool;
}
let mut stored: u32 = 0;
if Bun__JSObject__indexedStorageCovers(self, length, &mut stored) {
Ok(())
} else {
Err(stored)
}
}
/// `JSValue.getNameProperty` — the value's `.name` (function/class
/// name). Empty for empty/`undefined`/`null`.
pub fn get_name_property(self, global: &JSGlobalObject) -> JsResult<bun_core::String> {
Expand Down
4 changes: 4 additions & 0 deletions src/jsc/SavedSourceMap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ impl SavedSourceMap {
pub(crate) fn put_value(&mut self, path: &[u8], value: Value) -> bun_js_printer::Result<()> {
use bun_collections::zig_hash_map::MapEntry as Entry;

// The table owns `value`'s allocation through a tagged pointer, which LeakSanitizer cannot follow.
bun_core::asan::ignore_object(value.as_uintptr() as usize as *const c_void);
self.lock();
// Note: reshaped for borrowck — explicit unlock paired manually.

Expand Down Expand Up @@ -283,6 +285,8 @@ impl SavedSourceMap {
// the returned `Arc`.
let result = Arc::new(ParsedSourceMap::from_internal(ism));
*mapping = Value::init(Arc::into_raw(Arc::clone(&result))).ptr();
// As in `put_value`: owned by the table through a tagged pointer.
bun_core::asan::ignore_object(Arc::as_ptr(&result).cast());
self.unlock();
return SourceMap::ParseUrl {
map: Some(result),
Expand Down
7 changes: 7 additions & 0 deletions src/jsc/VM.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ unsafe extern "C" {
safe fn JSC__VM__releaseWeakRefs(vm: &VM);
safe fn JSC__VM__drainMicrotasks(vm: &VM);
safe fn JSC__VM__blockBytesAllocated(vm: &VM) -> usize;
safe fn Bun__JSCTaskScheduler__hasPendingWork(vm: &VM) -> bool;
}

bun_opaque::opaque_ffi! {
Expand Down Expand Up @@ -124,6 +125,12 @@ impl VM {
JSC__VM__executionForbidden(self)
}

/// JSC will still post deferred work to this VM (an `Atomics.waitAsync` timeout, a wasm
/// compile); not all of it keeps the loop alive.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub fn has_pending_deferred_work(&self) -> bool {
Bun__JSCTaskScheduler__hasPendingWork(self)
}

// These four functions fire VM traps. To understand what that means, see VMTraps.h for a giant explainer.
// These may be called concurrently from another thread.

Expand Down
Loading
Loading