Skip to content

rc: @owned declarations, so calls through function values give their results back with the default library - #923

Merged
ASDAlexander77 merged 2 commits into
mainfrom
rc-owned-declarations
Nov 18, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
rc-owned-declarations

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Fixes the -mm=rc leak of heap results returned through indirect calls (callbacks, function values, calls through any) in programs that use the default library.

Cause. OwnedReturnConsumptionPass consumes an indirect call's result only under closed world: no function in the module may return a heap value without retaining it. Every default-library declaration (String.trim, Number.toFixed, Error..new, …) has no body and no export, so it was unclassified, and closed world never held once the library was in. Direct library calls leaked as well.

Fix. A new declaration decorator, @owned, on a declare function, a declare class (its methods and ..new) or a declare namespace (its functions). It marks the FuncOp ts.declared_owned_result, meaning "TypeScript compiled elsewhere under the same memory model, returns +1". Under rc the pass treats it like an exported import.

  • Unmarked declarations stay unclassified, so a C function or one that returns a pointer into a buffer (convertNumber) leaks rather than being freed twice.
  • @owned on a function with a body is ignored, with a warning.
  • Only rc reads the marker. Under own it made JsonValue.get's lent result count as fresh, which broke DefaultLib json.ts and json_read.ts.
  • The parser leaves a namespace's statements without a parent. They are linked to the namespace only when it is @owned, so the decorator on the namespace can be found.

Paired with ASDAlexander77/TypeScriptCompilerDefaultLib#106, which marks lib.d.ts and the core.d.ts classes. The six buffer-returning core.os.d.ts declarations stay unmarked.

Measured (-mm=rc, peak working set, with DefaultLib#106):

Probe Before After
1M calls through (n) => any returning "s"+n and (n) => P returning new P 97 MB 5.6 MB (flat vs 100K)
.trim() in a loop, 1M 20.8 MB 5.7 MB
mixed library probe (map/reduce callbacks returning objects, Date.toString, Error subclass, split, toFixed), 300K 279 MB 69.7 MB, same output as gc

Tests

  • New test-rc-owned-declarations reads the IR of owned-declarations/owned_declarations.ts under rc. With @owned on a function, a class and a namespace, all four FuncOps are marked and the call through a parameter is consumed. With every @owned removed, nothing is marked and the call keeps retaining.
  • ctest -C Release -j 8: 8279/8279 pass.
  • DefaultLib tests.ps1 release (compile + JIT), built with this compiler and DefaultLib#106: passes in gc, rc, none and own.

Follow-up (not in this PR): toFixed still leaks inside the library, from convertNumber's char[] buffer.

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits November 18, 2026 18:32
…lls through function values give their results back

A program with the default library never had a closed world: every lib.d.ts declaration
(`String.trim`, `Number.toFixed`, `Error..new` ...) has no body, so OwnedReturnConsumptionPass
could not classify it, and one unclassified heap-returning function turned off result
consumption for every call through a function value, callback or `any`. Each such result
leaked, and so did every direct library call's.

`@owned` on a `declare function`, `declare class` (its methods and `..new`) or
`declare namespace` (its functions) marks the FuncOp ts.declared_owned_result: the function is
TypeScript compiled elsewhere under the same memory model, so it returns +1 like an `export`ed
import. Unmarked declarations stay unclassified (a C function, or one returning a pointer into a
buffer, such as convertNumber), so they leak rather than being freed twice. The parser leaves a
namespace's statements without a parent; they are linked for an `@owned` namespace only.

Probe (-mm=rc, 1M calls through `(n) => any` and `(n) => P`): 97 MB -> 5.6 MB peak, flat.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Under own, OwnedReturnConsumptionPass counting `JsonValue.get` as returning a fresh result
released what it lends: DefaultLib's json.ts / json_read.ts failed with "borrows the result of
'JsonValue.get' but is used here after it may be released". gc and none have no count to give
back, so only rc reads the marker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 2ceb889 into main Nov 18, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the rc-owned-declarations branch November 18, 2026 19:39
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