Skip to content

fix(no_std)!: respect std feature when target is windows/unix #11152

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

Merged
merged 12 commits into from
Jul 3, 2025

Conversation

salmans
Copy link
Contributor

@salmans salmans commented Jun 27, 2025

This PR fixes feature flag handling to properly respect the std feature when targeting Windows/Unix platforms.

Breaking Change: This fix will disable standard library features for dependents that use wasmtime with default-features = false.

Changes:

  • Updated feature dependencies in Cargo.toml files to conditionally enable std-dependent features
  • Added proper feature gating in runtime modules for no_std compatibility
  • Modified VM system module to respect the std feature flag on Windows/Unix targets

The fix ensures that when wasmtime is used without default features, it properly builds in no_std on all supported platforms.

BREAKING CHANGE: the fix disables standard features for dependents that use wasmtime with `default-features = false`.
@alexcrichton
Copy link
Member

Thanks for the PR! Could you elaborate on the motivation for this change as well? For example I'm not aware of a target that's cfg(unix) or cfg(windows) and doesn't have std.

@salmans
Copy link
Contributor Author

salmans commented Jun 27, 2025

@alexcrichton I’m experimenting with running Wasmtime (with Pulley) in the Windows kernel. The goal is to use WebAssembly to enable limited changes to kernel logic without requiring a full software update or system restart. I’m exploring Wasm as a potential alternative to eBPF for Windows.

@salmans salmans marked this pull request as ready for review June 27, 2025 22:42
@salmans salmans requested review from a team as code owners June 27, 2025 22:42
@salmans salmans requested review from fitzgen and removed request for a team June 27, 2025 22:42
@github-actions github-actions bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Jun 28, 2025
@alexcrichton
Copy link
Member

Oh nice! Do you know if there's a #[cfg] specific to the target that you're working with that this could be keyed on instead of feature = "std"? While the breakage here isn't the end of the world and something we could document, I'd prefer to shape the change a bit differently. For example there's no testing on CI that a no_std build works on Linux/Windows so this is something that will likely easily regress over time.

@salmans
Copy link
Contributor Author

salmans commented Jun 30, 2025

No, I don't think there's any specific target for (windows/unix) kernel. But just to clarify, are you suggesting that turning off std should still pull in standard library to avoid issues like the ones with CI? If so, an alternative solution that I can think of is to guard no_std with a new flag like override-std or force-no-std.

Fill out the `compile_error!` to avoid failing a build.
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Sorry I can be more clear, my concern was a big vague there. I originally had the gut reaction of "this feels weird" but I'm effectively getting over that gut reaction. There's still changes I'd like to see in this PR, but I can try to be more clear about them. I've left various review comments below.

Don't require `std` feature for invalid faults
@alexcrichton alexcrichton requested review from alexcrichton and removed request for fitzgen June 30, 2025 20:50
@salmans
Copy link
Contributor Author

salmans commented Jul 1, 2025

@alexcrichton after more experimentation on Windows, I realized win.rs in jit-icache-coherence pulls std. I'm happy to explore a no_std implementation for it in a separate PR but for now, I just pushed a new commit to disable icache coherence in Windows no_std. If that change makes sense, I'm going to address the remaining CI issues before finalizing the PR. Thanks again for your help!

@alexcrichton
Copy link
Member

For the stack switching issues can you update the stack-switching cargo feature to require std?

@salmans salmans force-pushed the win-unix-no-std branch from 17b6f16 to 2a8ca6f Compare July 1, 2025 19:05
// Do this before marking the memory as R+X, technically we should be able to do it after
// but there are some CPU's that have had errata about doing this with read only memory.
icache_coherence::clear_cache(text.as_ptr().cast(), text.len())
#[cfg(any(not(windows), feature = "std"))]
Copy link
Member

Choose a reason for hiding this comment

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

For this it's a pretty critical operation here to manage the icache so we'll want to avoid this #[cfg] which makes it difficult to see when it's used and when it isn't. I think the best solution here might be to sidestep the problem entirely and update this line to take the std feature into account. For example that could be (unix || windows) && cfg!(feature = "std") or something like that. A comment there would be good to explain that if std is disabled then it's not supported in Wasmtime right now to interact with the OS which means that it's no longer supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried that change but it doesn't solve the problem: Wasmtime still pulls std through jit-icache-coherence in no_std Windows.
Another possible solution is to use libc in jit-icache-coherence for no_std Windows at this line. For that, we could add a new std feature flag to jit-icache-coherence and enable it if std in wasmtime is enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just pushed a new commit that implements the change suggested above but happy to implement a different solution if this is problematic.

Copy link
Member

Choose a reason for hiding this comment

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

Your commit looks reasonable, thanks for doing that! I'll apologize again though about how this is a bit messy. I'd prefer to avoid having the crate be sort of half-std/half-not, so I think I've come up with an idea to sidestep this entirely which is to include the icache crate only if std is enabled in Wasmtime itself. That solves the concerns I have about things getting a bit weird sometimes and the only remaining issue is various CI bits and how well this works out. I've pushed up a commit to this PR which implements what I was thinking of and I'll watch CI to see if my strategy won't work due to something I haven't thought of

Copy link
Contributor Author

@salmans salmans Jul 3, 2025

Choose a reason for hiding this comment

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

Not at all! I appreciate your help and the time you've put into this. Okay, I'll wait for CI and merge the changes into the branch as soon as I see green. Thanks again!

salmans added 2 commits July 2, 2025 13:12
- The `std` feature gates the use of standard library for icache coherence in Windows; otherwise, defaults to the `libc` implementation.
- The `std` feature of Wasmtime now depends on the `wasmtime-jit-icache-coherence/std`
@salmans salmans force-pushed the win-unix-no-std branch from 2a8ca6f to ceae3ff Compare July 2, 2025 17:32
@alexcrichton alexcrichton enabled auto-merge July 3, 2025 21:53
@alexcrichton alexcrichton added this pull request to the merge queue Jul 3, 2025
Merged via the queue into bytecodealliance:main with commit b63c25a Jul 3, 2025
165 checks passed
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants