diff --git a/Cargo.lock b/Cargo.lock index 65b1381e64d..ea0545bb901 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3923,6 +3923,7 @@ dependencies = [ "distance", "fern", "http_req", + "libc", "log", "nuke-dir", "prettytable-rs", diff --git a/lib/cli/Cargo.toml b/lib/cli/Cargo.toml index 299980527ba..45987508bb9 100644 --- a/lib/cli/Cargo.toml +++ b/lib/cli/Cargo.toml @@ -67,6 +67,7 @@ walkdir = "2.3.2" regex = "1.6.0" toml = "0.5.9" url = "2.3.1" +libc = { version = "^0.2", default-features = false } nuke-dir = { version = "0.1.0", optional = true } webc = { version = "3.0.1", optional = true } diff --git a/lib/cli/src/error.rs b/lib/cli/src/error.rs index f23b50339c0..45040af9741 100644 --- a/lib/cli/src/error.rs +++ b/lib/cli/src/error.rs @@ -3,6 +3,7 @@ use anyhow::{Chain, Error}; use colored::*; use std::fmt::{self, Debug, Write}; +use wasmer::RuntimeError; /// A `PrettyError` for printing `anyhow::Error` nicely. pub struct PrettyError { @@ -25,8 +26,19 @@ impl PrettyError { std::process::exit(match result { Ok(_t) => 0, Err(error) => { + let runtime: Option<&RuntimeError> = error.downcast_ref(); + let trapcode = runtime.map(|e| e.clone().to_trap()); eprintln!("{:?}", PrettyError { error }); - 1 + // we don't use process:abort() here to avoid message from rust + // that could interfer with testing tools + // but still exit with the expected error code + match trapcode { + #[cfg(target_os = "windows")] + Some(_) => 3, + #[cfg(not(target_os = "windows"))] + Some(_) => 128 + libc::SIGABRT, + _ => 1, + } } }); } diff --git a/lib/types/src/trapcode.rs b/lib/types/src/trapcode.rs index 70875974384..7403f0f3e56 100644 --- a/lib/types/src/trapcode.rs +++ b/lib/types/src/trapcode.rs @@ -39,29 +39,26 @@ pub enum TrapCode { /// A `table_addr` instruction detected an out-of-bounds error. TableAccessOutOfBounds = 3, - /// Other bounds checking error. - OutOfBounds = 4, - /// Indirect call to a null table entry. - IndirectCallToNull = 5, + IndirectCallToNull = 4, /// Signature mismatch on indirect call. - BadSignature = 6, + BadSignature = 5, /// An integer arithmetic operation caused an overflow. - IntegerOverflow = 7, + IntegerOverflow = 6, /// An integer division by zero. - IntegerDivisionByZero = 8, + IntegerDivisionByZero = 7, /// Failed float-to-int conversion. - BadConversionToInteger = 9, + BadConversionToInteger = 8, /// Code that was supposed to have been unreachable was reached. - UnreachableCodeReached = 10, + UnreachableCodeReached = 9, /// An atomic memory access was attempted with an unaligned pointer. - UnalignedAtomic = 11, + UnalignedAtomic = 10, } impl TrapCode { @@ -72,7 +69,6 @@ impl TrapCode { Self::HeapAccessOutOfBounds => "out of bounds memory access", Self::HeapMisaligned => "misaligned heap", Self::TableAccessOutOfBounds => "undefined element: out of bounds table access", - Self::OutOfBounds => "out of bounds", Self::IndirectCallToNull => "uninitialized element", Self::BadSignature => "indirect call type mismatch", Self::IntegerOverflow => "integer overflow", @@ -91,7 +87,6 @@ impl Display for TrapCode { Self::HeapAccessOutOfBounds => "heap_get_oob", Self::HeapMisaligned => "heap_misaligned", Self::TableAccessOutOfBounds => "table_get_oob", - Self::OutOfBounds => "oob", Self::IndirectCallToNull => "icall_null", Self::BadSignature => "bad_sig", Self::IntegerOverflow => "int_ovf", @@ -113,7 +108,6 @@ impl FromStr for TrapCode { "heap_get_oob" => Ok(Self::HeapAccessOutOfBounds), "heap_misaligned" => Ok(Self::HeapMisaligned), "table_get_oob" => Ok(Self::TableAccessOutOfBounds), - "oob" => Ok(Self::OutOfBounds), "icall_null" => Ok(Self::IndirectCallToNull), "bad_sig" => Ok(Self::BadSignature), "int_ovf" => Ok(Self::IntegerOverflow), @@ -131,12 +125,11 @@ mod tests { use super::*; // Everything but user-defined codes. - const CODES: [TrapCode; 12] = [ + const CODES: [TrapCode; 11] = [ TrapCode::StackOverflow, TrapCode::HeapAccessOutOfBounds, TrapCode::HeapMisaligned, TrapCode::TableAccessOutOfBounds, - TrapCode::OutOfBounds, TrapCode::IndirectCallToNull, TrapCode::BadSignature, TrapCode::IntegerOverflow, diff --git a/lib/vm/src/trap/traphandlers.rs b/lib/vm/src/trap/traphandlers.rs index fc8af76fcfc..2899b5d182b 100644 --- a/lib/vm/src/trap/traphandlers.rs +++ b/lib/vm/src/trap/traphandlers.rs @@ -74,14 +74,13 @@ unsafe fn process_illegal_op(addr: usize) -> Option { 1 => Some(TrapCode::HeapAccessOutOfBounds), 2 => Some(TrapCode::HeapMisaligned), 3 => Some(TrapCode::TableAccessOutOfBounds), - 4 => Some(TrapCode::OutOfBounds), - 5 => Some(TrapCode::IndirectCallToNull), - 6 => Some(TrapCode::BadSignature), - 7 => Some(TrapCode::IntegerOverflow), - 8 => Some(TrapCode::IntegerDivisionByZero), - 9 => Some(TrapCode::BadConversionToInteger), - 10 => Some(TrapCode::UnreachableCodeReached), - 11 => Some(TrapCode::UnalignedAtomic), + 4 => Some(TrapCode::IndirectCallToNull), + 5 => Some(TrapCode::BadSignature), + 6 => Some(TrapCode::IntegerOverflow), + 7 => Some(TrapCode::IntegerDivisionByZero), + 8 => Some(TrapCode::BadConversionToInteger), + 9 => Some(TrapCode::UnreachableCodeReached), + 10 => Some(TrapCode::UnalignedAtomic), _ => None, }, } diff --git a/lib/wasi/src/state/mod.rs b/lib/wasi/src/state/mod.rs index e6e46395a40..69f83ee548e 100644 --- a/lib/wasi/src/state/mod.rs +++ b/lib/wasi/src/state/mod.rs @@ -1093,7 +1093,8 @@ impl WasiFs { { cur_inode = *entry; } else { - return Err(Errno::Noent); + // Root is not capable of having something other then preopenned folders + return Err(Errno::Notcapable); } } Kind::File { .. } diff --git a/lib/wasi/src/syscalls/mod.rs b/lib/wasi/src/syscalls/mod.rs index 7f210de0914..f92a1625e33 100644 --- a/lib/wasi/src/syscalls/mod.rs +++ b/lib/wasi/src/syscalls/mod.rs @@ -2396,8 +2396,12 @@ pub fn path_open( .map_err(fs_error_into_wasi_err))); } Kind::Buffer { .. } => unimplemented!("wasi::path_open for Buffer type files"), + Kind::Root { .. } => { + if !o_flags.contains(Oflags::DIRECTORY) { + return Errno::Notcapable; + } + } Kind::Dir { .. } - | Kind::Root { .. } | Kind::Socket { .. } | Kind::Pipe { .. } | Kind::EventNotifications { .. } => {}