UBSAN: left shifts into the sign bit in unaligned-read helpers

Aperta
#2,445 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
73/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c
Ambito
backend

Direzione di ricerca

Start with the listed shift expressions in src/libAtomVM/bitstring.h, src/libAtomVM/utils.h, src/libAtomVM/opcodesswitch.h, and src/libAtomVM/inet.c; read how each helper builds its integer value. Run the test suite with -fsanitize=undefined to reproduce the reports, then rerun it to confirm they are gone after the shifts are made defined. Done means all listed cases pass without UBSAN reports.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Running the test suite with -fsanitize=undefined reports "left shift of N by 24 places cannot be represented in type 'int'" (and by 56 places into 'long') from:

  • READ_32LE_UNALIGNED in src/libAtomVM/bitstring.h:45 and READ_32_UNALIGNED in src/libAtomVM/utils.h:57, :85 and :138: ((uint8_t *) ptr)[3] << 24 promotes the byte to int before shifting, so any byte >= 0x80 overflows.
  • large_integer_to_term in src/libAtomVM/opcodesswitch.h:1339 and :1378: ((int32_t) compact_term[0]) << 24 and ((int64_t) compact_term[0]) << 56.
  • src/libAtomVM/inet.c:66: (term_to_int32(...) & 0xFF) << 24.

The generated code does what is intended today, but it is undefined behaviour and a compiler may legitimately miscompile it. Casting each byte to uint32_t/uint64_t before the shift (and converting to the signed type afterwards) fixes all of them.

Lingua principale
Erlang
Stelle
2.3k
Fork
171
Merge medio
5g 11h
PR unite (30g)
19

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di atomvm/AtomVM

Tutte le issue di atomvm/AtomVM

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.