bytecodealliance / bytecodealliance/wit-bindgen

Resource list leak in C bindings

Offen
#1,401 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
gen-c
Vorherrschende Sprache
Rust
Sterne
1.5k
Forks
286
Ø Merge
6 Std. 32 Min.
Gemergte PRs (30 T.)
19

Beschreibung

### 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
```

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Beginne in crates/c/src/lib.rs, wo der C-Generator die *_list_free-Hilfsfunktionen erzeugt, und untersuche anschließend den resource_expected_lists-Laufzeittest sowie seine runner.c-Assertions. Führe den bereitgestellten cargo run-Testbefehl mit den aufgeführten Rust- und C-Sprachen aus. Erledigt bedeutet, dass besitzende Ressourcen-Handles in Listen gedroppt werden und der erwartete Dropped Count des Tests erfüllt ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
c, rust
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
58/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.