alexcrichton opened PR #11799 from alexcrichton:fix-c-api-onwership to bytecodealliance:main:
This commit is a follow-up to #11514 which was discovered through failing tests in the wasmtime-py repository when updating to Wasmtime 37.0.0. Effectively a combination of bugs in the Rust API meant that it wasn't possible to use
externreforanyrefbindings correctly. The Rust changes in this commit are:
wasmtime_val_unrootcorrectly drops the value now as opposed to effectively being a noop from before (typo of usingas_externrefvsfrom_externref).wasmtime_{anyref,externref,val}_tnow have aDropimplementation in Rust to correctly drop them if a value in Rust is dropped. This is required to correctly manage memory in thewasmtime_func_{call,new}implementations, for example.wasmtime_{anyref,externref,val}_cloneno longer have an unnecessary context parameter.wasmtime_{anyref,externref,val}_unrootno longer have an unnecessary context parameter.Changes in the C/C++ APIs are:
Result::{ok,err}_refAPIs were added in addition to the preexisting rvalue accessors.- Loading/storing typed arguments now has an overload for
const T&andT&&which behaves differently. Notably transferring ownership forT&&and not forconst T&. This means that passing parameters when calling a wasm function usesconst T&, but passing results from a host import usesT&&.TypedFunc::callnow usesconst Params&instead ofParamsto explicitly specify it doesn't modify the parameters and forces using theconst T&store method.Store::gcis now a convenience method forstore.context().gc()ExternRef,AnyRef, andValnow have ownership semantics and destructors. This matches the spirit of #11514 for Rust but models it in C++ as well. This required filling out move/copy constructors/assignments.- The explicit
ExternRefnow takesstd::anyinstead ofT.- Minor issues related to ownership are fixed in
Valbindings.Valgrind was used to ensure that there were no leaks for the test suite which additionally resulted in a number of
*_deletecalls being added to tests using the C API (accidental omissions).The original goal of this change was to be a patch release for 37.0.1 to enable updating wasmtime-py to the 37.0.x releases of Wasmtime. In the end though the changes here were broad enough that I no longer feel that this is a good idea, so wasmtime-py will be skipping the 37 version of Wasmtime.
<!--
Please make sure you include the following information:
If this work has been discussed elsewhere, please include a link to that
conversation. If it was discussed in an issue, just mention "issue #...".Explain why this change is needed. If the details are in an issue already,
this can be brief.Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.htmlPlease ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->
alexcrichton requested wasmtime-core-reviewers for a review on PR #11799.
alexcrichton requested pchickey for a review on PR #11799.
alexcrichton updated PR #11799.
alexcrichton has enabled auto merge for PR #11799.
alexcrichton commented on PR #11799:
Once this lands on main I'll backport the merged commit to the release-38.0.0 branch as well
cfallin submitted PR review:
Carrying over my review from the GHSA -- thanks again for catching this and fixing it!
alexcrichton merged PR #11799.
Last updated: Dec 06 2025 at 07:03 UTC