Skip to content
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

Migrate the wasmtime-types crate to no_std #8485

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
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-runtime 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
Expand Up @@ -234,7 +234,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 @@ -265,7 +265,7 @@ heck = "0.4"
similar = "2.1.0"
toml = "0.8.10"
# 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
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
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
@@ -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
@@ -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;
abrown marked this conversation as resolved.
Show resolved Hide resolved

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