wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

Resource list leak in C bindings

Open kavoc-brown opened this issue 2 months ago • 1 comments

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) and wasm32-wasip2 Rust target (rustup target add wasm32-wasip2).
  3. Download and extract PoC.tar.gz.
    tar -xzvf PoC.tar.gz
    
  4. Run test.
    cargo run test \
      --languages rust,c \
      --artifacts target/artifacts \
      --rust-wit-bindgen-path ./crates/guest-rust \
      tests/runtime/resource_expected_lists
    
  5. Result:
------ 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/[email protected]#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

kavoc-brown avatar Oct 20 '25 15:10 kavoc-brown

It seems that the previous download link was private. Sorry about that.

I've re-uploaded the relevant files to Gist: https://gist.github.com/kavoc-brown/7da8c34c13fbdf9b3ffb6f1681df55df

Please download the ZIP file (use the “Download ZIP” option at the top right) and extract its contents to tests/runtime/resource_expected_lists, then follow the same steps as before.

kavoc-brown avatar Oct 27 '25 12:10 kavoc-brown