Truly independent web browser
LibJS: Fill in the Rust heap region base before creating a heap
Andreas Kling505b973c7292
diff --git a/Libraries/LibJS/Heap/Cell.cpp b/Libraries/LibJS/Heap/Cell.cppindex 94021be5201..ad142977df8 100644--- a/Libraries/LibJS/Heap/Cell.cpp+++ b/Libraries/LibJS/Heap/Cell.cpp@@ -4,18 +4,10 @@* SPDX-License-Identifier: BSD-2-Clause*/-#include <LibGC/HeapRegion.h>#include <LibJS/Heap/Cell.h>-// The copy of the heap region base that the Rust half of LibJS decodes with (see layout_forward.rs).-extern "C" FlatPtr* js_heap_region_base_copy();-namespace JS {-// NB: Nothing in LibJS allocates a cell during static initialization, so this registers the copy before the first cell-// that the Rust half could decode exists.-static GC::HeapRegionBaseCopyRegistration s_rust_heap_region_base_copy_registration { *js_heap_region_base_copy() };-void Cell::initialize(Realm&){}diff --git a/Libraries/LibJS/Rust/src/gc/heap.rs b/Libraries/LibJS/Rust/src/gc/heap.rsindex 678aa14bcc3..b7b834b996c 100644--- a/Libraries/LibJS/Rust/src/gc/heap.rs+++ b/Libraries/LibJS/Rust/src/gc/heap.rs@@ -153,6 +153,7 @@ impl Heap {/// `context` must stay valid for as long as the heap exists, including while it is being destroyed.pub unsafe fn new(gather_roots: GCGatherRootsCallback, context: *mut c_void, become_process_default: bool) -> Self {let layout = check_layout();+ crate::layout_forward::initialize_heap_region_base();// SAFETY: The caller keeps the context alive for the heap's lifetime.let raw = unsafe { capi::gc_heap_create(gather_roots, context, become_process_default) };let raw = NonNull::new(raw).expect("LibGC creates the heap");diff --git a/Libraries/LibJS/Rust/src/layout_forward.rs b/Libraries/LibJS/Rust/src/layout_forward.rsindex 687b54ff825..edd7b073047 100644--- a/Libraries/LibJS/Rust/src/layout_forward.rs+++ b/Libraries/LibJS/Rust/src/layout_forward.rs@@ -76,12 +76,12 @@ pub use crate::runtime::shared_function_instance_data::SharedFunctionInstanceDatpub use crate::runtime::typed_array::TypedArrayBaseStorage;-/// A copy of LibGC's js_heap_region_base, which LibGC fills in through its registration (see-/// js_heap_region_base_copy()) before any cell exists, and never changes after that.+/// A copy of LibGC's js_heap_region_base, which initialize_heap_region_base() fills in before this copy of the runtime+/// creates its first heap, so before any cell that it could decode exists, and which never changes after that.struct HeapRegionBaseCopy(core::cell::UnsafeCell<usize>);-// SAFETY: LibGC writes the copy once, before any cell exists, and every read of it is of a cell that some thread has-// made since, which it had to synchronize with to get at.+// SAFETY: The copy is written once, before any cell that this runtime decodes exists, and every read of it is of a cell+// that some thread has made since, which it had to synchronize with to get at.unsafe impl Sync for HeapRegionBaseCopy {}// OPTIMIZATION: A copy of the base in this library, which a decode reads with one PC-relative load instead of a load of@@ -89,18 +89,27 @@ unsafe impl Sync for HeapRegionBaseCopy {}// of loops, where an atomic one would have to stay in them.static HEAP_REGION_BASE: HeapRegionBaseCopy = HeapRegionBaseCopy(core::cell::UnsafeCell::new(0));-/// The base of the heap region, which compressed cell pointers and cell values are offsets from. Zero until LibGC-/// reserves the region, which happens before any cell exists.+/// The base of the heap region, which compressed cell pointers and cell values are offsets from. Zero until the runtime+/// creates its first heap.#[inline(always)]pub fn heap_region_base() -> usize {// SAFETY: Nothing writes the copy after the first cell exists, see HeapRegionBaseCopy.unsafe { *HEAP_REGION_BASE.0.get() }}-/// The copy of the heap region base, which LibJS registers with LibGC at static initialization.-#[unsafe(no_mangle)]-pub extern "C" fn js_heap_region_base_copy() -> *mut usize {- HEAP_REGION_BASE.0.get()+/// Fills in the copy of the heap region base, which reserves the region if LibGC has not yet. Every heap that the runtime+/// reaches cells of is one that it creates, and it calls this before creating one.+///+/// NB: This is not left to a static initializer elsewhere, which a static link drops along with the rest of its object+/// when nothing references that object.+pub fn initialize_heap_region_base() {+ static INITIALIZE: std::sync::Once = std::sync::Once::new();+ INITIALIZE.call_once(|| {+ let base = crate::gc::capi::gc_heap_region_base();+ // SAFETY: No cell that this runtime could decode exists yet, so nothing reads the copy, and the Once keeps any+ // other thread that creates a heap waiting until the copy is written.+ unsafe { *HEAP_REGION_BASE.0.get() = base };+ });}const _: () =