-
Notifications
You must be signed in to change notification settings - Fork 5k
Fail fast at startup when simdutf has no usable implementation for this CPU #30642
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
1
commit into
main
Choose a base branch
from
farm/80eb7318/simdutf-unsupported-cpu-guard
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.
Open
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // https://github.com/oven-sh/bun/issues/30613 | ||
| // | ||
| // On CPUs below Bun's x64 baseline (e.g. QEMU's default TCG vCPU, which only | ||
| // advertises SSE3), simdutf's runtime dispatcher finds no usable kernel: | ||
| // the build is compiled with -march=nehalem, which defines __SSE4_2__ and | ||
| // therefore compiles out simdutf's scalar fallback. It then dispatches to an | ||
| // `unsupported_implementation` stub whose methods all return 0/false/{OTHER,0}. | ||
| // | ||
| // Before the fix, Bun trusted those return values: validate_utf8 rejects | ||
| // every file (bunfig.toml fails to load with "Invalid UTF-8 byte sequence"), | ||
| // and first_non_ascii reports a non-ASCII byte at offset 0 for any input | ||
| // longer than its 32-byte scalar fast path, so the scan loops built on it | ||
| // never advance and `bun app.js` hangs forever. The original report on | ||
| // v1.3.9 hit a slice-length underflow on the same stub and segfaulted after | ||
| // ~16 seconds instead. | ||
| // | ||
| // We simulate that CPU by forcing simdutf onto a nonexistent implementation | ||
| // name; simdutf's set_best() treats an unknown name exactly like an | ||
| // unsupported CPU and installs the same stub. | ||
|
|
||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isArm64 } from "harness"; | ||
|
|
||
| // On arm64 simdutf compiles exactly one kernel (NEON, which is mandatory on | ||
| // aarch64), so SIMDUTF_SINGLE_IMPLEMENTATION == 1 and runtime dispatch is | ||
| // bypassed entirely: SIMDUTF_FORCE_IMPLEMENTATION is ignored and there is no | ||
| // way to reach the unsupported stub from the outside. The startup probe still | ||
| // runs there; it just can never fail on real arm64 hardware. | ||
| test.concurrent.skipIf(isArm64)( | ||
| "fails fast with a clear error when simdutf has no supported implementation", | ||
| async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", "console.log('unreachable')"], | ||
| env: { | ||
| ...bunEnv, | ||
| // Any name not in simdutf's compiled-in list selects the unsupported | ||
| // stub, identical to running on a pre-SSE4.2 host. | ||
| SIMDUTF_FORCE_IMPLEMENTATION: "none-for-issue-30613", | ||
| }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| // The unfixed build either hangs in a first_non_ascii scan loop or fails | ||
| // with a bogus "Invalid UTF-8" error depending on what it reads first; the | ||
| // fixed build prints a diagnostic and exits cleanly before touching input. | ||
| expect(stderr).toContain("Bun requires"); | ||
| expect(stderr).toContain("SIMDUTF_FORCE_IMPLEMENTATION"); | ||
| expect(stdout).toBe(""); | ||
| expect(proc.signalCode).toBeNull(); | ||
| expect(exitCode).toBe(134); | ||
| }, | ||
| ); | ||
|
|
||
| test.concurrent("runs normally when a supported simdutf implementation is available", async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", "console.log('ok')"], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect(stderr).toBe(""); | ||
| expect(stdout).toBe("ok\n"); | ||
| 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.