Skip to content

TSLANG_ISSUES 6: a string returned by C is copied - #575

Merged
ASDAlexander77 merged 1 commit into
mainfrom
copy-c-strings
Oct 13, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
copy-c-strings

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

TSLANG_ISSUES.md 6: a string returned by C was C's char* itself.

It was worse than stale text:

  • a buffer C reuses changed under the program: a = strerror(2); b = strerror(13) printed the second text twice;
  • under -mm=rc the reference count lives in the 8 bytes before a string, so retaining C's pointer wrote into memory that isn't the program's. Storing strchr(base, 'k') turned abcdefghijklmnop1 into abddefghijklmnop1, then abedefghijklmnop1; a release reaching zero would free C's pointer.

Change: the result of a call into C, meaning a top-level declare function that nothing in the program defines or a @cpp method (ts.cpp_call), goes through ts.StringCopy. Null stays null, and the copy is owned like any fresh string (markFreshBlockOwned). Declaring the result Opaque gives the raw pointer, for pointer arithmetic or for a string the caller must free. A function in a declare namespace or declare class, as DefaultLib's lib.d.ts declares its methods, is TypeScript and isn't copied.

Merge order: DefaultLib must merge first: ASDAlexander77/TypeScriptCompilerDefaultLib#33. It declares strstr, setlocale, regexp_* and the C time functions Opaque (int for the two that return errno_t). DefaultLib built from older sources with this compiler would break indexOf and RegExp.replace.

Verified:

  • test 00c_string_copy.ts fails on alpha100 under gc and rc, and passes under gc, rc, own and none;
  • --verify-ownership is silent under rc and own;
  • 9M copies in a loop stay flat at ~500 KB private memory under rc and own;
  • DefaultLib built with this compiler: gc 163/163, rc 162/163 (1 gc-only skip);
  • full Release suite 4080/4080.

Known extra cost: DefaultLib's own TypeScript helpers declared top-level in core.os.d.ts (convertNumber, time_*) are in a separately compiled object, so lib.ts copies their results once more (toFixed, Date strings).

Follow-ups, not in this PR:

  • string parameters of @cpp methods that C++ calls (the method has ts.cpp_method): copying needs a release at function exit under rc;
  • string fields of C structs can't be told apart from TypeScript tuples.

Docs: docs/c-cpp-header-import.md and docs/cpp-classes.md; TSLANG_ISSUES.md row and section 6.

🤖 Generated with Claude Code

