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
//! A high-level API for manipulating wasm modules.

mod config;
mod custom;
mod data;
mod debug;
mod elements;
mod exports;
mod functions;
mod globals;
mod imports;
mod locals;
mod memories;
mod producers;
mod tables;
mod types;

use crate::emit::{Emit, EmitContext, IdsToIndices};
use crate::error::Result;
pub use crate::ir::InstrLocId;
pub use crate::module::custom::{
    CustomSection, CustomSectionId, ModuleCustomSections, RawCustomSection, TypedCustomSectionId,
    UntypedCustomSectionId,
};
pub use crate::module::data::{ActiveData, ActiveDataLocation, Data, DataId, DataKind, ModuleData};
pub use crate::module::debug::ModuleDebugData;
pub use crate::module::elements::ElementKind;
pub use crate::module::elements::{Element, ElementId, ModuleElements};
pub use crate::module::exports::{Export, ExportId, ExportItem, ModuleExports};
pub use crate::module::functions::{FuncParams, FuncResults};
pub use crate::module::functions::{Function, FunctionId, ModuleFunctions};
pub use crate::module::functions::{FunctionKind, ImportedFunction, LocalFunction};
pub use crate::module::globals::{Global, GlobalId, GlobalKind, ModuleGlobals};
pub use crate::module::imports::{Import, ImportId, ImportKind, ModuleImports};
pub use crate::module::locals::ModuleLocals;
pub use crate::module::memories::{Memory, MemoryId, ModuleMemories};
pub use crate::module::producers::ModuleProducers;
pub use crate::module::tables::{ModuleTables, Table, TableId};
pub use crate::module::types::ModuleTypes;
use crate::parse::IndicesToIds;
use anyhow::{bail, Context};
use id_arena::Id;
use log::warn;
use std::fs;
use std::mem;
use std::path::Path;
use wasmparser::{Parser, Payload, Validator, WasmFeatures};

pub use self::config::ModuleConfig;

/// A wasm module.
#[derive(Debug, Default)]
#[allow(missing_docs)]
pub struct Module {
    pub imports: ModuleImports,
    pub tables: ModuleTables,
    pub types: ModuleTypes,
    pub funcs: ModuleFunctions,
    pub globals: ModuleGlobals,
    pub locals: ModuleLocals,
    pub exports: ModuleExports,
    pub memories: ModuleMemories,
    /// Registration of passive data segments, if any
    pub data: ModuleData,
    /// Registration of passive element segments, if any
    pub elements: ModuleElements,
    /// The `start` function, if any
    pub start: Option<FunctionId>,
    /// Representation of the eventual custom section, `producers`
    pub producers: ModuleProducers,
    /// Custom sections found in this module.
    pub customs: ModuleCustomSections,
    /// Dwarf debug data.
    pub debug: ModuleDebugData,
    /// The name of this module, used for debugging purposes in the `name`
    /// custom section.
    pub name: Option<String>,
    pub(crate) config: ModuleConfig,
}

/// Code transformation records, which is used to transform DWARF debug entries.
#[derive(Debug, Default)]
pub struct CodeTransform {
    /// Maps from an offset of an instruction in the input Wasm to its offset in the
    /// output Wasm.
    ///
    /// Note that an input offset may be mapped to multiple output offsets, and vice
    /// versa, due to transformations like function inlinining or constant
    /// propagation.
    pub instruction_map: Vec<(InstrLocId, usize)>,

    /// Offset of code section from the front of Wasm binary
    pub code_section_start: usize,

    /// Emitted binary ranges of functions
    pub function_ranges: Vec<(Id<Function>, wasmparser::Range)>,
}

impl Module {
    /// Create a default, empty module that uses the given configuration.
    pub fn with_config(config: ModuleConfig) -> Self {
        Module {
            config,
            ..Default::default()
        }
    }

