macOS arm64: std::mem::alloc fails in optimized builds (arm64 syscall wrappers do not declare x1 as clobbered)
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 78/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- rust
- Ambito
- operating-systems
Direzione di ricerca
Inizia con std/sys/macos/arm64/syscall.wave e confronta i suoi wrapper di syscall con quelli di std/sys/macos/amd64/syscall.wave, citati nell’issue. Dichiara x1 come output in syscall0 fino a syscall6, quindi verifica le build ottimizzate di macOS arm64 con il reproducer di allocazione fornito, a -O1 o superiore; il lavoro è completato quando le allocazioni riescono senza corrompere lo stato. L’issue indica anche altri wrapper Darwin arm64 per un possibile audit successivo.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
On macOS arm64, std::mem::alloc fails (returns null) or corrupts state in optimized builds (-O1 and up), because the arm64 syscall wrappers do not declare that XNU overwrites x1 with the secondary return value. The x86_64 wrappers already handle the same thing for rdx (see the comment in std/sys/macos/amd64/syscall.wave).
Repro
import("std::mem::alloc")::{mem_alloc, mem_free};
extern(c, "clock") fun c_clock() -> i64;
fun main() -> i32 {
// A size only known at run time, used for three allocations in a row.
var n: i64 = 1520 + (c_clock() % 2);
var a: ptr<u8> = mem_alloc(n * 8);
var b: ptr<u8> = mem_alloc(n * 8);
var c: ptr<u8> = mem_alloc((n + 1) * 8);
if (a == null || b == null || c == null) {
println("out of memory for {} entries", n);
return 1;
}
println("ok {}", n);
mem_free(a, n * 8);
mem_free(b, n * 8);
mem_free(c, (n + 1) * 8);
return 0;
}
Result on a GitHub macos-26 (Apple Silicon) runner, wavec 0.2.1-pre-beta, aarch64-apple-darwin release:
| flags | result |
|---|---|
-O0 |
ok 1520 |
-O1, -O2, -O3 |
out of memory for 1520 entries (exit 1) |
I did not run this exact program elsewhere, but the larger programs below build with -O2 and pass on macos-26-intel (x86_64), Linux x64 and Linux arm64. In those larger programs (the examples of https://github.com/potatosl/wjson) the same bug showed up as null from mem_alloc in a CSV example at -O1 and above, and as a segmentation fault (exit 139) in a JSON editing example at -O2 and above. All of them pass at -O0.
Cause (my reading, confirmed by the fix below)
std/sys/macos/arm64/syscall.wave declares the inputs x16, x0..x5 and the output x0, and nothing else. XNU's arm64 syscall return writes x1 as well. With inlining, the compiler assumes x1 still holds the second argument (here the mmap length) after the svc, reuses that value for the next call, and the next mmap gets length 0 or garbage and fails. std/sys/macos/amd64/syscall.wave has this comment for rdx and declares out("rdx") secondary in every wrapper.
Fix
Declare x1 as an output in every arm64 wrapper (syscall0 .. syscall6):
pub fun syscall2(id: i64, a1: i64, a2: i64) -> i64 {
var ret: i64;
var secondary: i64;
asm {
"svc #0x80\ncneg x0, x0, cs"
in("x16") id
in("x0") a1
in("x1") a2
out("x0") ret
out("x1") secondary
}
return ret;
}
Verified on the same runner by patching all 7 wrappers in a copy of std and passing it with --std-root:
| flags | result |
|---|---|
-O1, -O2, -O3 |
ok 1520 / ok 1521, exit 0 |
The two larger examples (edit_config: segfault, csv2json: out of memory at -O1/-O2/-O3 with the stock std) also pass at -O2 and -O3 with the patched std.
Other wrappers that use the same raw-syscall pattern on Darwin arm64 (fs.wave, process.wave, time.wave, ...) may have the same gap; I only checked memory. Related audit: #434.
- Lingua principale
- Rust
- Stelle
- 56
- Fork
- 21
- Merge medio
- 3h 46m
- PR unite (30g)
- 47
Preparare l'ambiente
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di wavefnd/Wave
-
enhancement good first issue needs testing
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
bug good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
bug good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
documentation enhancement good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
enhancement good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di wavefnd/Wave
Issue simili
-
The book has a Nix install section that chelis.ch lacks, and it names two of the three systemsApertaarea:ecosystem documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
Chelis-Lang/chelis#3866 ·
I maintainer di solito rispondono entro 1 giorno
-
bug documentation sev-0
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
dekobon/big-code-analysis#1620 ·
I maintainer di solito rispondono entro 3 giorni
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100