wasmtime-environ 42.0.2

Standalone environment support for WebAssembly code in Cranelift
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! Frame-table parser and lookup logic.
//!
//! This module contains utilities to interpret the `.wasmtime.frame`
//! section in a compiled artifact as produced by
//! [`crate::compile::FrameTableBuilder`].

use crate::FuncKey;
use alloc::vec::Vec;
use object::{Bytes, LittleEndian, U32Bytes};

/// An index into the table of stack shapes.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FrameStackShape(pub(crate) u32);
impl FrameStackShape {
    pub(crate) fn index(self) -> usize {
        usize::try_from(self.0).unwrap()
    }

    /// Get the raw stack-shape index suitable for serializing into
    /// metadata.
    pub fn raw(self) -> u32 {
        self.0
    }

    /// Wrap a raw stack shape index (e.g. from debug tags) into a FrameStackShape.
    pub fn from_raw(index: u32) -> FrameStackShape {
        FrameStackShape(index)
    }
}

/// An index to a frame descriptor that can be referenced from a
/// program point descriptor.
#[derive(Clone, Copy, Debug)]
pub struct FrameTableDescriptorIndex(pub(crate) u32);
impl FrameTableDescriptorIndex {
    fn index(self) -> usize {
        usize::try_from(self.0).unwrap()
    }
}

/// A parser for a frame-table section.
///
/// This parser holds slices to the in-memory section data, and is
/// cheap to construct: it reads some header fields but does not
/// interpret or validate content data until queried.
pub struct FrameTable<'a> {
    frame_descriptor_ranges: &'a [U32Bytes<LittleEndian>],
    frame_descriptor_data: &'a [u8],

    frame_descriptor_fp_offsets: &'a [U32Bytes<LittleEndian>],

    progpoint_pcs: &'a [U32Bytes<LittleEndian>],
    progpoint_descriptor_offsets: &'a [U32Bytes<LittleEndian>],
    progpoint_descriptor_data: &'a [U32Bytes<LittleEndian>],

    breakpoint_pcs: &'a [U32Bytes<LittleEndian>],
    breakpoint_patch_offsets: &'a [U32Bytes<LittleEndian>],
    breakpoint_patch_data_ends: &'a [U32Bytes<LittleEndian>],
    breakpoint_patch_data: &'a [u8],

    original_text: &'a [u8],
}

impl<'a> FrameTable<'a> {
    /// Parse a frame table section from a byte-slice as produced by
    /// [`crate::compile::FrameTableBuilder`].
    pub fn parse(data: &'a [u8], original_text: &'a [u8]) -> anyhow::Result<FrameTable<'a>> {
        let mut data = Bytes(data);
        let num_frame_descriptors = data
            .read::<U32Bytes<LittleEndian>>()
            .map_err(|_| anyhow::anyhow!("Unable to read frame descriptor count prefix"))?;
        let num_frame_descriptors = usize::try_from(num_frame_descriptors.get(LittleEndian))?;
        let num_progpoint_descriptors = data
            .read::<U32Bytes<LittleEndian>>()
            .map_err(|_| anyhow::anyhow!("Unable to read progpoint descriptor count prefix"))?;
        let num_progpoint_descriptors =
            usize::try_from(num_progpoint_descriptors.get(LittleEndian))?;
        let num_breakpoints = data
            .read::<U32Bytes<LittleEndian>>()
            .map_err(|_| anyhow::anyhow!("Unable to read breakpoint count prefix"))?;
        let num_breakpoints = usize::try_from(num_breakpoints.get(LittleEndian))?;

