wasmtime_environ/address_map.rs
1//! Data structures to provide transformation of the source
2
3use object::{Bytes, LittleEndian, U32Bytes};
4use serde_derive::{Deserialize, Serialize};
5
6/// Single source location to generated address mapping.
7#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
8pub struct InstructionAddressMap {
9 /// Where in the source wasm binary this instruction comes from, specified
10 /// in an offset of bytes from the front of the file.
11 pub srcloc: FilePos,
12
13 /// Offset from the start of the function's compiled code to where this
14 /// instruction is located, or the region where it starts.
15 pub code_offset: u32,
16}
17
18/// A position within an original source file,
19///
20/// This structure is used as a newtype wrapper around a 32-bit integer which
21/// represents an offset within a file where a wasm instruction or function is
22/// to be originally found.
23#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
24pub struct FilePos(u32);
25
26impl FilePos {
27 /// Create a new file position with the given offset.
28 pub fn new(pos: u32) -> FilePos {
29 assert!(pos != u32::MAX);
30 FilePos(pos)
31 }
32
33 /// Returns the offset that this offset was created with.
34 ///
35 /// Note that the `Default` implementation will return `None` here, whereas
36 /// positions created with `FilePos::new` will return `Some`.
37 pub fn file_offset(self) -> Option<u32> {
38 if self.0 == u32::MAX {
39 None
40 } else {
41 Some(self.0)
42 }
43 }
44}
45
46impl Default for FilePos {
47 fn default() -> FilePos {
48 FilePos(u32::MAX)
49 }
50}
51
52/// Parse an `ELF_WASMTIME_ADDRMAP` section, returning the slice of code offsets
53/// and the slice of associated file positions for each offset.
54fn parse_address_map(
55 section: &[u8],
56) -> Option<(&[U32Bytes<LittleEndian>], &[U32Bytes<LittleEndian>])> {
57 let mut section = Bytes(section);
58 // NB: this matches the encoding written by `append_to` above.
59 let count = section.read::<U32Bytes<LittleEndian>>().ok()?;
60 let count = usize::try_from(count.get(LittleEndian)).ok()?;
61 let (offsets, section) =
62 object::slice_from_bytes::<U32Bytes<LittleEndian>>(section.0, count).ok()?;
63 let (positions, section) =
64 object::slice_from_bytes::<U32Bytes<LittleEndian>>(section, count).ok()?;
65 debug_assert!(section.is_empty());
66 Some((offsets, positions))
67}
68
69/// Lookup an `offset` within an encoded address map section, returning the
70/// original `FilePos` that corresponds to the offset, if found.
71///
72/// This function takes a `section` as its first argument which must have been
73/// created with `AddressMapSection` above. This is intended to be the raw
74/// `ELF_WASMTIME_ADDRMAP` section from the compilation artifact.
75///
76/// The `offset` provided is a relative offset from the start of the text
77/// section of the pc that is being looked up. If `offset` is out of range or
78/// doesn't correspond to anything in this file then `None` is returned.
79pub fn lookup_file_pos(section: &[u8], offset: usize) -> Option<FilePos> {
80 let (offsets, positions) = parse_address_map(section)?;
81
82 // First perform a binary search on the `offsets` array. This is a sorted
83 // array of offsets within the text section, which is conveniently what our
84 // `offset` also is. Note that we are somewhat unlikely to find a precise
85 // match on the element in the array, so we're largely interested in which
86 // "bucket" the `offset` falls into.
87 let offset = u32::try_from(offset).ok()?;
88 let index = match offsets.binary_search_by_key(&offset, |v| v.get(LittleEndian)) {
89 // Exact hit!
90 Ok(i) => i,
91
92 // This *would* be at the first slot in the array, so no
93 // instructions cover `pc`.
94 Err(0) => return None,
95
96 // This would be at the `nth` slot, so we're at the `n-1`th slot.
97 Err(n) => n - 1,
98 };
99
100 // Using the `index` we found of which bucket `offset` corresponds to we can
101 // lookup the actual `FilePos` value in the `positions` array.
102 let pos = positions.get(index)?;
103 Some(FilePos(pos.get(LittleEndian)))
104}
105
106/// Iterate over the address map contained in the given address map section.
107///
108/// This function takes a `section` as its first argument which must have been
109/// created with `AddressMapSection` above. This is intended to be the raw
110/// `ELF_WASMTIME_ADDRMAP` section from the compilation artifact.
111///
112/// The yielded offsets are relative to the start of the text section for this
113/// map's code object.
114pub fn iterate_address_map<'a>(
115 section: &'a [u8],
116) -> Option<impl Iterator<Item = (u32, FilePos)> + 'a> {
117 let (offsets, positions) = parse_address_map(section)?;
118
119 Some(
120 offsets
121 .iter()
122 .map(|o| o.get(LittleEndian))
123 .zip(positions.iter().map(|pos| FilePos(pos.get(LittleEndian)))),
124 )
125}