Truly independent web browser
LibJS: Keep JIT property checks from creating function lengths and names
Tim Ledbetter46e8b45edae7
diff --git a/Libraries/LibJS/Rust/src/jit/intrinsics.rs b/Libraries/LibJS/Rust/src/jit/intrinsics.rsindex 0a85e3ed6fd..dc0ecf348f2 100644--- a/Libraries/LibJS/Rust/src/jit/intrinsics.rs+++ b/Libraries/LibJS/Rust/src/jit/intrinsics.rs@@ -19,6 +19,7 @@ use crate::layout::object::Object;use crate::layout::value::Value;use crate::runtime::array::Array;use crate::runtime::completion::Must;+use crate::runtime::ecmascript_function_object::as_ecmascript_function_object;use crate::runtime::module_namespace_object::ModuleNamespaceObject;use crate::runtime::property_key::{PropertyKey, StringMayBeNumber};use crate::runtime::proxy_object::ProxyObject;@@ -106,9 +107,14 @@ fn has_ordinary_lookups(object: Gc<Object>) -> bool {&& !object.is_platform_object()}+/// Whether the object is an ECMAScript function that has the property `key` without having made it yet.+fn has_lazy_function_property(vm: &Vm, object: Gc<Object>, key: &PropertyKey) -> bool {+ as_ecmascript_function_object(object).is_some_and(|function| function.has_lazy_property(vm, key))+}+/// Whether looking up the property `key` of the object creates nothing.fn lookup_creates_nothing(vm: &Vm, object: Gc<Object>, key: &PropertyKey) -> bool {- if object.is_ecmascript_function_object() && *key == vm.names.prototype {+ if has_lazy_function_property(vm, object, key) {return false;}if key.is_number() {@@ -199,12 +205,17 @@ pub extern "C" fn libjs_jit_has_own_property(vm: &Vm, object: u64, key: u64) ->{return Value::from_bool(result).0;}- let Some(key) = string_or_symbol_key(vm, key).filter(|key| lookup_creates_nothing(vm, object.as_object(), key))- else {+ let Some(key) = string_or_symbol_key(vm, key) else {return Value::EMPTY.0;};+ let object = object.as_object();+ if has_lazy_function_property(vm, object, &key) {+ return Value::TRUE.0;+ }+ if !lookup_creates_nothing(vm, object, &key) {+ return Value::EMPTY.0;+ }object- .as_object().has_own_property(vm, &key).map_or(Value::EMPTY, Value::from_bool).0@@ -250,11 +261,15 @@ pub extern "C" fn libjs_jit_has_property(vm: &Vm, key: u64, object: u64) -> u64Some(true) => return Value::TRUE.0,Some(false) => holder = current.prototype(),None => {- let Some(key) = string_or_symbol_key(vm, key_value)- .filter(|key| prototype_chain_lookup_creates_nothing(vm, current, key))- else {+ let Some(key) = string_or_symbol_key(vm, key_value) else {return Value::EMPTY.0;};+ if has_lazy_function_property(vm, current, &key) {+ return Value::TRUE.0;+ }+ if !prototype_chain_lookup_creates_nothing(vm, current, &key) {+ return Value::EMPTY.0;+ }return current.has_property(vm, &key).map_or(Value::EMPTY, Value::from_bool).0;}}diff --git a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rsindex 30b78bf87ca..32bcaee0228 100644--- a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs@@ -494,6 +494,14 @@ impl EcmascriptFunctionObject {);}+ /// Whether the function has the property `property_key` without having made it yet, which looking it up makes.+ pub(crate) fn has_lazy_property(&self, vm: &Vm, property_key: &PropertyKey) -> bool {+ (self.has_flag(ecmascript_function_flags::HAS_LAZY_LENGTH_AND_NAME)+ && (*property_key == vm.names.length || *property_key == vm.names.name))+ || (self.has_flag(ecmascript_function_flags::MAY_NEED_LAZY_PROTOTYPE_INSTANTIATION)+ && *property_key == vm.names.prototype)+ }+pub(crate) fn reify_lazy_property(&self, vm: &Vm, property_key: &PropertyKey) {if self.has_flag(ecmascript_function_flags::HAS_LAZY_LENGTH_AND_NAME)&& (*property_key == vm.names.length || *property_key == vm.names.name)diff --git a/Tests/LibJS/Runtime/jit/regress/lazy-lengths-and-names-in-has-property-checks.js b/Tests/LibJS/Runtime/jit/regress/lazy-lengths-and-names-in-has-property-checks.jsnew file mode 100644index 00000000000..78ece74e81e--- /dev/null+++ b/Tests/LibJS/Runtime/jit/regress/lazy-lengths-and-names-in-has-property-checks.js@@ -0,0 +1,40 @@+// A compiled `in` or `hasOwnProperty` check of a function's `length` or `name` must not lose code or corrupt objects.++function makeCallee(parameters, body) {+ return new Function("pad", ...parameters, `if (pad === 1) { ${"pad = pad + 1; ".repeat(100)}} ${body}`);+}++function makeTarget() {+ const target = function target() {};+ target.a = 1;+ target.b = 2;+ return target;+}++function testCheck(check, key) {+ const callee = makeCallee(+ ["target", "key", "object", "hasOwnProperty"],+ `const a = target.a; const result = ${check}; target.b = 3; return [result, a, object.name];`+ );+ const caller = (target, key, object) => callee(undefined, target, key, object, Object.prototype.hasOwnProperty);+ jit.prepare(callee);+ jit.prepare(caller);+ for (let i = 0; i < 10; ++i) expect(caller(makeTarget(), "c", { name: "first" })).toEqual([false, 1, "first"]);+ jit.compile(callee);+ jit.compile(caller);+ const target = makeTarget();+ expect(caller(target, key, { other: 1, name: "second" })).toEqual([true, 1, "second"]);+ expect(target.b).toBe(3);+ expect(target.length).toBe(0);+ expect(target.name).toBe("target");+}++test("in", () => {+ testCheck("key in target", "length");+ testCheck("key in target", "name");+});++test("hasOwnProperty", () => {+ testCheck("hasOwnProperty.call(target, key)", "length");+ testCheck("hasOwnProperty.call(target, key)", "name");+});