Skip to content

Commit b45f244

Browse files
committed
[JSC] DFG should not fold ResolvePromiseFirstResolving into a fulfill when the value can be the promise
https://bugs.webkit.org/show_bug.cgi?id=328330 rdar://190624765 Reviewed by Marcus Plutowski and Keith Miller. Even if the object is not having `then` method, if it is Promise, then resolving a promise with itself will throw an error. let resolve; let p = new Promise((r) => { resolve = r; }); Object.setPrototypeOf(p, null); resolve(p); // p is fulfilled in DFG and FTL, should be rejected with a TypeError Thus, DFG ResolvePromiseFirstResolving should not accept a promise object. Test: JSTests/stress/promise-resolved-with-itself-rejects-in-jit.js * JSTests/stress/promise-resolved-with-itself-rejects-in-jit.js: Added. (resolveWithItself): * Source/JavaScriptCore/dfg/DFGConstantFoldingPhase.cpp: (JSC::DFG::ConstantFoldingPhase::foldConstants): Canonical link: https://commits.webkit.org/324393@main
1 parent 84363fa commit b45f244

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function resolveWithItself(i) {
2+
let resolve;
3+
let promise = new Promise((r) => { resolve = r; });
4+
Object.setPrototypeOf(promise, null);
5+
promise.foo = i;
6+
resolve(promise);
7+
return promise;
8+
}
9+
noInline(resolveWithItself);
10+
11+
let fulfilled = 0;
12+
let rejectedWithTypeError = 0;
13+
for (let i = 0; i < testLoopCount; ++i) {
14+
Promise.prototype.then.call(resolveWithItself(i), () => { fulfilled++; }, (error) => {
15+
if (error instanceof TypeError)
16+
rejectedWithTypeError++;
17+
});
18+
}
19+
drainMicrotasks();
20+
21+
if (fulfilled || rejectedWithTypeError !== testLoopCount)
22+
throw new Error("fulfilled " + fulfilled + " times, rejected with TypeError " + rejectedWithTypeError + " times");

‎Source/JavaScriptCore/dfg/DFGConstantFoldingPhase.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,9 +2025,10 @@ class ConstantFoldingPhase : public Phase {
20252025
}
20262026

20272027
// SpecObject | something.
2028-
// Only for types having structures, we check "then" existence.
2028+
// Only for types having structures, we check "then" existence. A promise resolved with
2029+
// itself must reject, so the argument must not be a promise.
20292030
auto& structureSet = argument.m_structure;
2030-
if (structureSet.isFinite() && structureSet.size() == 1) {
2031+
if (argument.isType(~SpecPromiseObject) && structureSet.isFinite() && structureSet.size() == 1) {
20312032
JSGlobalObject* globalObject = m_graph.globalObjectFor(node->origin.semantic);
20322033
auto conditionSet = m_graph.tryEnsureAbsence(globalObject, structureSet.toStructureSet(), CacheableIdentifier::createFromImmortalIdentifier(m_graph.m_vm.propertyNames->then.impl()));
20332034
if (conditionSet.isValid()) {

0 commit comments

Comments
 (0)