Skip to content

A template literal argument carries a reference of its own (TSLANG 27) - #777

Merged
ASDAlexander77 merged 1 commit into
mainfrom
rc-fresh-template-arg
Nov 7, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
rc-fresh-template-arg

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

What

TSLANG_ISSUES 27: under -mm=rc, the following crashed on the second call (exit 127):

class Hud {
    message: string = "";
    show(text: string) { console.log(text); this.message = text; }
}
hud.show(`Gravity: ${NAMES[i % 4]}`);

Why

"a" + b goes through markFreshBlockOwned: the new string is retained and marked __owned_result, so a receiver takes over that reference, or it's given back at the end of the block. A template literal's ts.StringConcat never went through it, so it reached the callee with a count of 0. In the callee, console.log's rest array retained the argument and released it again. That dropped the count to 0 and freed the string before this.message = text stored it. The result was a use after free, then a double free on the next store.

Probing found two more places with the same gap: an object or literal array turned into a string (<string>{ a: i }, <string>[1, 2], built in castConstArrayToString / fieldsToString). <string>{ a: i } passed as an argument also crashed under rc.

Change

  • MLIRGenExpressions.cpp (template literal), MLIRGenCast.cpp (castConstArrayToString, fieldsToString): call markFreshBlockOwned on the StringConcat they build.
  • Side effect: under rc and own, these strings were never released before. Passing 3M template strings as arguments peaked at about 570 MB, and now at about 50 MB. own did not crash before: its callee doesn't release the console.log argument array.
  • TSLANG_ISSUES.md: item 27 is moved into the "fixed" paragraph.

Tests

  • New 00tslang_issue_27.ts, JIT and compiled, under gc / rc / none / own. Under rc it exits with 127 on pre-alpha116 and passes now.
  • ctest Release: 6678/6678 passed.
  • DefaultLib rebuilt with this compiler: release tests.ps1 passes for rc and own (186/187 each; the one skip is the gc-only test). The debug pass was not run.

Unrelated bug found while probing, not fixed here: a class field f: (() => number) | null assigned a function fails to compile with "invalid cast from () => number to () => number | null" under every memory model.

🤖 Generated with Claude Code

A template literal, and an object or literal array printed into a string, built
a new string with no reference behind it, unlike `"a" + b`. Passed straight to a
call under -mm=rc, a callee that handed its parameter to console.log dropped the
count to zero and freed the string before storing it (use after free, then a
double free). Under rc and own the same strings were never released (3M template
arguments: ~570 MB, now ~50 MB).

They now go through markFreshBlockOwned as `+` does: retained and marked, taken
over by a receiver or given back at the end of the block.

Regression test 00tslang_issue_27.ts under gc, rc, none and own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 9e2b97e into main Nov 7, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the rc-fresh-template-arg branch November 7, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant