diff --git a/Cargo.lock b/Cargo.lock index 4d46ea97115..68eeeda0d1c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2870,7 +2870,6 @@ dependencies = [ "tempfile", "unix_mode", "wasmer", - "wasmer-artifact", "wasmer-cache", "wasmer-compiler", "wasmer-compiler-cranelift", @@ -2925,7 +2924,6 @@ dependencies = [ "wasmer-compiler-singlepass", "wasmer-engine-universal-artifact", "wasmer-types", - "wasmer-vfs", ] [[package]] diff --git a/lib/cli-compiler/Cargo.toml b/lib/cli-compiler/Cargo.toml index ba9257d93f3..fcc01acd650 100644 --- a/lib/cli-compiler/Cargo.toml +++ b/lib/cli-compiler/Cargo.toml @@ -21,7 +21,6 @@ doc = false wasmer-engine-universal-artifact = { version = "=2.2.1", path = "../universal-artifact", features = ["compiler"] } wasmer-compiler = { version = "=2.2.1", path = "../compiler" } wasmer-types = { version = "=2.2.1", path = "../types" } -wasmer-vfs = { version = "=2.2.1", path = "../vfs", default-features = false, features = ["host-fs"] } atty = "0.2" colored = "2.0" anyhow = "1.0" diff --git a/lib/cli/Cargo.toml b/lib/cli/Cargo.toml index efd72cd48a6..2303da10dd6 100644 --- a/lib/cli/Cargo.toml +++ b/lib/cli/Cargo.toml @@ -24,12 +24,19 @@ doc = false required-features = ["headless"] [dependencies] -wasmer-artifact = { version = "=2.2.1", path = "../artifact" } +wasmer = { version = "=2.2.1", path = "../api", default-features = false } wasmer-compiler = { version = "=2.2.1", path = "../compiler" } +wasmer-compiler-cranelift = { version = "=2.2.1", path = "../compiler-cranelift", optional = true } wasmer-compiler-singlepass = { version = "=2.2.1", path = "../compiler-singlepass", optional = true } +wasmer-compiler-llvm = { version = "=2.2.1", path = "../compiler-llvm", optional = true } +wasmer-emscripten = { version = "=2.2.1", path = "../emscripten", optional = true } wasmer-engine = { version = "=2.2.1", path = "../engine" } wasmer-engine-universal = { version = "=2.2.1", path = "../engine-universal", optional = true } +wasmer-engine-dylib = { version = "=2.2.1", path = "../engine-dylib", optional = true } +wasmer-engine-staticlib = { version = "=2.2.1", path = "../engine-staticlib", optional = true } +wasmer-vm = { version = "=2.2.1", path = "../vm" } wasmer-wasi = { version = "=2.2.1", path = "../wasi", optional = true } +wasmer-wasi-experimental-io-devices = { version = "=2.2.1", path = "../wasi-experimental-io-devices", optional = true } wasmer-wast = { version = "=2.2.1", path = "../../tests/lib/wast", optional = true } wasmer-cache = { version = "=2.2.1", path = "../cache", optional = true } wasmer-types = { version = "=2.2.1", path = "../types" } @@ -48,19 +55,6 @@ fern = { version = "0.6", features = ["colored"], optional = true } log = { version = "0.4", optional = true } tempfile = "3" -[target.'cfg(target_arch = "wasm32")'.dependencies] -wasmer = { version = "=2.2.1", path = "../api", default-features = false, features = ["js-default"] } - -[target.'cfg(not(target_arch = "wasm32"))'.dependencies] -wasmer = { version = "=2.2.1", path = "../api", default-features = false } -wasmer-vm = { version = "=2.2.1", path = "../vm" } -wasmer-compiler-cranelift = { version = "=2.2.1", path = "../compiler-cranelift", optional = true } -wasmer-compiler-llvm = { version = "=2.2.1", path = "../compiler-llvm", optional = true } -wasmer-wasi-experimental-io-devices = { version = "=2.2.1", path = "../wasi-experimental-io-devices", optional = true } -wasmer-emscripten = { version = "=2.2.1", path = "../emscripten", optional = true } -wasmer-engine-dylib = { version = "=2.2.1", path = "../engine-dylib", optional = true } -wasmer-engine-staticlib = { version = "=2.2.1", path = "../engine-staticlib", optional = true } - [target.'cfg(target_os = "linux")'.dependencies] unix_mode = "0.1.3" @@ -125,4 +119,4 @@ headless-minimal = ["headless", "disable-all-logging", "wasi", "dylib", "univers # Deprecated features. jit = ["universal"] -native = ["dylib"] +native = ["dylib"] \ No newline at end of file diff --git a/lib/cli/src/bin/wasmer_compiler.rs b/lib/cli/src/bin/wasmer_compiler.rs deleted file mode 100644 index 13c4ff72e7c..00000000000 --- a/lib/cli/src/bin/wasmer_compiler.rs +++ /dev/null @@ -1,13 +0,0 @@ -use wasmer_cli::cli::wasmer_main; - -#[cfg(not(any(feature = "cranelift", feature = "singlepass", feature = "llvm")))] -compile_error!( - "Either enable at least one compiler, or compile the wasmer-headless binary instead" -); - -#[cfg(featue = "run")] -compile_error!("Cannot enable run with the compile-only build"); - -fn main() { - wasmer_main(); -} diff --git a/lib/compiler-singlepass/Cargo.toml b/lib/compiler-singlepass/Cargo.toml index f9dee2065bb..1fbfc0c2f24 100644 --- a/lib/compiler-singlepass/Cargo.toml +++ b/lib/compiler-singlepass/Cargo.toml @@ -35,7 +35,7 @@ maintenance = { status = "actively-developed" } [features] default = ["std", "rayon", "unwind", "avx"] -wasm = ["std", "avx"] +wasm = ["std", "unwind", "avx"] std = ["wasmer-compiler/std", "wasmer-types/std"] core = ["hashbrown", "wasmer-types/core"] unwind = ["gimli"] diff --git a/lib/types/src/lib.rs b/lib/types/src/lib.rs index 9f443e708ed..439dafe10d7 100644 --- a/lib/types/src/lib.rs +++ b/lib/types/src/lib.rs @@ -100,9 +100,9 @@ pub use types::{ pub use archives::ArchivableIndexMap; pub use crate::libcalls::LibCall; -pub use crate::memory::{MemoryError, MemoryStyle}; +pub use crate::memory::MemoryStyle; pub use crate::table::TableStyle; -pub use crate::trapcode::{Trap, TrapCode}; +pub use crate::trapcode::TrapCode; pub use crate::vmoffsets::{TargetSharedSignatureIndex, VMBuiltinFunctionIndex, VMOffsets}; pub use crate::utils::is_wasm; diff --git a/lib/types/src/memory.rs b/lib/types/src/memory.rs index 4ad12c05afd..920abc06129 100644 --- a/lib/types/src/memory.rs +++ b/lib/types/src/memory.rs @@ -4,49 +4,6 @@ use loupe::MemoryUsage; use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize}; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; -use thiserror::Error; - -/// Error type describing things that can go wrong when operating on Wasm Memories. -#[derive(Error, Debug, Clone, PartialEq, Hash)] -pub enum MemoryError { - /// Low level error with mmap. - #[error("Error when allocating memory: {0}")] - Region(String), - /// The operation would cause the size of the memory to exceed the maximum or would cause - /// an overflow leading to unindexable memory. - #[error("The memory could not grow: current size {} pages, requested increase: {} pages", current.0, attempted_delta.0)] - CouldNotGrow { - /// The current size in pages. - current: Pages, - /// The attempted amount to grow by in pages. - attempted_delta: Pages, - }, - /// The operation would cause the size of the memory size exceed the maximum. - #[error("The memory is invalid because {}", reason)] - InvalidMemory { - /// The reason why the provided memory is invalid. - reason: String, - }, - /// Caller asked for more minimum memory than we can give them. - #[error("The minimum requested ({} pages) memory is greater than the maximum allowed memory ({} pages)", min_requested.0, max_allowed.0)] - MinimumMemoryTooLarge { - /// The number of pages requested as the minimum amount of memory. - min_requested: Pages, - /// The maximum amount of memory we can allocate. - max_allowed: Pages, - }, - /// Caller asked for a maximum memory greater than we can give them. - #[error("The maximum requested memory ({} pages) is greater than the maximum allowed memory ({} pages)", max_requested.0, max_allowed.0)] - MaximumMemoryTooLarge { - /// The number of pages requested as the maximum amount of memory. - max_requested: Pages, - /// The number of pages requested as the maximum amount of memory. - max_allowed: Pages, - }, - /// A user defined error value, used for error cases not listed above. - #[error("A user-defined error occurred: {0}")] - Generic(String), -} /// Implementation styles for WebAssembly linear memory. #[derive(Debug, Clone, PartialEq, Eq, Hash, MemoryUsage)] diff --git a/lib/types/src/trapcode.rs b/lib/types/src/trapcode.rs index 46812ce3576..648898fd6e5 100644 --- a/lib/types/src/trapcode.rs +++ b/lib/types/src/trapcode.rs @@ -3,7 +3,6 @@ //! Trap codes describing the reason for a trap. -use backtrace::Backtrace; use core::fmt::{self, Display, Formatter}; use core::str::FromStr; use loupe::MemoryUsage; @@ -11,7 +10,6 @@ use loupe::MemoryUsage; use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize}; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; -use std::error::Error; use thiserror::Error; /// A trap code describing the reason for a trap. @@ -166,72 +164,3 @@ mod tests { assert_eq!("users".parse::(), Err(())); } } - -/// Stores trace message with backtrace. -#[derive(Debug)] -pub enum Trap { - /// A user-raised trap through `raise_user_trap`. - User(Box), - - /// A trap raised from the Wasm generated code - /// - /// Note: this trap is deterministic (assuming a deterministic host implementation) - Wasm { - /// The program counter in generated code where this trap happened. - pc: usize, - /// Native stack backtrace at the time the trap occurred - backtrace: Backtrace, - /// Optional trapcode associated to the signal that caused the trap - signal_trap: Option, - }, - - /// A trap raised from a wasm libcall - /// - /// Note: this trap is deterministic (assuming a deterministic host implementation) - Lib { - /// Code of the trap. - trap_code: TrapCode, - /// Native stack backtrace at the time the trap occurred - backtrace: Backtrace, - }, - - /// A trap indicating that the runtime was unable to allocate sufficient memory. - /// - /// Note: this trap is nondeterministic, since it depends on the host system. - OOM { - /// Native stack backtrace at the time the OOM occurred - backtrace: Backtrace, - }, -} - -impl Trap { - /// Construct a new Wasm trap with the given source location and backtrace. - /// - /// Internally saves a backtrace when constructed. - pub fn wasm(pc: usize, backtrace: Backtrace, signal_trap: Option) -> Self { - Trap::Wasm { - pc, - backtrace, - signal_trap, - } - } - - /// Construct a new Wasm trap with the given trap code. - /// - /// Internally saves a backtrace when constructed. - pub fn lib(trap_code: TrapCode) -> Self { - let backtrace = Backtrace::new_unresolved(); - Trap::Lib { - trap_code, - backtrace, - } - } - - /// Construct a new OOM trap with the given source location and trap code. - /// - /// Internally saves a backtrace when constructed. - pub fn oom() -> Self { - let backtrace = Backtrace::new_unresolved(); - Trap::OOM { backtrace } - } -} diff --git a/lib/vfs/src/host_fs.rs b/lib/vfs/src/host_fs.rs index c2eb4908693..961f6685be0 100644 --- a/lib/vfs/src/host_fs.rs +++ b/lib/vfs/src/host_fs.rs @@ -9,8 +9,6 @@ use std::fs; use std::io::{self, Read, Seek, Write}; #[cfg(unix)] use std::os::unix::io::{AsRawFd, RawFd}; -#[cfg(target_arch = "wasm32")] -use std::os::wasi::io::{AsRawFd, RawFd}; #[cfg(windows)] use std::os::windows::io::{AsRawHandle, RawHandle}; use std::path::{Path, PathBuf}; @@ -69,31 +67,6 @@ impl TryInto for FileDescriptor { } } -#[cfg(target_arch = "wasm32")] -impl TryIntoFileDescriptor for T -where - T: AsRawFd, -{ - type Error = FsError; - - fn try_into_filedescriptor(&self) -> std::result::Result { - Ok(FileDescriptor( - self.as_raw_fd() - .try_into() - .map_err(|_| FsError::InvalidFd)?, - )) - } -} - -#[cfg(target_arch = "wasm32")] -impl TryInto for FileDescriptor { - type Error = FsError; - - fn try_into(self) -> std::result::Result { - self.0.try_into().map_err(|_| FsError::InvalidFd) - } -} - #[derive(Debug, Default, Clone)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] pub struct FileSystem; diff --git a/lib/vm/Cargo.toml b/lib/vm/Cargo.toml index 7c99658350f..21359bbbd09 100644 --- a/lib/vm/Cargo.toml +++ b/lib/vm/Cargo.toml @@ -26,8 +26,6 @@ loupe = { version = "0.1", features = ["enable-indexmap"] } enum-iterator = "0.7.0" scopeguard = "1.1.0" lazy_static = "1.4.0" - -[target.'cfg(not(target_arch = "wasm32"))'.dependencies] region = { version = "3.0" } corosensei = { version = "0.1.2" } diff --git a/lib/vm/src/instance/mod.rs b/lib/vm/src/instance/mod.rs index a42eb0b9533..bed09bbbfc6 100644 --- a/lib/vm/src/instance/mod.rs +++ b/lib/vm/src/instance/mod.rs @@ -17,7 +17,7 @@ use crate::export::VMExtern; use crate::func_data_registry::VMFuncRef; use crate::global::Global; use crate::imports::Imports; -use crate::memory::Memory; +use crate::memory::{Memory, MemoryError}; use crate::table::{Table, TableElement}; use crate::trap::{catch_traps, Trap, TrapCode, TrapHandler}; use crate::vmcontext::{ @@ -44,8 +44,8 @@ use std::sync::Arc; use wasmer_types::entity::{packed_option::ReservedValue, BoxedSlice, EntityRef, PrimaryMap}; use wasmer_types::{ DataIndex, DataInitializer, ElemIndex, ExportIndex, FunctionIndex, GlobalIndex, GlobalInit, - LocalFunctionIndex, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryError, - MemoryIndex, ModuleInfo, Pages, SignatureIndex, TableIndex, TableInitializer, + LocalFunctionIndex, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryIndex, + ModuleInfo, Pages, SignatureIndex, TableIndex, TableInitializer, }; /// The function pointer to call with data and an [`Instance`] pointer to diff --git a/lib/vm/src/lib.rs b/lib/vm/src/lib.rs index 1f3877e8bbc..8056736a28a 100644 --- a/lib/vm/src/lib.rs +++ b/lib/vm/src/lib.rs @@ -43,7 +43,7 @@ pub use crate::instance::{ ImportFunctionEnv, ImportInitializerFuncPtr, InstanceAllocator, InstanceHandle, WeakOrStrongInstanceRef, }; -pub use crate::memory::{LinearMemory, Memory}; +pub use crate::memory::{LinearMemory, Memory, MemoryError}; pub use crate::mmap::Mmap; pub use crate::probestack::PROBESTACK; pub use crate::sig_registry::SignatureRegistry; @@ -56,13 +56,13 @@ pub use crate::vmcontext::{ }; pub use wasmer_artifact::{FunctionBodyPtr, VMFunctionBody}; pub use wasmer_types::LibCall; +pub use wasmer_types::MemoryStyle; pub use wasmer_types::TableStyle; #[deprecated( since = "2.1.0", note = "ModuleInfo, ExportsIterator, ImportsIterator should be imported from wasmer_types." )] pub use wasmer_types::{ExportsIterator, ImportsIterator, ModuleInfo}; -pub use wasmer_types::{MemoryError, MemoryStyle}; pub use wasmer_types::{ TargetSharedSignatureIndex, VMBuiltinFunctionIndex, VMExternRef, VMOffsets, }; diff --git a/lib/vm/src/memory.rs b/lib/vm/src/memory.rs index 10768ebd1f4..75919bb7f8b 100644 --- a/lib/vm/src/memory.rs +++ b/lib/vm/src/memory.rs @@ -15,7 +15,50 @@ use std::convert::TryInto; use std::fmt; use std::ptr::NonNull; use std::sync::Mutex; -use wasmer_types::{Bytes, MemoryError, MemoryStyle, MemoryType, Pages}; +use thiserror::Error; +use wasmer_types::{Bytes, MemoryStyle, MemoryType, Pages}; + +/// Error type describing things that can go wrong when operating on Wasm Memories. +#[derive(Error, Debug, Clone, PartialEq, Hash)] +pub enum MemoryError { + /// Low level error with mmap. + #[error("Error when allocating memory: {0}")] + Region(String), + /// The operation would cause the size of the memory to exceed the maximum or would cause + /// an overflow leading to unindexable memory. + #[error("The memory could not grow: current size {} pages, requested increase: {} pages", current.0, attempted_delta.0)] + CouldNotGrow { + /// The current size in pages. + current: Pages, + /// The attempted amount to grow by in pages. + attempted_delta: Pages, + }, + /// The operation would cause the size of the memory size exceed the maximum. + #[error("The memory is invalid because {}", reason)] + InvalidMemory { + /// The reason why the provided memory is invalid. + reason: String, + }, + /// Caller asked for more minimum memory than we can give them. + #[error("The minimum requested ({} pages) memory is greater than the maximum allowed memory ({} pages)", min_requested.0, max_allowed.0)] + MinimumMemoryTooLarge { + /// The number of pages requested as the minimum amount of memory. + min_requested: Pages, + /// The maximum amount of memory we can allocate. + max_allowed: Pages, + }, + /// Caller asked for a maximum memory greater than we can give them. + #[error("The maximum requested memory ({} pages) is greater than the maximum allowed memory ({} pages)", max_requested.0, max_allowed.0)] + MaximumMemoryTooLarge { + /// The number of pages requested as the maximum amount of memory. + max_requested: Pages, + /// The number of pages requested as the maximum amount of memory. + max_allowed: Pages, + }, + /// A user defined error value, used for error cases not listed above. + #[error("A user-defined error occurred: {0}")] + Generic(String), +} /// Trait for implementing Wasm Memory used by Wasmer. pub trait Memory: fmt::Debug + Send + Sync + MemoryUsage { diff --git a/lib/vm/src/table.rs b/lib/vm/src/table.rs index 217be53aba0..3f7c8187a5b 100644 --- a/lib/vm/src/table.rs +++ b/lib/vm/src/table.rs @@ -7,6 +7,7 @@ use crate::func_data_registry::VMFuncRef; use crate::vmcontext::VMTableDefinition; +use crate::Trap; use crate::VMExternRef; use loupe::{MemoryUsage, MemoryUsageTracker}; use std::borrow::{Borrow, BorrowMut}; @@ -15,7 +16,7 @@ use std::convert::TryFrom; use std::fmt; use std::ptr::NonNull; use std::sync::Mutex; -use wasmer_types::{ExternRef, TableStyle, TableType, Trap, TrapCode, Type as ValType}; +use wasmer_types::{ExternRef, TableStyle, TableType, TrapCode, Type as ValType}; /// Trait for implementing the interface of a Wasm table. pub trait Table: fmt::Debug + Send + Sync + MemoryUsage { diff --git a/lib/vm/src/trap/mod.rs b/lib/vm/src/trap/mod.rs index 6473c2d1c33..ca764576ba4 100644 --- a/lib/vm/src/trap/mod.rs +++ b/lib/vm/src/trap/mod.rs @@ -3,11 +3,13 @@ //! This is the module that facilitates the usage of Traps //! in Wasmer Runtime +mod trap; mod traphandlers; +pub use trap::Trap; pub use traphandlers::{ catch_traps, on_host_stack, raise_lib_trap, raise_user_trap, wasmer_call_trampoline, TrapHandler, TrapHandlerFn, }; pub use traphandlers::{init_traps, resume_panic}; -pub use wasmer_types::{Trap, TrapCode}; +pub use wasmer_types::TrapCode; diff --git a/lib/vm/src/trap/trap.rs b/lib/vm/src/trap/trap.rs new file mode 100644 index 00000000000..fdb20d3f590 --- /dev/null +++ b/lib/vm/src/trap/trap.rs @@ -0,0 +1,72 @@ +use backtrace::Backtrace; +use std::error::Error; +use wasmer_types::TrapCode; + +/// Stores trace message with backtrace. +#[derive(Debug)] +pub enum Trap { + /// A user-raised trap through `raise_user_trap`. + User(Box), + + /// A trap raised from the Wasm generated code + /// + /// Note: this trap is deterministic (assuming a deterministic host implementation) + Wasm { + /// The program counter in generated code where this trap happened. + pc: usize, + /// Native stack backtrace at the time the trap occurred + backtrace: Backtrace, + /// Optional trapcode associated to the signal that caused the trap + signal_trap: Option, + }, + + /// A trap raised from a wasm libcall + /// + /// Note: this trap is deterministic (assuming a deterministic host implementation) + Lib { + /// Code of the trap. + trap_code: TrapCode, + /// Native stack backtrace at the time the trap occurred + backtrace: Backtrace, + }, + + /// A trap indicating that the runtime was unable to allocate sufficient memory. + /// + /// Note: this trap is nondeterministic, since it depends on the host system. + OOM { + /// Native stack backtrace at the time the OOM occurred + backtrace: Backtrace, + }, +} + +impl Trap { + /// Construct a new Wasm trap with the given source location and backtrace. + /// + /// Internally saves a backtrace when constructed. + pub fn wasm(pc: usize, backtrace: Backtrace, signal_trap: Option) -> Self { + Trap::Wasm { + pc, + backtrace, + signal_trap, + } + } + + /// Construct a new Wasm trap with the given trap code. + /// + /// Internally saves a backtrace when constructed. + pub fn lib(trap_code: TrapCode) -> Self { + let backtrace = Backtrace::new_unresolved(); + Trap::Lib { + trap_code, + backtrace, + } + } + + /// Construct a new OOM trap with the given source location and trap code. + /// + /// Internally saves a backtrace when constructed. + pub fn oom() -> Self { + let backtrace = Backtrace::new_unresolved(); + Trap::OOM { backtrace } + } +} diff --git a/lib/vm/src/trap/traphandlers.rs b/lib/vm/src/trap/traphandlers.rs index 5d83ec93541..baecaf387f3 100644 --- a/lib/vm/src/trap/traphandlers.rs +++ b/lib/vm/src/trap/traphandlers.rs @@ -5,6 +5,7 @@ //! signalhandling mechanisms. use crate::vmcontext::{VMFunctionBody, VMFunctionEnvironment, VMTrampoline}; +use crate::Trap; use backtrace::Backtrace; use corosensei::stack::DefaultStack; use corosensei::trap::{CoroutineTrapHandler, TrapHandlerRegs}; @@ -20,7 +21,7 @@ use std::mem::MaybeUninit; use std::ptr::{self, NonNull}; use std::sync::atomic::{compiler_fence, AtomicPtr, Ordering}; use std::sync::{Mutex, Once}; -use wasmer_types::{Trap, TrapCode}; +use wasmer_types::TrapCode; cfg_if::cfg_if! { if #[cfg(unix)] {