diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/lucet-module-wasmsbx/src/traps.rs | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/lucet-module-wasmsbx/src/traps.rs')
-rw-r--r-- | third_party/rust/lucet-module-wasmsbx/src/traps.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/third_party/rust/lucet-module-wasmsbx/src/traps.rs b/third_party/rust/lucet-module-wasmsbx/src/traps.rs new file mode 100644 index 0000000000..28f01c60ef --- /dev/null +++ b/third_party/rust/lucet-module-wasmsbx/src/traps.rs @@ -0,0 +1,63 @@ +use num_derive::FromPrimitive; +use num_traits::FromPrimitive; + +/// The type of a WebAssembly +/// [trap](http://webassembly.github.io/spec/core/intro/overview.html#trap). +#[repr(u32)] +#[derive(Copy, Clone, Debug, FromPrimitive, PartialEq)] +pub enum TrapCode { + StackOverflow = 0, + HeapOutOfBounds = 1, + OutOfBounds = 2, + IndirectCallToNull = 3, + BadSignature = 4, + IntegerOverflow = 5, + IntegerDivByZero = 6, + BadConversionToInteger = 7, + Interrupt = 8, + TableOutOfBounds = 9, + Unreachable = 10, +} + +impl TrapCode { + pub fn try_from_u32(v: u32) -> Option<TrapCode> { + Self::from_u32(v) + } +} + +/// Trap information for an address in a compiled function +/// +/// To support zero-copy deserialization of trap tables, this +/// must be repr(C) [to avoid cases where Rust may change the +/// layout in some future version, mangling the interpretation +/// of an old TrapSite struct] +#[repr(C)] +#[derive(Clone, Debug)] +pub struct TrapSite { + pub offset: u32, + pub code: TrapCode, +} + +/// A collection of trap sites, typically obtained from a +/// single function (see [`FunctionSpec::traps`]) +#[repr(C)] +#[derive(Clone, Debug)] +pub struct TrapManifest<'a> { + pub traps: &'a [TrapSite], +} + +impl<'a> TrapManifest<'a> { + pub fn new(traps: &'a [TrapSite]) -> TrapManifest<'_> { + TrapManifest { traps } + } + pub fn lookup_addr(&self, addr: u32) -> Option<TrapCode> { + // predicate to find the trapsite for the addr via binary search + let f = |ts: &TrapSite| ts.offset.cmp(&addr); + + if let Ok(i) = self.traps.binary_search_by(f) { + Some(self.traps[i].code) + } else { + None + } + } +} |