Skip to content

Truly independent web browser

LibJS: Keep JIT `in` and `hasOwnProperty` checks from creating values

Tim Ledbetter7c31bcf76bcb
diff --git a/Libraries/LibJS/Rust/src/jit/intrinsics.rs b/Libraries/LibJS/Rust/src/jit/intrinsics.rs
index 6937e8f6fbd..3274ee8c0e6 100644
--- a/Libraries/LibJS/Rust/src/jit/intrinsics.rs
+++ b/Libraries/LibJS/Rust/src/jit/intrinsics.rs
@@ -25,6 +25,8 @@ use crate::runtime::module_namespace_object::ModuleNamespaceObject;
use crate::runtime::object::PropertyKind;
use crate::runtime::property_key::{PropertyKey, StringMayBeNumber};
use crate::runtime::proxy_object::ProxyObject;
+use crate::runtime::string_object::StringObject;
+use crate::runtime::typed_array::{ContentType, TypedArrayBase};
use crate::utf16::{has_fly_string_storage, to_utf16_fly_string};
/// The property key of a string or symbol, which converting runs no code for.
@@ -45,6 +47,34 @@ fn has_ordinary_lookups(object: Gc<Object>) -> bool {
&& !object.is_platform_object()
}
+/// 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 || *key == vm.names.caller || *key == vm.names.arguments)
+ {
+ return false;
+ }
+ if key.is_number() {
+ return !object.is::<StringObject>()
+ && !object
+ .downcast::<TypedArrayBase>()
+ .is_some_and(|typed_array| typed_array.content_type() == ContentType::BigInt);
+ }
+ !key.is_string() || !object.has_intrinsic_accessors()
+}
+
+/// Whether looking up the property `key` creates nothing in the object or in its prototypes.
+fn prototype_chain_lookup_creates_nothing(vm: &Vm, object: Gc<Object>, key: &PropertyKey) -> bool {
+ let mut holder = Some(object);
+ while let Some(current) = holder {
+ if !lookup_creates_nothing(vm, current, key) {
+ return false;
+ }
+ holder = current.prototype();
+ }
+ true
+}
+
/// The fly string of a string key, if the string is stored as one already.
fn fly_string_key(key: Value) -> Option<Utf16FlyString> {
if !key.is_string() {
@@ -119,7 +149,8 @@ pub unsafe extern "C" fn libjs_jit_has_own_property(vm: *const Vm, object: u64,
{
return Value::from_bool(result).0;
}
- let Some(key) = string_or_symbol_key(vm, key) else {
+ let Some(key) = string_or_symbol_key(vm, key).filter(|key| lookup_creates_nothing(vm, object.as_object(), key))
+ else {
return Value::EMPTY.0;
};
object
@@ -153,6 +184,9 @@ pub unsafe extern "C" fn libjs_jit_has_property(vm: *const Vm, key: u64, object:
}
if index_key {
let key = PropertyKey::from(key_value.as_i32().cast_unsigned());
+ if !prototype_chain_lookup_creates_nothing(vm, object.as_object(), &key) {
+ return Value::EMPTY.0;
+ }
return object
.as_object()
.has_property(vm, &key)
@@ -169,7 +203,9 @@ pub unsafe extern "C" fn libjs_jit_has_property(vm: *const Vm, key: u64, object:
Some(true) => return Value::TRUE.0,
Some(false) => holder = current.prototype(),
None => {
- let Some(key) = string_or_symbol_key(vm, key_value) else {
+ let Some(key) = string_or_symbol_key(vm, key_value)
+ .filter(|key| prototype_chain_lookup_creates_nothing(vm, current, key))
+ else {
return Value::EMPTY.0;
};
return current.has_property(vm, &key).map_or(Value::EMPTY, Value::from_bool).0;
diff --git a/Tests/LibJS/Runtime/jit/regress/lazy-prototypes-in-has-property-checks.js b/Tests/LibJS/Runtime/jit/regress/lazy-prototypes-in-has-property-checks.js
new file mode 100644
index 00000000000..ce40cf7546d
--- /dev/null
+++ b/Tests/LibJS/Runtime/jit/regress/lazy-prototypes-in-has-property-checks.js
@@ -0,0 +1,31 @@
+// Checking whether a function has a `prototype` creates the prototype the first time. If the function is the
+// prototype of another object, this throws away compiled code that reads properties of that object. A compiled
+// function that runs such a check must keep its code. The callee has an unused branch so that it is too large to
+// inline, and the caller calls it directly. It gets `hasOwnProperty` as a parameter, because looking up `Object` in
+// it would register its frame early and hide the bug. The last call passes an object of another shape, so that the
+// callee falls back to the interpreter after the check, which fails if its code is gone.
+
+function makeCallee(parameters, body) {
+ return new Function("pad", ...parameters, `if (pad === 1) { ${"pad = pad + 1; ".repeat(16)}} ${body}`);
+}
+
+function testPrototypeCheck(check) {
+ const callee = makeCallee(["target", "object", "hasOwnProperty"], `return [${check}, object.name];`);
+ const caller = (target, object) => callee(undefined, target, object, Object.prototype.hasOwnProperty);
+ const target = function target() {};
+ const object = Object.create(target);
+ jit.prepare(callee);
+ jit.prepare(caller);
+ for (let i = 0; i < 10; ++i) expect(caller({}, object)).toEqual([false, "target"]);
+ jit.compile(callee);
+ jit.compile(caller);
+ expect(caller(target, { name: "other" })).toEqual([true, "other"]);
+}
+
+test("in", () => {
+ testPrototypeCheck(`"prototype" in target`);
+});
+
+test("hasOwnProperty", () => {
+ testPrototypeCheck(`hasOwnProperty.call(target, "prototype")`);
+});