Out-of-bounds store for n > 2^30: element index computed in signed 32 bits wraps (n = 1076168025)
Summary
For row lengths above 2^30 the generated kernel computes a float index (2 * t + offset) in signed 32-bit arithmetic. The index passes 2^31, becomes negative, and is then sign-extended into the address. The kernel stores 8 bytes about 8.58 GB before its internal workspace (ring), and the element that should be written is not written.
The README states "Any n >= 1 and any batch" and "It handles any length". n = 1076168025 (= 5^2 * 3^16) with batch 1 needs about 25.8 GB (x, y and the workspace: 8.61 GB each), which fits the 32 GB RTX 5090 that the package targets.
| Package | humanfia-lab/fft-c2c-1d, revision f89172ab8f29188bbf28fe0c7d2c5e8bdbab3e7e (head of branch v1), build/torch-cuda |
| Toolchain | nvidia-cutlass-dsl[cu13]==4.6.0, CUTE_DSL_ARCH=sm_120a |
| Input | x = torch.randn(1, 1076168025, 2, device="cuda") (any values; the defect does not depend on the data) |
| Effect | out-of-bounds 8-byte global stores; missing output elements |
| Severity | memory corruption or a CUDA illegal-address fault, depending on what is mapped at the wrapped address |
Reproduce
python repro.py
repro.py (at the end of this post) calls the public entry point fft.run(x) with a unit impulse, so the exact result is known without a second FFT. It runs a control length first (n = 358722675 = 5^2 * 3^15 < 2^30, same radices, must pass), then n = 1076168025. It checks: (1) a CUDA error, (2) that x is unchanged, (3) sampled output bins against the exact value.
Under compute-sanitizer --tool memcheck the first record is:
Invalid __global__ write of size 8 bytes
by thread (144,0,0) in block (0,0,0)
Access to 0x1af613f6c0 is out of bounds
and is 8,582,334,784 bytes before the nearest allocation at 0x1cf5a00000 of size 8,610,906,112 bytes
Root cause
In the kernel that the package generates for this (batch, n) (PTX kept with CUTE_DSL_KEEP_PTX=1), the store is:
shl.b32 %r3769, %r3768, 1; // 2 * t
add.s32 %r3770, %r3769, 1913188112; // float index, 32-bit signed: wraps at 2^31
mul.wide.s32 %rd326, %r3770, 4; // sign-extended byte offset
add.s64 %rd327, %rd2, %rd326; // ring base + offset
st.global.v2.b32 [%rd327], {%r375, %r376};
For CTA 0, thread 144, at the first schedule item (pass 0, tile 0):
| Quantity | Value |
|---|---|
| Float index, exact | 2,149,383,600 (>= 2^31 = 2,147,483,648) |
| Float index as signed 32 bits | -2,145,583,696 |
| Byte offset used | -8,582,334,784 (before the base of ring) |
| Byte offset intended | +8,597,534,400 (inside ring, 8,609,344,200 bytes) |
Threads 144 to 148 of every CTA do this at tile 0 (420 stores for a grid of 84). The same signed pattern (add.s32 then mul.wide.s32) is on the matching loads and on the stores of the later passes. All three kernel variants that the package can select (min CTAs per SM = 3, 2, 1) have it. By our analysis the branch decisions that lead to the store do not depend on the grid size.
Evidence
- Exact arithmetic on the package's own PTX for this input (traced launch record: grid, block, parameters): the address chain above, evaluated in 32/64-bit machine arithmetic, with no branch between the index computation and the store.
- Device run of that PTX. RTX A6000 (sm_86, 48 GB, 84 SMs), driver 595.71.05, compute-sanitizer 2026.2.1.0. The PTX is the package's, re-assembled with
ptxas -arch=sm_86; the only change is the.targetline. Buffers have the sizes of the launch record.- Plain run:
CUDA_ERROR_ILLEGAL_ADDRESSat synchronize. - memcheck, 3 kernel variants x 3 runs: 9 of 9 logs contain the record above (thread 144, CTA 0, 8,582,334,784 bytes before the 8,610,906,112-byte workspace) and 420 invalid 8-byte global writes each.
- Controls with every index below 2^31 (n = 100000 batch 1024, n = 2^20): no CUDA error, 0 changed words in a 9.66 GB guard region before the workspace.
- Plain run:
What we did not test
- We have no sm_120 device.
repro.py(the public entry point on an RTX 5090) is written from the package README and was not run by us. The device evidence is for the package's PTX re-assembled for sm_86. The defect is in integer index arithmetic, so we expect the same store on sm_120; the visible effect (fault, or silent corruption of a neighbouring buffer such asx) depends on the memory layout. - One failing length was tested (n = 1076168025). Other lengths above 2^30 on the same plan very likely have the same wrap; we did not check them. All 17 workloads of the README are at or below n = 2^27 and are not affected.
Suggested fix
Compute element and float offsets in 64 bits before they reach an address (for example Int64(index) * 2 in place of Int32(index * 2)), or reject n above the largest length for which 2 * n plus the plan's offsets stays below 2^31. A test at one length above 2^30 would catch a regression.
Found with a static analyzer for GPU kernels (check "address computed with a wrapped index"), then confirmed on a device as described.
repro.py
#!/usr/bin/env python3
"""Reproducer for humanfia-lab/fft-c2c-1d: signed 32-bit index wrap for n > 2^30.
python repro.py # control (n < 2^30) and then the failing length n = 1076168025
python repro.py --control # the control only
Needs: an sm_120 GPU with about 26 GB free (RTX 5090), the package's own requirements (see its README).
Input: one unit impulse, x[0, t0] = 1 + 0j, all else 0. Then the exact answer is known without a second FFT:
y[0, f] = exp(-2j * pi * f * t0 / n), so |y[f]| = 1 for every f.
Three checks, printed one by one:
1. CUDA error at the call or at synchronize.
2. x unchanged (the README says x is left unchanged): x must still hold exactly one non-zero element.
3. sampled output bins against the exact value (float64 on the host).
"""
import math
import sys
import torch
from kernels import get_kernel
N_CONTROL = 358_722_675 # 5^2 * 3^15 < 2^30 : every float index below 2^31
N_FAIL = 1_076_168_025 # 5^2 * 3^16 > 2^30 : float index 2 * t + 1 reaches 2^31
TOL = 1e-3 # fp32 error is about 1e-6 here
def run(fft, n: int) -> bool:
print(f"--- n = {n} (batch 1) ---")
t0 = n // 3 + 7
ok = True
try:
x = torch.zeros(1, n, 2, device="cuda", dtype=torch.float32)
x[0, t0, 0] = 1.0
y = fft.run(x)
torch.cuda.synchronize()
print("1. CUDA error: none")
except RuntimeError as e:
print("1. CUDA error:", str(e).splitlines()[0])
return False
nz = int(torch.count_nonzero(x).item())
same = nz == 1 and float(x[0, t0, 0].item()) == 1.0
print(f"2. x unchanged: {same} (non-zero elements of x: {nz}, expected 1)")
ok &= same
g = torch.Generator().manual_seed(0)
f = torch.cat([torch.randint(0, n, (4096,), generator=g),
torch.arange(0, 64), torch.arange(n - 64, n),
torch.arange((1 << 30) - 32, (1 << 30) + 32).clamp(max=n - 1)]).unique()
got = y[0, f.cuda()].double().cpu()
ph = [(-2.0 * math.pi * ((int(k) * t0) % n)) / n for k in f]
want = torch.tensor([[math.cos(p), math.sin(p)] for p in ph], dtype=torch.float64)
err = (got - want).norm(dim=1)
bad = int((err > TOL).sum().item())
print(f"3. sampled bins wrong: {bad} of {len(f)} (max error {float(err.max()):.3e}, tolerance {TOL})")
if bad:
i = int(err.argmax())
print(f" worst bin f = {int(f[i])}: got {got[i].tolist()}, exact {want[i].tolist()}")
ok &= bad == 0
del x, y
torch.cuda.empty_cache()
return ok
def main() -> int:
p = torch.cuda.get_device_properties(0)
print(f"device: {p.name}, capability {p.major}.{p.minor}, {p.total_memory / 2**30:.1f} GiB, torch {torch.__version__}")
fft = get_kernel("humanfia-lab/fft-c2c-1d", version=1, trust_remote_code=True)
c = run(fft, N_CONTROL)
print("control:", "PASS" if c else "FAIL (the reproducer itself is not valid on this setup)")
if "--control" in sys.argv:
return 0 if c else 2
w = run(fft, N_FAIL)
print("n = 1076168025:", "PASS (bug not reproduced)" if w else "FAIL (bug reproduced)")
return 0 if w else 1
if __name__ == "__main__":
sys.exit(main())