        let frame_descriptor_pool_length = data
            .read::<U32Bytes<LittleEndian>>()
            .map_err(|_| anyhow::anyhow!("Unable to read frame descriptor pool length"))?;
        let frame_descriptor_pool_length =
            usize::try_from(frame_descriptor_pool_length.get(LittleEndian))?;
        let progpoint_descriptor_pool_length = data
            .read::<U32Bytes<LittleEndian>>()
            .map_err(|_| anyhow::anyhow!("Unable to read progpoint descriptor pool length"))?;
        let progpoint_descriptor_pool_length =
            usize::try_from(progpoint_descriptor_pool_length.get(LittleEndian))?;
        let breakpoint_patch_pool_length = data
            .read::<U32Bytes<LittleEndian>>()
            .map_err(|_| anyhow::anyhow!("Unable to read breakpoint patch pool length"))?;
        let breakpoint_patch_pool_length =
            usize::try_from(breakpoint_patch_pool_length.get(LittleEndian))?;

        let (frame_descriptor_ranges, data) =
            object::slice_from_bytes::<U32Bytes<LittleEndian>>(data.0, 2 * num_frame_descriptors)
                .map_err(|_| anyhow::anyhow!("Unable to read frame descriptor ranges slice"))?;
        let (frame_descriptor_fp_offsets, data) =
            object::slice_from_bytes::<U32Bytes<LittleEndian>>(data, num_frame_descriptors)
                .map_err(|_| anyhow::anyhow!("Unable to read frame descriptor FP offset slice"))?;

        let (progpoint_pcs, data) =
            object::slice_from_bytes::<U32Bytes<LittleEndian>>(data, num_progpoint_descriptors)
                .map_err(|_| anyhow::anyhow!("Unable to read progpoint PC slice"))?;
        let (progpoint_descriptor_offsets, data) =
            object::slice_from_bytes::<U32Bytes<LittleEndian>>(data, num_progpoint_descriptors)
                .map_err(|_| anyhow::anyhow!("Unable to read progpoint descriptor offset slice"))?;
        let (breakpoint_pcs, data) =
            object::slice_from_bytes::<U32Bytes<LittleEndian>>(data, num_breakpoints)
                .map_err(|_| anyhow::anyhow!("Unable to read breakpoint PC slice"))?;
        let (breakpoint_patch_offsets, data) =
            object::slice_from_bytes::<U32Bytes<LittleEndian>>(data, num_breakpoints)
                .map_err(|_| anyhow::anyhow!("Unable to read breakpoint patch offsets slice"))?;
        let (breakpoint_patch_data_ends, data) =
            object::slice_from_bytes::<U32Bytes<LittleEndian>>(data, num_breakpoints)
                .map_err(|_| anyhow::anyhow!("Unable to read breakpoint patch data ends slice"))?;

        let (frame_descriptor_data, data) = data
            .split_at_checked(frame_descriptor_pool_length)
            .ok_or_else(|| anyhow::anyhow!("Unable to read frame descriptor pool"))?;

        let (progpoint_descriptor_data, data) = object::slice_from_bytes::<U32Bytes<LittleEndian>>(
            data,
            progpoint_descriptor_pool_length,
        )
        .map_err(|_| anyhow::anyhow!("Unable to read progpoint descriptor pool"))?;

        let (breakpoint_patch_data, _) = data
            .split_at_checked(breakpoint_patch_pool_length)
            .ok_or_else(|| anyhow::anyhow!("Unable to read breakpoint patch pool"))?;

