Skip to content

Commit

Permalink
Migrate the wasmtime-types crate to no_std (#8485)
Browse files Browse the repository at this point in the history
* Migrate the wasmtime-types crate to no_std

This commit is where no_std for Wasmtime starts to get a bit
interesting. Specifically the `wasmtime-types` crate is the first crate
that depends on some nontrivial crates that also need to be migrated to
`no_std`. This PR disables the default feature of `wasmparser` by
default and additionally does the same for `serde`. This enables them to
compile in `no_std` contexts by default and default features will be
enabled elsewhere in this repository as necessary.

This also opts to drop the `thiserror` dependency entirely in favor of a
manual `Display` implementation with a cfg'd implementation of `Error`.

As before CI checks are added for `wasmtime-types` with a `no_std`
target itself to ensure the crate and all dependencies all avoid `std`.

* Fix adapter build
  • Loading branch information
alexcrichton authored May 2, 2024
1 parent 01c07a1 commit d911f4b
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ jobs:
- run: cargo check -p wasmtime-slab
env:
CARGO_BUILD_TARGET: x86_64-unknown-none
- run: cargo check -p wasmtime-types
env:
CARGO_BUILD_TARGET: x86_64-unknown-none

# Check that wasmtime compiles with panic=abort since there's some `#[cfg]`
# for specifically panic=abort there.
Expand Down
1 change: 0 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ rustix = "0.38.31"
wit-bindgen = { version = "0.22.0", default-features = false }

# wasm-tools family:
wasmparser = "0.206.0"
wasmparser = { version = "0.206.0", default-features = false }
wat = "1.206.0"
wast = "206.0.0"
wasmprinter = "0.206.0"
Expand Down Expand Up @@ -270,7 +270,7 @@ proptest = "1.0.0"
rand = { version = "0.8.3", features = ["small_rng"] }
sptr = "0.3.2"
# serde and serde_derive must have the same version
serde = "1.0.188"
serde = { version = "1.0.188", default-features = false, features = ['alloc'] }
serde_derive = "1.0.188"
serde_json = "1.0.80"
glob = "0.3.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/environ/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ anyhow = { workspace = true, features = ['std'] }
postcard = { workspace = true }
cpp_demangle = { version = "0.4.3", optional = true }
cranelift-entity = { workspace = true }
wasmtime-types = { workspace = true }
wasmtime-types = { workspace = true, features = ['std'] }
wasmparser = { workspace = true }
indexmap = { workspace = true, features = ["serde"] }
thiserror = { workspace = true }
Expand Down
4 changes: 3 additions & 1 deletion crates/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ cranelift-entity = { workspace = true, features = ['enable-serde'] }
serde = { workspace = true }
serde_derive = { workspace = true }
smallvec = { workspace = true, features = ["serde"] }
thiserror = { workspace = true }
wasmparser = { workspace = true }

[lints]
workspace = true

[features]
std = ['wasmparser/std']
36 changes: 29 additions & 7 deletions crates/types/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use thiserror::Error;
use alloc::string::String;
use core::fmt;

/// A WebAssembly translation error.
///
/// When a WebAssembly function can't be translated, one of these error codes will be returned
/// to describe the failure.
#[derive(Error, Debug)]
#[derive(Debug)]
pub enum WasmError {
/// The input WebAssembly code is invalid.
///
/// This error code is used by a WebAssembly translator when it encounters invalid WebAssembly
/// code. This should never happen for validated WebAssembly code.
#[error("Invalid input WebAssembly code at offset {offset}: {message}")]
InvalidWebAssembly {
/// A string describing the validation error.
message: String,
Expand All @@ -21,7 +21,6 @@ pub enum WasmError {
/// A feature used by the WebAssembly code is not supported by the embedding environment.
///
/// Embedding environments may have their own limitations and feature restrictions.
#[error("Unsupported feature: {0}")]
Unsupported(String),

/// An implementation limit was exceeded.
Expand All @@ -30,19 +29,17 @@ pub enum WasmError {
/// limits][limits] that cause compilation to fail when they are exceeded.
///
/// [limits]: https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/docs/ir.md#implementation-limits
#[error("Implementation limit exceeded")]
ImplLimitExceeded,

/// Any user-defined error.
#[error("User error: {0}")]
User(String),
}

/// Return an `Err(WasmError::Unsupported(msg))` where `msg` the string built by calling `format!`
/// on the arguments to this macro.
#[macro_export]
macro_rules! wasm_unsupported {
($($arg:tt)*) => { $crate::WasmError::Unsupported(format!($($arg)*)) }
($($arg:tt)*) => { $crate::WasmError::Unsupported($crate::__format!($($arg)*)) }
}

impl From<wasmparser::BinaryReaderError> for WasmError {
Expand All @@ -57,3 +54,28 @@ impl From<wasmparser::BinaryReaderError> for WasmError {

/// A convenient alias for a `Result` that uses `WasmError` as the error type.
pub type WasmResult<T> = Result<T, WasmError>;

impl fmt::Display for WasmError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
WasmError::InvalidWebAssembly { message, offset } => {
write!(
f,
"Invalid input WebAssembly code at offset {offset}: {message}"
)
}
WasmError::Unsupported(s) => {
write!(f, "Unsupported feature: {s}")
}
WasmError::ImplLimitExceeded => {
write!(f, "Implementation limit exceeded")
}
WasmError::User(s) => {
write!(f, "User error: {s}")
}
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for WasmError {}
14 changes: 12 additions & 2 deletions crates/types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
//! Internal dependency of Wasmtime and Cranelift that defines types for
//! WebAssembly.

use smallvec::SmallVec;
#![no_std]

extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

pub use wasmparser;

#[doc(hidden)]
pub use alloc::format as __format;

use alloc::boxed::Box;
use core::{fmt, ops::Range};
use cranelift_entity::entity_impl;
use serde_derive::{Deserialize, Serialize};
use std::{fmt, ops::Range};
use smallvec::SmallVec;

mod error;
pub use error::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi-preview1-component-adapter/verify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ publish = false
workspace = true

[dependencies]
wasmparser = { workspace = true }
wasmparser = { workspace = true, features = ['std'] }
wat = { workspace = true }
anyhow = { workspace = true, features = ['std'] }

0 comments on commit d911f4b

Please sign in to comment.