-
-
Notifications
You must be signed in to change notification settings - Fork 163
fix(runtime): implement resizable ArrayBuffer — new ArrayBuffer(n, { maxByteLength }), resize, length-tracking views (#10873)
#10916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
proggeramlug
wants to merge
2
commits into
PerryTS:main
from
proggeramlug:fix/10873-resizable-arraybuffer
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| Implemented resizable `ArrayBuffer` (ES2024): `new ArrayBuffer(length, { maxByteLength })`, `ArrayBuffer.prototype.resize`, real `resizable` / `maxByteLength` getters, length-tracking and fixed-length views, and `transfer()` preserving resizability (#10873). | ||
|
|
||
| `new ArrayBuffer(len, { maxByteLength })` silently returned a fixed-length buffer: the `"ArrayBuffer"` arm in `perry-codegen`'s `lower_call/builtin.rs` lowered only the first argument (the options bag was never evaluated), `ArrayBuffer.prototype.resize` did not exist, and `get_field_by_name_tail.rs` hard-coded `resizable` to `false`. A program using one — guest271314's TypeScript Native Messaging host keeps a single `new ArrayBuffer(0, { maxByteLength: 64 MiB })` and `resize()`s it per message — died on its first `.resize()` with `TypeError: (Buffer).resize is not a function`. | ||
|
|
||
| Storage model (`perry-runtime/src/buffer/resizable.rs`): buffer bytes live inline after the `BufferHeader` and every view aliases its backing by raw address, so a resize must never move the payload. A resizable buffer reserves `maxByteLength` once (its `capacity`) and `resize()` only rewrites `length`. Construction clears the initial `length` bytes only; a per-buffer `dirty_end` boundary records where the reserved tail is known-zero, so a grow clears exactly what may be dirty — `new ArrayBuffer(0, { maxByteLength: 64 MiB })` reserves address space, not resident memory, and `resize(64 MiB)` into never-touched or released pages costs 0.04 ms (a first cut that memset the range cost 224 ms). A shrink of at least 64 KiB hands the dropped pages back to the OS with the same `madvise` detach uses, so RSS follows `byteLength`, not the high-water mark. | ||
|
|
||
| Views are re-lengthed eagerly on every `resize` — the way detach zeroes them — so every fast tier that reads a view's length is unchanged: a view constructed without an explicit length (and a `subarray()` without `end` of one) tracks `byteLength`; a fixed-length view reads as length 0 / byteOffset 0 while it no longer fits and comes back when the buffer regrows; an out-of-bounds `DataView` throws `TypeError` from its accessors and `byteLength`. `transfer()` keeps a buffer resizable (and `RangeError`s past `maxByteLength`), `transferToFixedLength()` drops it. `resize` / `transfer` / `transferToFixedLength` and the `resizable` / `maxByteLength` / `detached` accessors are installed on `ArrayBuffer.prototype`; the dynamic constructor path (`class_registry/construct.rs`) passes the options too. Every probe added to a shared path is gated on one `RegistryLatch` load, and the per-access `ViewInfo` / `ViewMeta` copies stay two words; measured `instructions:u` on typed-array, view, DataView and ArrayBuffer micro-rows are within ±0.6% of `main`. | ||
|
|
||
| Verified: `test-files/test_gap_10873_resizable_arraybuffer.ts` byte-identical against node 26.5.1 (fails on unpatched `main` at its first line), also under `PERRY_GC_SCHEDULE_SEED=1 PERRY_GC_SCHEDULE_RATE=1 PERRY_GC_SCHEDULE_ALLOC_KB=0 PERRY_GC_PROTECT_FROMSPACE=1 PERRY_GC_VERIFY_EVACUATION=1` (1572 forced copying minors); eleven unit tests in `buffer/resizable_tests.rs`; the test262 `resizable-arraybuffer` built-ins subset goes from 33 to 156 of 408 passing. Not covered yet: growable `SharedArrayBuffer`, and `%TypedArray%.prototype` method semantics for a receiver that shrinks mid-iteration (the bulk of the remaining test262 cases; `test-compat/test262/features-applicable.txt` says so). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🛡️ Detected with Advanced Tier | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 717
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 24340
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 13473
Information Disclosure
Reachability: External
CWE: CWE-908
Restrict the zero-fill optimization to Linux.
decommit_payload_pages_zeroedis documented as safe only on Linux, but the currentcfgenables it on every non-macOS Unix target. IfMADV_DONTNEEDdoes not zero-fill private anonymous pages on such a target, a later grow can expose stale bytes.🔒️ Proposed fix
🤖 Prompt for AI Agents