cranelift: improve syscall error/oom handling in JIT module (#5173)

* cranelift: improve syscall error/oom handling in JIT module

The JIT module has several places where it `expect`s or `panic`s
on syscall or allocator errors. For example, `mmap` and `mprotect`
can fail if Linux `vm.max_map_count` is not high enough, and some
users may wish to handle this error rather than immediately
crashing.

This commit plumbs these errors upward as new `ModuleError`
types, so that callers of jit module functions like
`finalize_definitions` and `define_function` can handle them
(or just `unwrap()`, as desired).

* cranelift: Remove ModuleError::Syscall variant

Syscall errors can just be folded into the generic Backend error,
which is an anyhow::Error

* cranelift-jit: return io::ErrorKind::OutOfMemory for alloc failure

Just using `io::Error::last_os_error()` is not correct as global
allocator impls are not required to set errno
This commit is contained in:
11evan
2022-11-03 16:59:41 -07:00
committed by GitHub
parent 5285ba15b1
commit 387426e7f4
6 changed files with 69 additions and 24 deletions

View File

@@ -229,6 +229,14 @@ pub enum ModuleError {
/// Wraps a `cranelift-codegen` error
Compilation(CodegenError),
/// Memory allocation failure from a backend
Allocation {
/// Tell where the allocation came from
message: &'static str,
/// Io error the allocation failed with
err: std::io::Error,
},
/// Wraps a generic error from a backend
Backend(anyhow::Error),
}
@@ -250,6 +258,7 @@ impl std::error::Error for ModuleError {
| Self::DuplicateDefinition { .. }
| Self::InvalidImportDefinition { .. } => None,
Self::Compilation(source) => Some(source),
Self::Allocation { err: source, .. } => Some(source),
Self::Backend(source) => Some(&**source),
}
}
@@ -284,6 +293,9 @@ impl std::fmt::Display for ModuleError {
Self::Compilation(err) => {
write!(f, "Compilation error: {}", err)
}
Self::Allocation { message, err } => {
write!(f, "Allocation error: {}: {}", message, err)
}
Self::Backend(err) => write!(f, "Backend error: {}", err),
}
}