        Ok(FrameTable {
            frame_descriptor_ranges,
            frame_descriptor_data,
            frame_descriptor_fp_offsets,
            progpoint_pcs,
            progpoint_descriptor_offsets,
            progpoint_descriptor_data,
            breakpoint_pcs,
            breakpoint_patch_offsets,
            breakpoint_patch_data_ends,
            breakpoint_patch_data,
            original_text,
        })
    }

    /// Get raw frame descriptor data and slot-to-FP-offset for a
    /// given frame descriptor.
    pub fn frame_descriptor(
        &self,
        frame_descriptor: FrameTableDescriptorIndex,
    ) -> Option<(&'a [u8], u32)> {
        let range_start = self
            .frame_descriptor_ranges
            .get(frame_descriptor.index() * 2)?
            .get(LittleEndian);
        let range_end = self
            .frame_descriptor_ranges
            .get(frame_descriptor.index() * 2 + 1)?
            .get(LittleEndian);
        let range_start = usize::try_from(range_start).unwrap();
        let range_end = usize::try_from(range_end).unwrap();
        if range_end < range_start || range_end > self.frame_descriptor_data.len() {
            return None;
        }
        let descriptor = &self.frame_descriptor_data[range_start..range_end];
        let slot_to_fp_offset = self
            .frame_descriptor_fp_offsets
            .get(frame_descriptor.index())?
            .get(LittleEndian);
        Some((descriptor, slot_to_fp_offset))
    }

    /// Get frames for the program point at the PC upper-bounded by a
    /// given search PC (offset in text section).
    pub fn find_program_point(
        &self,
        search_pc: u32,
        search_pos: FrameInstPos,
    ) -> Option<impl Iterator<Item = (u32, FrameTableDescriptorIndex, FrameStackShape)>> {
        let key = FrameInstPos::encode(search_pc, search_pos);
        let index = match self
            .progpoint_pcs
            .binary_search_by_key(&key, |entry| entry.get(LittleEndian))
        {
            Ok(idx) => idx,
            Err(idx) if idx > 0 => idx - 1,
            Err(_) => return None,
        };

        Some(self.program_point_frame_iter(index))
    }

    /// Get all program point records with iterators over
    /// corresponding frames for each.
    pub fn into_program_points(
        self,
    ) -> impl Iterator<
        Item = (
            u32,
            FrameInstPos,
            Vec<(u32, FrameTableDescriptorIndex, FrameStackShape)>,
        ),
    > + 'a {
        self.progpoint_pcs.iter().enumerate().map(move |(i, pc)| {
            let pc_and_pos = pc.get(LittleEndian);
            let (pc, pos) = FrameInstPos::decode(pc_and_pos);
            (
                pc,
                pos,
                self.program_point_frame_iter(i).collect::<Vec<_>>(),
            )
        })
    }

    fn program_point_frame_iter(
        &self,
        index: usize,
    ) -> impl Iterator<Item = (u32, FrameTableDescriptorIndex, FrameStackShape)> {
        let offset =
            usize::try_from(self.progpoint_descriptor_offsets[index].get(LittleEndian)).unwrap();
        let mut data = &self.progpoint_descriptor_data[offset..];

        core::iter::from_fn(move || {
            if data.len() < 3 {
                return None;
            }
            let wasm_pc = data[0].get(LittleEndian);
            let frame_descriptor = FrameTableDescriptorIndex(data[1].get(LittleEndian));
            let stack_shape = FrameStackShape(data[2].get(LittleEndian));
            data = &data[3..];
            let not_last = wasm_pc & 0x8000_0000 != 0;
            let wasm_pc = wasm_pc & 0x7fff_ffff;
            if !not_last {
                data = &[];
            }
            Some((wasm_pc, frame_descriptor, stack_shape))
        })
    }

    /// For a given breakpoint index, return the patch offset in text,
    /// the patch data, and the original data.
    fn breakpoint_patch(&self, i: usize) -> FrameTableBreakpointData<'_> {
        let patch_pool_start = if i == 0 {
            0
        } else {
            self.breakpoint_patch_data_ends[i - 1].get(LittleEndian)
        };
        let patch_pool_end = self.breakpoint_patch_data_ends[i].get(LittleEndian);
        let patch_pool_start = usize::try_from(patch_pool_start).unwrap();
        let patch_pool_end = usize::try_from(patch_pool_end).unwrap();
        let len = patch_pool_end - patch_pool_start;
        let offset = self.breakpoint_patch_offsets[i].get(LittleEndian);
        let offset = usize::try_from(offset).unwrap();
        let original_data = &self.original_text[offset..offset + len];
        FrameTableBreakpointData {
            offset,
            enable: &self.breakpoint_patch_data[patch_pool_start..patch_pool_end],
            disable: original_data,
        }
    }

    /// Find a list of breakpoint patches for a given Wasm PC.
    pub fn lookup_breakpoint_patches_by_pc(
        &self,
        pc: u32,
    ) -> impl Iterator<Item = FrameTableBreakpointData<'_>> + '_ {
        // Find *some* entry with a matching Wasm PC. Note that there
        // may be multiple entries for one PC.
        let range = match self
            .breakpoint_pcs
            .binary_search_by_key(&pc, |p| p.get(LittleEndian))
        {
            Ok(mut i) => {
                // Scan backward to first index with this PC.
                while i > 0 && self.breakpoint_pcs[i - 1].get(LittleEndian) == pc {
                    i -= 1;
                }

                // Scan forward to find the end of the range.
                let mut end = i;
                while end < self.breakpoint_pcs.len()
                    && self.breakpoint_pcs[end].get(LittleEndian) == pc
                {
                    end += 1;
                }

                i..end
            }
            Err(_) => 0..0,
        };

        range.map(|i| self.breakpoint_patch(i))
    }

    /// Return an iterator over all breakpoint patches.
    ///
    /// Returned tuples are (Wasm PC, breakpoint data).
    pub fn breakpoint_patches(
        &self,
    ) -> impl Iterator<Item = (u32, FrameTableBreakpointData<'_>)> + '_ {
        self.breakpoint_pcs.iter().enumerate().map(|(i, wasm_pc)| {
            let wasm_pc = wasm_pc.get(LittleEndian);
            let data = self.breakpoint_patch(i);
            (wasm_pc, data)
        })
    }
}

