Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c-api: Remove allocations from wasmtime_val_t #8451

Merged
merged 9 commits into from
Apr 24, 2024

Conversation

alexcrichton
Copy link
Member

This commit redesigns how GC references work in the C API. previously wasmtime_{any,extern}ref_t were both opaque pointers in the C API represented as a Box. Wasmtime did not, however, provide the ability to deallocate just the Box part. This was intended to be handled with unrooting APIs but unrooting requires a wasmtime_context_t parameter, meaning that destructors of types in other languages don't have a suitable means of managing the memory around the
wasmtime_{any,extern}ref_t which might lead to leaks.

This PR takes an alternate approach for the representation of these types in the C API. Instead of being an opaque pointer they're now actual structures with definitions in the header file. These structures mirror the internals in Rust and Rust is tagged to ensure that changes are reflected in the C API as well. This is similar to how wasmtime_func_t matches wasmtime::Func. This enables embedders to not need to worry about memory management of these values outside of the manual rooting otherwise required.

The hope is that this will reduce the likelihood of memory leaks and otherwise not make it any harder to manage references in the C API.

This commit redesigns how GC references work in the C API. previously
`wasmtime_{any,extern}ref_t` were both opaque pointers in the C API
represented as a `Box`. Wasmtime did not, however, provide the ability
to deallocate just the `Box` part. This was intended to be handled with
unrooting APIs but unrooting requires a `wasmtime_context_t` parameter,
meaning that destructors of types in other languages don't have a
suitable means of managing the memory around the
`wasmtime_{any,extern}ref_t` which might lead to leaks.

This PR takes an alternate approach for the representation of these
types in the C API. Instead of being an opaque pointer they're now
actual structures with definitions in the header file. These structures
mirror the internals in Rust and Rust is tagged to ensure that changes
are reflected in the C API as well. This is similar to how
`wasmtime_func_t` matches `wasmtime::Func`. This enables embedders to
not need to worry about memory management of these values outside of the
manual rooting otherwise required.

The hope is that this will reduce the likelihood of memory leaks and
otherwise not make it any harder to manage references in the C API.
@alexcrichton alexcrichton marked this pull request as ready for review April 23, 2024 22:06
@alexcrichton alexcrichton requested a review from a team as a code owner April 23, 2024 22:06
@alexcrichton alexcrichton requested review from fitzgen and removed request for a team April 23, 2024 22:06
crates/c-api/src/ref.rs Outdated Show resolved Hide resolved
Comment on lines +196 to +200
const _: () = {
// NB: these match the C API which should also be updated if this changes
assert!(std::mem::size_of::<GcRootIndex>() == 16);
assert!(std::mem::align_of::<GcRootIndex>() == 8);
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe these asserts should be for ManuallyRooted rather than GcRootIndex, just so that if we add a field to the former, but leave the latter as-is, we get build errors because that mismatch would introduce unsafety.

@github-actions github-actions bot added wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:c-api Issues pertaining to the C API. labels Apr 23, 2024
alexcrichton added a commit to alexcrichton/wasmtime that referenced this pull request Apr 24, 2024
Try to avoid using a `transmute` in the implementation of the C API and
instead use a `union` like is being done in bytecodealliance#8451. Additionally add some
helper functions to the header to work with null funcref values instead
of only documenting the implementation.
@alexcrichton alexcrichton added this pull request to the merge queue Apr 24, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 24, 2024
github-merge-queue bot pushed a commit that referenced this pull request Apr 24, 2024
* c-api: Tidy up some `wasmtime_func_t` usage

Try to avoid using a `transmute` in the implementation of the C API and
instead use a `union` like is being done in #8451. Additionally add some
helper functions to the header to work with null funcref values instead
of only documenting the implementation.

* Try to fix doxygen
@alexcrichton alexcrichton added this pull request to the merge queue Apr 24, 2024
prtest:full
@alexcrichton alexcrichton removed this pull request from the merge queue due to a manual request Apr 24, 2024
@alexcrichton alexcrichton added this pull request to the merge queue Apr 24, 2024
Merged via the queue into bytecodealliance:main with commit 77405cc Apr 24, 2024
48 checks passed
@alexcrichton alexcrichton deleted the new-c-api-for-refs branch April 24, 2024 22:09
kpreisser added a commit to kpreisser/wasmtime-dotnet that referenced this pull request Jun 24, 2024
This includes updates for:
- bytecodealliance/wasmtime#8451
- bytecodealliance/wasmtime#8461
- bytecodealliance/wasmtime#8011

TODOs:
- Allocating an `externref` can now fail (by `wasmtime_externref_new` returning `false`). Currently, we throw a `WasmtimeException` in that case. We need to check where that exception can be thrown, and whether we need to do any additional clean-up (e.g. when converting arguments for a function call).
- Check whether it's ok to compare the `__private` field of externs (which has been remaned in the C API, previously it was `index`).
- `anyref` type is not yet supported, but I'm not sure what exactly it is and whether we need to add it.

Fixes bytecodealliance#315
jsturtevant pushed a commit to bytecodealliance/wasmtime-dotnet that referenced this pull request Jun 28, 2024
* Update to recent Wasmtime C API changes regarding values.

This includes updates for:
- bytecodealliance/wasmtime#8451
- bytecodealliance/wasmtime#8461
- bytecodealliance/wasmtime#8011

TODOs:
- Allocating an `externref` can now fail (by `wasmtime_externref_new` returning `false`). Currently, we throw a `WasmtimeException` in that case. We need to check where that exception can be thrown, and whether we need to do any additional clean-up (e.g. when converting arguments for a function call).
- Check whether it's ok to compare the `__private` field of externs (which has been remaned in the C API, previously it was `index`).
- `anyref` type is not yet supported, but I'm not sure what exactly it is and whether we need to add it.

Fixes #315

* Follow-Up: Make fields private.

* Ensure to clean-up `Value` instances (in arguments for a function call, and in results for an untyped callback) when e.g. allocating an `externref` fails.

We don't need to do such a clean-up for unchecked function calls that use `ValueRaw` because in that case we don't own `externref` values.

* Avoid accessing the `__private` fields in tests by checking the whole struct for equality (which is the case when all members are equal).

* Use separate dictionaries for caching `Function`, `Memory`, and `Global` objects in the `Store`, which avoids having to explicitly accessing the `__private` field (because the whole struct is now compared).

Additionally, it is more type-safe (since we don't need to cast the `object`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:c-api Issues pertaining to the C API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants