[BUG] device_info() buffers are 64 bytes; CUDA backend writes 257, corrupting the stack
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 76/100
Línea de trabajo
Comienza en src/core/device.rs:109-121 e inspecciona los búferes que se pasan a device_info(); compáralos con el contrato documentado y con el comportamiento de CUDA descrito en src/backend/cuda/platform.cpp:290-307. Ejecuta examples/helloworld.rs con el backend de CUDA para confirmar que la llamada es segura, y considera completado el issue cuando device_info() ya no permita que el backend de CUDA sobrescriba la pila de quien lo llama.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Description
device_info() allocates the buffer sizes recommended by the ArrayFire documentation, but the CUDA backend writes up to 257 bytes into the first one. On the CUDA backend this corrupts up to 193 bytes of the caller's stack on every call.
This is a plausible root cause for several long-standing crash reports here: #106, #285, #311.
pub fn device_info() -> (String, String, String, String) {
let mut name: [c_char; 64] = [0; 64];
let mut platform: [c_char; 10] = [0; 10];
let mut toolkit: [c_char; 64] = [0; 64];
let mut compute: [c_char; 10] = [0; 10];
unsafe {
let err_val = af_device_info(
&mut name[0], &mut platform[0], &mut toolkit[0], &mut compute[0],
);
These sizes match ArrayFire's documented contract exactly (docs/details/device.dox:10-16, "Recommended minimum size is 64 / 10 / 64 / 10" across the four params), and af_device_info takes no length arguments, so there is nothing else to go on.
The CUDA backend does not honour it (src/backend/cuda/platform.cpp:290-307, identical from 3.8.0 through master):
snprintf(d_name, 256, "%s", dev.name);
// Sanitize input
for (int i = 0; i < 256; i++) {
if (d_name[i] == ' ') { // reads d_name[0..256]
if (d_name[i + 1] == 0 || d_name[i + 1] == ' ') {
d_name[i] = 0; // writes d_name[0..255]
} else {
d_name[i] = '_';
}
}
}
The sanitize loop does not stop at the NUL terminator, so it runs the full 256 iterations regardless of the actual device-name length, writing 0x00 or '_' wherever it reads a 0x20 byte. The overflow therefore is not conditional on having a long GPU name — it happens on every call.
The CPU and OpenCL backends are correct here (snprintf(..., 64, ...), and OpenCL bounds its sanitize loop at i < 31), so this affects the CUDA backend only — which matches the reported pattern of crashes that disappear when users switch to CPU or OpenCL.
I've filed the backend-side bug upstream as arrayfire/arrayfire#<CPP_ISSUE>.
Impact
Writing ~193 bytes past a stack array clobbers the other three buffers, spilled registers, the /GS cookie and the return address. On Windows that surfaces as STATUS_ACCESS_VIOLATION (0xC0000005) or STATUS_STACK_BUFFER_OVERRUN (0xC0000409).
The corruption is deterministic but the crash is intermittent, because whether it is fatal depends on which stack bytes happen to contain 0x20 and on the frame layout rustc chose. That also gives a mechanism for #299 (crash only at opt-level = 3): more aggressive inlining means less dead stack padding to absorb the overflow.
examples/helloworld.rs:9 calls device_info(), so this is in the first program a new user runs.
Reproducible Code and/or Steps
see above
System Information
general code issue, system independent
Checklist
- Using the latest available ArrayFire release
- GPU drivers are up to date
Suggested fix
Independent of any upstream change, and safe against every 3.8.x:
let mut name: [c_char; 1024] = [0; 1024];
(or at least 257)
Happy to open a PR if that's useful.
Disclaimer
Found by Claude Opus 5 while investigating intermittent 0xC0000005 errors on Windows/CUDA. I checked this manually and it seems like a real bug.
- Lenguaje dominante
- Rust
- Estrellas
- 826
- Forks
- 60
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Sin guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de arrayfire/arrayfire-rust
-
Bug
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
arrayfire/arrayfire-rust#388 ·
-
Bug
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
arrayfire/arrayfire-rust#387 ·
-
[BUG] `DType::from` rejects `S16`, `U16` and `F16`Posiblemente ocupada @chiliec la tomó hace 46 días. AbiertoBug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
arrayfire/arrayfire-rust#386 ·
-
Bug
Dificultad 5/5 Más de una semana Aptitud para principiantes 35/100
arrayfire/arrayfire-rust#385 · 1 comentario ·
-
[BUG] opencl-interop `get_device_type` always returns `DeviceType::ALL`Posiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. AbiertoBug
Dificultad 3/5 1-2 días Aptitud para principiantes 45/100
arrayfire/arrayfire-rust#382 ·
Todos los issues de arrayfire/arrayfire-rust
Issues similares
-
app bug session
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Los mantenedores suelen responder en 1 día
-
marketing
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 1/5 Menos de una hora Aptitud para principiantes 92/100
Los mantenedores suelen responder en 1 día
-
macOS autosubmit enable passes unsupported --wait to launchctl bootoutPosiblemente ocupada @iqdoctor la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
junhoyeo/tokscale#1713 · 3 comentarios ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
Los mantenedores suelen responder en 1 día