Skip to main content

wasmtime_environ/
trap_encoding.rs

1use crate::bytes;
2use core::fmt;
3use object::{Bytes, LittleEndian, U32};
4
5/// Information about trap.
6#[derive(Debug, PartialEq, Eq, Clone)]
7pub struct TrapInformation {
8    /// The offset of the trapping instruction in native code.
9    ///
10    /// This is relative to the beginning of the function.
11    pub code_offset: u32,
12
13    /// Code of the trap.
14    pub trap_code: CompiledTrap,
15}
16
17/// Possible traps that can be compiled into WebAssembly modules.
18#[derive(Debug, PartialEq, Eq, Clone)]
19pub enum CompiledTrap {
20    /// A normal trap, expected to possibly be hit at runtime.
21    Normal(Trap),
22    /// An internal assertion in the compiled code itself.
23    InternalAssert,
24    /// The GC heap was detected as being corrupt.
25    GcHeapCorrupt,
26}
27
28impl CompiledTrap {
29    /// Encodes this as a byte.
30    pub fn as_u8(&self) -> u8 {
31        match self {
32            CompiledTrap::Normal(trap) => {
33                let ret = *trap as u8;
34                debug_assert_ne!(ret, CompiledTrap::InternalAssert.as_u8());
35                debug_assert_ne!(ret, CompiledTrap::GcHeapCorrupt.as_u8());
36                ret
37            }
38            CompiledTrap::InternalAssert => 0xFF,
39            CompiledTrap::GcHeapCorrupt => 0xFE,
40        }
41    }
42
43    /// Decodes a byte as a trap.
44    pub fn from_u8(byte: u8) -> Option<CompiledTrap> {
45        if byte == 0xFF {
46            Some(CompiledTrap::InternalAssert)
47        } else if byte == 0xFE {
48            Some(CompiledTrap::GcHeapCorrupt)
49        } else {
50            Trap::from_u8(byte).map(CompiledTrap::Normal)
51        }
52    }
53}
54
55impl From<Trap> for CompiledTrap {
56    fn from(trap: Trap) -> CompiledTrap {
57        CompiledTrap::Normal(trap)
58    }
59}
60
61macro_rules! generate_trap_type {
62    (pub enum Trap {
63        $(
64            $(#[$doc:meta])*
65            $name:ident = $msg:tt,
66        )*
67    }) => {
68        #[non_exhaustive]
69        #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
70        #[expect(missing_docs, reason = "self-describing variants")]
71        pub enum Trap {
72            $(
73                $(#[$doc])*
74                $name,
75            )*
76        }
77
78        impl Trap {
79            /// Converts a byte back into a `Trap` if its in-bounds
80            pub fn from_u8(byte: u8) -> Option<Trap> {
81                $(
82                    if byte == Trap::$name as u8 {
83                        return Some(Trap::$name);
84                    }
85                )*
86                None
87            }
88        }
89
90        impl fmt::Display for Trap {
91            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92                let desc = match self {
93                    $(Self::$name => $msg,)*
94                };
95                write!(f, "wasm trap: {desc}")
96            }
97        }
98    }
99}
100
101// The code can be accessed from the c-api, where the possible values are
102// translated into enum values defined there:
103//
104// *  the const assertions in c-api/src/trap.rs, and
105// * `wasmtime_trap_code_enum` in c-api/include/wasmtime/trap.h.
106//
107// These need to be kept in sync.
108generate_trap_type! {
109    pub enum Trap {
110        /// The current stack space was exhausted.
111        StackOverflow = "call stack exhausted",
112
113        /// An out-of-bounds memory access.
114        MemoryOutOfBounds = "out of bounds memory access",
115
116        /// A wasm atomic operation was presented with a not-naturally-aligned linear-memory address.
117        HeapMisaligned = "unaligned atomic",
118
119        /// An out-of-bounds access to a table.
120        TableOutOfBounds = "undefined element: out of bounds table access",
121
122        /// Indirect call to a null table entry.
123        IndirectCallToNull = "uninitialized element",
124
125        /// Signature mismatch on indirect call.
126        BadSignature = "indirect call type mismatch",
127
128        /// An integer arithmetic operation caused an overflow.
129        IntegerOverflow = "integer overflow",
130
131        /// An integer division by zero.
132        IntegerDivisionByZero = "integer divide by zero",
133
134        /// Failed float-to-int conversion.
135        BadConversionToInteger = "invalid conversion to integer",
136
137        /// Code that was supposed to have been unreachable was reached.
138        UnreachableCodeReached = "wasm `unreachable` instruction executed",
139
140        /// Execution has potentially run too long and may be interrupted.
141        Interrupt = "interrupt",
142
143        /// When wasm code is configured to consume fuel and it runs out of fuel
144        /// then this trap will be raised.
145        OutOfFuel = "all fuel consumed by WebAssembly",
146
147        /// Used to indicate that a trap was raised by atomic wait operations on non shared memory.
148        AtomicWaitNonSharedMemory = "atomic wait on non-shared memory",
149
150        /// Call to a null reference.
151        NullReference = "null reference",
152
153        /// Attempt to access beyond the bounds of an array.
154        ArrayOutOfBounds = "out of bounds array access",
155
156        /// Attempted an allocation that was too large to succeed.
157        AllocationTooLarge = "allocation size too large",
158
159        /// Attempted to cast a reference to a type that it is not an instance of.
160        CastFailure = "cast failure",
161
162        /// When the `component-model` feature is enabled this trap represents a
163        /// scenario where one component tried to call another component but it
164        /// would have violated the reentrance rules of the component model,
165        /// triggering a trap instead.
166        CannotEnterComponent = "cannot enter component instance",
167
168        /// Async-lifted export failed to produce a result by calling `task.return`
169        /// before returning `STATUS_DONE` and/or after all host tasks completed.
170        NoAsyncResult = "async-lifted export failed to produce a result",
171
172        /// We are suspending to a tag for which there is no active handler.
173        UnhandledTag = "unhandled tag",
174
175        /// Attempt to resume a continuation twice.
176        ContinuationAlreadyConsumed = "continuation already consumed",
177
178        /// A Pulley opcode was executed at runtime when the opcode was disabled at
179        /// compile time.
180        DisabledOpcode = "pulley opcode disabled at compile time was executed",
181
182        /// Async event loop deadlocked; i.e. it cannot make further progress given
183        /// that all host tasks have completed and any/all host-owned stream/future
184        /// handles have been dropped.
185        AsyncDeadlock = "deadlock detected: event loop cannot make further progress",
186
187        /// When the `component-model` feature is enabled this trap represents a
188        /// scenario where a component instance tried to call an import or intrinsic
189        /// when it wasn't allowed to, e.g. from a post-return function.
190        CannotLeaveComponent = "cannot leave component instance",
191
192        /// A synchronous task attempted to make a potentially blocking call prior
193        /// to returning.
194        CannotBlockSyncTask = "cannot block a synchronous task before returning",
195
196        /// A component tried to lift a `char` with an invalid bit pattern.
197        InvalidChar = "invalid `char` bit pattern",
198
199        /// Debug assertion generated for a fused adapter regarding the expected
200        /// completion of a string encoding operation.
201        DebugAssertStringEncodingFinished = "should have finished string encoding",
202
203        /// Debug assertion generated for a fused adapter regarding a string
204        /// encoding operation.
205        DebugAssertEqualCodeUnits = "code units should be equal",
206
207        /// Debug assertion generated for a fused adapter regarding the alignment of
208        /// a pointer.
209        DebugAssertPointerAligned = "pointer should be aligned",
210
211        /// Debug assertion generated for a fused adapter regarding the upper bits
212        /// of a 64-bit value.
213        DebugAssertUpperBitsUnset = "upper bits should be unset",
214
215        /// A component tried to lift or lower a string past the end of its memory.
216        StringOutOfBounds = "string content out-of-bounds",
217
218        /// A component tried to lift or lower a list past the end of its memory.
219        ListOutOfBounds = "list content out-of-bounds",
220
221        /// A component used an invalid discriminant when lowering a variant value.
222        InvalidDiscriminant = "invalid variant discriminant",
223
224        /// A component passed an unaligned pointer when lifting or lowering a
225        /// value.
226        UnalignedPointer = "unaligned pointer",
227
228        /// `task.cancel` was called by a task which has not been cancelled.
229        TaskCancelNotCancelled = "`task.cancel` called by task which has not been cancelled",
230
231        /// `task.return` or `task.cancel` was called more than once for the
232        /// current task.
233        TaskCancelOrReturnTwice = "`task.return` or `task.cancel` called more than once for current task",
234
235        /// `subtask.cancel` was called after terminal status was already
236        /// delivered.
237        SubtaskCancelAfterTerminal = "`subtask.cancel` called after terminal status delivered",
238
239        /// Invalid `task.return` signature and/or options for the current task.
240        TaskReturnInvalid = "invalid `task.return` signature and/or options for current task",
241
242        /// Cannot drop waitable set with waiters in it.
243        WaitableSetDropHasWaiters = "cannot drop waitable set with waiters",
244
245        /// Cannot drop a subtask which has not yet resolved.
246        SubtaskDropNotResolved = "cannot drop a subtask which has not yet resolved",
247
248        /// Start function does not match the expected type.
249        ThreadNewIndirectInvalidType = "start function does not match expected type (currently only `(i32) -> ()` is supported)",
250
251        /// The start function index points to an uninitialized function.
252        ThreadNewIndirectUninitialized = "the start function index points to an uninitialized function",
253
254        /// Backpressure-related intrinsics overflowed the built-in 16-bit
255        /// counter.
256        BackpressureOverflow = "backpressure counter overflow",
257
258        /// Invalid code returned from `callback` of `async`-lifted function.
259        UnsupportedCallbackCode = "unsupported callback code",
260
261        /// Cannot resume a thread which is not suspended.
262        CannotResumeThread = "cannot resume thread which is not suspended",
263
264        /// Cannot issue a read/write on a future/stream while there is a
265        /// pending operation already.
266        ConcurrentFutureStreamOp = "cannot have concurrent operations active on a future/stream",
267
268        /// A reference count (for e.g. an `error-context`) overflowed.
269        ReferenceCountOverflow = "reference count overflow",
270
271        /// A read/write on a stream must be <2**28 items.
272        StreamOpTooBig = "stream read/write count too large",
273
274        /// The guest either attempted to add a waitable to a waitable set while
275        /// it was being used in a synchronous operation or tried to use it in a
276        /// synchronous operation while it was added to a waitable set.
277        WaitableSyncAndAsync = "waitable cannot be used synchronously while added to a waitable set",
278
279        /// An exception propagated out of a component without being caught.
280        UncaughtException = "uncaught exception propagated out of component",
281
282        // if adding a variant here be sure to update `trap.rs` and `trap.h` as
283        // mentioned above
284    }
285}
286
287impl core::error::Error for Trap {}
288
289/// Number of trap entries packed into one block of the trap section.
290///
291/// See `TrapEncodingBuilder` in `crate::compile` for the full section format.
292/// Chosen as a balance between the fixed-width index overhead per block (8
293/// bytes, amortized across entries) and the amount of linear decoding required
294/// to look up a single pc within a block.
295pub(crate) const TRAP_BLOCK_SIZE: usize = 128;
296
297/// Decodes the provided trap information section and attempts to find the trap
298/// code corresponding to the `offset` specified.
299///
300/// The `section` provided is expected to have been built by
301/// `TrapEncodingBuilder` in `crate::compile`, whose documentation describes
302/// the format decoded here. Additionally the `offset` should be a relative
303/// offset within the text section of the compilation image.
304pub fn lookup_trap_code(section: &[u8], offset: usize) -> Option<CompiledTrap> {
305    let section = parse(section)?;
306    let offset = u32::try_from(offset).ok()?;
307
308    // Find the last block whose first pc is `<= offset`; only that block can
309    // contain `offset`. Note that this is a precise search because trap pcs
310    // should always be precise as well as our metadata about them, which means
311    // we expect an exact match to correspond to a trap opcode.
312    let block = section
313        .block_index
314        .partition_point(|[first_offset, _]| first_offset.get(LittleEndian) <= offset)
315        .checked_sub(1)?;
316
317    for (pc, byte) in section.block_entries(block)? {
318        if pc == offset {
319            let trap = CompiledTrap::from_u8(byte);
320            debug_assert!(trap.is_some(), "missing mapping for {byte}");
321            return trap;
322        }
323        if pc > offset {
324            break;
325        }
326    }
327    None
328}
329
330/// A parsed view of the trap section.
331///
332/// The fields here correspond to the pieces of the section layout described
333/// on `TrapEncodingBuilder` in `crate::compile`.
334#[derive(Clone, Copy)]
335struct TrapSection<'a> {
336    /// Total number of trap entries in this section.
337    entries: usize,
338    /// One `(first_offset, block_pos)` pair per block.
339    block_index: &'a [[U32<LittleEndian>; 2]],
340    /// Variable-length block bodies, index by `block_pos` in the `block_index`
341    /// table above.
342    block_bodies: &'a [u8],
343}
344
345impl<'a> TrapSection<'a> {
346    /// Returns an iterator of `(text_offset, trap_code_byte)` for all entries
347    /// in `block`, or `None` if the section is malformed.
348    fn block_entries(&self, block_index: usize) -> Option<BlockEntries<'a>> {
349        let [first_offset, block_pos] = self.block_index.get(block_index)?;
350        let first_offset = first_offset.get(LittleEndian);
351        let block_pos = block_pos.get(LittleEndian);
352        let mut block = self.block_bodies.get(usize::try_from(block_pos).ok()?..)?;
353        let default_code = bytes::pop(&mut block)?;
354        let remaining = core::cmp::min(
355            TRAP_BLOCK_SIZE,
356            self.entries.checked_sub(block_index * TRAP_BLOCK_SIZE)?,
357        );
358        Some(BlockEntries {
359            block,
360            prev_offset: first_offset,
361            default_code,
362            remaining,
363        })
364    }
365}
366
367/// Iterator over the entries of a single block, decoding the
368/// delta-and-code-flag varints described in the "block body" portion of the
369/// section format on `TrapEncodingBuilder` in `crate::compile`.
370struct BlockEntries<'a> {
371    block: &'a [u8],
372    prev_offset: u32,
373    default_code: u8,
374    remaining: usize,
375}
376
377impl Iterator for BlockEntries<'_> {
378    type Item = (u32, u8);
379
380    fn next(&mut self) -> Option<(u32, u8)> {
381        self.remaining = self.remaining.checked_sub(1)?;
382        let token = bytes::read_uleb(&mut self.block)?;
383        let delta = u32::try_from(token >> 1).ok()?;
384        let cur_offset = self.prev_offset.checked_add(delta)?;
385        self.prev_offset = cur_offset;
386        let code = if token & 1 != 0 {
387            bytes::pop(&mut self.block)?
388        } else {
389            self.default_code
390        };
391        Some((cur_offset, code))
392    }
393}
394
395fn parse(section: &[u8]) -> Option<TrapSection<'_>> {
396    let mut section = Bytes(section);
397    // NB: this matches the encoding written by `TrapEncodingBuilder`.
398    let entries = section.read::<U32<LittleEndian>>().ok()?;
399    let entries = usize::try_from(entries.get(LittleEndian)).ok()?;
400    let num_blocks = section.read::<U32<LittleEndian>>().ok()?;
401    let num_blocks = usize::try_from(num_blocks.get(LittleEndian)).ok()?;
402    let (block_index, block_bodies) =
403        object::slice_from_bytes::<[U32<LittleEndian>; 2]>(section.0, num_blocks).ok()?;
404    Some(TrapSection {
405        entries,
406        block_index,
407        block_bodies,
408    })
409}
410
411/// Returns an iterator over all of the traps encoded in `section`, which should
412/// have been produced by `TrapEncodingBuilder`.
413pub fn iterate_traps(section: &[u8]) -> Option<impl Iterator<Item = (u32, CompiledTrap)> + '_> {
414    let section = parse(section)?;
415    Some(
416        (0..section.block_index.len())
417            .flat_map(move |block| section.block_entries(block).into_iter().flatten())
418            .map(|(pc, byte)| (pc, CompiledTrap::from_u8(byte).unwrap())),
419    )
420}