Skip to content

Commit

Permalink
Merge #3266
Browse files Browse the repository at this point in the history
3266: Return ENotCapable error when accessing unknown files on root (for #3263 and #3264) r=ptitSeb a=ptitSeb

Fixes for wasm.fyi tests:
 * When accessing bad path from the root, return ENOTCAPABLE error instead of ENOENT (root can only contains pre-openned folder, no other capability)
 * Accessing ROOT for other things then entering folder should result in ENOTCAPABLE


Co-authored-by: ptitSeb <sebastien.chev@gmail.com>
  • Loading branch information
bors[bot] and ptitSeb authored Nov 3, 2022
2 parents 778c04d + 9dc53cb commit 9a7877b
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 26 deletions.
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 @@ -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 }

Expand Down
14 changes: 13 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,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,
}
}
});
}
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

0 comments on commit 9a7877b

Please sign in to comment.