Skip to content

Truly independent web browser

LibJS: Allocate objects that calls can read through `f.arguments`

Tim Ledbetter695db34547d9
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/escape_analysis.rs b/Libraries/LibJS/JIT/Rust/src/passes/escape_analysis.rs
index 6470dd1e806..d41b23b0649 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/escape_analysis.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/escape_analysis.rs
@@ -9,8 +9,9 @@
//! An object an `AllocateObject` makes escapes when anything could see it
//! besides accesses of its named properties: when it is stored into the
//! frame or into an object that escapes, passed to anything but property
-//! accesses and checks (an operand of a slow path or a call included), or
-//! merged in a phi. An object that does not escape is never allocated: its
+//! accesses and checks (an operand of a slow path or a call included),
+//! passed to an inlined call whose legacy `arguments` code it runs can read,
+//! or merged in a phi. An object that does not escape is never allocated: its
//! properties become SSA values, initialized to `undefined`. Stores update
//! them, loads become the value stored last, checks of the object (whose
//! shape is known) go away, and where paths merge, phis merge the values.
@@ -112,6 +113,14 @@ fn analyze(graph: &Graph, placed: &[NodeId], escaping: &[NodeId]) -> Analysis {
}
}
+ for node in placed {
+ for value in graph.legacy_argument_values_read_by(*node) {
+ if let Some(root) = roots.get(&value) {
+ escaped.insert(root.index());
+ }
+ }
+ }
+
let shapes_before = shapes_before_uses(graph, &candidates, &roots, &mut escaped);
// Values stored into candidates: (value's candidate, the candidate it is stored into).
diff --git a/Tests/LibJS/JIT/ir/legacy-arguments-eligibility.js b/Tests/LibJS/JIT/ir/legacy-arguments-eligibility.js
new file mode 100644
index 00000000000..b6779b8cb13
--- /dev/null
+++ b/Tests/LibJS/JIT/ir/legacy-arguments-eligibility.js
@@ -0,0 +1,56 @@
+// Only functions with legacy arguments properties expose their inlined arguments to calls.
+
+function opaque() {
+ // A for-in loop keeps this function from being inlined.
+ for (const key in {}) {
+ }
+}
+const arrow = object => {
+ opaque();
+ return object.x;
+};
+const holder = {
+ method(object) {
+ opaque();
+ return object.x;
+ },
+};
+function ordinary(object) {
+ opaque();
+ return object.x;
+}
+function throughArrow(x) {
+ return arrow({ x });
+}
+function throughMethod(x) {
+ return holder.method({ x });
+}
+function throughOrdinary(x) {
+ return ordinary({ x });
+}
+for (const fn of [opaque, arrow, holder.method, ordinary, throughArrow, throughMethod, throughOrdinary])
+ jit.prepare(fn);
+for (let i = 0; i < 10; ++i) {
+ throughArrow(i);
+ throughMethod(i);
+ throughOrdinary(i);
+}
+jit.compile(throughArrow);
+jit.compile(throughMethod);
+jit.compile(throughOrdinary);
+
+// CHECK-FUNCTION: throughArrow
+// CHECK-PASS: graph building
+// CHECK: AllocateObject
+// CHECK-PASS: escape analysis
+// CHECK-NOT: AllocateObject
+
+// CHECK-FUNCTION: throughMethod
+// CHECK-PASS: graph building
+// CHECK: AllocateObject
+// CHECK-PASS: escape analysis
+// CHECK-NOT: AllocateObject
+
+// CHECK-FUNCTION: throughOrdinary
+// CHECK-PASS: escape analysis
+// CHECK: AllocateObject
diff --git a/Tests/LibJS/Runtime/jit/regress/legacy-arguments-of-inlined-functions.js b/Tests/LibJS/Runtime/jit/regress/legacy-arguments-of-inlined-functions.js
index 037e8fecd4c..2f55fb72534 100644
--- a/Tests/LibJS/Runtime/jit/regress/legacy-arguments-of-inlined-functions.js
+++ b/Tests/LibJS/Runtime/jit/regress/legacy-arguments-of-inlined-functions.js
@@ -86,3 +86,26 @@ test("a function called by an inlined function adds a property to an object in i
);
expect(failures).toEqual([]);
});
+
+test("a function called by an inlined function changes a property of an object in its legacy arguments", () => {
+ const failures = compareCompiledWithInterpreter(
+ () => {
+ function changeProperty(change) {
+ // A for-in loop keeps this function from being inlined.
+ for (const key in {}) {
+ }
+ if (change) middle.arguments[0].x = 2;
+ }
+ function middle(object, change) {
+ changeProperty(change);
+ }
+ return change => {
+ const object = { x: 1 };
+ middle(object, change);
+ return object.x;
+ };
+ },
+ { warmUp: () => [false], probes: { change: () => [true] } }
+ );
+ expect(failures).toEqual([]);
+});