    /// Construct a new module from the given path with the default
    /// configuration.
    pub fn from_file<P>(path: P) -> Result<Module>
    where
        P: AsRef<Path>,
    {
        Module::from_buffer(&fs::read(path)?)
    }

    /// Construct a new module from the given path and configuration.
    pub fn from_file_with_config<P>(path: P, config: &ModuleConfig) -> Result<Module>
    where
        P: AsRef<Path>,
    {
        config.parse(&fs::read(path)?)
    }

    /// Construct a new module from the in-memory wasm buffer with the default
    /// configuration.
    pub fn from_buffer(wasm: &[u8]) -> Result<Module> {
        ModuleConfig::new().parse(wasm)
    }

    fn parse(wasm: &[u8], config: &ModuleConfig) -> Result<Module> {
        let mut ret = Module::default();
        ret.config = config.clone();
        let mut indices = IndicesToIds::default();
        let mut validator = Validator::new();
        validator.wasm_features(WasmFeatures {
            reference_types: !config.only_stable_features,
            multi_value: true,
            bulk_memory: !config.only_stable_features,
            simd: !config.only_stable_features,
            threads: !config.only_stable_features,
            multi_memory: !config.only_stable_features,
            ..WasmFeatures::default()
        });

        let mut local_functions = Vec::new();
        let mut debug_sections = Vec::new();

        for payload in Parser::new(0).parse_all(wasm) {
            match payload? {
                Payload::Version { num, range } => {
                    validator.version(num, &range)?;
                }
                Payload::DataSection(s) => {
                    validator
                        .data_section(&s)
                        .context("failed to parse data section")?;
                    ret.parse_data(s, &mut indices)?;
                }
                Payload::TypeSection(s) => {
                    validator
                        .type_section(&s)
                        .context("failed to parse type section")?;
                    ret.parse_types(s, &mut indices)?;
                }
                Payload::ImportSection(s) => {
                    validator
                        .import_section(&s)
                        .context("failed to parse import section")?;
                    ret.parse_imports(s, &mut indices)?;
                }
                Payload::TableSection(s) => {
                    validator
                        .table_section(&s)
                        .context("failed to parse table section")?;
                    ret.parse_tables(s, &mut indices)?;
                }
                Payload::MemorySection(s) => {
                    validator
                        .memory_section(&s)
                        .context("failed to parse memory section")?;
                    ret.parse_memories(s, &mut indices)?;
                }
                Payload::GlobalSection(s) => {
                    validator
                        .global_section(&s)
                        .context("failed to parse global section")?;
                    ret.parse_globals(s, &mut indices)?;
                }
                Payload::ExportSection(s) => {
                    validator
                        .export_section(&s)
                        .context("failed to parse export section")?;
                    ret.parse_exports(s, &mut indices)?;
                }
                Payload::ElementSection(s) => {
                    validator
                        .element_section(&s)
                        .context("failed to parse element section")?;
                    ret.parse_elements(s, &mut indices)?;
                }
                Payload::StartSection { func, range, .. } => {
                    validator.start_section(func, &range)?;
                    ret.start = Some(indices.get_func(func)?);
                }
                Payload::FunctionSection(s) => {
                    validator
                        .function_section(&s)
                        .context("failed to parse function section")?;
                    ret.declare_local_functions(s, &mut indices)?;
                }
                Payload::DataCountSection { count, range } => {
                    validator.data_count_section(count, &range)?;
                    ret.reserve_data(count, &mut indices);
                }
                Payload::CodeSectionStart { count, range, .. } => {
                    validator.code_section_start(count, &range)?;
                    ret.funcs.code_section_offset = range.start;
                }
                Payload::CodeSectionEntry(body) => {
                    let validator = validator.code_section_entry()?;
                    local_functions.push((body, validator));
                }
                Payload::CustomSection {
                    name,
                    data,
                    data_offset,
                    range: _range,
                } => {
                    let result = match name {
                        "producers" => wasmparser::ProducersSectionReader::new(data, data_offset)
                            .map_err(anyhow::Error::from)
                            .and_then(|s| ret.parse_producers_section(s)),
                        "name" => wasmparser::NameSectionReader::new(data, data_offset)
                            .map_err(anyhow::Error::from)
                            .and_then(|r| ret.parse_name_section(r, &indices)),
                        _ => {
                            log::debug!("parsing custom section `{}`", name);
                            if name.starts_with(".debug") {
                                debug_sections.push(RawCustomSection {
                                    name: name.to_string(),
                                    data: data.to_vec(),
                                });
                            } else {
                                ret.customs.add(RawCustomSection {
                                    name: name.to_string(),
                                    data: data.to_vec(),
                                });
                            }
                            continue;
                        }
                    };
                    if let Err(e) = result {
                        log::warn!("failed to parse `{}` custom section {}", name, e);
                    }
                }
                Payload::UnknownSection { id, range, .. } => {
                    validator.unknown_section(id, &range)?;
                    unreachable!()
                }

                Payload::End => validator.end()?,

                // the module linking proposal is not implemented yet.
                Payload::AliasSection(s) => {
                    validator.alias_section(&s)?;
                    bail!("not supported yet");
                }
                Payload::InstanceSection(s) => {
                    validator.instance_section(&s)?;
                    bail!("not supported yet");
                }
                Payload::ModuleSectionEntry {
                    parser: _,
                    range: _,
                } => {
                    validator.module_section_entry();
                    bail!("not supported yet");
                }
                Payload::ModuleSectionStart {
                    count,
                    range,
                    size: _,
                } => {
                    validator.module_section_start(count, &range)?;
                    bail!("not supported yet");
                }
                // exception handling is not implemented yet.
                Payload::TagSection(s) => {
                    validator.tag_section(&s)?;
                    bail!("not supported yet");
                }
            }
        }

        ret.parse_local_functions(
            local_functions,
            &mut indices,
            config.on_instr_loc.as_ref().map(|f| f.as_ref()),
        )
        .context("failed to parse code section")?;

        ret.parse_debug_sections(debug_sections)
            .context("failed to parse debug data section")?;

        ret.producers
            .add_processed_by("walrus", env!("CARGO_PKG_VERSION"));

        if let Some(on_parse) = &config.on_parse {
            on_parse(&mut ret, &indices)?;
        }

        log::debug!("parse complete");
        Ok(ret)
    }

