bytecodealliance / bytecodealliance/wit-bindgen
Resource list leak in C bindings
- Lingua principale
- Rust
- Stelle
- 1.5k
- Fork
- 286
- Merge medio
- 6h 32m
- PR unite (30g)
- 19
Descrizione
### Summary
C bindings generated by wit-bindgen 0.46.0 never call the resource drop helper when freeing a list.
### Details
The C generator (`crates/c/src/lib.rs`) walks each list element when emitting `*_list_free` helpers. For plain records and variants it looks up the destructor function and calls it. When the element type is `own resource` the generator also performs a lookup, but it *stops there*: it never calls the `[resource-drop]` intrinsic for each handle. The resulting C helper loop has an empty body, so the runtime simply releases the list buffer without dropping any of the owned handles.
### PoC
1. Clone wit-bindgen 0.46.0.
2. Install wasi-sdk-27 toolchain ([link](https://github.com/WebAssembly/wasi-sdk)) and wasm32-wasip2 Rust target (`rustup target add wasm32-wasip2`).
3. Download and extract [PoC.tar.gz](https://github.com/user-attachments/files/22989663/PoC.tar.gz).
```bash
tar -xzvf PoC.tar.gz
```
5. Run test.
```bash
cargo run test \
--languages rust,c \
--artifacts target/artifacts \
--rust-wit-bindgen-path ./crates/guest-rust \
tests/runtime/resource_expected_lists
```
6. Result:
```text
------ Failure: resource_expected_lists --------
runner: tests/runtime/resource_expected_lists/runner.c
compiled runner: /tmp/reproduce/wit-bindgen/target/artifacts/resource_expected_lists/runner-c.wasm
test: tests/runtime/resource_expected_lists/test.rs
compiled test: /tmp/reproduce/wit-bindgen/target/artifacts/resource_expected_lists/test-rust.wasm
error: failed to run `resource_expected_lists`
Caused by:
command execution failed
command: "wasmtime" "/tmp/reproduce/wit-bindgen/target/artifacts/resource_expected_lists/composed-runner.c-test.rs.wasm"
status: exit status: 134
stderr:
Assertion failed: stats.dropped == expected_dropped && "dropped mismatch" (tests/runtime/resource_expected_lists/runner.c: assert_stats: 11)
Error: failed to run main module `/tmp/reproduce/wit-bindgen/target/artifacts/resource_expected_lists/composed-runner.c-test.rs.wasm`
Caused by:
0: failed to invoke `run` function
1: error while executing at wasm backtrace:
0: 0x49e6 - runner-c.wasm!abort
1: 0x4a7d - runner-c.wasm!__assert_fail
2: 0x192f - runner-c.wasm!assert_stats
3: 0x17dc - runner-c.wasm!__original_main
4: 0x1769 - runner-c.wasm!_start
5: 0x3abba - wit-component:adapter:wasi_snapshot_preview1!wasi:cli/run@0.2.3#run
note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information
2: wasm trap: wasm `unreachable` instruction executed
1 tests FAILED
```
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Direzione di ricerca
Inizia da crates/c/src/lib.rs, dove il generatore C emette gli helper *_list_free, poi esamina il test di runtime resource_expected_lists e le relative asserzioni in runner.c. Esegui il comando di test cargo run fornito con i linguaggi Rust e C elencati. Il lavoro è completato quando gli handle delle risorse posseduti nelle liste vengono droppati e il conteggio atteso delle risorse droppate dal test passa.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- c, rust
- Ambito
- tooling
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 58/100