Skip to content

Commit b4f5cba

Browse files
committed
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 bytecodealliance#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.
1 parent 4b9f53a commit b4f5cba

File tree

12 files changed

+42
-13
lines changed

12 files changed

+42
-13
lines changed

.github/workflows/main.yml

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

360+
# Checks for no_std support, ensure that crates can build on a no_std
361+
# target
362+
- run: rustup target add x86_64-unknown-none
363+
- run: cargo check -p wasmtime-jit-icache-coherence
364+
env:
365+
CARGO_BUILD_TARGET: x86_64-unknown-none
366+
- run: cargo check -p wasmtime-component-util
367+
env:
368+
CARGO_BUILD_TARGET: x86_64-unknown-none
369+
- run: cargo check -p wasmtime-asm-macros
370+
env:
371+
CARGO_BUILD_TARGET: x86_64-unknown-none
372+
360373
# Check that wasmtime-runtime compiles with panic=abort since there's some
361374
# #[cfg] for specifically panic=abort there.
362375
- 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ wit-component = "0.205.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" }

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/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
bincode = { 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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
use std::ffi::c_void;
2-
use std::io::Result;
1+
use core::ffi::c_void;
32

43
#[cfg(all(
54
target_arch = "aarch64",
65
any(target_os = "linux", target_os = "android")
76
))]
87
mod details {
8+
extern crate std;
9+
910
use super::*;
1011
use libc::{syscall, EINVAL, EPERM};
1112
use std::io::Error;
13+
pub use std::io::Result;
1214

1315
const MEMBARRIER_CMD_GLOBAL: libc::c_int = 1;
1416
const MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE: libc::c_int = 32;
@@ -87,7 +89,10 @@ mod details {
8789
any(target_os = "linux", target_os = "android")
8890
)))]
8991
mod details {
90-
pub(crate) fn pipeline_flush_mt() -> std::io::Result<()> {
92+
// NB: this uses `anyhow::Result` instead of `std::io::Result` to compile on
93+
// `no_std`.
94+
pub use anyhow::Result;
95+
pub(crate) fn pipeline_flush_mt() -> Result<()> {
9196
Ok(())
9297
}
9398
}

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'] }

0 commit comments

Comments
 (0)