Skip to content

fix: bound --gpu sizes before the cast, mask U32 host arguments (#2348, #2347) - #2349

Open
Giulio2002 wants to merge 2 commits into
bendlang:mainfrom
Giulio2002:fix/host-u32-gpu-size
Open

Giulio2002 wants to merge 2 commits into
bendlang:mainfrom
Giulio2002:fix/host-u32-gpu-size

Conversation

@Giulio2002

Copy link
Copy Markdown
Contributor

Fixes #2348 and #2347, one commit each.

#2348: --gpu size past 2^64 bytes

Repro on main (6180be5), tiny.bend from the issue, built with -fsanitize=undefined,float-cast-overflow:

$ ./tiny_u +RTS --gpu 999999999999GB
tiny.c:2897:13: runtime error: 1.07374e+21 is outside the range of representable values of type 'unsigned long long'
ok
$ ./tiny_u +RTS --gpu 17179869184GB
tiny.c:2897:13: runtime error: 1.84467e+19 is outside the range of representable values of type 'unsigned long long'
ok

Fix: the C --gpu arm keeps the size as a double and sets mem only for a well formed size in [16384, 2^64), so no out-of-range cast happens and an oversize size gets the usual "expected on, off or a size like 4GB after --gpu". The JS runtime takes the same upper bound, so both lanes accept and reject the same sizes (checked on 19 edge inputs, including 17179869183GB accepted, 17179869184GB, 1e400GB, nanGB, -4GB, .GB rejected). Its regex reads the number as [\d.]*, the same language as before, and runtime_gpu_invalid still passes.

The arm is restructured (gpu = strcmp(v, "off") != 0, one error branch) so comp.ts stays under its 66k ttok cap: 65997 after both commits, 65989 before.

After: no UBSan report, exit 1 with the flag error, and a real GPU build of tests/run/gpu_mark.bend now rejects 999999999999GB instead of dying with "reservation failed".

#2347: U32 argument from a JS caller

Repro on main, host_u32.bend and its caller from the issue:

U32 4294967296 show -> "4294967296"  add(x,1) -> 1  is_zero -> false
U32 -1 show -> "/"  add(x,1) -> 0  is_zero -> false

Fix: host() masks a U32 coming in with x >>> 0, like it rounds an F32 with Math.fround. After:

U32 4294967296 show -> "0"  add(x,1) -> 1  is_zero -> true
U32 -1 show -> "4294967295"  add(x,1) -> 0  is_zero -> false

which matches the issue's expected.bend. I went with masking rather than a throwing u32_in: it is what the U32 ops already do, it matches the F32 edge, and it needs no new error text under the cap. Happy to switch if you'd rather fail-stop.

The Nat remark in #2347 (nat_in bounds at 2^53, not 2^48-1) is left alone: #1656, nat_host_export and nat_host_bounds pin 2^53 as the host range on purpose.

Tests

  • tests/io/runtime_gpu_huge: builds a program as C and JS, checks both reject 999999999999GB, 17179869184GB, 1e400GB and accept 17179869183GB, 4GB.
  • tests/io/u32_host_export: 2^32, -1, 2^32+5 through show, is_zero and add, one call and curried.

Both fail on main (exit codes 1 and 2) and pass here. These also still pass: runtime_gpu_invalid, f32_host_export, nat_host_export, nat_host_bounds, nat_host_neg, u32_to_f32_host_zero. I could not run the cluster gates from here. I ran these locally under bun 1.4.5 on Darwin arm64.

🤖 Generated with Claude Code

Giulio2002 and others added 2 commits November 24, 2026 22:39
…lang#2348)

The C runtime cast n * mul to u64 before checking it, so a size past
2^64 bytes (999999999999GB, 1e400GB) was an out-of-range double to u64
cast: UB, saturating to a 16 EiB request on arm64 that then failed as
"reservation failed". The size is now kept as a double and becomes mem
only when it is a well-formed size in [16384, 2^64). The JS runtime
takes the same bound, so both lanes reject the same sizes, and its
regex reads the digits as [\d.]* (same language, still linear).

Fixes bendlang#2348

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
host() coerced Char, String, Nat and F32 arguments at a .mjs edge but
passed a U32 through as is, so U32.show(2^32) printed "4294967296",
U32.show(-1) printed "/" and U32.is_zero(2^32) was false while U32.add
had already treated it as 0. A U32 argument (and a foreign U32 answer)
now comes in as x >>> 0, the same mask the U32 operations use.

Fixes bendlang#2347

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.

A --gpu size past the 64 bit range is cast out of range and accepted

1 participant