Skip to content

Truly independent web browser

LibJS: Keep shape checks on objects reachable through `f.arguments`

Tim Ledbetter1804bce52566
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
index 611d074c721..2565be6f827 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
@@ -76,7 +76,9 @@ pub(super) struct InlineCall {
/// The callee's `this`, or the empty value if it never reads it.
pub this: NodeId,
pub passed_argument_count: u32,
- /// For callees that are not strict: the values the call passes beyond
+ /// Whether this call must preserve arguments for legacy `arguments`.
+ pub has_legacy_arguments: bool,
+ /// For callees with legacy `arguments`: the values the call passes beyond
/// the arguments the callee's bytecode reads (see
/// `legacy_argument_values()`).
pub untracked_arguments: Vec<NodeId>,
@@ -239,6 +241,7 @@ impl GraphBuilder<'_> {
in_frame,
parent: self.parent_frame_state(),
passed_argument_count: None,
+ legacy_arguments: self.legacy_argument_slots(),
}
}
@@ -297,7 +300,7 @@ impl GraphBuilder<'_> {
values
}
- /// For an inlined callee being walked that is not strict: the values of
+ /// For an inlined callee with legacy `arguments`: the values of
/// all its passed arguments, which its legacy `arguments` shows. Those
/// its bytecode reads are its frame's, the others the values the call
/// passed.
@@ -320,6 +323,16 @@ impl GraphBuilder<'_> {
values
}
+ fn legacy_argument_slots(&self) -> core::ops::Range<u32> {
+ let function = &self.function;
+ let Some(call) = function.inline.as_ref().filter(|_| function.has_legacy_arguments()) else {
+ return 0..0;
+ };
+ let layout = function.layout;
+ let count = layout.number_of_arguments + call.untracked_arguments.len() as u32;
+ layout.arguments_base()..layout.arguments_base() + count
+ }
+
/// The parent of frame states of the function being walked.
pub(super) fn parent_frame_state(&self) -> Option<FrameStateId> {
self.function.inline.as_ref().map(|call| call.caller_frame_state)
@@ -923,6 +936,10 @@ impl GraphBuilder<'_> {
if !within_budget {
return Ok(None);
}
+ // NB: CheckClosure checks shared data, not legacy-property eligibility.
+ // Keep non-strict closure calls conservative.
+ let has_legacy_arguments = callee_executable.supports_legacy_arguments
+ || (closure.is_some() && callee_executable.is_non_strict_function());
let Ok(mut callee_function) = Function::new(index, callee_executable, decoded) else {
return Ok(None);
};
@@ -1055,7 +1072,8 @@ impl GraphBuilder<'_> {
callback,
this: receiver,
passed_argument_count: *argument_count,
- untracked_arguments: if callee_function.is_non_strict_function() {
+ has_legacy_arguments,
+ untracked_arguments: if has_legacy_arguments {
argument_values
.get(layout.number_of_arguments as usize..)
.unwrap_or_default()
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
index e6ef8782c07..35aabf8e9bb 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
@@ -456,15 +456,9 @@ impl<'a> Function<'a> {
self.inline.is_some()
}
- /// Whether this is an inlined callee with legacy `arguments`: a function
- /// that is not strict.
+ /// Whether this inlined call must preserve legacy `arguments`.
fn has_legacy_arguments(&self) -> bool {
- self.is_virtual() && self.is_non_strict_function()
- }
-
- /// Whether this is a function that is not strict (and no builtin).
- fn is_non_strict_function(&self) -> bool {
- !self.executable.builtin && self.executable.function.is_some_and(|function| !function.strict)
+ self.inline.as_ref().is_some_and(|call| call.has_legacy_arguments)
}
}
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
index f9acf5571d9..014d8c3c56e 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
@@ -1543,6 +1543,7 @@ pub(crate) mod inlining {
let mut callee_snapshot = snapshot_for(callee, test_layout()).executables.remove(0);
callee_snapshot.cell = CellId(0x200);
callee_snapshot.function = Some(function);
+ callee_snapshot.supports_legacy_arguments = !function.strict;
callee_snapshot.environment = Some(CellId(ENVIRONMENT));
snapshot.executables.push(callee_snapshot);
let callee_bytes = snapshot.executables[1].bytecode.as_ptr() as u64;
@@ -1631,6 +1632,69 @@ pub(crate) mod inlining {
assert!(!dump.contains("Accessor"), "{dump}");
}
+ #[test]
+ fn inlined_legacy_arguments_use_eligibility_but_keep_closures_conservative() {
+ use super::property_access::{cache, get_by_id, own};
+ use crate::snapshot::{AccessorFunctionSnapshot, PropertyCacheEntrySnapshot, PropertyCacheEntryType};
+ let caller = assemble(|_| vec![get_by_id(r(5), a(0), 0), Instruction::Return { value: r(5) }]);
+ let callee = assemble(|_| {
+ vec![
+ Instruction::Enter,
+ call(r(5), r(6), c(0), Vec::new()),
+ Instruction::Return { value: c(0) },
+ ]
+ });
+ for (strict, eligible, closures) in [
+ (false, false, false),
+ (false, true, false),
+ (false, false, true),
+ (true, false, true),
+ ] {
+ let mut snapshot = snapshot_with_callee(&caller, &callee, function(strict, false));
+ snapshot.executables[1].supports_legacy_arguments = eligible;
+ let entries = (0..if closures { 2 } else { 1 })
+ .map(|index| PropertyCacheEntrySnapshot {
+ accessor_function: Some(AccessorFunctionSnapshot {
+ cached_value_offset: None,
+ function: CellId(CALLEE_FUNCTION + index * 0x100),
+ intrinsic: None,
+ inline_executable: Some(1),
+ }),
+ holds_accessor: true,
+ ..own(PropertyCacheEntryType::GetOwnProperty, 0x100 + index * 0x100, 0)
+ })
+ .collect();
+ snapshot.executables[0].property_caches = vec![cache(entries)];
+ let graph = build_graph(&snapshot).unwrap();
+ let frames = graph
+ .frame_states
+ .iter()
+ .filter(|frame| frame.executable == 1)
+ .collect::<Vec<_>>();
+ assert!(!frames.is_empty());
+ let expected = eligible || (closures && !strict);
+ assert!(
+ frames
+ .iter()
+ .all(|frame| !frame.legacy_arguments.is_empty() == expected)
+ );
+ let argument = test_layout().arguments_base();
+ assert_eq!(
+ frames
+ .iter()
+ .any(|frame| frame.values.iter().any(|(slot, _)| *slot == argument)),
+ expected
+ );
+ assert_eq!(
+ graph
+ .nodes
+ .iter()
+ .any(|node| matches!(node.op, Op::CheckClosure { .. })),
+ closures
+ );
+ }
+ }
+
#[test]
fn native_callees_forward_their_arguments() {
// function identity(v) { return v; }
diff --git a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
index d48462147f9..6d4528f3e11 100644
--- a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
@@ -1869,6 +1869,10 @@ pub struct FrameState {
/// arguments than its `Call` instruction's (a call forwarded through
/// `Function.prototype.apply` or `call`, or a bound function): how many.
pub passed_argument_count: Option<u32>,
+ /// The argument slots whose values code that calls in the frame run can
+ /// read through the legacy `arguments` of the frame's function. Only
+ /// frames of inlined calls of functions that are not strict have them.
+ pub legacy_arguments: core::ops::Range<u32>,
}
#[derive(Debug, Clone, Default)]
@@ -1988,6 +1992,23 @@ impl Graph {
)
}
+ pub fn legacy_argument_values_read_by(&self, node: NodeId) -> Vec<NodeId> {
+ let data = self.node(node);
+ let Some(frame_state) = data.frame_state.filter(|_| data.op.properties().runs_code) else {
+ return Vec::new();
+ };
+ let mut values = Vec::new();
+ for id in self.frame_state_chain(frame_state) {
+ let frame_state = self.frame_state(id);
+ for (slot, value) in &frame_state.values {
+ if frame_state.legacy_arguments.contains(slot) {
+ values.push(*value);
+ }
+ }
+ }
+ values
+ }
+
/// The values of every frame of a frame state chain, innermost frame first.
///
/// They are followed by the properties of the virtual objects among them
@@ -2167,6 +2188,7 @@ mod tests {
in_frame: Vec::new(),
parent,
passed_argument_count: None,
+ legacy_arguments: 0..0,
})
}
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs b/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs
index 7891c020f24..d6431e3a0c3 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs
@@ -324,6 +324,9 @@ fn visit_node(
facts.expose(*input);
}
}
+ for value in graph.legacy_argument_values_read_by(node_id) {
+ facts.expose(decisions.value(graph, value));
+ }
// NB: A check known to pass is replaced by what refined its input
// before (see `Facts::refinement()`), and stays where nothing did.
let refines = node.op.refined_input().is_some() && node.repr.is_some();
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/dce.rs b/Libraries/LibJS/JIT/Rust/src/passes/dce.rs
index 419ff8707aa..8f731875c21 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/dce.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/dce.rs
@@ -136,6 +136,7 @@ mod tests {
in_frame: vec![],
parent: None,
passed_argument_count: None,
+ legacy_arguments: 0..0,
});
graph.blocks[0].body.push(boxed);
for _ in 0..2 {
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/tests.rs b/Libraries/LibJS/JIT/Rust/src/passes/tests.rs
index 1105b0fede2..50ea92d5391 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/tests.rs
@@ -177,6 +177,7 @@ fn verifier_rejects_slow_path_outputs_away_from_their_call() {
in_frame: Vec::new(),
parent: None,
passed_argument_count: None,
+ legacy_arguments: 0..0,
});
graph.nodes[call.index()].frame_state = Some(frame_state);
let output = node(
@@ -214,6 +215,7 @@ fn simplification_removes_number_checks_of_numeric_constants() {
in_frame: Vec::new(),
parent: None,
passed_argument_count: None,
+ legacy_arguments: 0..0,
}));
let boxed = node(&mut graph, Op::BoxFloat64, vec![check], Some(Repr::Tagged));
let ret = node(&mut graph, Op::Return, vec![boxed], None);
@@ -347,6 +349,7 @@ fn typed_array_storage_offsets_are_reused_across_stores_but_not_calls() {
in_frame: Vec::new(),
parent: None,
passed_argument_count: None,
+ legacy_arguments: 0..0,
}));
graph.blocks[0].body.extend([
object,
@@ -507,6 +510,7 @@ fn check_elimination_keeps_checks_that_a_later_entry_reaches_blocks_before_it()
in_frame: Vec::new(),
parent: None,
passed_argument_count: None,
+ legacy_arguments: 0..0,
});
let first = node(&mut graph, Op::CheckObject, vec![constant], Some(Repr::Tagged));
graph.nodes[first.index()].frame_state = Some(frame_state);
@@ -659,6 +663,7 @@ fn constant_bounds_checks_only_fold_when_the_unsigned_index_is_in_bounds() {
in_frame: Vec::new(),
parent: None,
passed_argument_count: None,
+ legacy_arguments: 0..0,
}));
let boxed = node(&mut graph, Op::BoxInt32, vec![check], Some(Repr::Tagged));
let ret = node(&mut graph, Op::Return, vec![boxed], None);
@@ -748,6 +753,7 @@ fn hoisted_checks_gate_exits_by_the_loop_headers_executable() {
in_frame: vec![],
parent: None,
passed_argument_count: None,
+ legacy_arguments: 0..0,
});
let state = graph.add_frame_state(FrameState {
executable: 1,
@@ -757,6 +763,7 @@ fn hoisted_checks_gate_exits_by_the_loop_headers_executable() {
in_frame: vec![],
parent: Some(parent),
passed_argument_count: None,
+ legacy_arguments: 0..0,
});
let check = node(&mut graph, Op::CheckObject, vec![object], Some(Repr::Tagged));
graph.nodes[check.index()].frame_state = Some(state);
diff --git a/Libraries/LibJS/JIT/Rust/src/regalloc/liveness.rs b/Libraries/LibJS/JIT/Rust/src/regalloc/liveness.rs
index c4b55a3b45f..86d7d299aef 100644
--- a/Libraries/LibJS/JIT/Rust/src/regalloc/liveness.rs
+++ b/Libraries/LibJS/JIT/Rust/src/regalloc/liveness.rs
@@ -313,6 +313,7 @@ mod tests {
in_frame: vec![],
parent: None,
passed_argument_count: None,
+ legacy_arguments: 0..0,
});
let mut checks = Vec::new();
for _ in 0..2 {
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index 3fecac14d9f..79337135729 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -89,6 +89,9 @@ pub struct ExecutableSnapshot {
/// Whether the function's mapped arguments object (made by `CreateArguments`
/// of the mapped kind) aliases parameters, so that it changes when they do.
pub mapped_arguments_alias_parameters: bool,
+ /// Whether the snapshotted function has a legacy `arguments` property.
+ /// Other closures of its executable need not have the same eligibility.
+ pub supports_legacy_arguments: bool,
/// The shape `NewObject` gives an object without a cached shape: an
/// empty shape with `%Object.prototype%` of the executable's realm.
pub new_object_shape: Option<ShapeSnapshot>,
@@ -358,6 +361,11 @@ impl ExecutableSnapshot {
+ u64::from(passed_argument_count.max(formal_parameter_count));
u64::from(execution_context_size) + 8 * slots
}
+
+ /// Whether this is a function that is not strict (and no builtin).
+ pub fn is_non_strict_function(&self) -> bool {
+ !self.builtin && self.function.is_some_and(|function| !function.strict)
+ }
}
/// A copy of the interpreter feedback of one executable, indexed by the
diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rs
index 5ca21e80f9c..6b9b235b1eb 100644
--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs
+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs
@@ -943,6 +943,8 @@ impl SnapshotBuilder<'_> {
builtin: matches!(entry.function, Some(InlinedFunction::Builtin(_))),
mapped_arguments_alias_parameters: arguments_function
.is_some_and(|function| !function.mapped_argument_names().is_empty()),
+ supports_legacy_arguments: arguments_function
+ .is_some_and(|function| function.supports_legacy_caller_or_arguments()),
new_object_shape: Some(self.shape(intrinsics.new_object_shape())),
string_prototype: Some(self.cell(realm.string_prototype(vm))),
number_prototype: Some(self.cell(realm.number_prototype(vm))),
diff --git a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
index 2529d707c15..35cd6e6f180 100644
--- a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
@@ -942,7 +942,7 @@ impl EcmascriptFunctionObject {
self.shared_data().function_environment_needed()
}
- fn supports_legacy_caller_or_arguments(&self) -> bool {
+ pub fn supports_legacy_caller_or_arguments(&self) -> bool {
// https://tc39.es/ecma262/#sec-forbidden-extensions
//
// ECMAScript function objects defined using syntactic constructors in strict mode code must not be created with own
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 f2f53b5ff02..037e8fecd4c 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
@@ -62,3 +62,27 @@ test("a function called by an inlined function reads its legacy arguments withou
);
expect(failures).toEqual([]);
});
+
+test("a function called by an inlined function adds a property to an object in its legacy arguments", () => {
+ const failures = compareCompiledWithInterpreter(
+ () => {
+ function addProperty(add) {
+ // A for-in loop keeps this function from being inlined.
+ for (const key in {}) {
+ }
+ if (add) middle.arguments[0].z = 3;
+ }
+ function middle(object, add) {
+ addProperty(add);
+ object.y = 2;
+ }
+ return add => {
+ const object = { x: 1 };
+ middle(object, add);
+ return object;
+ };
+ },
+ { warmUp: () => [false], probes: { add: () => [true] } }
+ );
+ expect(failures).toEqual([]);
+});