Truly independent web browser
LibJS: Give non-extensible objects shapes of their own
Shannon Booth9e2237d29dce
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/property_additions.rs b/Libraries/LibJS/JIT/Rust/src/builder/property_additions.rsindex 171c5274495..9554cee0a3f 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/property_additions.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/property_additions.rs@@ -17,8 +17,7 @@//! usually knows, so that one case remains. The allocation makes room for the property inline, so the addition//! never grows the object's storage.//!-//! Plain objects compiled code allocates can always get properties: they are-//! extensible, and nothing about them makes additions take the slow path.+//! Preventing extensions changes an object's shape, so the shape check covers it.use super::GraphBuilder;use super::caches::Action;diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rsindex f36f93c42f0..bcebd650a8d 100644--- a/Libraries/LibJS/Rust/src/runtime/object.rs+++ b/Libraries/LibJS/Rust/src/runtime/object.rs@@ -1241,13 +1241,16 @@ impl Object {target_shape = target_shape.create_configure_transition(vm, property_key, new_attributes);}}+ if !target_shape.is_non_extensible() {+ target_shape = target_shape.create_non_extensible_transition(vm);+ }shape.set_cached_integrity_level_shape(vm, level, target_shape);target_shape}};- self.set_extensible(false);self.shape.set(target_shape);+ self.set_non_extensible(vm);true}@@ -1551,6 +1554,7 @@ impl Object {&& !from.requires_slow_add_own_property()&& !from.shape().is_dictionary()&& !from.shape().is_prototype_shape()+ && !from.shape().is_non_extensible()// NB: No other object may share the shape of a global object (see `Dependency::GlobalPropertyUnassigned`).&& !from.has_global_object_flag()&& from.shape().realm() == current_realm()@@ -2160,9 +2164,9 @@ impl Object {}// 10.1.4 [[PreventExtensions]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-preventextensions- pub fn ordinary_prevent_extensions(&self, _vm: &Vm) -> ThrowCompletionOr<bool> {+ pub fn ordinary_prevent_extensions(&self, vm: &Vm) -> ThrowCompletionOr<bool> {// 1. Set O.[[Extensible]] to false.- self.set_extensible(false);+ self.set_non_extensible(vm);// 2. Return true.Ok(true)@@ -3479,11 +3483,19 @@ impl Object {self.has_flag(object_flag::IS_EXTENSIBLE)}- pub fn set_extensible(&self, value: bool) {- if value {- self.set_flag(object_flag::IS_EXTENSIBLE);+ pub fn set_non_extensible(&self, vm: &Vm) {+ self.clear_flag(object_flag::IS_EXTENSIBLE);+ if self.shape().is_non_extensible() {+ return;+ }+ // NB: Reifying lazy function properties rebuilds the shape without the flag, so it comes first.+ self.reify_lazy_function_properties(vm);+ let shape = self.shape();+ if shape.is_dictionary() {+ self.dictionary_shape_to_change_in_place(vm)+ .set_non_extensible_without_transition(vm);} else {- self.clear_flag(object_flag::IS_EXTENSIBLE);+ self.shape.set(shape.create_non_extensible_transition(vm));}}diff --git a/Libraries/LibJS/Rust/src/runtime/shape.rs b/Libraries/LibJS/Rust/src/runtime/shape.rsindex b7c960c8dd0..b2567a4e761 100644--- a/Libraries/LibJS/Rust/src/runtime/shape.rs+++ b/Libraries/LibJS/Rust/src/runtime/shape.rs@@ -49,6 +49,10 @@ mod shape_flag {/// Something relies on the value in a slot of the object of this prototype shape (see/// `Shape::watch_slot_for_replacements()`).pub const HAS_WATCHED_SLOTS: u8 = 1 << 2;+ /// See `Shape::is_non_extensible()`.+ pub const NON_EXTENSIBLE: u8 = 1 << 3;+ /// 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()`).pub const UNSTABLE: u8 = 1 << 6;/// JIT code depends on the shape staying stable.@@ -118,6 +122,8 @@ struct RareData {assign_copy: Option<AssignCopy>,/// The shapes that sealing and freezing objects of this shape give them, by integrity level.integrity_level_shapes: [Option<Gc<Shape>>; 2],+ /// The shape that preventing extensions gives objects of this shape.+ non_extensible_transition: Option<GcWeak<Shape>>,/// What is known of the replacement of the values in the slots of the object that has this prototype shape, by/// offset (see `Shape::watch_slot_for_replacements()`).watched_slots: HashMap<u32, WatchedSlot, RandomState>,@@ -418,9 +424,7 @@ impl Shape {property_count_change: PropertyCountChange,) -> Self {let shape = Self::new(defer_gc, previous_shape.realm());- shape- .flags- .set(previous_shape.flags.get() & shape_flag::HAS_PARAMETER_MAP);+ shape.flags.set(previous_shape.flags.get() & shape_flag::INHERITED);shape.prototype.set(previous_shape.prototype());shape.previous.set(Some(previous_shape.as_gc()));let mut property_count = previous_shape.property_count();@@ -446,9 +450,7 @@ impl Shape {new_prototype: Option<Gc<Object>>,) -> Self {let shape = Self::new(defer_gc, realm);- shape- .flags- .set(previous_shape.flags.get() & shape_flag::HAS_PARAMETER_MAP);+ shape.flags.set(previous_shape.flags.get() & shape_flag::INHERITED);shape.prototype.set(new_prototype);// NB: Unlike the other transitions, this one does not keep the shape it came from alive. An object that changes// its prototype time and again (like a WindowProxy as its browsing context navigates) would otherwise keep@@ -564,7 +566,7 @@ impl Shape {new_shape.become_dictionary_shape(vm);new_shape.flags- .set(new_shape.flags.get() | (self.flags.get() & shape_flag::HAS_PARAMETER_MAP));+ .set(new_shape.flags.get() | (self.flags.get() & shape_flag::INHERITED));new_shape.prototype.set(self.prototype());self.invalidate_prototype_if_needed_for_new_prototype(vm, new_shape);self.copy_properties_to_dictionary_shape(vm, &new_shape);@@ -848,6 +850,38 @@ impl Shape {new_shape}+ /// The shape objects of this shape get when they stop being extensible, which has the same properties.+ pub fn create_non_extensible_transition(&self, vm: &Vm) -> Gc<Shape> {+ assert!(!self.is_dictionary() && !self.is_non_extensible());+ self.mark_unstable(vm);+ if !self.is_prototype_shape()+ && let Some(existing_shape) = self+ .rare_data()+ .and_then(|rare_data| rare_data.non_extensible_transition.as_ref()?.get())+ {+ return existing_shape;+ }+ let new_shape = vm+ .heap()+ .allocate(|defer_gc| Self::new_from_previous_shape(defer_gc, self, PropertyCountChange::Preserve));+ new_shape.flags.set(new_shape.flags.get() | shape_flag::NON_EXTENSIBLE);+ new_shape.set_descriptors(self.descriptors());+ self.invalidate_prototype_if_needed_for_new_prototype(vm, new_shape);+ if !self.is_prototype_shape() {+ let target = GcWeak::new(vm.heap(), new_shape);+ self.with_rare_data(vm, |rare_data| rare_data.non_extensible_transition = Some(target));+ }+ new_shape+ }++ /// Like create_non_extensible_transition(), for a dictionary shape, which changes in place.+ pub fn set_non_extensible_without_transition(&self, vm: &Vm) {+ self.invalidate_prototype_if_needed_for_change_without_transition(vm);+ assert!(self.is_dictionary());+ self.flags.set(self.flags.get() | shape_flag::NON_EXTENSIBLE);+ self.increment_dictionary_generation();+ }+fn ensure_descriptor_array(&self, vm: &Vm) {assert!(!self.is_dictionary());if self.descriptors().is_some() {@@ -1034,7 +1068,7 @@ impl Shape {let new_shape = Self::create(vm, self.realm());new_shape.flags- .set(new_shape.flags.get() | (self.flags.get() & shape_flag::HAS_PARAMETER_MAP));+ .set(new_shape.flags.get() | (self.flags.get() & shape_flag::INHERITED));new_shape.prototype.set(self.prototype());if self.is_dictionary() && self.property_count() > MAX_DESCRIPTOR_COUNT {new_shape.become_dictionary_shape(vm);@@ -1182,6 +1216,12 @@ impl Shape {self.flags.set(self.flags.get() | shape_flag::HAS_DEPENDENT_CODE);}+ /// 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 {+ self.flags.get() & shape_flag::NON_EXTENSIBLE != 0+ }+pub fn has_parameter_map(&self) -> bool {self.flags.get() & shape_flag::HAS_PARAMETER_MAP != 0}diff --git a/Tests/LibJS/Runtime/jit/property-additions.js b/Tests/LibJS/Runtime/jit/property-additions.jsindex 1e0980894c4..e9616720e93 100644--- a/Tests/LibJS/Runtime/jit/property-additions.js+++ b/Tests/LibJS/Runtime/jit/property-additions.js@@ -84,3 +84,73 @@ describe("property additions to allocated objects", () => {expect(build(4, object => object)).toEqual({ even: 4, extra: 0, flag: true });});});++describe("property additions after preventing extensions", () => {+ const lockers = {+ preventExtensions: object => Object.preventExtensions(object),+ reflectPreventExtensions: object => Reflect.preventExtensions(object),+ seal: object => Object.seal(object),+ freeze: object => Object.freeze(object),+ };+ function gatedLockers(lock) {+ const state = { armed: false };+ // Keep the call polymorphic so it is not inlined and its cold locking branch cannot exit the caller.+ const callbacks = [+ object => {+ if (state.armed) lock(object);+ },+ object => {+ if (state.armed) lock(object);+ },+ ];+ return { state, callbacks };+ }++ for (const [name, lock] of Object.entries(lockers)) {+ for (const strict of [false, true]) {+ const directive = strict ? "'use strict';" : "";+ const mode = strict ? "strict" : "sloppy";++ test(`${mode} object literal: ${name}`, () => {+ const { state, callbacks } = gatedLockers(lock);+ const make = new Function(+ "lock",+ "p",+ `${directive} const object = {}; if (p) lock(object); object.x = 1; return object;`+ );+ jit.prepare(make);+ for (let i = 0; i < 50; ++i) make(callbacks[i % 2], true);+ expect(jit.compile(make)).toBe(jit.enabled);++ state.armed = true;+ if (strict) {+ expect(() => make(callbacks[0], true)).toThrow(TypeError);+ } else {+ const object = make(callbacks[0], true);+ expect(Object.hasOwn(object, "x")).toBeFalse();+ expect(Object.isExtensible(object)).toBeFalse();+ }+ expect(make(lock, false).x).toBe(1);+ });++ test(`${mode} inlined constructor: ${name}`, () => {+ const { state, callbacks } = gatedLockers(lock);+ const Constructor = new Function("lock", "p", `${directive} if (p) lock(this); this.x = 1;`);+ const make = new Function("Constructor", "lock", "p", "return new Constructor(lock, p);");+ jit.prepare(make);+ for (let i = 0; i < 50; ++i) make(Constructor, callbacks[i % 2], true);+ expect(jit.compile(make)).toBe(jit.enabled);++ state.armed = true;+ if (strict) {+ expect(() => make(Constructor, callbacks[0], true)).toThrow(TypeError);+ } else {+ const object = make(Constructor, callbacks[0], true);+ expect(Object.hasOwn(object, "x")).toBeFalse();+ expect(Object.isExtensible(object)).toBeFalse();+ }+ expect(make(Constructor, lock, false).x).toBe(1);+ });+ }+ }+});