/// Data describing how to patch code to enable or disable one
/// breakpoint.
pub struct FrameTableBreakpointData<'a> {
    /// Offset in the code image's text section.
    pub offset: usize,
    /// Code bytes to patch in to enable the breakpoint.
    pub enable: &'a [u8],
    /// Code bytes to patch in to disable the breakpoint.
    pub disable: &'a [u8],
}

/// An instruction position for a program point.
///
/// We attach debug metadata to a *position* on an offset in the text
/// (code) section, either "post" or "pre". The "post" position
/// logically comes first, and is associated with the instruction that
/// ends at this offset (i.e., the previous instruction). The "pre"
/// position comes next, and is associated with the instruction that
/// begins at this offset (i.e., the next instruction).
///
/// We make this distinction because metadata lookups sometimes occur
/// with a PC that is after the instruction (e.g., the return address
/// after a call instruction), and sometimes at the instruction (e.g.,
/// a trapping PC address). The lookup context will know which one to
/// use -- e.g., when walking the stack, "pre" for a trapping PC and
/// "post" for every frame after that -- so we simply encode it as
/// part of the position and allow searching on it.
///
/// The need for this distinction can be understood by way of an
/// example; say we have:
///
/// ```plain
/// call ...
/// trapping_store ...
/// ```
///
/// where both instructions have debug metadata. We might look up the
/// PC of `trapping_store` once as we walk the stack from within the
/// call (we will get this PC because it is the return address) and
/// once when `trapping_store` itself traps; and we want different
/// metadata in each case.
///
/// An alternative is to universally attach tags to the end offset of
/// an instruction, which allows us to handle return addresses
/// naturally but requires traps to adjust their PC. However, this
/// requires trap handlers to know the length of the trapping
/// instruction, which is not always easy -- in the most general case,
/// on variable-length instruction sets, it requires a full
/// instruction decoder.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FrameInstPos {
    /// The "post" position at an offset attaches to the instruction
    /// that ends at this offset, i.e., came previously.
    Post,
    /// The "pre" position at an offset attaches to the instruction
    /// that begins at this offset, i.e., comes next.
    Pre,
}

impl FrameInstPos {
    pub(crate) fn encode(pc: u32, pos: FrameInstPos) -> u32 {
        let lsb = match pos {
            Self::Post => 0,
            Self::Pre => 1,
        };
        debug_assert!(pc < 0x8000_0000);
        (pc << 1) | lsb
    }
    pub(crate) fn decode(bits: u32) -> (u32, FrameInstPos) {
        let pos = match bits & 1 {
            0 => Self::Post,
            1 => Self::Pre,
            _ => unreachable!(),
        };
        let pc = bits >> 1;
        (pc, pos)
    }
}

