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
//! Contains utilities for parsing a WASM module to retrieve the information needed by [`super::WasmObject`]

use super::WasmError;
use crate::base::{ObjectKind, Symbol};
use wasmparser::{ImportSectionEntryType, Payload, Validator};

impl<'data> super::WasmObject<'data> {
    /// Tries to parse a WASM from the given slice.
    pub fn parse(data: &'data [u8]) -> Result<Self, WasmError> {
        let mut code_offset = 0;
        let mut build_id = None;
        let mut dwarf_sections = Vec::new();
        let mut kind = ObjectKind::Debug;

        // In "normal" wasm modules the only types will be function signatures, but in the future it
        // could contain types used for module linking, but we don't actually care about the types,
        // just that the function references a valid signature, so we just keep a bitset of the function
        // signatures to verify that
        let mut func_sigs = bitvec::vec::BitVec::<usize, bitvec::order::Lsb0>::new();
        let mut validator = Validator::new();
        let mut funcs = Vec::<Symbol>::new();
        let mut num_imported_funcs = 0u32;

        // Parse the wasm file to pull out the function and their starting address, size, and name
        // Note that the order of the payloads here are the order that they will appear in (valid)
        // wasm binaries, other than the sections that we need to parse to validate the module, which
        // are at the end
        for payload in wasmparser::Parser::new(0).parse_all(data) {
            let payload = payload?;
            match payload {
                // This should always be first, and is necessary to prepare the validator since the
                // version determines which parts of the spec can be used
                Payload::Version { num, range } => {
                    validator.version(num, &range)?;
                }
                // The type section contains, well, types, specifically, function signatures that are
                // later referenced by the function section.
                Payload::TypeSection(tsr) => {
                    validator.type_section(&tsr)?;
                    func_sigs.resize(tsr.get_count() as usize, false);
                    let fs = func_sigs.as_mut_bitslice();

                    for (i, ty) in tsr.into_iter().enumerate() {
                        if let wasmparser::TypeDef::Func(_) = ty? {
                            fs.set(i, true);
                        }
                    }
                }
                // Imported functions and local functions both use the same ID space, but imported
                // functions are never exposed, so we just need to account for the id offset later
                // when parsing the local functions
                Payload::ImportSection(isr) => {
                    validator.import_section(&isr)?;

                    for import in isr {
                        let import = import?;
                        if let ImportSectionEntryType::Function(id) = import.ty {
                            if !func_sigs
                                .as_bitslice()
                                .get(id as usize)
                                .as_deref()
                                .unwrap_or(&false)
                            {
                                return Err(WasmError::UnknownFunctionType);
                            }

                            num_imported_funcs += 1;
                        }
                    }
                }
                // The function section declares all of the local functions present in the module
                Payload::FunctionSection(fsr) => {
                    validator.function_section(&fsr)?;

                    if fsr.get_count() > 0 {
                        kind = ObjectKind::Library;
                    }

                    funcs.reserve(fsr.get_count() as usize);

                    // We actually don't care about the type signature of the function, other than that
                    // they exist
                    for id in fsr {
                        if !func_sigs
                            .as_bitslice()
                            .get(id? as usize)
                            .as_deref()
                            .unwrap_or(&false)
                        {
                            return Err(WasmError::UnknownFunctionType);
                        }
                    }
                }
                // The code section contains the actual function bodies, this payload is emitted at
                // the beginning of the section. This one is important as the code section offset is
                // used to calculate relative addresses in a `DwarfDebugSession`
                Payload::CodeSectionStart { range, count, .. } => {
                    code_offset = range.start as u64;
                    validator.code_section_start(count, &range)?;
                }
                // We get one of these for each local function body
                Payload::CodeSectionEntry(body) => {
                    let validator = validator.code_section_entry()?;

                    let (address, size) = get_function_info(body, validator)?;

                    // Though we have an accurate? size of the function body, the old method of symbol
                    // iterating with walrus extends the size of each body to be contiguous with the
                    // next function, so we do the same, other than the final function
                    if let Some(prev) = funcs.last_mut() {
                        prev.size = address - prev.address;
                    }

                    funcs.push(Symbol {
                        name: None,
                        address,
                        size,
                    });
                }
                Payload::ModuleSectionStart { count, range, .. } => {
                    validator.module_section_start(count, &range)?;
                }
                Payload::DataSection(dsr) => {
                    validator.data_section(&dsr)?;
                }
                // There are several custom sections that we need
                Payload::CustomSection {
                    name,
                    data,
                    data_offset,
                    ..
                } => {
                    match name {
                        // this section is not defined yet
                        // see https://github.com/WebAssembly/tool-conventions/issues/133
                        "build_id" => {
                            build_id = Some(data);
                        }
                        // All of the dwarf debug sections (.debug_frame, .debug_info etc) start with a `.`, and
                        // are the only ones we need for walking the debug info
                        debug if debug.starts_with('.') => {
                            dwarf_sections.push((name, data));
                        }
                        // The name section contains the symbol names for items, notably functions
                        "name" => {
                            let nsr = wasmparser::NameSectionReader::new(data, data_offset)?;

                            for name in nsr {
                                if let wasmparser::Name::Function(fnames) = name? {
                                    let mut map = fnames.get_map()?;
                                    for _ in 0..map.get_count() {
                                        let fname = map.read()?;

                                        // The names for imported functions are also in this table, but
                                        // we don't care about them
                                        if fname.index >= num_imported_funcs {
                                            if let Some(func) = funcs.get_mut(
                                                (fname.index - num_imported_funcs) as usize,
                                            ) {
                                                func.name =
                                                    Some(std::borrow::Cow::Borrowed(fname.name));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        _ => {}
                    }
                }
                // Final
                Payload::End => validator.end()?,

                // The following sections are not used by this crate, but some (eg table/memory/global)
                // are needed to validate the sections that we do care about, so we just validate all
                // of the sections we don't use to be sure
                Payload::TableSection(tsr) => {
                    validator.table_section(&tsr)?;
                }
                Payload::MemorySection(msr) => {
                    validator.memory_section(&msr)?;
                }
                Payload::TagSection(tsr) => {
                    validator.tag_section(&tsr)?;
                }
                Payload::GlobalSection(gsr) => {
                    validator.global_section(&gsr)?;
                }
                Payload::ExportSection(esr) => {
                    validator.export_section(&esr)?;
                }
                Payload::StartSection { func, range } => {
                    validator.start_section(func, &range)?;
                }
                Payload::ElementSection(esr) => {
                    validator.element_section(&esr)?;
                }
                Payload::DataCountSection { count, range } => {
                    validator.data_count_section(count, &range)?;
                }
                Payload::UnknownSection { id, range, .. } => {
                    validator.unknown_section(id, &range)?;
                }
                _ => {}
            }
        }

        Ok(Self {
            dwarf_sections,
            funcs,
            build_id,
            data,
            code_offset,
            kind,
        })
    }
}

fn get_function_info(
    body: wasmparser::FunctionBody,
    mut validator: wasmparser::FuncValidator<wasmparser::ValidatorResources>,
) -> Result<(u64, u64), WasmError> {
    let mut body = body.get_binary_reader();

    let function_address = body.original_position() as u64;

    // locals, we _can_ just skip this, but might as well validate while we're here
    {
        for _ in 0..body.read_var_u32()? {
            let pos = body.original_position();
            let count = body.read_var_u32()?;
            let ty = body.read_type()?;
            validator.define_locals(pos, count, ty)?;
        }
    }

    while !body.eof() {
        let pos = body.original_position();
        let inst = body.read_operator()?;
        validator.op(pos, &inst)?;
    }

    validator.finish(body.original_position())?;

    Ok((
        function_address,
        body.original_position() as u64 - function_address,
    ))
}