    /// Emit this module into a `.wasm` file at the given path.
    pub fn emit_wasm_file<P>(&mut self, path: P) -> Result<()>
    where
        P: AsRef<Path>,
    {
        let buffer = self.emit_wasm();
        fs::write(path, buffer).context("failed to write wasm module")?;
        Ok(())
    }

    /// Emit this module into an in-memory wasm buffer.
    pub fn emit_wasm(&mut self) -> Vec<u8> {
        log::debug!("start emit");

        let indices = &mut IdsToIndices::default();

        let mut customs = mem::take(&mut self.customs);

        let mut cx = EmitContext {
            module: self,
            indices,
            wasm_module: wasm_encoder::Module::new(),
            locals: Default::default(),
            code_transform: Default::default(),
        };
        self.types.emit(&mut cx);
        self.imports.emit(&mut cx);
        self.funcs.emit_func_section(&mut cx);
        self.tables.emit(&mut cx);
        self.memories.emit(&mut cx);
        // TODO: tag section
        self.globals.emit(&mut cx);
        self.exports.emit(&mut cx);
        if let Some(start) = self.start {
            let idx = cx.indices.get_func_index(start);
            cx.wasm_module.section(&wasm_encoder::StartSection {
                function_index: idx,
            });
        }
        self.elements.emit(&mut cx);
        self.data.emit_data_count(&mut cx);
        self.funcs.emit(&mut cx);
        self.data.emit(&mut cx);

        if !self.config.skip_name_section {
            emit_name_section(&mut cx);
        }
        if !self.config.skip_producers_section {
            self.producers.emit(&mut cx);
        }

        if self.config.generate_dwarf {
            self.debug.emit(&mut cx);
        } else {
            log::debug!("skipping DWARF custom section");
        }

        let indices = mem::replace(cx.indices, Default::default());

        for (_id, section) in customs.iter_mut() {
            if section.name().starts_with(".debug") {
                continue;
            }

            log::debug!("emitting custom section {}", section.name());

            if self.config.preserve_code_transform {
                section.apply_code_transform(&cx.code_transform);
            }

            cx.wasm_module.section(&wasm_encoder::CustomSection {
                name: section.name().into(),
                data: section.data(&indices),
            });
        }

        let out = cx.wasm_module.finish();
        log::debug!("emission finished");

        // let mut validator = Validator::new();
        // if let Err(err) = validator.validate_all(&out) {
        //     eprintln!("{:?}", err);
        //     panic!("Unable to validate serialized output");
        // }

        out
    }

