Skip to content
New issue

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

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

Already on GitHub? # to your account

Add an unsafe API to unload process trap handlers #9022

Merged
merged 4 commits into from
Jul 29, 2024

Conversation

alexcrichton
Copy link
Member

This commit adds a new unsafe API Engine::unload_process_handlers which can be used to de-initialize trap-handling state in a process. In general this is an unsafe operation that we have no means of making safe, hence the unsafe. There are particular situations where this can be done safely though and this is provided for such situations. The safety conditions rely on invariants that Wasmtime itself cannot maintain so the burden is on callers to ensure that this is invoked in a safe manner.

This is inspired by discussion on Zulip where Wasmtime's trap handling infrastructure prevented unloading a DLL with Wasmtime in it. There's possibly other reasons that unloading DLLs are unsafe in Rust but it feels appropriate to at least provide this knob to embedders to be able to turn if they like.

This commit adds a new unsafe API `Engine::unload_process_handlers`
which can be used to de-initialize trap-handling state in a process. In
general this is an unsafe operation that we have no means of making
safe, hence the `unsafe`. There are particular situations where this can
be done safely though and this is provided for such situations. The
safety conditions rely on invariants that Wasmtime itself cannot
maintain so the burden is on callers to ensure that this is invoked in a
safe manner.

This is inspired by discussion [on Zulip][zulip] where Wasmtime's trap
handling infrastructure prevented unloading a DLL with Wasmtime in it.
There's possibly other reasons that unloading DLLs are unsafe in Rust
but it feels appropriate to at least provide this knob to embedders to
be able to turn if they like.

[zulip]: https://bytecodealliance.zulipchat.com/#narrow/stream/206238-general/topic/How.20to.20safely.20drop.20wasmtime.3F/near/453366905
@alexcrichton alexcrichton requested a review from a team as a code owner July 26, 2024 16:29
@alexcrichton alexcrichton requested review from fitzgen and removed request for a team July 26, 2024 16:29
Copy link
Contributor

@lann lann left a comment

Choose a reason for hiding this comment

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

Given how specialized this is, consider all the functional comments just questions.

Comment on lines +737 to +740
/// * All existing threads which have used this DLL or copy of Wasmtime may
/// no longer use this copy of Wasmtime. Per-thread state is not iterated
/// and destroyed. Only future threads may use future instances of this
/// Wasmtime itself.
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to explicitly poison this copy of wasmtime with a global or is that already effectively happening?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm hesitant to go out of our way to do that as it might slow down other parts for this niche unsafe part, so I'd lean towards making this part of the unsafe contract rather than trying to add more guard rails.

@alexcrichton
Copy link
Member Author

Good suggestions/questions! I've tried to wordsmith and update a bit.

@github-actions github-actions bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Jul 26, 2024
Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
@alexcrichton alexcrichton enabled auto-merge July 29, 2024 14:20
@alexcrichton alexcrichton added this pull request to the merge queue Jul 29, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jul 29, 2024
@alexcrichton alexcrichton enabled auto-merge July 29, 2024 14:46
@alexcrichton alexcrichton added this pull request to the merge queue Jul 29, 2024
Merged via the queue into bytecodealliance:main with commit b6da82b Jul 29, 2024
38 checks passed
@alexcrichton alexcrichton deleted the deinit-traps branch July 29, 2024 15:10
# for free to join this conversation on GitHub. Already have an account? # to comment
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