-
Notifications
You must be signed in to change notification settings - Fork 5k
jsc: TypedArray indexed access at 4294967295 on a 2**32-length view #35876
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
Open
robobun
wants to merge
5
commits into
main
Choose a base branch
from
farm/d4d0d8bc/typedarray-uint32max-index
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+147
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f345798
jsc: TypedArray indexed access at 4294967295 on a 2**32-length view
robobun fa77606
[autofix.ci] apply automated fixes
autofix-ci[bot] a901bb3
test: allow ASAN allocator to return null so the 4 GiB skip path works
robobun d692f52
jsc: also widen HasOwnPropertyCache and IC absence-condition guards
robobun e83092d
ci: retrigger (WebKit preview build is now available)
robobun 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
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,146 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe } from "harness"; | ||
|
|
||
| // A Uint8Array can have length up to MAX_ARRAY_BUFFER_SIZE (2**32 on 64-bit), | ||
| // so 4294967295 (UINT32_MAX) is a valid index. Regular JS arrays top out at | ||
| // MAX_ARRAY_INDEX (2**32 - 2), and the TypedArray [[Get]]/[[Set]] paths were | ||
| // reusing that cap, which left the last element unreachable via bracket access | ||
| // while [[HasProperty]] and DataView could still see it. | ||
| // https://tc39.es/ecma262/#sec-isvalidintegerindex | ||
|
|
||
| const fixture = /* js */ ` | ||
| "use strict"; | ||
| const LEN = 2 ** 32; | ||
| const IDX = LEN - 1; // 4294967295 | ||
| const KEY = String(IDX); // "4294967295" | ||
|
|
||
| let u; | ||
| try { | ||
| u = new Uint8Array(LEN); | ||
| } catch { | ||
| console.log(JSON.stringify({ skipped: true })); | ||
| process.exit(0); | ||
| } | ||
| if (u.length !== LEN) throw new Error("unexpected length " + u.length); | ||
|
|
||
| // Seed the last byte via a path that was never broken. | ||
| new DataView(u.buffer).setUint8(IDX, 11); | ||
|
|
||
| const out = { | ||
| initialGetNum: u[IDX], | ||
| initialGetStr: u[KEY], | ||
| at: u.at(IDX), | ||
| inNum: IDX in u, | ||
| inStr: KEY in u, | ||
| hasOwn: Object.hasOwn(u, KEY), | ||
| gopd: Object.getOwnPropertyDescriptor(u, KEY), | ||
| }; | ||
|
|
||
| u[IDX] = 22; | ||
| out.afterIndexedSet = u[IDX]; | ||
| out.afterIndexedSetDV = new DataView(u.buffer).getUint8(IDX); | ||
|
|
||
| Reflect.set(u, KEY, 33); | ||
| out.afterReflectSet = u[IDX]; | ||
| out.afterReflectSetDV = new DataView(u.buffer).getUint8(IDX); | ||
|
|
||
| try { | ||
| Object.defineProperty(u, KEY, { value: 44, writable: true, enumerable: true, configurable: true }); | ||
| out.afterDefine = new DataView(u.buffer).getUint8(IDX); | ||
| } catch (e) { | ||
| out.afterDefine = "threw: " + e.constructor.name; | ||
| } | ||
|
|
||
| out.deleteInBounds = Reflect.deleteProperty(u, KEY); | ||
|
|
||
| // Keep matching spec for adjacent cases: | ||
| out.oneBeyond = u["4294967296"]; | ||
| out.minusZero = u["-0"]; | ||
|
|
||
| // On a short view, the same index is out of bounds. | ||
| const small = new Uint8Array(8); | ||
| out.smallGet = small[IDX]; | ||
| out.smallIn = KEY in small; | ||
| out.smallDelete = Reflect.deleteProperty(small, KEY); | ||
| let smallDefineThrew = false; | ||
| try { | ||
| Object.defineProperty(small, KEY, { value: 1, writable: true, enumerable: true, configurable: true }); | ||
| } catch { | ||
| smallDefineThrew = true; | ||
| } | ||
| out.smallDefineThrew = smallDefineThrew; | ||
|
|
||
| // Other 1-byte element types at max length. | ||
| const i8 = new Int8Array(u.buffer); | ||
| i8[IDX] = -7; | ||
| out.i8 = i8[IDX]; | ||
| const uc = new Uint8ClampedArray(u.buffer); | ||
| uc[IDX] = 300; | ||
| out.uc = uc[IDX]; | ||
|
|
||
| // Structure-keyed caches (HasOwnPropertyCache, GetBy/InBy IC miss entries) | ||
| // must not be keyed on the shared TypedArray structure for this index, since | ||
| // the answer is length-dependent. Allocate both views first so GC between the | ||
| // two probes doesn't hide a stale entry by clearing the cache. | ||
| const icSmall = new Uint8Array(u.buffer, 0, 8); | ||
| new DataView(u.buffer).setUint8(IDX, 55); | ||
| out.hasOwnSmallFirst = Object.hasOwn(icSmall, KEY); | ||
| out.hasOwnBigAfter = Object.hasOwn(u, KEY); | ||
| function probeGet(x) { return x["4294967295"]; } | ||
| function probeIn(x) { return "4294967295" in x; } | ||
| for (let i = 0; i < 200; i++) { probeGet(icSmall); probeIn(icSmall); } | ||
| out.icGetBig = probeGet(u); | ||
| out.icInBig = probeIn(u); | ||
|
|
||
| console.log(JSON.stringify(out)); | ||
| `; | ||
|
|
||
| test("TypedArray indexed access at 4294967295 on a 2**32-length view", async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", fixture], | ||
| env: { | ||
| ...bunEnv, | ||
| ASAN_OPTIONS: [bunEnv.ASAN_OPTIONS, "allocator_may_return_null=1"].filter(Boolean).join(":"), | ||
| }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect(stderr).toBe(""); | ||
| const out = JSON.parse(stdout.trim()); | ||
| if (out.skipped) { | ||
| console.log("skipping: could not allocate a 2**32-byte Uint8Array"); | ||
| expect(exitCode).toBe(0); | ||
| return; | ||
| } | ||
|
|
||
| expect(out).toEqual({ | ||
| initialGetNum: 11, | ||
| initialGetStr: 11, | ||
| at: 11, | ||
| inNum: true, | ||
| inStr: true, | ||
| hasOwn: true, | ||
| gopd: { value: 11, writable: true, enumerable: true, configurable: true }, | ||
| afterIndexedSet: 22, | ||
| afterIndexedSetDV: 22, | ||
| afterReflectSet: 33, | ||
| afterReflectSetDV: 33, | ||
| afterDefine: 44, | ||
| deleteInBounds: false, | ||
| oneBeyond: undefined, | ||
| minusZero: undefined, | ||
| smallGet: undefined, | ||
| smallIn: false, | ||
| smallDelete: true, | ||
| smallDefineThrew: true, | ||
| i8: -7, | ||
| uc: 255, | ||
| hasOwnSmallFirst: false, | ||
| hasOwnBigAfter: true, | ||
| icGetBig: 55, | ||
| icInBig: true, | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.