    /// Returns an iterator over all functions in this module
    pub fn functions(&self) -> impl Iterator<Item = &Function> {
        self.funcs.iter()
    }

    fn parse_name_section(
        &mut self,
        names: wasmparser::NameSectionReader,
        indices: &IndicesToIds,
    ) -> Result<()> {
        log::debug!("parse name section");
        for name in names {
            match name? {
                wasmparser::Name::Module(m) => {
                    self.name = Some(m.get_name()?.to_string());
                }
                wasmparser::Name::Function(f) => {
                    let mut map = f.get_map()?;
                    for _ in 0..map.get_count() {
                        let naming = map.read()?;
                        match indices.get_func(naming.index) {
                            Ok(id) => self.funcs.get_mut(id).name = Some(naming.name.to_string()),
                            // If some tool fails to GC function names properly,
                            // it doesn't really hurt anything to ignore the
                            // broken references and keep going.
                            Err(e) => warn!("in name section: {}", e),
                        }
                    }
                }
                wasmparser::Name::Type(t) => {
                    let mut map = t.get_map()?;
                    for _ in 0..map.get_count() {
                        let naming = map.read()?;
                        match indices.get_type(naming.index) {
                            Ok(id) => self.types.get_mut(id).name = Some(naming.name.to_string()),
                            Err(e) => warn!("in name section: {}", e),
                        }
                    }
                }
                wasmparser::Name::Memory(m) => {
                    let mut map = m.get_map()?;
                    for _ in 0..map.get_count() {
                        let naming = map.read()?;
                        match indices.get_memory(naming.index) {
                            Ok(id) => {
                                self.memories.get_mut(id).name = Some(naming.name.to_string())
                            }
                            Err(e) => warn!("in name section: {}", e),
                        }
                    }
                }
                wasmparser::Name::Table(t) => {
                    let mut map = t.get_map()?;
                    for _ in 0..map.get_count() {
                        let naming = map.read()?;
                        match indices.get_table(naming.index) {
                            Ok(id) => self.tables.get_mut(id).name = Some(naming.name.to_string()),
                            Err(e) => warn!("in name section: {}", e),
                        }
                    }
                }
                wasmparser::Name::Data(d) => {
                    let mut map = d.get_map()?;
                    for _ in 0..map.get_count() {
                        let naming = map.read()?;
                        match indices.get_data(naming.index) {
                            Ok(id) => self.data.get_mut(id).name = Some(naming.name.to_string()),
                            Err(e) => warn!("in name section: {}", e),
                        }
                    }
                }
                wasmparser::Name::Element(e) => {
                    let mut map = e.get_map()?;
                    for _ in 0..map.get_count() {
                        let naming = map.read()?;
                        match indices.get_element(naming.index) {
                            Ok(id) => {
                                self.elements.get_mut(id).name = Some(naming.name.to_string())
                            }
                            Err(e) => warn!("in name section: {}", e),
                        }
                    }
                }
                wasmparser::Name::Global(e) => {
                    let mut map = e.get_map()?;
                    for _ in 0..map.get_count() {
                        let naming = map.read()?;
                        match indices.get_global(naming.index) {
                            Ok(id) => self.globals.get_mut(id).name = Some(naming.name.to_string()),
                            Err(e) => warn!("in name section: {}", e),
                        }
                    }
                }
                wasmparser::Name::Local(l) => {
                    let mut reader = l.get_indirect_map()?;
                    for _ in 0..reader.get_indirect_count() {
                        let name = reader.read()?;
                        let func_id = indices.get_func(name.indirect_index)?;
                        let mut map = name.get_map()?;
                        for _ in 0..map.get_count() {
                            let naming = map.read()?;
                            // Looks like tools like `wat2wasm` generate empty
                            // names for locals if they aren't specified, so
                            // just ignore empty names which would in theory
                            // make debugging a bit harder.
                            if self.config.generate_synthetic_names_for_anonymous_items
                                && naming.name.is_empty()
                            {
                                continue;
                            }
                            match indices.get_local(func_id, naming.index) {
                                Ok(id) => {
                                    self.locals.get_mut(id).name = Some(naming.name.to_string())
                                }
                                // It looks like emscripten leaves broken
                                // function references in the locals subsection
                                // sometimes.
                                Err(e) => warn!("in name section: {}", e),
                            }
                        }
                    }
                }
                wasmparser::Name::Unknown { ty, .. } => warn!("unknown name subsection {}", ty),
                wasmparser::Name::Label(_) => warn!("labels name subsection ignored"),
            }
        }
        Ok(())
    }
}

