10000 Allow multiple registration of same image in the GlobalRegistry by Milek7 · Pull Request #10874 · bytecodealliance/wasmtime · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Allow multiple registration of same image in the GlobalRegistry #10874

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Milek7
Copy link
Contributor
@Milek7 Milek7 commented May 30, 2025

With Module::deserialize_raw it is possible to create multiple CodeMemory objects that refer to the same underlying image. This is useful when embedder uses multiple Engine objects with different config. Currently this fails on assertion when trying to register same region of code second time in the global_code registry.

Global code registry stores Arc<CodeMemory>, but only needs to access trap data section in the image. Storing list of these in code registry would be silly, because on PC lookup one item would need to be picked arbitrarily, possibly different than the one which is currently executing, only to access trap_data field which must be identical for all items.

This commit modifies global registry to only store raw pointers to the trap_data section along with an usage count. Unregistration is moved into Drop guard to ensure that underlying data is not freed before unregistration is manually called.

With `Module::deserialize_raw` it is possible to create multiple `CodeMemory`
objects that refer to the same underlying image. This is useful when embedder
uses multiple `Engine` objects with different config. Currently this fails
on assertion when trying to register same region of code second time in the
`global_code` registry.

Global code registry stores `Arc<CodeMemory>`, but only needs to access trap
data section in the image. Storing list of these in code registry would be
silly, because on PC lookup one item would need to be picked arbitrarily,
possibly different than the one which is currently executing, only to access
`trap_data` field which must be identical for all items.

This commit modifies global registry to only store raw pointers to the
`trap_data` section along with an usage count. Unregistration is moved
into `Drop` guard to ensure that underlying data is not freed before
unregistration is manually called.
@Milek7 Milek7 requested a review from a team as a code owner May 30, 2025 19:54
@Milek7 Milek7 requested review from fitzgen and removed request for a team May 30, 2025 19:54
@fitzgen
Copy link
Member
fitzgen commented May 30, 2025

This is useful when embedder uses multiple Engine objects with different config.

If the engines have different configs, then the same compiled artifact will not be compatible with all engines, only the one using the config that it was compiled with.

@fitzgen
Copy link
Member
fitzgen commented May 30, 2025

(And if the engines all have the same config, then you might as well just use one engine.)

@Milek7
Copy link
Contributor Author
Milek7 commented May 30, 2025

I think it's not nice if it crashes on assert just because you used same image more than once. There's no such limitation for modules loaded from normal file. (and to be precise there are config settings that are not dependent on the artifact, like stack limits, memory allocators and few others)

@github-actions github-actions bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label May 30, 2025
@alexcrichton
Copy link
Member

One comment on this I've got is that we've generally been trying to reduce unsafe usage in Wasmtime wherever possible, so if this is added it'd be great to do it without increasing the amount of unsafe. Additionally can you ensure there's a test for this? Otherwise it's pretty likely functionality is lost in a future refactoring.

@Milek7
Copy link
Contributor Author
Milek7 commented Jun 6, 2025

I think avoiding unsafe would require storing registry as BTreeMap<usize, Vec<Arc<CodeMemory>>>, would that be preferable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Assignees
No one assigned
Labels
wasmtime:api Related to the API of the `wasmtime` crate itself
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0