compiler: zero-sized slice lengths truncate to 16 bits on AVR

Aperta
#6,175 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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
54/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
arduino, go

Direzione di ricerca

Start with the AVR slice length conversion in compiler/compiler.go, then trace sliceAppend and sliceGrow in src/runtime/slice.go; issue #6174 covers related overflow behavior on other targets. Reproduce the listed zero-sized slice cases on arduino-uno under qemu-system-avr. Done means the reported lengths and capacities are preserved or rejected with a compile error, and the runtime cases no longer wrap or produce invalid capacities.

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

Descrizione

On AVR, int is 32 bits but uintptr is 16 bits. Slice headers store len and cap as uintptr. A slice of zero-sized elements needs no memory, so its length can exceed 65535 on AVR. Some paths truncate these lengths and do not report an error.

Results on arduino-uno under qemu-system-avr -M uno:

Program Result Expected
var a [65536]struct{}; s := a[:] len 0, cap 0 len 65536, cap 65536
make([]struct{}, 65536) with a constant length len 0, cap 0 len 65536, cap 65536, or a compile error
make([]struct{}, n) with n 65536 at run time panic: slice out of range same as the constant case
s := make([]struct{}, 32768); s = append(s, struct{}{}) len 32769, cap 0 cap >= len
s := make([]struct{}, 65535); s = append(s, struct{}{}) len 0, cap 65535 len 65536

The array conversion creates the length with llvm.ConstInt(b.uintptrType, uint64(length), false) in compiler/compiler.go. This drops the upper bits. The constant make also truncates its length, but the run-time make checks it. The two append results come from sliceAppend and sliceGrow in src/runtime/slice.go. #6174 covers overflow in these functions on other targets.

unsafe.Slice and reflect.MakeSlice reject these lengths, so they do not wrap. A suspected index bounds problem did not occur in QEMU: a dynamic a[65535] on a [65536]struct{} array succeeded and a[65536] panicked.

Lingua principale
Go
Stelle
17.8k
Fork
1.1k
Merge medio
1g 4h
PR unite (30g)
380

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

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 tinygo-org/tinygo

Tutte le issue di tinygo-org/tinygo

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.