/// An offset into the state slot.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FrameStateSlotOffset(pub(crate) u32);
impl FrameStateSlotOffset {
    #[cfg(feature = "compile")]
    pub(crate) fn add(self, offset: u32) -> FrameStateSlotOffset {
        FrameStateSlotOffset(self.0 + offset)
    }

    /// Get the offset into the state stackslot, suitable for use in a
    /// `stack_store`/`stack_load` instruction.
    pub fn offset(self) -> i32 {
        i32::try_from(self.0).unwrap()
    }
}

/// A type stored in a frame.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[allow(missing_docs, reason = "self-describing variants")]
pub enum FrameValType {
    I32,
    I64,
    F32,
    F64,
    V128,
    AnyRef,
    FuncRef,
    ExternRef,
    ExnRef,
    ContRef,
}

impl FrameValType {
    #[cfg(feature = "compile")]
    pub(crate) fn storage_size(&self, pointer_size: u32) -> u32 {
        match self {
            FrameValType::I32 => 4,
            FrameValType::I64 => 8,
            FrameValType::F32 => 4,
            FrameValType::F64 => 8,
            FrameValType::V128 => 16,
            FrameValType::AnyRef | FrameValType::ExternRef | FrameValType::ExnRef => 4,
            FrameValType::FuncRef => pointer_size,
            FrameValType::ContRef => 2 * pointer_size,
        }
    }
}

impl From<FrameValType> for u8 {
    fn from(value: FrameValType) -> u8 {
        match value {
            FrameValType::I32 => 0,
            FrameValType::I64 => 1,
            FrameValType::F32 => 2,
            FrameValType::F64 => 3,
            FrameValType::V128 => 4,
            FrameValType::AnyRef => 5,
            FrameValType::FuncRef => 6,
            FrameValType::ExternRef => 7,
            FrameValType::ExnRef => 8,
            FrameValType::ContRef => 9,
        }
    }
}

impl TryFrom<u8> for FrameValType {
    type Error = anyhow::Error;
    fn try_from(value: u8) -> anyhow::Result<Self> {
        match value {
            0 => Ok(Self::I32),
            1 => Ok(Self::I64),
            2 => Ok(Self::F32),
            3 => Ok(Self::F64),
            4 => Ok(Self::V128),
            5 => Ok(Self::AnyRef),
            6 => Ok(Self::FuncRef),
            7 => Ok(Self::ExternRef),
            8 => Ok(Self::ExnRef),
            9 => Ok(Self::ContRef),
            _ => Err(anyhow::anyhow!("Invalid type")),
        }
    }
}

/// Parser for a frame state slot descriptor.
///
/// This provides the ability to extract offsets and types for locals
/// and for the stack given a stack shape.
pub struct FrameStateSlot<'a> {
    func_key: FuncKey,
    local_offsets: &'a [U32Bytes<LittleEndian>],
    stack_shape_parents: &'a [U32Bytes<LittleEndian>],
    stack_shape_offsets: &'a [U32Bytes<LittleEndian>],
    local_types: &'a [u8],
    stack_shape_types: &'a [u8],
}

