Truly independent web browser
LibJS: Make Gc::from_ref() safe by checking for the heap region
Andreas Kling6b6b5f616943
diff --git a/Libraries/LibJS/Rust/src/embedding/hooks.rs b/Libraries/LibJS/Rust/src/embedding/hooks.rsindex 0d799582fae..23e4f72d83e 100644--- a/Libraries/LibJS/Rust/src/embedding/hooks.rs+++ b/Libraries/LibJS/Rust/src/embedding/hooks.rs@@ -495,8 +495,7 @@ impl CompletionPayload for HandledByHost {/// The address of an object that the VM lends a hook.fn lent_object_into_abi(object: &Object) -> *mut JSObject {- // SAFETY: The VM only lends a hook objects that are cells of its heap.- object_into_abi(unsafe { Gc::from_ref(object) })+ object_into_abi(Gc::from_ref(object))}fn ensure_can_add_private_element(vm: &Vm, object: &Object) -> ThrowCompletionOr<()> {diff --git a/Libraries/LibJS/Rust/src/gc/class.rs b/Libraries/LibJS/Rust/src/gc/class.rsindex 0d580f9320d..a93bd7c4884 100644--- a/Libraries/LibJS/Rust/src/gc/class.rs+++ b/Libraries/LibJS/Rust/src/gc/class.rs@@ -319,17 +319,29 @@ impl<T: GcCell> core::ops::Deref for crate::layout::cell::Gc<T> {}impl<T: GcCell> crate::layout::cell::Gc<T> {- /// The Gc of a cell the caller only has a reference to, like the receiver of a method.- ///- /// # Safety- ///- /// `cell` must be a cell in the heap, not a value that has yet to be allocated.- pub unsafe fn from_ref(cell: &T) -> Self {- // SAFETY: The caller guarantees that the reference is to a live cell.- unsafe { Self::from_non_null(core::ptr::NonNull::from(cell)) }+ /// The Gc of a cell the caller only has a reference to, like the receiver of a method. Panics if the reference is+ /// to a value that has yet to be allocated.+ pub fn from_ref(cell: &T) -> Self {+ assert!(+ is_in_heap_region(core::ptr::from_ref(cell).addr()),+ "a {} that has yet to be allocated is not a cell",+ T::CLASS.name+ );+ // SAFETY: The reference is into the heap region, where every value of a cell type is a cell that was allocated+ // as one, since cells only ever embed the cell they extend, at their start.+ let gc = unsafe { Self::from_non_null(core::ptr::NonNull::from(cell)) };+ debug_assert!(class_of(gc).is_subclass_of(T::CLASS));+ gc}}+/// Whether `address` is in the region LibGC allocates every cell in, which has no cells before it is set up.+pub(crate) fn is_in_heap_region(address: usize) -> bool {+ // SAFETY: LibGC sets the base once, when it sets up the region, before any cell exists.+ let base = unsafe { super::capi::js_heap_region_base };+ base != 0 && address.wrapping_sub(base) as u64 <= crate::build_configuration::HEAP_REGION_OFFSET_MASK+}+impl<T> crate::layout::cell::Gc<T> {pub fn upcast<Base>(self) -> crate::layout::cell::Gc<Base>wherediff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rsindex 8b61746658b..b0cce1a38e9 100644--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs@@ -1791,7 +1791,13 @@ impl Vm {/// The VM whose heap `cell` lives in.pub(crate) fn of_cell<T: GcCell>(cell: &T) -> &Vm {- // SAFETY: The VM creates its heap with itself as the context, and outlives every cell in it.+ assert!(+ crate::gc::class::is_in_heap_region(core::ptr::from_ref(cell).addr()),+ "a {} that has yet to be allocated is in no heap",+ T::CLASS.name+ );+ // SAFETY: The cell is in the heap region, so it is a cell of a heap, which the VM creates with itself as the+ // context and outlives every cell in.unsafe { &*capi::gc_cell_heap_context(core::ptr::from_ref(cell).cast()).cast::<Vm>() }}diff --git a/Libraries/LibJS/Rust/src/runtime/async_generator.rs b/Libraries/LibJS/Rust/src/runtime/async_generator.rsindex 7443a888ff0..dec8199659d 100644--- a/Libraries/LibJS/Rust/src/runtime/async_generator.rs+++ b/Libraries/LibJS/Rust/src/runtime/async_generator.rs@@ -117,8 +117,7 @@ impl AsyncGenerator {}fn as_gc(&self) -> Gc<AsyncGenerator> {- // SAFETY: Async generators only exist as cells once constructed.- unsafe { Gc::from_ref(self) }+ Gc::from_ref(self)}/// [[AsyncGeneratorContext]]. It lives out of line until the generator is finalized, so the pointer stays valid fordiff --git a/Libraries/LibJS/Rust/src/runtime/cyclic_module.rs b/Libraries/LibJS/Rust/src/runtime/cyclic_module.rsindex d5972b4b8fa..29f10a6aeba 100644--- a/Libraries/LibJS/Rust/src/runtime/cyclic_module.rs+++ b/Libraries/LibJS/Rust/src/runtime/cyclic_module.rs@@ -136,8 +136,7 @@ impl CyclicModule {}pub fn as_cyclic_module_gc(&self) -> Gc<CyclicModule> {- // SAFETY: Modules only exist as cells once constructed.- unsafe { Gc::from_ref(self) }+ Gc::from_ref(self)}pub fn status(&self) -> ModuleStatus {diff --git a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rsindex 7b7d40f10fb..8e208d577af 100644--- a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs@@ -279,8 +279,7 @@ impl EcmascriptFunctionObject {}pub fn as_ecmascript_function_gc(&self) -> Gc<EcmascriptFunctionObject> {- // SAFETY: ECMAScript function objects only exist as cells once constructed.- unsafe { Gc::from_ref(self) }+ Gc::from_ref(self)}/// The function's executable, compiled first if the function never ran.diff --git a/Libraries/LibJS/Rust/src/runtime/generator_object.rs b/Libraries/LibJS/Rust/src/runtime/generator_object.rsindex 68e020f4237..52921e50306 100644--- a/Libraries/LibJS/Rust/src/runtime/generator_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/generator_object.rs@@ -203,8 +203,7 @@ impl GeneratorObject {}pub fn as_gc(&self) -> Gc<GeneratorObject> {- // SAFETY: Generator objects only exist as cells once constructed.- unsafe { Gc::from_ref(self) }+ Gc::from_ref(self)}/// [[GeneratorContext]]. It lives out of line until the generator is finalized, so the pointer stays valid fordiff --git a/Libraries/LibJS/Rust/src/runtime/map.rs b/Libraries/LibJS/Rust/src/runtime/map.rsindex 5df972475fe..d483096e6d6 100644--- a/Libraries/LibJS/Rust/src/runtime/map.rs+++ b/Libraries/LibJS/Rust/src/runtime/map.rs@@ -245,8 +245,7 @@ impl Map {pub fn begin(&self) -> ConstIterator {ConstIterator {- // SAFETY: Maps only exist as cells, since every way to create one allocates it.- map: unsafe { Gc::from_ref(self) },+ map: Gc::from_ref(self),index: Cell::new(0),generation: Cell::new(self.storage.borrow().generation),next_insertion_id: Cell::new(0),diff --git a/Libraries/LibJS/Rust/src/runtime/module.rs b/Libraries/LibJS/Rust/src/runtime/module.rsindex 129f85e8eda..4f7b92ade88 100644--- a/Libraries/LibJS/Rust/src/runtime/module.rs+++ b/Libraries/LibJS/Rust/src/runtime/module.rs@@ -253,8 +253,7 @@ impl Module {}pub fn as_gc(&self) -> Gc<Module> {- // SAFETY: Modules only exist as cells once constructed.- unsafe { Gc::from_ref(self) }+ Gc::from_ref(self)}/// The module as a `T`, if it was allocated as one.diff --git a/Libraries/LibJS/Rust/src/runtime/native_function.rs b/Libraries/LibJS/Rust/src/runtime/native_function.rsindex 1cd03754861..55b44d3a7b9 100644--- a/Libraries/LibJS/Rust/src/runtime/native_function.rs+++ b/Libraries/LibJS/Rust/src/runtime/native_function.rs@@ -239,8 +239,7 @@ impl NativeFunction {}pub fn as_native_function_gc(&self) -> Gc<NativeFunction> {- // SAFETY: Native functions only exist as cells once constructed.- unsafe { Gc::from_ref(self) }+ Gc::from_ref(self)}fn methods(&self) -> &'static NativeFunctionMethods {@@ -444,8 +443,7 @@ impl NativeFunction {impl FunctionObject {pub fn as_function_object_gc(&self) -> Gc<FunctionObject> {- // SAFETY: Function objects only exist as cells once constructed.- unsafe { Gc::from_ref(self) }+ Gc::from_ref(self)}}diff --git a/Libraries/LibJS/Rust/src/runtime/native_javascript_backed_function.rs b/Libraries/LibJS/Rust/src/runtime/native_javascript_backed_function.rsindex 257029b6889..dda0cb8f2ee 100644--- a/Libraries/LibJS/Rust/src/runtime/native_javascript_backed_function.rs+++ b/Libraries/LibJS/Rust/src/runtime/native_javascript_backed_function.rs@@ -123,8 +123,7 @@ impl NativeJavaScriptBackedFunction {}pub fn as_native_javascript_backed_function_gc(&self) -> Gc<NativeJavaScriptBackedFunction> {- // SAFETY: NativeJavaScriptBackedFunctions only exist as cells once constructed.- unsafe { Gc::from_ref(self) }+ Gc::from_ref(self)}fn get_stack_frame_info(object: &Object, vm: &Vm, stack_frame_info: &mut StackFrameInfo) {diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rsindex 0dd0b37e415..ddceef47b48 100644--- a/Libraries/LibJS/Rust/src/runtime/object.rs+++ b/Libraries/LibJS/Rust/src/runtime/object.rs@@ -746,8 +746,7 @@ impl Object {}pub(crate) fn as_gc(&self) -> Gc<Object> {- // SAFETY: Objects only exist as cells once constructed, since allocate_object() moves every one into the heap.- unsafe { Gc::from_ref(self) }+ Gc::from_ref(self)}pub fn class(&self) -> &'static Class {diff --git a/Libraries/LibJS/Rust/src/runtime/primitive_string.rs b/Libraries/LibJS/Rust/src/runtime/primitive_string.rsindex d8d07b7b1ba..b6dd4adf3cf 100644--- a/Libraries/LibJS/Rust/src/runtime/primitive_string.rs+++ b/Libraries/LibJS/Rust/src/runtime/primitive_string.rs@@ -462,8 +462,7 @@ impl PrimitiveString {return PropertyKey::from(fly_string);}*entry = StringToAtomCacheEntry {- // SAFETY: Strings only exist as cells, since every way to create one allocates it.- string: Some(unsafe { Gc::from_ref(self) }),+ string: Some(Gc::from_ref(self)),atom: Some(fly_string.clone()),};PropertyKey::from(fly_string)@@ -506,8 +505,7 @@ impl PrimitiveString {return Ok(None);}- // SAFETY: Strings only exist as cells, since every way to create one allocates it.- let this = unsafe { Gc::from_ref(self) };+ let this = Gc::from_ref(self);Ok(Some(Value::from_string(Self::create_from_substring(vm, this, index, 1,))))diff --git a/Libraries/LibJS/Rust/src/runtime/promise.rs b/Libraries/LibJS/Rust/src/runtime/promise.rsindex 72e6a529e79..f2db24c62c3 100644--- a/Libraries/LibJS/Rust/src/runtime/promise.rs+++ b/Libraries/LibJS/Rust/src/runtime/promise.rs@@ -139,8 +139,7 @@ impl Promise {}pub fn as_gc(&self) -> Gc<Promise> {- // SAFETY: Promises only exist as cells once constructed.- unsafe { Gc::from_ref(self) }+ Gc::from_ref(self)}pub fn state(&self) -> PromiseState {diff --git a/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs b/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rsindex a7838e13dc9..6b591935249 100644--- a/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs+++ b/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs@@ -143,8 +143,7 @@ impl PromiseResolvingFunction {/// The function whose [[AlreadyResolved]] this one uses: the resolve function keeps the bit both share.fn already_resolved_owner(&self) -> Gc<PromiseResolvingFunction> {if self.kind == PromiseResolvingFunctionKind::Resolve {- // SAFETY: Promise resolving functions only exist as cells once constructed.- return unsafe { Gc::from_ref(self) };+ return Gc::from_ref(self);}self.resolve_functiondiff --git a/Libraries/LibJS/Rust/src/runtime/realm.rs b/Libraries/LibJS/Rust/src/runtime/realm.rsindex d73b183f467..f40a7d1e153 100644--- a/Libraries/LibJS/Rust/src/runtime/realm.rs+++ b/Libraries/LibJS/Rust/src/runtime/realm.rs@@ -216,8 +216,7 @@ impl Realm {}fn as_gc(&self) -> Gc<Realm> {- // SAFETY: Realms only exist as cells, since Realm::create() allocates every one.- unsafe { Gc::from_ref(self) }+ Gc::from_ref(self)}pub fn intrinsics(&self) -> Gc<Intrinsics> {diff --git a/Libraries/LibJS/Rust/src/runtime/shape.rs b/Libraries/LibJS/Rust/src/runtime/shape.rsindex a15af6c0be6..b9ceb460723 100644--- a/Libraries/LibJS/Rust/src/runtime/shape.rs+++ b/Libraries/LibJS/Rust/src/runtime/shape.rs@@ -353,8 +353,7 @@ impl Shape {}fn as_gc(&self) -> Gc<Shape> {- // SAFETY: Shapes only exist as cells, since every way to create one allocates it.- unsafe { Gc::from_ref(self) }+ Gc::from_ref(self)}fn descriptors(&self) -> Option<Gc<DescriptorArray>> {diff --git a/Libraries/LibJS/Rust/src/runtime/synthetic_module.rs b/Libraries/LibJS/Rust/src/runtime/synthetic_module.rsindex f69a93d91ea..47475fe393f 100644--- a/Libraries/LibJS/Rust/src/runtime/synthetic_module.rs+++ b/Libraries/LibJS/Rust/src/runtime/synthetic_module.rs@@ -161,8 +161,7 @@ impl SyntheticModule {}fn as_synthetic_module_gc(&self) -> Gc<SyntheticModule> {- // SAFETY: Modules only exist as cells once constructed.- unsafe { Gc::from_ref(self) }+ Gc::from_ref(self)}// 16.2.1.8.3 SetSyntheticModuleExport ( module, exportName, exportValue ), https://tc39.es/ecma262/#sec-setsyntheticmoduleexportdiff --git a/Libraries/LibJS/Rust/src/runtime/typed_array.rs b/Libraries/LibJS/Rust/src/runtime/typed_array.rsindex 9f9535403ab..aeb407c552b 100644--- a/Libraries/LibJS/Rust/src/runtime/typed_array.rs+++ b/Libraries/LibJS/Rust/src/runtime/typed_array.rs@@ -769,8 +769,7 @@ pub fn make_typed_array_with_buffer_witness_record(// 4. Return the TypedArray With Buffer Witness Record { [[Object]]: obj, [[CachedBufferByteLength]]: byteLength }.TypedArrayWithBufferWitness {- // SAFETY: Typed arrays only exist as cells.- object: unsafe { Gc::from_ref(typed_array) },+ object: Gc::from_ref(typed_array),cached_buffer_byte_length: byte_length,}}