Skip to content

Commit 5d5248a

Browse files
committed
[JSC] Install the private class brand before evaluating class elements
https://bugs.webkit.org/show_bug.cgi?id=327984 Reviewed by Yusuke Suzuki. The brand of static private methods and accessors was stored in the class scope after the class elements were evaluated, so a closure created in a computed property name could read the empty value and pass it to op_stricteq. DFG and FTL speculated that the brand is an object and crashed by reading the type of a null cell. let f; function probe(g) { f = g; g({}); } for (let i = 0; i < testLoopCount; i++) { class C { static #m() { } static [probe(o => #m in o)]() { } } f(C); } This patch stores it right after the constructor is created. Accessing a static private method at that point now throws a TypeError instead of a ReferenceError. op_check_tdz in emitCheckPrivateBrand is no longer needed. No user code runs between the creation of the class scope and this store, so the brand is never empty when it is read. Test: JSTests/stress/static-private-brand-check-in-computed-property-name.js * JSTests/stress/static-private-brand-check-in-computed-property-name.js: Added. (shouldBe): (shouldThrowTypeError): (checkWithoutBrand): (checkWithBrand): (abortIf): (define.return.method): (define.return.get getter): (define.return.set setter): (define.return): (define): * Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp: (JSC::BytecodeGenerator::emitCheckPrivateBrand): * Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp: (JSC::ClassExprNode::emitBytecode): Canonical link: https://commits.webkit.org/324222@main
1 parent f2af25b commit 5d5248a

3 files changed

Lines changed: 77 additions & 4 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error(`expected ${expected} but got ${actual}`);
4+
}
5+
6+
function shouldThrowTypeError(func, argument) {
7+
let error;
8+
try {
9+
func(argument);
10+
} catch (e) {
11+
error = e;
12+
}
13+
if (!(error instanceof TypeError))
14+
throw new Error(`expected TypeError but got ${error}`);
15+
}
16+
17+
let hasMethod, hasGetter, hasSetter, getMethod, callGetter, callSetter;
18+
19+
function checkWithoutBrand(object) {
20+
shouldBe(hasMethod(object), false);
21+
shouldBe(hasGetter(object), false);
22+
shouldBe(hasSetter(object), false);
23+
shouldThrowTypeError(getMethod, object);
24+
shouldThrowTypeError(callGetter, object);
25+
shouldThrowTypeError(callSetter, object);
26+
}
27+
28+
function checkWithBrand(object) {
29+
shouldBe(hasMethod(object), true);
30+
shouldBe(hasGetter(object), true);
31+
shouldBe(hasSetter(object), true);
32+
shouldBe(getMethod(object)(), 1);
33+
shouldBe(callGetter(object), 2);
34+
shouldBe(callSetter(object), 3);
35+
}
36+
37+
function abortIf(condition) {
38+
if (condition)
39+
throw new Error("abort");
40+
return "second";
41+
}
42+
43+
function define(shouldAbort) {
44+
return class {
45+
static #method() { return 1; }
46+
static get #getter() { return 2; }
47+
static set #setter(value) { }
48+
static [(
49+
hasMethod = object => #method in object,
50+
hasGetter = object => #getter in object,
51+
hasSetter = object => #setter in object,
52+
getMethod = object => object.#method,
53+
callGetter = object => object.#getter,
54+
callSetter = object => object.#setter = 3,
55+
checkWithoutBrand({ }),
56+
"first"
57+
)]() { }
58+
static [abortIf(shouldAbort)]() { }
59+
};
60+
}
61+
62+
for (let i = 0; i < testLoopCount; i++) {
63+
let shouldAbort = !!(i & 1);
64+
let constructor = null;
65+
try {
66+
constructor = define(shouldAbort);
67+
} catch { }
68+
69+
if (shouldAbort) {
70+
shouldBe(constructor, null);
71+
checkWithoutBrand({ });
72+
} else
73+
checkWithBrand(constructor);
74+
}

‎Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3160,7 +3160,6 @@ void BytecodeGenerator::emitCheckPrivateBrand(RegisterID* base, RegisterID* bran
31603160
{
31613161
if (isStatic) {
31623162
Ref<Label> brandCheckOkLabel = newLabel();
3163-
OpCheckTdz::emit(this, brand);
31643163
emitJumpIfTrue(emitEqualityOp<OpStricteq>(newTemporary(), base, brand), brandCheckOkLabel.get());
31653164
emitThrowTypeError("Cannot access static private method or accessor"_s);
31663165
emitLabel(brandCheckOkLabel.get());

‎Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5813,6 +5813,9 @@ RegisterID* ClassExprNode::emitBytecode(BytecodeGenerator& generator, RegisterID
58135813
} else
58145814
constructor = generator.emitNewDefaultConstructor(constructor.get(), m_classHeritage ? ConstructorKind::Extends : ConstructorKind::Base, m_name, ecmaName(), m_classSource, needsClassFieldInitializer, privateBrandRequirement);
58155815

5816+
if (shouldInstallBrandOnConstructor)
5817+
generator.emitInstallPrivateClassBrand(constructor.get());
5818+
58165819
const auto& propertyNames = generator.propertyNames();
58175820
RefPtr<RegisterID> prototype = generator.emitNewObject(generator.newTemporary());
58185821

@@ -5869,9 +5872,6 @@ RegisterID* ClassExprNode::emitBytecode(BytecodeGenerator& generator, RegisterID
58695872
generator.emitPutToScope(scope.get(), classNameVar, constructor.get(), ThrowIfNotFound, InitializationMode::Initialization);
58705873
}
58715874

5872-
if (shouldInstallBrandOnConstructor)
5873-
generator.emitInstallPrivateClassBrand(constructor.get());
5874-
58755875
if (!staticElementDefinitions.isEmpty()) {
58765876
RefPtr<RegisterID> staticFieldInitializer = generator.emitNewClassFieldInitializerFunction(generator.newTemporary(), WTF::move(staticElementDefinitions), m_classHeritage);
58775877
// FIXME: Skip this if the initializer function isn't going to need a home object (no eval or super properties)

0 commit comments

Comments
 (0)