-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
alexcrichton
merged 9 commits into
bytecodealliance:main
from
alexcrichton:new-c-api-for-refs
Apr 24, 2024
Merged
c-api: Remove allocations from wasmtime_val_t
#8451
alexcrichton
merged 9 commits into
bytecodealliance:main
from
alexcrichton:new-c-api-for-refs
Apr 24, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
fitzgen
approved these changes
Apr 23, 2024
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); | ||
}; |
There was a problem hiding this comment.
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
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.
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
prtest:full
alexcrichton
force-pushed
the
new-c-api-for-refs
branch
from
April 24, 2024 20:05
658a02e
to
c59491d
Compare
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
Merged
2 tasks
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 aBox
. Wasmtime did not, however, provide the ability to deallocate just theBox
part. This was intended to be handled with unrooting APIs but unrooting requires awasmtime_context_t
parameter, meaning that destructors of types in other languages don't have a suitable means of managing the memory around thewasmtime_{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
matcheswasmtime::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.