Skip to content

Commit

Permalink
Start migrating some Wasmtime crates to no_std
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alexcrichton committed Apr 25, 2024
1 parent 79885bd commit 3b4ed0a
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 13 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,19 @@ jobs:
- run: cargo check -p wasmtime-c-api --no-default-features --features wat
- run: cargo check -p wasmtime-c-api --no-default-features --features wasi

# Checks for no_std support, ensure that crates can build on a no_std
# target
- run: rustup target add x86_64-unknown-none
- run: cargo check -p wasmtime-jit-icache-coherence
env:
CARGO_BUILD_TARGET: x86_64-unknown-none
- run: cargo check -p wasmtime-component-util
env:
CARGO_BUILD_TARGET: x86_64-unknown-none
- run: cargo check -p wasmtime-asm-macros
env:
CARGO_BUILD_TARGET: x86_64-unknown-none

# Check that wasmtime-runtime compiles with panic=abort since there's some
# #[cfg] for specifically panic=abort there.
- run: cargo check -p wasmtime-runtime
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ wit-component = "0.206.0"
# --------------------------
object = { version = "0.33", default-features = false, features = ['read_core', 'elf', 'std'] }
gimli = { version = "0.28.0", default-features = false, features = ['read', 'std'] }
anyhow = "1.0.22"
anyhow = { version = "1.0.22", default-features = false }
windows-sys = "0.52.0"
env_logger = "0.10"
log = { version = "0.4.8", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ edition.workspace = true
workspace = true

[dependencies]
anyhow = { workspace = true, optional = true }
anyhow = { workspace = true, optional = true, features = ['std'] }
bumpalo = "3"
capstone = { workspace = true, optional = true }
cranelift-codegen-shared = { path = "./shared", version = "0.108.0" }
Expand Down
2 changes: 2 additions & 0 deletions crates/asm-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//! function) and additionally handles visibility across platforms. All symbols
//! should be visible to Rust but not visible externally outside of a `*.so`.

#![no_std]

cfg_if::cfg_if! {
if #[cfg(target_os = "macos")] {
#[macro_export]
Expand Down
2 changes: 2 additions & 0 deletions crates/component-util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![no_std]

/// Represents the possible sizes in bytes of the discriminant of a variant type in the component model
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum DiscriminantSize {
Expand Down
2 changes: 1 addition & 1 deletion crates/environ/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ edition.workspace = true
workspace = true

[dependencies]
anyhow = { workspace = true }
anyhow = { workspace = true, features = ['std'] }
postcard = { workspace = true }
cpp_demangle = { version = "0.4.3", optional = true }
cranelift-entity = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/jit-icache-coherence/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ workspace = true

[dependencies]
cfg-if = { workspace = true }
anyhow = { workspace = true }

[target.'cfg(target_os = "windows")'.dependencies.windows-sys]
workspace = true
Expand Down
11 changes: 6 additions & 5 deletions crates/jit-icache-coherence/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//! # len: usize,
//! # }
//! #
//! # fn main() -> io::Result<()> {
//! # fn main() -> anyhow::Result<()> {
//! #
//! # let run_code = || {};
//! # let code = vec![0u8; 64];
Expand Down Expand Up @@ -67,8 +67,9 @@
//!
//! [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

use std::ffi::c_void;
use std::io::Result;
#![no_std]

use core::ffi::c_void;

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

Expand All @@ -103,6 +104,6 @@ pub fn pipeline_flush_mt() -> Result<()> {
///
/// It is necessary to call [pipeline_flush_mt] after this function if you are running in a multi-threaded
/// environment.
pub unsafe fn clear_cache(ptr: *const c_void, len: usize) -> Result<()> {
pub unsafe fn clear_cache(ptr: *const c_void, len: usize) -> imp::Result<()> {
imp::clear_cache(ptr, len)
}
11 changes: 8 additions & 3 deletions crates/jit-icache-coherence/src/libc.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::ffi::c_void;
use std::io::Result;
use core::ffi::c_void;

#[cfg(all(
target_arch = "aarch64",
any(target_os = "linux", target_os = "android")
))]
mod details {
extern crate std;

use super::*;
use libc::{syscall, EINVAL, EPERM};
use std::io::Error;
pub use std::io::Result;

const MEMBARRIER_CMD_GLOBAL: libc::c_int = 1;
const MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE: libc::c_int = 32;
Expand Down Expand Up @@ -87,7 +89,10 @@ mod details {
any(target_os = "linux", target_os = "android")
)))]
mod details {
pub(crate) fn pipeline_flush_mt() -> std::io::Result<()> {
// NB: this uses `anyhow::Result` instead of `std::io::Result` to compile on
// `no_std`.
pub use anyhow::Result;
pub(crate) fn pipeline_flush_mt() -> Result<()> {
Ok(())
}
}
Expand Down
6 changes: 5 additions & 1 deletion crates/jit-icache-coherence/src/win.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
extern crate std;

use std::ffi::c_void;
use std::io::{Error, Result};
use std::io::Error;
use windows_sys::Win32::System::Diagnostics::Debug::FlushInstructionCache;
use windows_sys::Win32::System::Threading::FlushProcessWriteBuffers;
use windows_sys::Win32::System::Threading::GetCurrentProcess;

pub use std::io::Result;

/// See docs on [crate::pipeline_flush_mt] for a description of what this function is trying to do.
#[inline]
pub(crate) fn pipeline_flush_mt() -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "Apache-2.0 WITH LLVM-exception"
workspace = true

[dependencies]
anyhow = { workspace = true }
anyhow = { workspace = true, features = ['std'] }
wasi = "0.11.0"
wasi-nn = "0.6.0"
wit-bindgen = { workspace = true, features = ['default'] }
Expand Down

0 comments on commit 3b4ed0a

Please sign in to comment.