Bug and fix: `core::fmt` hangs with formatting flags (`{:#x}`, `{:#o}`, `{:#b}`, etc.)
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 52/100
Direzione di ricerca
Inizia con compiler-builtins/src/int/bswap.rs nei tre percorsi elencati, quindi riproduci i casi di formattazione tramite run.sh e ispeziona la catena di chiamate generata. Verifica che le implementazioni di bswap non ricorsano più in rustc_codegen_gcc e che gli esempi di alternate, alignment e padding terminino senza bloccarsi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Symptoms
When using formatting flags with an alternate flag or alignment/padding, the program hangs, produces no output, and raises no exception:
println!("{:#x}", 42u32); // hangs
println!("{:#010x}", 42u32); // hangs
println!("{:>10}", 42u32); // hangs
println!("{:x}", 42u32); // works normally
println!("{}", 42u32); // works normally
Root Cause
bswap.rs in compiler_builtins uses #[maybe_use_optimized_c_shim] + x.swap_bytes():
rust
// Original code (problematic)
#[maybe_use_optimized_c_shim]
pub extern "C" fn __bswapsi2(x: u32) -> u32 {
x.swap_bytes() // ← rustc_codegen_gcc compiles this into a call to __bswapsi2 itself
}
rustc_codegen_gcc compiles u32::swap_bytes() into a function call to __bswapsi2, leading to:
text
__bswapsi2 → ___bswapsi2 → __bswapsi2 → ___bswapsi2 → ... (infinite recursion)
After stack overflow, the PC jumps to an invalid address (e.g., 0xfb079370), causing the program to hang.
Inside core::fmt, handling the alternate flag (#) calls pad_integral, which in turn triggers __bswapsi2, exposing this bug.
Debugging Process
QEMU -d exec revealed the program stuck at 0x0dfc (OpenRISC exception vector area)
Vector 0xC00 = syscall exception, triggered by l.sys 0x8060
GDB breakpoint at 0xC00, ppc = 0x26914, located in the __bswapsi2 call chain
Disassembly showed mutual recursion: __bswapsi2 → ___bswapsi2 → __bswapsi2
Files to Modify
The bswap.rs files in the following three paths have identical content (same md5) and all need modification:
text
~/or1k-build/rustc_codegen_gcc/build/build_sysroot/sysroot/lib/rustlib/src/rust/library/compiler-builtins/compiler-builtins/src/int/bswap.rs
~/or1k-build/rustc_codegen_gcc/build/build_sysroot/sysroot_src/library/compiler-builtins/compiler-builtins/src/int/bswap.rs
~/or1k-build/rust-src/library/compiler-builtins/compiler-builtins/src/int/bswap.rs
The actual path used by run.sh (with -Z build-std=core) is the stage1 sysroot path, which can be confirmed with:
bash
find ~/or1k-build/rust-src/build -name "bswap.rs" -path "*/compiler-builtins/*" 2>/dev/null
Fix
Replace x.swap_bytes() with pure shift operations to eliminate recursion:
bash
# Backup (all three paths have identical content, backup together)
find /home/openrisc/or1k-build -name "bswap.rs" -path "*/compiler-builtins/*" | \
while read f; do cp "$f" "${f}.bak"; echo "Backed up: $f"; done
# Modify (apply to all three paths)
find /home/openrisc/or1k-build -name "bswap.rs" -path "*/compiler-builtins/*" \
! -name "*.bak" | while read f; do
cat > "$f" << 'BSWAP_EOF'
intrinsics! {
pub extern "C" fn __bswapsi2(x: u32) -> u32 {
((x & 0x000000FFu32) << 24) |
((x & 0x0000FF00u32) << 8) |
((x & 0x00FF0000u32) >> 8) |
((x & 0xFF000000u32) >> 24)
}
pub extern "C" fn __bswapdi2(x: u64) -> u64 {
((x & 0x00000000000000FFu64) << 56) |
((x & 0x000000000000FF00u64) << 40) |
((x & 0x0000000000FF0000u64) << 24) |
((x & 0x00000000FF000000u64) << 8) |
((x & 0x000000FF00000000u64) >> 8) |
((x & 0x0000FF0000000000u64) >> 24) |
((x & 0x00FF000000000000u64) >> 40) |
((x & 0xFF00000000000000u64) >> 56)
}
pub extern "C" fn __bswapti2(x: u128) -> u128 {
let hi = ((x >> 64) & 0xFFFFFFFFFFFFFFFFu128) as u64;
let lo = (x & 0xFFFFFFFFFFFFFFFFu128) as u64;
let hi_swapped = __bswapdi2(hi) as u128;
let lo_swapped = __bswapdi2(lo) as u128;
(lo_swapped << 64) | hi_swapped
}
}
BSWAP_EOF
echo "Modified: $f"
done
# Clear compilation cache to trigger recompilation
find ~/riscion/sw/rust/testfmt/target -name "*compiler_builtins*" -delete
After modification, simply re-run run.sh. This fix must be reapplied after upgrading the nightly version.
Impact Scope
All code using core::fmt with alternate/alignment/padding formatting flags, including but not limited to:
{:#x}, {:#X}, {:#o}, {:#b}
{:#010x} and other width-specified alternate formats
{:>10}, {:<10}, {:^10} and other alignment formats
{:*^12}, {:0>8} and other custom padding formats
- Lingua principale
- Rust
- Stelle
- 1.2k
- Fork
- 105
- Merge medio
- 6h 28m
- PR unite (30g)
- 14
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
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 rust-lang/rustc_codegen_gcc
-
Re-enable -fno-lto testApertatests
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
rust-lang/rustc_codegen_gcc#993 ·
I maintainer di solito rispondono entro 1 giorno
-
tests
Difficoltà 4/5 3-5 giorni Idoneità per principianti 30/100
rust-lang/rustc_codegen_gcc#992 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
rust-lang/rustc_codegen_gcc#970 · 5 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Support `#[no_builtin]`Aperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 20/100
rust-lang/rustc_codegen_gcc#943 ·
I maintainer di solito rispondono entro 1 giorno
-
documentation
Difficoltà 3/5 1-2 giorni Idoneità per principianti 64/100
rust-lang/rustc_codegen_gcc#930 · 5 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di rust-lang/rustc_codegen_gcc
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
swc-project/swc#12516 ·
I maintainer di solito rispondono entro 1 giorno
-
mls-rs-crypto-awslc: exact aws-lc-rs =1.16.3 pin blocks rustls security updateForse già presa @muscariello l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
-
[work-item] adam-adae-worsening-emergence: conform spec.yaml to the specification style contract (REQ-1280)Forse già presa @muse-yamaa-bot l’ha presa oggi. Apertawork-item
Difficoltà 1/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
Possible memory leak: message iterators from pactffi_pact_handle_get_*message_iter are never deletedAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
pact-foundation/pact-go#652 ·
I maintainer di solito rispondono entro 1 giorno