use serde::Deserialize;
use serde::Serialize;
pub type Error = anyhow::Error;
pub use anyhow::Context;
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, Eq, PartialEq)]
pub enum FailureKind {
#[default]
Error,
Panic,
PolicyRefusal,
SkidOvershoot { count: u64 },
RunTimeout,
}
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub struct SerializableError {
error: String,
context: Vec<String>,
#[serde(default)]
kind: FailureKind,
cleanup_stage: Option<crate::HermitCleanupStage>,
}
impl SerializableError {
pub fn kind(&self) -> FailureKind {
self.kind
}
pub fn cleanup_stage(&self) -> Option<crate::HermitCleanupStage> {
self.cleanup_stage
}
pub fn into_panic(mut self) -> Self {
self.kind = FailureKind::Panic;
self
}
}
fn is_policy_refusal(err: &Error) -> bool {
if err
.chain()
.any(|cause| cause.is::<detcore::UnsupportedSyscallError>())
{
return true;
}
err.chain().any(|cause| {
cause
.downcast_ref::<reverie::Error>()
.and_then(|reverie_error| match reverie_error {
reverie::Error::Tool(inner) => {
inner.downcast_ref::<detcore::UnsupportedSyscallError>()
}
_ => None,
})
.is_some()
})
}
pub(crate) fn classify_ptrace_primary(error: &reverie::Error) -> FailureKind {
match error {
reverie::Error::Tool(inner) => classify_unwrapped(inner),
_ => FailureKind::Error,
}
}
fn classify_unwrapped(error: &Error) -> FailureKind {
if let Some(error) = error.downcast_ref::<crate::SkidOvershootError>() {
FailureKind::SkidOvershoot {
count: error.count(),
}
} else if is_policy_refusal(error)
|| error
.downcast_ref::<crate::LiteinstInGuestRefusal>()
.is_some()
{
FailureKind::PolicyRefusal
} else if error.downcast_ref::<crate::GuestTimedOut>().is_some() {
FailureKind::RunTimeout
} else {
FailureKind::Error
}
}
impl From<Error> for SerializableError {
fn from(err: Error) -> Self {
let error = err.to_string();
let context = err.chain().skip(1).map(ToString::to_string).collect();
let (kind, cleanup_stage) =
if let Some(error) = err.downcast_ref::<crate::HermitCleanupUnconfirmed>() {
(error.primary_kind(), Some(error.stage()))
} else if let Some(error) = err.downcast_ref::<crate::HermitPtraceFailure>() {
(error.kind(), None)
} else {
(classify_unwrapped(&err), None)
};
Self {
error,
context,
kind,
cleanup_stage,
}
}
}
impl From<SerializableError> for Error {
fn from(mut err: SerializableError) -> Self {
if let Some(root_cause) = err.context.pop() {
let mut error = Self::msg(root_cause);
while let Some(context) = err.context.pop() {
error = error.context(context);
}
error.context(err.error)
} else {
Self::msg(err.error)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn into_serializable_error() {
let error = Error::msg("root cause")
.context("a")
.context("b")
.context("c");
assert_eq!(
SerializableError::from(error),
SerializableError {
error: "c".into(),
context: vec!["b".into(), "a".into(), "root cause".into(),],
kind: FailureKind::Error,
cleanup_stage: None,
}
);
}
#[test]
fn from_serializable_error() {
let error = Error::from(SerializableError {
error: "c".into(),
context: vec!["b".into(), "a".into(), "root cause".into()],
kind: FailureKind::Error,
cleanup_stage: None,
});
assert_eq!(
error
.chain()
.map(ToString::to_string)
.collect::<Vec<String>>(),
["c", "b", "a", "root cause"]
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>(),
)
}
#[test]
fn skid_overshoot_is_serialized_as_a_policy_refusal() {
let refusal = SerializableError::from(Error::new(crate::SkidOvershootError::new(2)));
assert_eq!(refusal.kind(), FailureKind::SkidOvershoot { count: 2 });
assert!(refusal.error.contains("observed 2 HERMIT_SKID_OVERSHOOT"));
let contextual = SerializableError::from(
Error::msg("guest execution also failed").context(crate::SkidOvershootError::new(1)),
);
assert_eq!(contextual.kind(), FailureKind::SkidOvershoot { count: 1 });
assert!(
contextual
.error
.contains("observed 1 HERMIT_SKID_OVERSHOOT")
);
let ordinary = SerializableError::from(Error::msg("ordinary failure"));
assert_eq!(ordinary.kind(), FailureKind::Error);
}
#[test]
fn in_guest_liteinst_refusal_is_serialized_as_a_policy_refusal() {
let refusal = SerializableError::from(Error::new(crate::LiteinstInGuestRefusal::new(
"guest program /static is statically linked",
)));
assert_eq!(refusal.kind(), FailureKind::PolicyRefusal);
assert_eq!(
refusal.error,
"HERMIT_LITEINST_IN_GUEST=1 (in-guest LiteInst) refuses this run: guest program \
/static is statically linked"
);
}
}