Skip to content

Truly independent web browser

LibJS: Store block functions of scripts and evals only where allowed

Shannon Boothd5ccb67ba735
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
index edc078f6ade..3d33b85dcdb 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
@@ -75,6 +75,7 @@ asm_dispatch_table:
.quad asm_handler_SetLexicalBinding
.quad asm_handler_SetVariableBinding
.quad asm_handler_DynamicSetLexicalBinding
+ .quad asm_handler_SetVariableBindingOfBlockFunction
.quad asm_handler_DynamicSetVariableBinding
.quad asm_handler_ResolveBinding
.quad asm_handler_ResolveSuperBase
@@ -268,7 +269,6 @@ asm_dispatch_table:
.quad asm_handler_fallback
.quad asm_handler_fallback
.quad asm_handler_fallback
- .quad asm_handler_fallback
.globl CSYM(js_interpreter_debug_dispatch_table)
CSYM(js_interpreter_debug_dispatch_table):
@@ -781,6 +781,10 @@ CSYM(js_interpreter_handler_ranges):
.quad asm_handler_DynamicSetLexicalBinding_hot_end
.quad asm_handler_DynamicSetLexicalBinding_cold
.quad asm_handler_DynamicSetLexicalBinding_cold_end
+ .quad asm_handler_SetVariableBindingOfBlockFunction
+ .quad asm_handler_SetVariableBindingOfBlockFunction_hot_end
+ .quad asm_handler_SetVariableBindingOfBlockFunction_cold
+ .quad asm_handler_SetVariableBindingOfBlockFunction_cold_end
.quad asm_handler_DynamicSetVariableBinding
.quad asm_handler_DynamicSetVariableBinding_hot_end
.quad asm_handler_DynamicSetVariableBinding_cold
@@ -1553,10 +1557,6 @@ CSYM(js_interpreter_handler_ranges):
.quad asm_handler_fallback_hot_end
.quad asm_handler_fallback_cold
.quad asm_handler_fallback_cold_end
- .quad asm_handler_fallback
- .quad asm_handler_fallback_hot_end
- .quad asm_handler_fallback_cold
- .quad asm_handler_fallback_cold_end
.text
@@ -10903,6 +10903,47 @@ asm_handler_DynamicSetLexicalBinding_cold:
ldr x10, [x19, x9, lsl #3]
br x10
asm_handler_DynamicSetLexicalBinding_cold_end:
+.p2align 4
+asm_handler_SetVariableBindingOfBlockFunction:
+ mov x0, x20
+ sub w1, w21, w26
+ str w1, [x28, #64]
+ ldr w9, [x21, #8]
+ ldr x10, [x27, x9, lsl #3]
+.Lasm_SetVariableBindingOfBlockFunction.scalar_value_ready_0:
+ mov x3, x10
+ mov x0, x20
+ sub w1, w21, w26
+ mov x2, x21
+ bl CSYM(asm_slow_path_set_variable_binding_of_block_function)
+ tbnz x0, #63, .Lasm_SetVariableBindingOfBlockFunction.scalar_result_done_1
+ tbz x0, #32, .Lasm_SetVariableBindingOfBlockFunction.scalar_result_done_1
+ add x21, x26, w0, uxtw
+ ldrb w9, [x21]
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+.Lasm_SetVariableBindingOfBlockFunction.scalar_result_done_1:
+ tbz x0, #63, 1f
+ b .Lexit
+1:
+ tbz x0, #32, .Lasm_SetVariableBindingOfBlockFunction.slow_path_transfer_2
+ add x21, x26, w0, uxtw
+ ldrb w9, [x21]
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+.Lasm_SetVariableBindingOfBlockFunction.slow_path_transfer_2:
+ ldr x28, [x20]
+ ldr x9, [x28, #88]
+ ldr x26, [x9, #40]
+ add x27, x28, #128
+ add x21, x26, w0, uxtw
+ ldrb w9, [x21]
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+asm_handler_SetVariableBindingOfBlockFunction_hot_end:
+asm_handler_SetVariableBindingOfBlockFunction_cold:
+asm_handler_SetVariableBindingOfBlockFunction_cold_end:
+
asm_handler_DynamicSetVariableBinding_cold:
// Cold paths for DynamicSetVariableBinding
.Lasm_DynamicSetVariableBinding.ssa_block_1:
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
index 4566efb2c18..f22c0e4d424 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
@@ -76,6 +76,7 @@ asm_dispatch_table:
.quad asm_handler_SetLexicalBinding
.quad asm_handler_SetVariableBinding
.quad asm_handler_DynamicSetLexicalBinding
+ .quad asm_handler_SetVariableBindingOfBlockFunction
.quad asm_handler_DynamicSetVariableBinding
.quad asm_handler_ResolveBinding
.quad asm_handler_ResolveSuperBase
@@ -269,7 +270,6 @@ asm_dispatch_table:
.quad asm_handler_fallback
.quad asm_handler_fallback
.quad asm_handler_fallback
- .quad asm_handler_fallback
.globl CSYM(js_interpreter_debug_dispatch_table)
CSYM(js_interpreter_debug_dispatch_table):
@@ -782,6 +782,10 @@ CSYM(js_interpreter_handler_ranges):
.quad asm_handler_DynamicSetLexicalBinding_hot_end
.quad asm_handler_DynamicSetLexicalBinding_cold
.quad asm_handler_DynamicSetLexicalBinding_cold_end
+ .quad asm_handler_SetVariableBindingOfBlockFunction
+ .quad asm_handler_SetVariableBindingOfBlockFunction_hot_end
+ .quad asm_handler_SetVariableBindingOfBlockFunction_cold
+ .quad asm_handler_SetVariableBindingOfBlockFunction_cold_end
.quad asm_handler_DynamicSetVariableBinding
.quad asm_handler_DynamicSetVariableBinding_hot_end
.quad asm_handler_DynamicSetVariableBinding_cold
@@ -1554,10 +1558,6 @@ CSYM(js_interpreter_handler_ranges):
.quad asm_handler_fallback_hot_end
.quad asm_handler_fallback_cold
.quad asm_handler_fallback_cold_end
- .quad asm_handler_fallback
- .quad asm_handler_fallback_hot_end
- .quad asm_handler_fallback_cold
- .quad asm_handler_fallback_cold_end
.text
@@ -10759,6 +10759,46 @@ asm_handler_DynamicSetLexicalBinding_cold:
movzx eax, BYTE PTR [r14 + r13]
jmp [r12 + rax * 8]
asm_handler_DynamicSetLexicalBinding_cold_end:
+.p2align 4
+asm_handler_SetVariableBindingOfBlockFunction:
+ mov DWORD PTR [rbx + -64], r13d
+ lea r11, [r14 + r13]
+ mov r10d, DWORD PTR [r11 + 8]
+ mov rax, QWORD PTR [rbx + r10 * 8]
+.Lasm_SetVariableBindingOfBlockFunction.scalar_value_ready_0:
+ mov rcx, rax
+ mov rdi, QWORD PTR [rbp - 48]
+ mov esi, r13d
+ lea rdx, [r14 + r13]
+ call CSYM(asm_slow_path_set_variable_binding_of_block_function)
+ test rax, rax
+ js .Lasm_SetVariableBindingOfBlockFunction.scalar_result_done_1
+ bt rax, 32
+ jnc .Lasm_SetVariableBindingOfBlockFunction.scalar_result_done_1
+ mov r13d, eax
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+.Lasm_SetVariableBindingOfBlockFunction.scalar_result_done_1:
+ test rax, rax
+ js .Lexit
+ bt rax, 32
+ jnc .Lasm_SetVariableBindingOfBlockFunction.slow_path_transfer_2
+ mov r13d, eax
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+.Lasm_SetVariableBindingOfBlockFunction.slow_path_transfer_2:
+ mov rcx, QWORD PTR [rbp - 48]
+ mov rbx, QWORD PTR [rcx]
+ add rbx, 128
+ mov rcx, QWORD PTR [rbx + -40]
+ mov r14, QWORD PTR [rcx + 40]
+ mov r13d, eax
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+asm_handler_SetVariableBindingOfBlockFunction_hot_end:
+asm_handler_SetVariableBindingOfBlockFunction_cold:
+asm_handler_SetVariableBindingOfBlockFunction_cold_end:
+
asm_handler_DynamicSetVariableBinding_cold:
# Cold paths for DynamicSetVariableBinding
.Lasm_DynamicSetVariableBinding.ssa_block_1:
diff --git a/Libraries/LibJS/Interpreter/interpreter.flap b/Libraries/LibJS/Interpreter/interpreter.flap
index d4a341eb640..e45c95801c7 100644
--- a/Libraries/LibJS/Interpreter/interpreter.flap
+++ b/Libraries/LibJS/Interpreter/interpreter.flap
@@ -2278,6 +2278,15 @@ handler DynamicSetLexicalBinding(
dispatch_next;
}
+# Evaluates a block function in the code of a script or an eval, which stores it into the var of its name if the checks
+# of declaration instantiation still pass. program_environment_depth is how many environments out from the running one
+# the environment of the program's own code is.
+handler SetVariableBindingOfBlockFunction(
+ identifier: IdentifierTableIndex,
+ src: in Operand,
+ program_environment_depth: u32
+) @cold = call_slow_path(asm_slow_path_set_variable_binding_of_block_function);
+
handler DynamicSetVariableBinding(
identifier: IdentifierTableIndex,
src: in Operand,
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
index 76082c6451f..505fb2a7b32 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
@@ -323,6 +323,7 @@ pub fn handling(opcode: OpCode) -> Handling {
| O::GetObjectPropertyIterator
| O::IteratorNext
| O::IteratorStep
+ | O::SetVariableBindingOfBlockFunction
| O::IteratorToArray
| O::CacheObjectShape
| O::InitObjectLiteralProperty
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
index 61450bd79f5..7ed43abad30 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
@@ -1748,7 +1748,13 @@ impl Generator<'_> {
self.value_of_identifier(inner_ident);
let (value, _) = self.pop_operand();
let identifier = self.intern_identifier(name);
- emit_set_variable_binding(self, identifier, value);
+ // NB: Whether the function gets stored is decided again when it is evaluated.
+ let program_environment_depth = self.context.scope.scope_environment_depth;
+ self.put(op::SetVariableBindingOfBlockFunction {
+ identifier,
+ src: value,
+ program_environment_depth,
+ });
self.end_of_statement();
}
return;
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs
index a980c460d38..9109921067f 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs
@@ -1200,6 +1200,63 @@ pub fn dynamic_set_variable_binding(
advance_or_continue(pc, next_pc, op::DynamicSetVariableBinding::LENGTH)
}
+/// What evaluating a block function in non-strict program code does in place of FunctionDeclaration Evaluation:
+/// GlobalDeclarationInstantiation step 12.b.ii.2.a.ii.3 for a script, EvalDeclarationInstantiation step 13.b.ii.7.b for
+/// an eval. Declaration instantiation decides whether it applies, which gets decided again here.
+pub fn set_variable_binding_of_block_function(
+ vm: &Vm,
+ pc: u32,
+ instruction: &op::SetVariableBindingOfBlockFunction,
+ values: &mut op::SetVariableBindingOfBlockFunctionValues,
+) -> SlowPathControl {
+ let next = pc + op::SetVariableBindingOfBlockFunction::LENGTH;
+ let name = get_identifier(vm, instruction.identifier);
+ let context = running_execution_context(vm);
+ let variable_environment = context
+ .variable_environment
+ .get()
+ .expect("program code has a VariableEnvironment");
+ let mut environment = context
+ .lexical_environment
+ .get()
+ .expect("program code has a LexicalEnvironment");
+ for _ in 0..instruction.program_environment_depth {
+ environment = environment
+ .outer_environment()
+ .expect("the environment of the program encloses the running one");
+ }
+
+ // NB: This is the walk of EvalDeclarationInstantiation step 13.b.ii.4. The lexical environment of a script is its
+ // variable environment, so only an eval gets through it.
+ while environment != variable_environment {
+ if !environment.is_object_environment()
+ && environment.has_binding(vm, &name, None).must()
+ && !environment.is_catch_environment()
+ {
+ return SlowPathControl::continue_at(next);
+ }
+ environment = environment
+ .outer_environment()
+ .expect("the variable environment encloses the lexical environment");
+ }
+ if let Some(global_environment) = variable_environment.downcast::<GlobalEnvironment>() {
+ // NB: This is GlobalDeclarationInstantiation step 12.b.ii.2.a and EvalDeclarationInstantiation step 13.b.ii.5.
+ let is_definable = !global_environment.has_lexical_declaration(&name)
+ && asm_try!(vm, pc, global_environment.can_declare_global_var(vm, &name));
+ if !is_definable {
+ return SlowPathControl::continue_at(next);
+ }
+ }
+
+ // Perform ? globalEnv.SetMutableBinding(funcName, funcObj, false).
+ asm_try!(
+ vm,
+ pc,
+ variable_environment.set_mutable_binding(vm, &name, values.src, false)
+ );
+ SlowPathControl::continue_at(next)
+}
+
pub fn resolve_binding(
vm: &Vm,
pc: u32,
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
index 41d792a8451..95154a5c352 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
@@ -899,6 +899,15 @@ impl RuntimeFunctions for Runtime {
bindings::dynamic_set_variable_binding(vm, pc, instruction, values)
}
+ fn set_variable_binding_of_block_function(
+ vm: &Vm,
+ pc: u32,
+ instruction: &op::SetVariableBindingOfBlockFunction,
+ values: &mut op::SetVariableBindingOfBlockFunctionValues,
+ ) -> SlowPathControl {
+ bindings::set_variable_binding_of_block_function(vm, pc, instruction, values)
+ }
+
fn dynamic_typeof_binding(
vm: &Vm,
pc: u32,
diff --git a/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs b/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs
index b4f2f62c439..2964762afdb 100644
--- a/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs
+++ b/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs
@@ -1641,6 +1641,7 @@ pub fn eval_declaration_instantiation(
// iii. Let funcObj be ! blockEnv.GetBindingValue(funcName, false).
// iv. Perform ? globalEnv.SetMutableBinding(funcName, funcObj, false).
// v. Return unused.
+ // NB: The bytecode of the function does this with SetVariableBindingOfBlockFunction.
}
}
diff --git a/Tests/LibJS/Bytecode/expected/block-function-declaration-order.txt b/Tests/LibJS/Bytecode/expected/block-function-declaration-order.txt
index 7f54c888d35..6414ce585f4 100644
--- a/Tests/LibJS/Bytecode/expected/block-function-declaration-order.txt
+++ b/Tests/LibJS/Bytecode/expected/block-function-declaration-order.txt
@@ -10,6 +10,6 @@ block0:
[ 8] Mov dst:reg5, src:Undefined
[ 18] NewFunction dst:f1~0, shared_function_data_index:0
[ 30] NewFunction dst:f2~1, shared_function_data_index:1
- [ 48] DynamicSetVariableBinding `f1`, src:f1~0
- [ 58] DynamicSetVariableBinding `f2`, src:f2~1
+ [ 48] SetVariableBindingOfBlockFunction `f1`, src:f1~0, program_environment_depth:0
+ [ 58] SetVariableBindingOfBlockFunction `f2`, src:f2~1, program_environment_depth:0
[ 68] End value:reg5
diff --git a/Tests/LibJS/Bytecode/expected/for-in-lexical-scope.txt b/Tests/LibJS/Bytecode/expected/for-in-lexical-scope.txt
index 6afd31ec867..6dd838e5bc3 100644
--- a/Tests/LibJS/Bytecode/expected/for-in-lexical-scope.txt
+++ b/Tests/LibJS/Bytecode/expected/for-in-lexical-scope.txt
@@ -33,7 +33,7 @@ block3:
[ 120] CreateVariable `x`, is_immutable:false, is_global:false, is_strict:false
[ 130] InitializeLexicalBinding `x`, src:reg10
[ 148] NewFunction dst:probeDecl~0, shared_function_data_index:0
- [ 160] DynamicSetVariableBinding `probeDecl`, src:probeDecl~0
+ [ 160] SetVariableBindingOfBlockFunction `probeDecl`, src:probeDecl~0, program_environment_depth:1
[ 170] SetLexicalEnvironment environment:reg4
[ 178] JumpLoop target:block2
diff --git a/Tests/LibJS/Runtime/functions/annex-b-function-after-blocking-let.js b/Tests/LibJS/Runtime/functions/annex-b-function-after-blocking-let.js
index 0bf1a909819..9b959753b8f 100644
--- a/Tests/LibJS/Runtime/functions/annex-b-function-after-blocking-let.js
+++ b/Tests/LibJS/Runtime/functions/annex-b-function-after-blocking-let.js
@@ -107,3 +107,28 @@ test("a function named arguments hoists in script and eval code that reads argum
}
expect(eval("{ function arguments() {} } typeof arguments")).toBe("function");
});
+
+let lexicalOfThisScript = 1;
+const constantOfThisScript = 2;
+
+test("a function in a block of eval code does not get past a binding of its name", () => {
+ (0, eval)("{ function lexicalOfThisScript() {} }");
+ expect(lexicalOfThisScript).toBe(1);
+ (0, eval)("{ function constantOfThisScript() {} }");
+ expect(constantOfThisScript).toBe(2);
+
+ function lexicalInFunction() {
+ let x = 4;
+ eval("{ function x() {} }");
+ return x;
+ }
+ expect(lexicalInFunction()).toBe(4);
+ expect(Object.hasOwn(globalThis, "x")).toBeFalse();
+
+ function deletedVar() {
+ var deleted = eval("var d = delete g; { function g() {} } d");
+ return [deleted, typeof g];
+ }
+ expect(deletedVar()).toEqual([true, "function"]);
+ expect(Object.hasOwn(globalThis, "g")).toBeFalse();
+});