forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change proc_exit to unwind instead of exit the host process.
This implements the semantics in WebAssembly/WASI#235. Fixes bytecodealliance#783. Fixes bytecodealliance#993.
- Loading branch information
1 parent
6dd85ef
commit a7b4cb0
Showing
26 changed files
with
401 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::frame_info::FRAME_INFO; | ||
use crate::{Exit, Trap}; | ||
use anyhow::Error; | ||
use wasmtime_environ::ir::TrapCode; | ||
|
||
/// Convert from an internal unwinding code into an `Error`. | ||
pub(crate) fn from_runtime(jit: wasmtime_runtime::Trap) -> Error { | ||
let info = FRAME_INFO.read().unwrap(); | ||
match jit { | ||
wasmtime_runtime::Trap::User(error) => { | ||
// Since we're the only one using the wasmtime internals (in | ||
// theory) we should only see user errors which were originally | ||
// created from our own `Trap` and `Exit` types (see the trampoline | ||
// module with functions). | ||
let e = match error.downcast::<Exit>() { | ||
Ok(exit) => Error::new(exit), | ||
Err(e) => match e.downcast::<wasmtime_runtime::InvalidWASIExitStatus>() { | ||
Ok(invalid) => Error::new(invalid), | ||
Err(e) => Error::new(dbg!(e | ||
.downcast::<Trap>() | ||
.expect("only `Trap` and `Exit` user errors are supported"))), | ||
}, | ||
}; | ||
dbg!(&e); | ||
dbg!(e.is::<Trap>()); | ||
dbg!(e.is::<wasmtime_runtime::Trap>()); | ||
e | ||
} | ||
wasmtime_runtime::Trap::Jit { | ||
pc, | ||
backtrace, | ||
maybe_interrupted, | ||
} => { | ||
let mut code = info | ||
.lookup_trap_info(pc) | ||
.map(|info| info.trap_code) | ||
.unwrap_or(TrapCode::StackOverflow); | ||
if maybe_interrupted && code == TrapCode::StackOverflow { | ||
code = TrapCode::Interrupt; | ||
} | ||
Error::new(Trap::new_wasm(&info, Some(pc), code, backtrace)) | ||
} | ||
wasmtime_runtime::Trap::Wasm { | ||
trap_code, | ||
backtrace, | ||
} => Error::new(Trap::new_wasm(&info, None, trap_code, backtrace)), | ||
wasmtime_runtime::Trap::OOM { backtrace } => Error::new(Trap::new_with_trace( | ||
&info, | ||
None, | ||
"out of memory".to_string(), | ||
backtrace, | ||
)), | ||
wasmtime_runtime::Trap::Exit { status } => Error::new(Exit::new(status)), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::fmt; | ||
use std::num::NonZeroI32; | ||
|
||
/// A struct representing an explicit program exit, with a code | ||
/// indicating the status. | ||
#[derive(Clone, Debug)] | ||
pub struct Exit { | ||
status: NonZeroI32, | ||
} | ||
|
||
impl Exit { | ||
/// Creates a new `Exit` with `status`. The status value must be in the range [1..126]. | ||
/// # Example | ||
/// ``` | ||
/// let exit = wasmtime::Exit::new(NonZeroI32::new(1).unwrap()); | ||
/// assert_eq!(1, trap.status()); | ||
/// ``` | ||
pub fn new(status: NonZeroI32) -> Self { | ||
assert!(status.get() > 0 && status.get() < 126); | ||
Self { status } | ||
} | ||
|
||
/// Returns a reference the `status` stored in `Exit`. | ||
pub fn status(&self) -> NonZeroI32 { | ||
self.status | ||
} | ||
} | ||
|
||
impl fmt::Display for Exit { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
// Error codes traditionally defined in <sysexits.h>, though somewhat generalized. | ||
match self.status.get() { | ||
64 => write!(f, "command line usage error"), | ||
65 => write!(f, "input data format error"), | ||
66 => write!(f, "inadequate input"), | ||
67 => write!(f, "named identity not recognized"), | ||
68 => write!(f, "named resource could not be located"), | ||
69 => write!(f, "service unavailable"), | ||
70 => write!(f, "internal software error"), | ||
71 => write!(f, "system error"), | ||
72 => write!(f, "unexpected host environment configuration"), | ||
73 => write!(f, "inadequate output"), | ||
74 => write!(f, "input/output error"), | ||
75 => write!(f, "temporary failure; user is invited to retry"), | ||
76 => write!(f, "interactive protocol error"), | ||
77 => write!(f, "permission denied"), | ||
78 => write!(f, "application configuration error"), | ||
_ => write!(f, "non-zero status {}", self.status), | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for Exit {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.