Skip to content

Commit d8f7c77

Browse files
authored
Start migrating some Wasmtime crates to no_std (#8463)
* Start migrating some Wasmtime crates to no_std This commit is the first in what will be multiple PRs to migrate Wasmtime to being compatible with `#![no_std]`. This work is outlined in #8341 and the rough plan I have in mind is to go on a crate-by-crate basis and use CI as a "ratchet" to ensure that `no_std` compat is preserved. In that sense this PR is a bit of a template for future PRs. This PR migrates a few small crates to `no_std`, basically those that need no changes beyond simply adding the attribute. The nontrivial parts introduced in this PR are: * CI is introduced to verify that a subset of crates can indeed be built on a `no_std` target. The target selected is `x86_64-unknown-none` which is known to not have `std` and will result in a build error if it's attempted to be used. * The `anyhow` crate, which `wasmtime-jit-icache-coherence` now depends on, has its `std` feature disabled by default in Wasmtime's workspace. This means that some crates which require `std` now need to explicitly enable the feature, but it means that by-default its usage is appropriate for `no_std`. The first point should provide CI checks that compatibility with `no_std` indeed works, at least from an "it compiles" perspective. Note that it's not sufficient to test with a target like `x86_64-unknown-linux-gnu` because `extern crate std` will work on that target, even when `#![no_std]` is active. The second point however is likely to increase maintenance burden in Wasmtime unfortunately. Namely we'll inevitably, either here or in the future, forget to turn on some feature for some crate that's not covered in CI checks. While I've tried to do my best here in covering it there's no guarantee that everything will work and the combinatorial explosion of what could be checked in CI can't all be added to CI. Instead we'll have to rely on bug fixes, users, and perhaps point releases to add more use cases to CI over time as we see fit. * Add another std feature * Another std feature * Enable anyhow/std for another crate * Activate `std` in more crates * Fix miri build * Fix compile on riscv64 prtest:full * Fix min-platform example build * Fix icache-coherence again
1 parent 1a17dfb commit d8f7c77

File tree

18 files changed

+62
-24
lines changed

18 files changed

+62
-24
lines changed

.github/workflows/main.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,19 @@ jobs:
360360
- run: cargo check -p wasmtime-c-api --no-default-features --features wat
361361
- run: cargo check -p wasmtime-c-api --no-default-features --features wasi
362362

363+
# Checks for no_std support, ensure that crates can build on a no_std
364+
# target
365+
- run: rustup target add x86_64-unknown-none
366+
- run: cargo check -p wasmtime-jit-icache-coherence
367+
env:
368+
CARGO_BUILD_TARGET: x86_64-unknown-none
369+
- run: cargo check -p wasmtime-component-util
370+
env:
371+
CARGO_BUILD_TARGET: x86_64-unknown-none
372+
- run: cargo check -p wasmtime-asm-macros
373+
env:
374+
CARGO_BUILD_TARGET: x86_64-unknown-none
375+
363376
# Check that wasmtime-runtime compiles with panic=abort since there's some
364377
# #[cfg] for specifically panic=abort there.
365378
- run: cargo check -p wasmtime-runtime

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ wasmtime-cranelift = { workspace = true, optional = true }
3232
wasmtime-environ = { workspace = true }
3333
wasmtime-explorer = { workspace = true, optional = true }
3434
wasmtime-wast = { workspace = true, optional = true }
35-
wasi-common = { workspace = true, default-features = true, features = ["exit" ], optional = true }
35+
wasi-common = { workspace = true, default-features = true, features = ["exit"], optional = true }
3636
wasmtime-wasi = { workspace = true, default-features = true, optional = true }
3737
wasmtime-wasi-nn = { workspace = true, optional = true }
3838
wasmtime-wasi-threads = { workspace = true, optional = true }
3939
wasmtime-wasi-http = { workspace = true, optional = true }
4040
wasmtime-runtime = { workspace = true, optional = true }
4141
clap = { workspace = true }
42-
anyhow = { workspace = true }
42+
anyhow = { workspace = true, features = ['std'] }
4343
target-lexicon = { workspace = true }
4444
once_cell = { workspace = true }
4545
listenfd = { version = "1.0.0", optional = true }
@@ -104,7 +104,7 @@ object = { workspace = true }
104104
windows-sys = { workspace = true, features = ["Win32_System_Memory"] }
105105

106106
[build-dependencies]
107-
anyhow = { workspace = true }
107+
anyhow = { workspace = true, features = ['std'] }
108108

109109
[profile.release.build-override]
110110
opt-level = 0
@@ -248,7 +248,7 @@ wit-component = "0.206.0"
248248
# --------------------------
249249
object = { version = "0.33", default-features = false, features = ['read_core', 'elf', 'std'] }
250250
gimli = { version = "0.28.0", default-features = false, features = ['read', 'std'] }
251-
anyhow = "1.0.22"
251+
anyhow = { version = "1.0.22", default-features = false }
252252
windows-sys = "0.52.0"
253253
env_logger = "0.10"
254254
log = { version = "0.4.8", default-features = false }

cranelift/codegen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ edition.workspace = true
1616
workspace = true
1717

1818
[dependencies]
19-
anyhow = { workspace = true, optional = true }
19+
anyhow = { workspace = true, optional = true, features = ['std'] }
2020
bumpalo = "3"
2121
capstone = { workspace = true, optional = true }
2222
cranelift-codegen-shared = { path = "./shared", version = "0.108.0" }

cranelift/module/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ workspace = true
1717
cranelift-codegen = { workspace = true }
1818
cranelift-control = { workspace = true }
1919
hashbrown = { workspace = true, optional = true }
20-
anyhow = { workspace = true }
20+
anyhow = { workspace = true, features = ['std'] }
2121
serde = { workspace = true, optional = true }
2222
serde_derive = { workspace = true, optional = true }
2323

cranelift/reader/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ edition.workspace = true
1313
workspace = true
1414

1515
[dependencies]
16-
anyhow.workspace = true
16+
anyhow = { workspace = true, features = ['std'] }
1717
cranelift-codegen = { workspace = true }
1818
smallvec = { workspace = true }
1919
target-lexicon = { workspace = true }

crates/asm-macros/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//! function) and additionally handles visibility across platforms. All symbols
77
//! should be visible to Rust but not visible externally outside of a `*.so`.
88
9+
#![no_std]
10+
911
cfg_if::cfg_if! {
1012
if #[cfg(target_os = "macos")] {
1113
#[macro_export]

crates/cache/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ edition.workspace = true
1212
workspace = true
1313

1414
[dependencies]
15-
anyhow = { workspace = true }
15+
anyhow = { workspace = true, features = ['std'] }
1616
base64 = "0.21.0"
1717
postcard = { workspace = true }
1818
directories-next = "2.0"

crates/component-util/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![no_std]
2+
13
/// Represents the possible sizes in bytes of the discriminant of a variant type in the component model
24
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
35
pub enum DiscriminantSize {

crates/environ/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ edition.workspace = true
1414
workspace = true
1515

1616
[dependencies]
17-
anyhow = { workspace = true }
17+
anyhow = { workspace = true, features = ['std'] }
1818
postcard = { workspace = true }
1919
cpp_demangle = { version = "0.4.3", optional = true }
2020
cranelift-entity = { workspace = true }

crates/jit-icache-coherence/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ workspace = true
1313

1414
[dependencies]
1515
cfg-if = { workspace = true }
16+
anyhow = { workspace = true }
1617

1718
[target.'cfg(target_os = "windows")'.dependencies.windows-sys]
1819
workspace = true

crates/jit-icache-coherence/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
//! # len: usize,
3535
//! # }
3636
//! #
37-
//! # fn main() -> io::Result<()> {
37+
//! # fn main() -> anyhow::Result<()> {
3838
//! #
3939
//! # let run_code = || {};
4040
//! # let code = vec![0u8; 64];
@@ -67,8 +67,9 @@
6767
//!
6868
//! [ARM Community - Caches and Self-Modifying Code]: https://community.arm.com/arm-community-blogs/b/architectures-and-processors-blog/posts/caches-and-self-modifying-code
6969
70-
use std::ffi::c_void;
71-
use std::io::Result;
70+
#![no_std]
71+
72+
use core::ffi::c_void;
7273

7374
cfg_if::cfg_if! {
7475
if #[cfg(target_os = "windows")] {
@@ -91,7 +92,7 @@ cfg_if::cfg_if! {
9192
/// after all calls to [clear_cache].
9293
///
9394
/// If the architecture does not require a pipeline flush, this function does nothing.
94-
pub fn pipeline_flush_mt() -> Result<()> {
95+
pub fn pipeline_flush_mt() -> imp::Result<()> {
9596
imp::pipeline_flush_mt()
9697
}
9798

@@ -103,6 +104,6 @@ pub fn pipeline_flush_mt() -> Result<()> {
103104
///
104105
/// It is necessary to call [pipeline_flush_mt] after this function if you are running in a multi-threaded
105106
/// environment.
106-
pub unsafe fn clear_cache(ptr: *const c_void, len: usize) -> Result<()> {
107+
pub unsafe fn clear_cache(ptr: *const c_void, len: usize) -> imp::Result<()> {
107108
imp::clear_cache(ptr, len)
108109
}

crates/jit-icache-coherence/src/libc.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
use std::ffi::c_void;
2-
use std::io::Result;
1+
use core::ffi::c_void;
2+
3+
#[cfg(any(target_os = "linux", target_os = "android"))]
4+
extern crate std;
5+
#[cfg(any(target_os = "linux", target_os = "android"))]
6+
pub use std::io::Result;
7+
8+
#[cfg(not(any(target_os = "linux", target_os = "android")))]
9+
pub use anyhow::Result;
310

411
#[cfg(all(
512
target_arch = "aarch64",
613
any(target_os = "linux", target_os = "android")
714
))]
815
mod details {
16+
extern crate std;
17+
918
use super::*;
1019
use libc::{syscall, EINVAL, EPERM};
1120
use std::io::Error;
@@ -87,20 +96,25 @@ mod details {
8796
any(target_os = "linux", target_os = "android")
8897
)))]
8998
mod details {
90-
pub(crate) fn pipeline_flush_mt() -> std::io::Result<()> {
99+
// NB: this uses `anyhow::Result` instead of `std::io::Result` to compile on
100+
// `no_std`.
101+
pub(crate) fn pipeline_flush_mt() -> super::Result<()> {
91102
Ok(())
92103
}
93104
}
105+
94106
#[cfg(all(target_arch = "riscv64", target_os = "linux"))]
95107
fn riscv_flush_icache(start: u64, end: u64) -> Result<()> {
96108
cfg_if::cfg_if! {
97109
if #[cfg(feature = "one-core")] {
98-
use std::arch::asm;
110+
use core::arch::asm;
99111
unsafe {
100112
asm!("fence.i");
101113
};
102114
Ok(())
103115
} else {
116+
extern crate std;
117+
104118
match unsafe {
105119
libc::syscall(
106120
{

crates/jit-icache-coherence/src/miri.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::ffi::c_void;
2-
use std::io::Result;
1+
pub use anyhow::Result;
2+
use core::ffi::c_void;
33

44
pub(crate) fn pipeline_flush_mt() -> Result<()> {
55
Ok(())

crates/jit-icache-coherence/src/win.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
extern crate std;
2+
13
use std::ffi::c_void;
2-
use std::io::{Error, Result};
4+
use std::io::Error;
35
use windows_sys::Win32::System::Diagnostics::Debug::FlushInstructionCache;
46
use windows_sys::Win32::System::Threading::FlushProcessWriteBuffers;
57
use windows_sys::Win32::System::Threading::GetCurrentProcess;
68

9+
pub use std::io::Result;
10+
711
/// See docs on [crate::pipeline_flush_mt] for a description of what this function is trying to do.
812
#[inline]
913
pub(crate) fn pipeline_flush_mt() -> Result<()> {

crates/test-programs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ license = "Apache-2.0 WITH LLVM-exception"
1010
workspace = true
1111

1212
[dependencies]
13-
anyhow = { workspace = true }
13+
anyhow = { workspace = true, features = ['std'] }
1414
wasi = "0.11.0"
1515
wasi-nn = "0.6.0"
1616
wit-bindgen = { workspace = true, features = ['default'] }

crates/wasi-preview1-component-adapter/verify/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ workspace = true
1111
[dependencies]
1212
wasmparser = { workspace = true }
1313
wat = { workspace = true }
14-
anyhow = { workspace = true }
14+
anyhow = { workspace = true, features = ['std'] }

examples/min-platform/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ rust-version.workspace = true
99
workspace = true
1010

1111
[dependencies]
12-
anyhow = { workspace = true }
12+
anyhow = { workspace = true, features = ['std'] }
1313
libloading = "0.8"
1414
object = { workspace = true }

0 commit comments

Comments
 (0)