bytecodealliance / bytecodealliance/wit-bindgen

Resource list leak in C bindings

Open
#1,401 1 comment 0 reactions 0 assignees View on GitHub
gen-c
Dominant language
Rust
Stars
1.5k
Forks
286
Avg merge
6h 32m
Merged PRs (30d)
19

Description

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.