Skip to content

Array.fill stops before end - #15

Merged
ASDAlexander77 merged 1 commit into
rc-owning-copiesfrom
rc-overruns
Sep 30, 2026
Merged

ASDAlexander77 merged 1 commit into
rc-owning-copiesfrom
rc-overruns

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Summary

This PR is stacked on #14: merge that one first. It goes with the compiler PR ASDAlexander77/TypeScriptCompiler#414.

Array.fill stops before end

The loop in fill ran i <= end, so it always filled one element past the requested range. With the default end = this.length, that extra element lay past the end of the array fill had just copied: a heap overrun.

The allocator usually rounds the block up, which hides the extra write. This overrun is the likely cause of array_fill's intermittent 0xC0000374 under -mm=rc. A guard-page diagnostic heap catches the write on every run. The loop now runs i < end.

Tests

array_fill.ts now asserts its results instead of only printing them. It also adds a case with end inside the array, [1, 2, 3].fill(9, 1, 2), which returned [1, 9, 9] before the fix.

Results

With ASDAlexander77/TypeScriptCompiler#414:

before after
rc, compile + JIT 20 of 316 runs fail 3 of 316 fail (sleep, weakref_basic)
gc, compile + JIT 158/158 158/158

The only rc runs still failing are sleep and weakref_basic, which fail to link because they reference GC_* symbols.

🤖 Generated with Claude Code

fill's loop ran `i <= end`, so it filled one element past the range, and
with the default `end = this.length` it wrote one element past the end of
the array it had just copied: a heap overrun, usually hidden by the
allocator rounding the block up, and the likely cause of array_fill's
intermittent 0xC0000374 under -mm=rc. A guard-page heap catches the write
every run. It now runs `i < end`.

array_fill.ts now asserts its results and adds `[1, 2, 3].fill(9, 1, 2)`,
which gave [1, 9, 9] before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit ee419b9 into rc-owning-copies Sep 30, 2026
ASDAlexander77 added a commit that referenced this pull request Sep 30, 2026
…ne copy short (#14)

* Copy owning elements by assignment, not memcpy; String.repeat stops one copy short

Array.concat, slice and copyWithin, and Set and Map growing their entries,
copied elements with memcpy/memmove. Under -mm=rc an element can own what it
holds (a string built at run time, an `any` box, a Set<any> entry's value),
and a raw copy takes no reference to it: the copy and the original then both
release it, which freed it early and corrupted the heap - set_union and
set_symmetricDifference crashed at JIT teardown, and concat of non-literal
strings double-freed. The elements are now copied one assignment at a time,
which takes and gives up references as every store does and is the same copy
under gc. copyWithin copies backwards when the ranges overlap that way, as
memmove did, and no longer copies past the end of the array.

String.repeat wrote `count` copies after the one clone() had already placed,
running one copy past the end of the string it had sized; it now writes
count - 1.

New test owning_copies.ts (non-literal strings through concat, slice,
copyWithin, and a growing Set and Map; a literal is immortal and would hide
the bug), and string_repeat.ts now asserts its results. Needs the compiler
changes for `s.length = n`, rest-parameter packing and grown array slots
(TypeScriptCompiler PR) for the rc tests to pass.

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

* Array.fill stops before `end` (#15)

fill's loop ran `i <= end`, so it filled one element past the range, and
with the default `end = this.length` it wrote one element past the end of
the array it had just copied: a heap overrun, usually hidden by the
allocator rounding the block up, and the likely cause of array_fill's
intermittent 0xC0000374 under -mm=rc. A guard-page heap catches the write
every run. It now runs `i < end`.

array_fill.ts now asserts its results and adds `[1, 2, 3].fill(9, 1, 2)`,
which gave [1, 9, 9] before.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

---------

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