A `string` result of a C function (a top-level `declare function` with no
definition in the program) or of a @cpp method was C's `char*` itself:
a buffer C may reuse (`strerror(2)` read the next call's text) or the
middle of another string (`strstr`). Under -mm=rc the reference count
lives in the 8 bytes before a string, so retaining it wrote into memory
that isn't the program's: storing `strchr(base, 'k')` rewrote two
characters of `base`, and a release reaching zero would free C's pointer.

The call's result now goes through ts.StringCopy (null stays null), owned
like any fresh string. `Opaque` is the way to get the pointer itself. A
function in a `declare namespace` or `declare class` (the default
library's lib.d.ts) is TypeScript and isn't copied.

TSLANG_ISSUES.md 6. Needs TypeScriptCompilerDefaultLib#33, which declares strstr,
setlocale, regexp_* and the C time functions `Opaque` (or `int`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit d784e54 into main Oct 13, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the copy-c-strings branch October 13, 2026 18:36
ASDAlexander77 added a commit that referenced this pull request Oct 17, 2026
All 19 issues are fixed on main; the section of fixes after pre-alpha97
is no longer headed "Open".

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
ASDAlexander77 added a commit that referenced this pull request Oct 31, 2026
…first

TSLANG_ISSUES 21. The string a C function returns is copied into a string of the
program's own (#575), but not when the `.d.ts` declaring it was first reached through a
module this file imports: `declaredFunctions` kept the module of that earlier pass, and
isForeignCall wanted the one being generated. The raw `char*` was taken as it was - stored
strerror() results showed the buffer's last text, and under -mm=rc the count was written
into C's memory and the release freed it (native_ts_graphics on Android: "Scudo ERROR:
misaligned pointer when deallocating").

A top-level body-less `declare function` is C whichever module's pass loaded its `.d.ts`:
isForeignCall now asks that, and that this module's function has no body.

Test c-string-import/ (gc, rc, none, own; compiled and JIT), whose `.d.ts` is outside
tests/, where *.d.ts are build output and ignored. ctest 6560/6560; fuzz seeds
85000-85599 clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ASDAlexander77 added a commit that referenced this pull request Oct 31, 2026
…n imported .d.ts (#761)

* @value T | null: ?: of a literal holding a new object (rc), ??, and print

Three failures of a @value type that may be missing, all on main before #760:

- rc: `d < 0 ? null : { at: d, owner: new Owner(d) }` returned as `Mark | null`. The
  branch released the new object before the result's retain after the join, which then
  retained freed memory. consumeConditionalResults' producerOf looks through a tuple
  cast to the @value type it is laid out as, so yieldsLiteralOfBranchTemporary sees the
  literal and the branch retains first.
- `h ?? find(3)` was typed `Hit | (Hit | null)` and did not compile: an optional member of
  a union gave its element as one member. processUnionTypeItem now processes the element
  like any other member, so a union in it gives its members.
- print / string conversion: an undefined `T | undefined` printed as null or as a zeroed
  T (the optional went through ValueOrDefault); it is "undefined" now. A null `T | null`
  stored without a tag (#760) printed T's fields; castNullableToString takes a @value
  base too and prints "null".

Test value/value_missing_ops (gc, rc, none; own cannot yet tell which branch of a `?:`
owns a literal it merges). ctest 6546/6546; fuzz seeds 83000-83599 clean; DefaultLib
compiles under own and rc.

Printing a @value value holding an interface still crashes, as printing any interface
does - not changed here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* An interface printed shows its fields, as an object literal does

Printing an interface value, or putting it into a string, crashed at run time,
whatever object was behind it - a class, an object literal, or one a @value value
holds. USER RULING: print its fields. castInterfaceToString:

- its `toString` when the interface has one, as a class's is;
- else its fields, read through the interface, those of the interfaces it extends
  first, in an object literal's format (`{ n: 3, name: 'b' }`, fieldsToString,
  which castTupleToString now shares);
- null as "null";
- a field of an interface type already being printed is `[Object]`: the text is
  made inline, and `interface Chain { next: Chain }` nests without end.

Test tests/00interface_print (every model). ctest 6552/6552; fuzz seeds
84000-84599 clean; DefaultLib compiles under all four models.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* A C string result is copied when an imported module loaded its .d.ts first

TSLANG_ISSUES 21. The string a C function returns is copied into a string of the
program's own (#575), but not when the `.d.ts` declaring it was first reached through a
module this file imports: `declaredFunctions` kept the module of that earlier pass, and
isForeignCall wanted the one being generated. The raw `char*` was taken as it was - stored
strerror() results showed the buffer's last text, and under -mm=rc the count was written
into C's memory and the release freed it (native_ts_graphics on Android: "Scudo ERROR:
misaligned pointer when deallocating").

A top-level body-less `declare function` is C whichever module's pass loaded its `.d.ts`:
isForeignCall now asks that, and that this module's function has no body.

Test c-string-import/ (gc, rc, none, own; compiled and JIT), whose `.d.ts` is outside
tests/, where *.d.ts are build output and ignored. ctest 6560/6560; fuzz seeds
85000-85599 clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
ASDAlexander77 added a commit that referenced this pull request Oct 31, 2026
… through an import (#762)

#575's copy of a C function's `string` result is skipped when the
`declare function` is in a .d.ts that an imported module also references
(isForeignCall checks the module the declaration was recorded with).
The raw char* then becomes a program string again: aliased buffers under
gc, C++ memory counted and freed under rc (native_ts_graphics's
SampleGame crashed on Android). Repro run on pre-alpha111, the IR,
where to look and a suggested regression test.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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