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

Return ENotCapable error when accessing unknown files on root (for #3263 and #3264) #3266

Merged
merged 8 commits into from
Nov 3, 2022
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.

1 change: 1 addition & 0 deletions lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ walkdir = "2.3.2"
regex = "1.6.0"
toml = "0.5.9"
url = "2.3.1"
libc = { version = "^0.2", default-features = false }

[build-dependencies]
chrono = { version = "^0.4", default-features = false, features = [ "std", "clock" ] }
Expand Down
11 changes: 10 additions & 1 deletion lib/cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,8 +26,16 @@ 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
match trapcode {
#[cfg(target_os = "windows")]
Some(_) => 3,
#[cfg(not(target_os = "windows"))]
Some(_) => 128 + libc::SIGABRT,
_ => 1,
}
syrusakbary marked this conversation as resolved.
Show resolved Hide resolved
}
});
}
Expand Down
23 changes: 8 additions & 15 deletions lib/types/src/trapcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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),
Expand All @@ -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,
Expand Down
15 changes: 7 additions & 8 deletions lib/vm/src/trap/traphandlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,13 @@ unsafe fn process_illegal_op(addr: usize) -> Option<TrapCode> {
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,
},
}
Expand Down
3 changes: 2 additions & 1 deletion lib/wasi/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { .. }
Expand Down
6 changes: 5 additions & 1 deletion lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2396,8 +2396,12 @@ pub fn path_open<M: MemorySize>(
.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 { .. } => {}
Expand Down