impl<'a> FrameStateSlot<'a> {
    /// Parse a slot descriptor.
    ///
    /// This parses the descriptor bytes as provided by
    /// [`FrameTable::frame_descriptor`].
    pub fn parse(descriptor: &'a [u8]) -> anyhow::Result<FrameStateSlot<'a>> {
        let mut data = Bytes(descriptor);
        let func_key_namespace = data
            .read::<U32Bytes<LittleEndian>>()
            .map_err(|_| anyhow::anyhow!("Unable to read func key namespace"))?
            .get(LittleEndian);
        let func_key_index = data
            .read::<U32Bytes<LittleEndian>>()
            .map_err(|_| anyhow::anyhow!("Unable to read func key index"))?
            .get(LittleEndian);
        let func_key = FuncKey::from_raw_parts(func_key_namespace, func_key_index);

        let num_locals = data
            .read::<U32Bytes<LittleEndian>>()
            .map_err(|_| anyhow::anyhow!("Unable to read num_locals"))?
            .get(LittleEndian);
        let num_locals = usize::try_from(num_locals)?;
        let num_stack_shapes = data
            .read::<U32Bytes<LittleEndian>>()
            .map_err(|_| anyhow::anyhow!("Unable to read num_stack_shapes"))?
            .get(LittleEndian);
        let num_stack_shapes = usize::try_from(num_stack_shapes)?;

        let (local_offsets, data) =
            object::slice_from_bytes::<U32Bytes<LittleEndian>>(data.0, num_locals)
                .map_err(|_| anyhow::anyhow!("Unable to read local_offsets slice"))?;
        let (stack_shape_parents, data) =
            object::slice_from_bytes::<U32Bytes<LittleEndian>>(data, num_stack_shapes)
                .map_err(|_| anyhow::anyhow!("Unable to read stack_shape_parents slice"))?;
        let (stack_shape_offsets, data) =
            object::slice_from_bytes::<U32Bytes<LittleEndian>>(data, num_stack_shapes)
                .map_err(|_| anyhow::anyhow!("Unable to read stack_shape_offsets slice"))?;
        let (local_types, data) = data
            .split_at_checked(num_locals)
            .ok_or_else(|| anyhow::anyhow!("Unable to read local_types slice"))?;
        let (stack_shape_types, _) = data
            .split_at_checked(num_stack_shapes)
            .ok_or_else(|| anyhow::anyhow!("Unable to read stack_shape_types slice"))?;

        Ok(FrameStateSlot {
            func_key,
            local_offsets,
            stack_shape_parents,
            stack_shape_offsets,
            local_types,
            stack_shape_types,
        })
    }

    /// Get the FuncKey for the function that produced this frame
    /// slot.
    pub fn func_key(&self) -> FuncKey {
        self.func_key
    }

    /// Get the local offsets and types.
    pub fn locals(&self) -> impl Iterator<Item = (FrameStateSlotOffset, FrameValType)> {
        (0..self.num_locals()).map(|i| self.local(i).unwrap())
    }

    /// Get the type and offset for a given local.
    pub fn local(&self, index: usize) -> Option<(FrameStateSlotOffset, FrameValType)> {
        let offset = FrameStateSlotOffset(self.local_offsets.get(index)?.get(LittleEndian));
        let ty = FrameValType::try_from(*self.local_types.get(index)?).expect("Invalid type");
        Some((offset, ty))
    }

    /// Get the number of locals in the frame.
    pub fn num_locals(&self) -> usize {
        self.local_offsets.len()
    }

    /// Get the offsets and types for operand stack values, from top
    /// of stack (most recently pushed) down.
    pub fn stack(
        &self,
        shape: FrameStackShape,
    ) -> impl Iterator<Item = (FrameStateSlotOffset, FrameValType)> {
        fn unpack_option_shape(shape: FrameStackShape) -> Option<FrameStackShape> {
            if shape.0 == u32::MAX {
                None
            } else {
                Some(shape)
            }
        }

        let mut shape = unpack_option_shape(shape);
        core::iter::from_fn(move || {
            shape.map(|s| {
                let parent = FrameStackShape(self.stack_shape_parents[s.index()].get(LittleEndian));
                let parent = unpack_option_shape(parent);
                let offset =
                    FrameStateSlotOffset(self.stack_shape_offsets[s.index()].get(LittleEndian));
                let ty = FrameValType::try_from(self.stack_shape_types[s.index()])
                    .expect("Invalid type");
                shape = parent;
                (offset, ty)
            })
        })
    }

    /// Returns an iterator over all storage in this frame.
    pub fn stack_and_locals(
        &self,
        shape: FrameStackShape,
    ) -> impl Iterator<Item = (FrameStateSlotOffset, FrameValType)> + '_ {
        self.locals().chain(self.stack(shape))
    }
}