Skip to content

Truly independent web browser

LibJS: Upcast cells to their header instead of casting pointers

Andreas Kling6fa4cc36df7e
diff --git a/Libraries/LibJS/Rust/src/bytecode/feedback.rs b/Libraries/LibJS/Rust/src/bytecode/feedback.rs
index 68701049ad5..136f48d3f62 100644
--- a/Libraries/LibJS/Rust/src/bytecode/feedback.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/feedback.rs
@@ -17,7 +17,6 @@ use core::ptr::NonNull;
use crate::build_configuration::HEAP_REGION_OFFSET_MASK;
use crate::gc::capi;
-use crate::gc::class::{GcCell, class_of};
use crate::gc::heap::cell_is_dead;
use crate::gc::primitive_storage::{OwnedPrimitiveStorage, ZeroFillNewBytes};
use crate::layout::cell::{CellHeader, Gc};
@@ -212,13 +211,7 @@ impl CallFeedback {
/// Whether two cells are closures of one ECMAScript function.
pub fn are_closures_of_one_function(first: Gc<CellHeader>, second: Gc<CellHeader>) -> bool {
- let function_of = |cell: Gc<CellHeader>| {
- if !class_of(cell).is_subclass_of(Object::CLASS) {
- return None;
- }
- // SAFETY: The cell is an object.
- as_ecmascript_function_object(unsafe { Gc::<Object>::from_non_null(cell.as_non_null().cast()) })
- };
+ let function_of = |cell: Gc<CellHeader>| as_ecmascript_function_object(cell.downcast::<Object>()?);
match (function_of(first), function_of(second)) {
(Some(first), Some(second)) => first.shared_data() == second.shared_data(),
_ => false,
diff --git a/Libraries/LibJS/Rust/src/gc/class.rs b/Libraries/LibJS/Rust/src/gc/class.rs
index 2078071965c..0d580f9320d 100644
--- a/Libraries/LibJS/Rust/src/gc/class.rs
+++ b/Libraries/LibJS/Rust/src/gc/class.rs
@@ -275,6 +275,9 @@ unsafe impl<T: GcCell> Extends<T> for T {}
// SAFETY: Every cell starts with its header.
unsafe impl<T: GcCell> Extends<CellHeader> for T {}
+// SAFETY: A header starts with itself.
+unsafe impl Extends<CellHeader> for CellHeader {}
+
/// The class a cell was allocated with, which may be a subclass of its static type.
pub fn class_of<T>(cell: crate::layout::cell::Gc<T>) -> &'static Class {
// SAFETY: Every cell starts with its class.
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
index a7e18cedf12..84298587b86 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
@@ -1052,8 +1052,7 @@ pub(crate) fn record_callback(
vm,
frame,
pc,
- // SAFETY: Every cell starts with a cell header.
- unsafe { Gc::<CellHeader>::from_non_null(builtin.as_non_null().cast()) },
+ builtin.upcast::<CellHeader>(),
callback.as_cell(),
CallFeedbackForwarding::Callback,
0,
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/feedback.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/feedback.rs
index 3dc757ab730..def70afed56 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/feedback.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/feedback.rs
@@ -211,8 +211,7 @@ pub fn record_forwarded_call_from_native(
};
// SAFETY: A frame's caller outlives it.
let caller = unsafe { &*caller };
- // SAFETY: Every cell starts with a cell header.
- let function = unsafe { Gc::<CellHeader>::from_non_null(function.as_non_null().cast()) };
+ let function = function.upcast::<CellHeader>();
record_forwarded_call(
vm,
caller,
diff --git a/Libraries/LibJS/Rust/src/jit/allocation.rs b/Libraries/LibJS/Rust/src/jit/allocation.rs
index 685db0e8180..2a16c101718 100644
--- a/Libraries/LibJS/Rust/src/jit/allocation.rs
+++ b/Libraries/LibJS/Rust/src/jit/allocation.rs
@@ -21,7 +21,7 @@ use libjs_jit::snapshot::{
use super::offset;
use crate::bytecode::executable::Executable;
-use crate::gc::class::{GcCell, class_of};
+use crate::gc::class::{Extends, GcCell, class_of};
use crate::interpreter::slow_paths::bindings::create_lexical_environment;
use crate::interpreter::vm::Vm;
use crate::layout::cell::{CellHeader, Gc};
@@ -177,9 +177,8 @@ pub fn allocation_infos(vm: &Vm, realm: Gc<Realm>) -> AllocationInfos {
}
}
-fn header_of<T>(cell: Gc<T>) -> Gc<CellHeader> {
- // SAFETY: Every cell starts with a cell header.
- unsafe { Gc::from_non_null(cell.as_non_null().cast()) }
+fn header_of<T: Extends<CellHeader>>(cell: Gc<T>) -> Gc<CellHeader> {
+ cell.upcast()
}
/// The bytes of a new rope string, with null halves.
diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rs
index 9670d8a8b26..e0d9416873b 100644
--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs
+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs
@@ -32,6 +32,7 @@ use super::runtime_info::RAW_NATIVE_FUNCTIONS_RETURN_IN_REGISTERS;
use crate::bytecode::executable::{Executable, PropertyLookupCacheEntryData, PropertyLookupCacheTier};
use crate::bytecode::feedback::{CallFeedback, CallFeedbackForwarding, call_feedback_flags};
use crate::bytecode::instruction::instruction_length_from_bytes;
+use crate::gc::class::Extends;
use crate::interpreter::vm::Vm;
use crate::layout::cell::{CellHeader, Gc};
use crate::layout::environment::DeclarativeEnvironment;
@@ -335,14 +336,13 @@ fn word_of<T>(cell: Gc<T>, offset: usize) -> u64 {
}
impl SnapshotBuilder<'_> {
- fn cell<T>(&mut self, cell: Gc<T>) -> CellId {
- // SAFETY: Every cell starts with a cell header.
- let header = unsafe { Gc::<CellHeader>::from_non_null(cell.as_non_null().cast()) };
+ fn cell<T: Extends<CellHeader>>(&mut self, cell: Gc<T>) -> CellId {
+ let header = cell.upcast::<CellHeader>();
self.cells.push(header);
CellId(cell.as_ptr() as u64)
}
- fn optional_cell<T>(&mut self, cell: Option<Gc<T>>) -> Option<CellId> {
+ fn optional_cell<T: Extends<CellHeader>>(&mut self, cell: Option<Gc<T>>) -> Option<CellId> {
cell.map(|cell| self.cell(cell))
}
@@ -1154,8 +1154,7 @@ pub fn capture_snapshot(
let Some(sample) = sample else {
continue;
};
- // SAFETY: Every cell starts with a cell header.
- let sample_cell = unsafe { Gc::<CellHeader>::from_non_null(sample.as_non_null().cast()) };
+ let sample_cell = sample.upcast::<CellHeader>();
let inline_executable = builder.inline_executable_index(Some(sample_cell), &root);
if let Some(Some(template)) = executables[0].closure_templates.get_mut(template_index) {
template.inline_executable = inline_executable;
diff --git a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
index 61b319f0543..7b7d40f10fb 100644
--- a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
@@ -124,13 +124,9 @@ fn as_ecmascript_function(object: &Object) -> &EcmascriptFunctionObject {
cast_ref(object)
}
-/// The object as an ECMAScript function object, if its flag says that it is one.
+/// The object as an ECMAScript function object, if it is one.
pub fn as_ecmascript_function_object<T: Extends<Object>>(cell: Gc<T>) -> Option<Gc<EcmascriptFunctionObject>> {
- let object = cell.upcast::<Object>();
- object
- .is_ecmascript_function_object()
- // SAFETY: Only ECMAScript function objects have the flag.
- .then(|| unsafe { Gc::from_non_null(object.as_non_null().cast()) })
+ cell.upcast::<Object>().downcast()
}
/// Value::as_if<ECMAScriptFunctionObject>().
diff --git a/Libraries/LibJS/Rust/src/runtime/environment_shape.rs b/Libraries/LibJS/Rust/src/runtime/environment_shape.rs
index 1b9c41f6406..b800e4ffee3 100644
--- a/Libraries/LibJS/Rust/src/runtime/environment_shape.rs
+++ b/Libraries/LibJS/Rust/src/runtime/environment_shape.rs
@@ -11,7 +11,7 @@ use std::collections::HashMap;
use ak::Utf16FlyString;
use libjs_runtime_macros::Trace;
-use crate::gc::class::{Finalize, GcCell, define_cell};
+use crate::gc::class::{Extends, Finalize, GcCell, define_cell};
use crate::gc::visitor::{Trace, Visitor};
use crate::interpreter::vm::Vm;
use crate::layout::buffer::InterpreterBuffer;
@@ -115,10 +115,9 @@ impl EnvironmentShapeCache {
/// # Safety
///
/// `slot` must be part of the cell `owner`, or of memory that `owner` owns and only frees when it is destroyed.
- pub unsafe fn new<Owner>(owner: Gc<Owner>, slot: &Cell<Option<Gc<EnvironmentShape>>>) -> Self {
+ pub unsafe fn new<Owner: Extends<CellHeader>>(owner: Gc<Owner>, slot: &Cell<Option<Gc<EnvironmentShape>>>) -> Self {
Self {
- // SAFETY: Every cell starts with its header.
- owner: unsafe { Gc::from_non_null(owner.as_non_null().cast()) },
+ owner: owner.upcast(),
slot: NonNull::from(slot),
}
}