Skip to content

Truly independent web browser

LibJS: Keep cells whose addresses JIT code copies from moving

Andreas Kling3374d249388b
diff --git a/Libraries/LibJS/Rust/src/jit/allocation.rs b/Libraries/LibJS/Rust/src/jit/allocation.rs
index db280b73ab5..d735ea12ff1 100644
--- a/Libraries/LibJS/Rust/src/jit/allocation.rs
+++ b/Libraries/LibJS/Rust/src/jit/allocation.rs
@@ -36,6 +36,7 @@ use crate::layout::primitive_string::PrimitiveString;
use crate::layout::realm::Realm;
use crate::layout::shape::Shape;
use crate::layout::value::Value;
+use crate::layout_forward::IDENTITY_HASH_STATE_OFFSET;
use crate::runtime::array::Array;
use crate::runtime::completion::Must;
use crate::runtime::declarative_environment::{DECLARATIVE_ENVIRONMENT_CELL_SIZES, inline_binding_size_class};
@@ -67,6 +68,10 @@ fn for_each_cell_word<T>(cell: Gc<T>, size: usize, mut word: impl FnMut(u64)) {
let len = (size - start).min(8);
// SAFETY: The cell is live and has `size` bytes, of which these `len` are some, and `bytes` has room for 8.
unsafe { core::ptr::copy_nonoverlapping(cell.as_ptr().cast::<u8>().add(start), bytes.as_mut_ptr(), len) };
+ // NB: A copy is a new cell, which has not been hashed, whatever the identity hash state of the sample is.
+ if (start..start + len).contains(&IDENTITY_HASH_STATE_OFFSET) {
+ bytes[IDENTITY_HASH_STATE_OFFSET - start] = 0;
+ }
word(u64::from_ne_bytes(bytes));
}
}
@@ -110,6 +115,7 @@ pub fn allocation_infos<'d>(
};
let object_template = Object::create(vm, realm, Some(realm.object_prototype()));
+ object_template.shape().note_in_allocation_template();
let mut object = ObjectAllocationInfo {
empty_object_inline_capacity: u32::from(EMPTY_PLAIN_OBJECT_INLINE_CAPACITY),
heap: heap_address,
@@ -149,6 +155,7 @@ pub fn allocation_infos<'d>(
let array_template = Array::create(vm, realm, 0, None).must();
let array_shape = array_template.shape();
+ array_shape.note_in_allocation_template();
// NB: Storage is only allocated inline where the heap gives the addresses of all the free lists it comes from.
let storage_size_classes = snapshot.begin();
let mut has_all_free_lists = true;
@@ -580,6 +587,7 @@ fn closure_template(
if !sample.can_be_copied() {
return None;
}
+ sample.shape().note_in_allocation_template();
let words = cell_words(snapshot, sample, size_of::<EcmascriptFunctionObject>());
Some((sample, words))
}
diff --git a/Libraries/LibJS/Rust/src/jit/tier_up.rs b/Libraries/LibJS/Rust/src/jit/tier_up.rs
index 663b8f0dfec..f8f586811a1 100644
--- a/Libraries/LibJS/Rust/src/jit/tier_up.rs
+++ b/Libraries/LibJS/Rust/src/jit/tier_up.rs
@@ -357,13 +357,27 @@ fn install(
defer_gc: &DeferGc<'_>,
) {
let artifact = compiled.artifact();
- let embedded_cells = artifact
+ let mut embedded_cells: Vec<Gc<CellHeader>> = artifact
.embedded_cells()
.map(|cell| {
// SAFETY: The snapshot kept every cell it captured alive, and the code embeds only those.
unsafe { Gc::from_non_null(NonNull::new(cell.0 as *mut CellHeader).expect("embedded cells are not null")) }
})
.collect();
+ // NB: Code that embeds an ECMAScript function may also embed the words its frames start with (see
+ // `FunctionFrameFields`), whose environments the code then keeps from moving like the cells it lists.
+ let frame_field_cells: Vec<Gc<CellHeader>> = embedded_cells
+ .iter()
+ .filter_map(|cell| cell.downcast::<EcmascriptFunctionObject>())
+ .flat_map(|function| {
+ [
+ function.environment().map(Gc::upcast),
+ function.private_environment().map(Gc::upcast),
+ ]
+ })
+ .flatten()
+ .collect();
+ embedded_cells.extend(frame_field_cells);
let id = vm.jit.next_code_id();
let mut code = JitCode::new(
memory,
diff --git a/Libraries/LibJS/Rust/src/runtime/shape.rs b/Libraries/LibJS/Rust/src/runtime/shape.rs
index ae8d378c57d..e57afeca4c4 100644
--- a/Libraries/LibJS/Rust/src/runtime/shape.rs
+++ b/Libraries/LibJS/Rust/src/runtime/shape.rs
@@ -51,6 +51,9 @@ mod shape_flag {
pub const HAS_WATCHED_SLOTS: u8 = 1 << 2;
/// See `Shape::is_non_extensible()`.
pub const NON_EXTENSIBLE: u8 = 1 << 3;
+ /// JIT code allocates objects of the shape from a template that holds its address (see
+ /// `Shape::note_in_allocation_template()`).
+ pub const IN_ALLOCATION_TEMPLATE: u8 = 1 << 4;
/// The flags a shape gets from the shape it comes from.
pub const INHERITED: u8 = HAS_PARAMETER_MAP | NON_EXTENSIBLE;
/// An object may have left the shape (see `Shape::is_stable()`).
@@ -323,9 +326,9 @@ define_cell!(Shape, Other);
// the storage owns.
unsafe impl Trace for Shape {
fn trace(&self, visitor: &mut Visitor) {
- // NB: The VM keeps the compiled code that depends on a shape by the address of the shape, so such a shape
- // stays where it is.
- if self.flags.get() & shape_flag::HAS_DEPENDENT_CODE != 0 {
+ // NB: The VM keeps the compiled code that depends on a shape by the address of the shape, and JIT code copies
+ // the address from allocation templates, so such shapes stay where they are.
+ if self.flags.get() & (shape_flag::HAS_DEPENDENT_CODE | shape_flag::IN_ALLOCATION_TEMPLATE) != 0 {
visitor.pin(Gc::from_ref(self));
}
self.realm.trace(visitor);
@@ -1320,6 +1323,12 @@ impl Shape {
self.flags.set(self.flags.get() | shape_flag::HAS_DEPENDENT_CODE);
}
+ /// Notes that the words of a template JIT code allocates cells from hold the address of the shape, which keeps the
+ /// shape from moving from now on.
+ pub fn note_in_allocation_template(&self) {
+ self.flags.set(self.flags.get() | shape_flag::IN_ALLOCATION_TEMPLATE);
+ }
+
/// Whether the objects of this shape are not extensible. Preventing extensions moves an object to such a shape, so
/// that caches of additions to objects of the shape it had no longer apply to it.
pub fn is_non_extensible(&self) -> bool {