Skip to content

fix(quickjs): yield in a for-of head no longer fails with "stack underflow" - #223

Open
avijit213 wants to merge 2 commits into
Snapchat:mainfrom
avijit213:fix/quickjs-yield-in-for-of-head
Open

avijit213 wants to merge 2 commits into
Snapchat:mainfrom
avijit213:fix/quickjs-yield-in-for-of-head

Conversation

@avijit213

Copy link
Copy Markdown
Contributor

Description

Valdi compiles TypeScript to ES2016 (_configs/base.tsconfig.json), so in an async function

for (const n of await items) { … }

becomes for (const n of yield items) inside a generator. Valdi's QuickJS (2021-03-27) marks the loop as holding an iterator before it parses the iterable expression, so a yield in that expression emits iterator cleanup for an iterator that doesn't exist yet and pops the stack too far. The bytecode step fails and the module doesn't build:

[ERROR] Compiler Toolbox error: stack underflow (op=114, pc=13)

Any module with this common pattern is affected. No module in the repo uses it today.

This backports upstream bellard/quickjs@69090b9 ("Fix stack underflow with generator in iterable", saghul, #488, MIT). It sets has_iterator and drop_count after the iterable is parsed. The change is 6 added and 2 removed lines in js_parse_for_in_of, the same as upstream apart from this file's formatting.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Testing

  • Manual testing performed (described below)

Evidence

1. Valdi's compiler on a 7-line module. I bootstrapped an app with valdi bootstrap -l <this checkout> and added:

export async function sum(items: Promise<number[]>): Promise<number> {
  let total = 0;
  for (const n of await items) {
    total += n;
  }
  return total;
}
  • Before the fix: bazel build //modules/<app>:<app> fails with Compiler Toolbox error: stack underflow (op=114, pc=13).
  • After the fix: INFO: Build completed successfully. The Android .valdimodule (zstd-decompressed) contains ForOfAwait.js. Valdi's TypeScript output for the loop is exactly for (const n of yield items), the form that failed.

2. Compile-only, vendored QuickJS built standalone (JS_EVAL_FLAG_COMPILE_ONLY).

Source Before After
function* g(){ for (const k of yield 1) {} } stack underflow OK
function* g(){ for (var k of yield 1) {} } stack underflow OK
function* g(){ for (const k of (0, yield 1)) {} } stack underflow OK
function* g(){ for (const k of [...(yield 1)]) {} } nip_catch: no catch op OK
function* g(){ for (const k of id(yield 1)) {} } nip_catch: no catch op OK
function* g(){ try { for (const k of yield 1) {} } finally {} } stack underflow OK
async function g(){ for await (const k of await f()) {} } OK OK
function* g(){ const a = yield 1; for (const k of a) {} } OK OK
function* g(){ for (const k in yield 1) {} } OK OK

3. Runtime semantics, fixed QuickJS vs V8 (Node). One script exercises:

  • iteration;
  • return inside the loop, which must close the iterator;
  • break, after which the generator continues;
  • return() and throw() while suspended at the yield in the loop head, before any iterator exists;
  • the async for (… of await …) shape.

Output from the fixed QuickJS and from Node is identical:

basic:1,2,3 | early:2 closed=true | after-break closed=true | finally-ran | return-in-head:{"value":"X","done":true} | caught:boom | async:6

The unfixed QuickJS can't compile that script (stack underflow).

Checklist

  • Code follows project style guidelines (the file's 4-space style)
  • No breaking changes: only code that failed to compile changes. Bytecode for every form that already compiled is unchanged, because the two fields are now set later in the same function, before any later code reads them.
  • Commit messages follow conventional format
  • No secrets, API keys, or internal URLs included
  • Touches third-party/quickjs/** (hot system jsengine, gate guard). This is a compiler front-end fix, so a runtime tweak can't cover it. It changes bytecode only for sources that failed to compile before. The other engines (Hermes, JSC) don't share this parser.

Related Issues

None open. This is a known upstream QuickJS bug (bellard/quickjs#189, fixed in #488).

Reverts / partially reverts: none

Additional Context

Third-party code: the change is upstream QuickJS's own fix (MIT), attributed above and in the commit message.

…rflow"

Valdi compiles TypeScript to ES2016, so `for (const x of await y)` in an async function becomes
`for (const x of yield y)` inside a generator. QuickJS 2021-03-27 marked the loop as holding an
iterator before it parsed the iterable expression, so a `yield` there emitted iterator cleanup for an
iterator that did not exist yet and popped the stack too far. The bytecode step then failed:
"Compiler Toolbox error: stack underflow".

Backport of upstream bellard/quickjs@69090b9 ("Fix stack underflow with generator in iterable",
saghul, #488, MIT): set has_iterator and drop_count after the iterable is parsed.
@github-actions

Copy link
Copy Markdown

Valdi hot systems touched

What to do: your PR got this comment, now what?

  • JS engines (hot/jsengine, gate: guard): QuickJS, Hermes and JSC. Run the tests on every engine in use, and check that release bytecode still loads on 32-bit Android.
    • third-party/quickjs/src/quickjs/quickjs.c

Significant changes to the guard systems above can't be covered by a runtime flag; they need the system's tests to pass and a release bake instead.

A change is significant if it alters lifetime or teardown order, threading or locks, a bridge contract, error behavior, a default, a base-class contract, the startup path or a build-host format, or if it removes something that looks dead, adds hot-path logging, or refactors hot code. See the policy.

  • Not significant (say why in the PR description), or
  • Behind a flag: VALDI_ENABLE_<AREA>_<CHANGE>, default off, or
  • Guard system: no runtime flag can cover it, and the system's tests ran
  • Lifetime / raising-callsite audit or thread and lock map in the PR description (teardown, threading, marshalling)

Advisory only. This comment never blocks a merge.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant