Skip to content

Truly independent web browser

LibJS: Leave the private environment of a class on an early exit

Tim Ledbetter52c102d5fe81
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
index 59311d67494..8092f842c48 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
@@ -42,6 +42,7 @@ use super::operators::emit_to_primitive_property_key;
use super::registers::ScopedOperand;
use super::scope::ParameterShape;
use super::scope::ScopeBindings;
+use super::stream::ClassPrivateEnvironment;
use super::stream::Control;
use super::stream::Handler;
use super::stream::LoopKind;
@@ -286,7 +287,7 @@ impl Generator<'_> {
};
if let Some(name) = private_name_of(key) {
if !has_private_environment {
- self.put(op::CreatePrivateEnvironment {});
+ self.enter_class_private_environment();
has_private_environment = true;
}
let identifier = intern_private_name(self, name);
@@ -333,6 +334,12 @@ impl Generator<'_> {
elements: class_elements,
});
+ let private_environment = has_private_environment.then(|| {
+ let Some(Control::PrivateEnvironment(private_environment)) = self.pop_control() else {
+ unreachable!("the private environment of a class is what is going on");
+ };
+ private_environment
+ });
self.end_class_scope(environment);
self.put(op::NewClass {
dst,
@@ -345,8 +352,8 @@ impl Generator<'_> {
element_keys_count: u32_from_usize(element_keys.len()),
element_keys: &element_keys,
});
- if has_private_environment {
- self.put(op::LeavePrivateEnvironment {});
+ if let Some(private_environment) = private_environment {
+ self.leave_class_private_environment(private_environment);
}
self.strict = saved_strict;
self.release_held_registers(held);
@@ -354,6 +361,43 @@ impl Generator<'_> {
Ok(())
}
+ /// The private names of a class come next, whose environment is current until the class is done.
+ fn enter_class_private_environment(&mut self) {
+ self.put(op::CreatePrivateEnvironment {});
+ let outer_handler = self.handler();
+ // NB: An exception that leaves the function takes the private environment with its execution context.
+ let handler = outer_handler.map(|_| Handler {
+ label: self.new_label(),
+ catches: false,
+ });
+ if handler.is_some() {
+ self.set_handler(handler);
+ }
+ let private_environment = ClassPrivateEnvironment { handler, outer_handler };
+ self.push_control(Control::PrivateEnvironment(private_environment));
+ }
+
+ /// The class is done, and the private environment around it is current again, also for exceptions.
+ fn leave_class_private_environment(&mut self, private_environment: ClassPrivateEnvironment) {
+ self.put(op::LeavePrivateEnvironment {});
+ let Some(handler) = private_environment.handler else {
+ return;
+ };
+ self.set_handler(private_environment.outer_handler);
+ let end_label = self.new_label();
+ self.jump(end_label);
+ self.bind_reachable_label(handler.label);
+ let error = self.temporary();
+ let value_feedback = self.next_value_feedback();
+ self.put(op::Catch {
+ value_feedback,
+ dst: error,
+ });
+ self.put(op::LeavePrivateEnvironment {});
+ self.put(op::Throw { src: error });
+ self.bind_label(end_label);
+ }
+
/// The key of an element of a class, which `NewClass` turns into a property key.
fn sweep_class_element_key(&mut self, key: ClassElementKey, first_row: usize) -> Result<Operand, OutOfStack> {
let tree = self.tree;
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
index 14462b2d8c5..de98f178b7f 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
@@ -229,6 +229,15 @@ pub(super) struct OpenIterator {
pub(super) is_async: bool,
}
+/// The private environment of a class, while its elements are being evaluated.
+#[derive(Clone, Copy)]
+pub(super) struct ClassPrivateEnvironment {
+ /// Where exceptions go until the class is done, if the function has a handler for them.
+ pub(super) handler: Option<Handler>,
+ /// Where exceptions went before the private environment.
+ pub(super) outer_handler: Option<Handler>,
+}
+
/// The environment of a scope, while its code is running.
#[derive(Clone, Copy)]
pub(super) struct ScopeEnvironment {
@@ -321,6 +330,8 @@ pub(super) enum Control {
/// Everything that leaves disposes of the resources at this index of `disposals`.
Disposal(u32),
Iterator(OpenIterator),
+ /// Everything that leaves goes back to the private environment around a class.
+ PrivateEnvironment(ClassPrivateEnvironment),
}
#[derive(Clone, Copy)]
@@ -733,6 +744,12 @@ impl Generator<'_> {
self.set_handler(iterator.outer_handler);
self.put_iterator_close(iterator);
}
+ Control::PrivateEnvironment(private_environment) => {
+ if self.handler() != private_environment.outer_handler {
+ self.set_handler(private_environment.outer_handler);
+ }
+ self.put(op::LeavePrivateEnvironment {});
+ }
Control::Disposal(disposal) => {
let finally = self.context.stream.disposals[disposal as usize].finally;
self.exit_through_finally(finally as usize, exit, value);
diff --git a/Tests/LibJS/Runtime/classes/class-private-fields.js b/Tests/LibJS/Runtime/classes/class-private-fields.js
index 5e3f870c369..298134adaf4 100644
--- a/Tests/LibJS/Runtime/classes/class-private-fields.js
+++ b/Tests/LibJS/Runtime/classes/class-private-fields.js
@@ -275,6 +275,102 @@ test("unknown private name gives SyntaxError", () => {
expect(`1?.n.#n`).not.toEval();
});
+describe("private names refer to the outer class after an inner class definition ends abruptly", () => {
+ const thrower = () => {
+ throw new Error();
+ };
+
+ test("exception from a computed key", () => {
+ class Outer {
+ #x = 1;
+ static read(object) {
+ try {
+ class Inner {
+ #x = 2;
+ [thrower()] = 1;
+ }
+ } catch {}
+ return object.#x;
+ }
+ }
+ expect(Outer.read(new Outer())).toBe(1);
+ });
+
+ test("exception from a static initialization block", () => {
+ class Outer {
+ #x = 1;
+ static read(object) {
+ try {
+ class Inner {
+ #x = 2;
+ static {
+ thrower();
+ }
+ }
+ } catch {}
+ return object.#x;
+ }
+ }
+ expect(Outer.read(new Outer())).toBe(1);
+ });
+
+ test("exception from the heritage", () => {
+ class Outer {
+ #x = 1;
+ static read(object) {
+ try {
+ class Inner extends thrower() {
+ #x = 2;
+ }
+ } catch {}
+ return object.#x;
+ }
+ }
+ expect(Outer.read(new Outer())).toBe(1);
+ });
+
+ test("exception from a computed key of a class in a computed key", () => {
+ class Outer {
+ #x = 1;
+ static read(object) {
+ try {
+ class Middle {
+ #x = 2;
+ [(class {
+ #x = 3;
+ [thrower()] = 1;
+ },
+ "key")] = 1;
+ }
+ } catch {}
+ return object.#x;
+ }
+ }
+ expect(Outer.read(new Outer())).toBe(1);
+ });
+
+ test("generator return from a computed key", () => {
+ const seen = [];
+ class Outer {
+ #x = 1;
+ static *read(object) {
+ try {
+ class Inner {
+ #x = 2;
+ [yield] = 1;
+ }
+ } finally {
+ seen.push(object.#x);
+ }
+ }
+ }
+ const generator = Outer.read(new Outer());
+ generator.next();
+ expect(generator.return("r")).toEqual({ value: "r", done: true });
+ expect(seen).toEqual([1]);
+ });
+});
+
// OSS-FUZZ Issue 53363: top level unknown private names seg faults
expect(() => eval(`#n`)).toThrowWithMessage(SyntaxError, "Private identifier must be followed by 'in'");
expect(() => eval(`obj.#n`)).toThrowWithMessage(SyntaxError, "Reference to undeclared private field or method '#n'");