fn emit_name_section(cx: &mut EmitContext) {
    log::debug!("emit name section");

    let mut wasm_name_section = wasm_encoder::NameSection::new();

    let mut funcs = cx
        .module
        .funcs
        .iter()
        .filter_map(|func| func.name.as_ref().map(|name| (func, name)))
        .map(|(func, name)| (cx.indices.get_func_index(func.id()), name))
        .collect::<Vec<_>>();
    funcs.sort_by_key(|p| p.0); // sort by index

    let mut locals = cx
        .module
        .funcs
        .iter()
        .filter_map(|func| cx.locals.get(&func.id()).map(|l| (func, l)))
        .filter_map(|(func, locals)| {
            let local_names = locals
                .iter()
                .filter_map(|id| {
                    let name = cx.module.locals.get(*id).name.as_ref()?;
                    let index = cx.indices.locals.get(&func.id())?.get(id)?;
                    Some((*index, name))
                })
                .collect::<Vec<_>>();
            if local_names.len() == 0 {
                None
            } else {
                Some((cx.indices.get_func_index(func.id()), local_names))
            }
        })
        .collect::<Vec<_>>();
    locals.sort_by_key(|p| p.0); // sort by index

    if cx.module.name.is_none() && funcs.len() == 0 && locals.len() == 0 {
        return;
    }

    if let Some(name) = &cx.module.name {
        wasm_name_section.module(name);
    }

    if funcs.len() > 0 {
        let mut name_map = wasm_encoder::NameMap::new();
        for (index, name) in funcs {
            name_map.append(index, name);
        }
        wasm_name_section.functions(&name_map);
    }

    if locals.len() > 0 {
        let mut indirect_name_map = wasm_encoder::IndirectNameMap::new();
        for (index, mut map) in locals {
            let mut name_map = wasm_encoder::NameMap::new();
            map.sort_by_key(|p| p.0); // sort by index
            for (index, name) in map {
                name_map.append(index, name);
            }
            indirect_name_map.append(index, &name_map);
        }
        wasm_name_section.locals(&indirect_name_map);
    }

    cx.wasm_module.section(&wasm_name_section);
}