runtime: check for overflow when growing slices and concatenating strings

Aperta
#6,174 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

  • #6189 di @davecheney — chiusa senza merge

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go, wasm

Direzione di ricerca

Start by reading runtime.sliceGrow and runtime.stringConcat, then trace how they calculate capacities, lengths, and allocation sizes. Reproduce the slice case with the supplied program on a 32-bit target; add coverage for capacity and allocation overflow, plus string length overflow. Done when capacities and allocation sizes stay within int limits and oversized concatenations are rejected.

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

Descrizione

runtime.sliceGrow and runtime.stringConcat do not check their size calculations. On 32-bit targets, these calculations can produce lengths or capacities that do not fit in an int, or allocation sizes that wrap around.

For strings and slices with non-zero-sized elements, the issue is unlikely on 64-bit targets because the required allocations exceed the typical 48-bit address space. Slices of zero-sized elements are an exception: they can reach the capacity limit without a large allocation.

Slice growth

sliceGrow rounds the requested capacity up to the next power of two. On a 32-bit target, a request for a capacity of 1 << 30 is rounded up to 1 << 31, which does not fit in an int. The allocation size can also wrap around when the capacity is multiplied by the element size, resulting in an allocation that is too small.

This program reproduces the capacity error without a large allocation:

package main

func main() {
	n := int(^uint(0)>>2) + 1
	s := make([]struct{}, n)
	s = append(s, struct{}{})
	println(len(s), cap(s))
}

On a 32-bit target, tinygo run -target=wasm main.go prints:

1073741825 -2147483648

The capacity is negative, but it should be at least the length. The same program also produces a negative capacity on a 64-bit target.

String concatenation

stringConcat adds the lengths of its arguments without checking the result. On a 32-bit target, concatenating two strings of 1 GiB each can produce a length of 1 << 31, which does not fit in an int.

Expected behavior

sliceGrow should keep capacities within the range of int and check the allocation size for overflow. stringConcat should reject a result whose length does not fit in an int.

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.