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
use crate::ast;
use log::debug;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::Mutex;
use threadpool::ThreadPool;

pub fn locals_flatten(locals: Vec<ast::CodeLocal>) -> Vec<ast::CodeLocal> {
    let mut out = Vec::new();

    for local in locals {
        for _ in 0..local.count {
            out.push(ast::CodeLocal {
                count: 1,
                value_type: local.value_type.clone(),
            });
        }
    }

    out
}

pub struct WasmModule {
    pub inner: Arc<ast::Module>,
    types: Mutex<HashMap<u32, ast::Type>>,
    /// Mapping between funcidx and count of func locals
    func_locals: HashMap<u32, Vec<ast::CodeLocal>>,
    func_to_typeidx: Mutex<Vec<u32>>,
    func_starts: HashMap<u32, usize>,
    imports: Vec<ast::Import>,
    exports: Vec<ast::Export>,
}
impl WasmModule {
    pub fn new(inner: Arc<ast::Module>) -> Self {
        let mut types = HashMap::new();
        let mut func_locals = HashMap::new();
        let mut func_to_typeidx = Vec::new();
        let mut imports = Vec::new();
        let mut exports = Vec::new();
        let mut func_starts = HashMap::new();

        for section in inner.sections.lock().unwrap().iter() {
            match &section.value {
                ast::Section::Type((_size, content)) => {
                    let mut typeidx = 0;
                    for t in &*content.lock().unwrap() {
                        types.insert(typeidx, t.to_owned());
                        typeidx += 1;
                    }
                }

                ast::Section::Import((_size, content)) => {
                    imports = content.lock().unwrap().clone();
                }

                ast::Section::Func((_size, content)) => {
                    func_to_typeidx = content.lock().unwrap().clone();
                }

                ast::Section::Export((_section_size, content)) => {
                    exports = content.lock().unwrap().clone();
                }

                ast::Section::Code((_section_size, content)) => {
                    let mut funcidx = imports.len() as u32;

                    for c in &content.lock().unwrap().value {
                        func_starts.insert(funcidx as u32, c.body.lock().unwrap().start_offset);
                        func_locals.insert(funcidx, c.locals.clone());
                        funcidx += 1;
                    }
                }
                _ => {}
            }
        }

        Self {
            inner,
            imports,
            exports,
            func_locals,
            func_starts,
            types: Mutex::new(types),
            func_to_typeidx: Mutex::new(func_to_typeidx),
        }
    }

    pub fn add_data(&self, offset: u32, bytes: &[u8]) -> (u32, u32) {
        for section in self.inner.sections.lock().unwrap().iter() {
            match &section.value {
                ast::Section::Data((_section_size, content)) => {
                    let segment = ast::DataSegment {
                        offset: ast::Value::new(vec![
                            ast::Value::new(ast::Instr::i32_const(offset as i64)),
                            ast::Value::new(ast::Instr::end),
                        ]),
                        bytes: bytes.to_vec(),
                    };
                    content.lock().unwrap().push(segment);
                }
                _ => {}
            }
        }

        (offset, offset + bytes.len() as u32)
    }

    pub fn is_func_imported(&self, funcidx: u32) -> bool {
        (funcidx as usize) < self.imports.len()
    }

    pub fn func_locals_count(&self, funcidx: u32) -> u32 {
        let locals = self.func_locals(funcidx);
        let mut count = 0;
        for local in locals {
            count += local.count;
        }

        count
    }

    pub fn func_locals(&self, funcidx: u32) -> Vec<ast::CodeLocal> {
        let locals = self
            .func_locals
            .get(&funcidx)
            .expect(&format!("locals for funcidx {}", funcidx));
        locals.to_owned()
    }

    pub fn is_func_exported(&self, funcidx: u32) -> bool {
        for export in &self.exports {
            match &export.descr {
                ast::ExportDescr::Func(f) => {
                    if *f.lock().unwrap() == funcidx {
                        return true;
                    }
                }
                _ => {}
            }
        }

        false
    }

    /// Retrieve the type of a function,
    /// note that this doesn't work for imported functions as they
    /// have their type expressed differently.
    pub fn get_func_type(&self, funcidx: u32) -> ast::Type {
        let typeidx = self.get_func_typeidx(funcidx);
        let types = self.types.lock().unwrap();
        types.get(&typeidx).expect("type not found").clone()
    }

    pub fn get_func_typeidx(&self, funcidx: u32) -> u32 {
        let func_to_typeidx = self.func_to_typeidx.lock().unwrap();

        if (funcidx as usize) < self.imports.len() {
            // Func is an imported function
            let import = &self.imports[funcidx as usize];
            import.typeidx
        } else {
            // Func is an implemented function
            let funcidx = funcidx as usize - self.imports.len();

            *func_to_typeidx
                .get(funcidx as usize)
                .expect(&format!("type not found for funcidx: {}", funcidx))
        }
    }

    pub fn get_code_section_start_offset(&self) -> Option<usize> {
        for section in self.inner.sections.lock().unwrap().iter() {
            match &section.value {
                ast::Section::Code(_) => return Some(section.start_offset),
                _ => {}
            }
        }
        None
    }

    pub fn get_start_of_func(&self, funcidx: u32) -> Option<usize> {
        if let Some(start) = self.func_starts.get(&funcidx) {
            Some(*start)
        } else {
            None
        }
    }

    pub fn get_custom_section(&self, name: &str) -> Option<Vec<u8>> {
        for section in self.inner.sections.lock().unwrap().iter() {
            match &section.value {
                ast::Section::Custom((_size, section)) => match &*section.lock().unwrap() {
                    ast::CustomSection::Unknown(section_name, bytes) => {
                        if section_name == name {
                            return Some(bytes.to_owned());
                        }
                    }
                    _ => {}
                },
                _ => {}
            }
        }

        None
    }

    pub fn get_func_name(&self, funcidx: u32) -> Option<String> {
        for section in self.inner.sections.lock().unwrap().iter() {
            match &section.value {
                ast::Section::Custom((_size, section)) => match &*section.lock().unwrap() {
                    ast::CustomSection::Name(names) => {
                        if let Some(name) = names.func_names.get(&funcidx) {
                            return Some(name.clone());
                        }
                    }
                    _ => {}
                },
                _ => {}
            }
        }

        None
    }

    pub fn find_import(&self, name: &str) -> u32 {
        let mut funcidx = 0;
        for section in self.inner.sections.lock().unwrap().iter() {
            match &section.value {
                ast::Section::Import((_section_size, content)) => {
                    for import in &*content.lock().unwrap() {
                        if import.name == name {
                            return funcidx;
                        }
                        funcidx += 1;
                    }
                }
                _ => {}
            }
        }

        0
    }

    pub fn add_import(&self, _import: &ast::Import) -> u32 {
        unimplemented!("Adding an import requires to shifts all references to functions by one, which is unsafe (func tables) or inconvenient (name section).");
    }

    pub fn add_global(&self, global: &ast::Global) -> Option<u32> {
        for section in self.inner.sections.lock().unwrap().iter() {
            match &section.value {
                ast::Section::Global((_section_size, content)) => {
                    let globalidx = content.lock().unwrap().len() as u32;
                    content.lock().unwrap().push(global.to_owned());
                    return Some(globalidx);
                }
                _ => {}
            }
        }

        None
    }

    pub fn add_function(&self, func: &ast::Code, typeidx: u32) -> u32 {
        let mut funcidx = 0;

        for section in self.inner.sections.lock().unwrap().iter() {
            match &section.value {
                ast::Section::Import((_section_size, content)) => {
                    // TODO: why count Import and not Func section?
                    funcidx += content.lock().unwrap().len() as u32;
                }
                ast::Section::Code((_section_size, content)) => {
                    funcidx += content.lock().unwrap().value.len() as u32;
                    content.lock().unwrap().value.push(func.to_owned());
                }
                ast::Section::Func((_section_size, content)) => {
                    content.lock().unwrap().push(typeidx);
                }
                _ => {}
            }
        }

        self.func_to_typeidx.lock().unwrap().push(typeidx);
        funcidx
    }

    pub fn add_type(&self, t: &ast::Type) -> u32 {
        let mut typeidx = 0;

        for section in self.inner.sections.lock().unwrap().iter() {
            match &section.value {
                ast::Section::Type((_section_size, content)) => {
                    typeidx = content.lock().unwrap().len() as u32;
                    content.lock().unwrap().push(t.clone());

                    self.types.lock().unwrap().insert(typeidx, t.to_owned());
                }
                _ => {}
            }
        }

        typeidx
    }
}

pub struct VisitorContext<'a, T> {
    pub module: Arc<WasmModule>,
    insert_nodes_after: Vec<T>,
    insert_nodes_before: Vec<T>,
    replace_node: Option<T>,
    pub curr_funcidx: Option<u32>,
    pub node: &'a T,
    traverse_stop: bool,
}
impl<'a, T> VisitorContext<'a, T> {
    pub fn new(module: Arc<WasmModule>, node: &'a T) -> Self {
        Self {
            node,
            module,
            insert_nodes_after: vec![],
            insert_nodes_before: vec![],
            replace_node: None,
            curr_funcidx: None,
            traverse_stop: false,
        }
    }
}

impl<'a, T> VisitorContext<'a, Vec<T>> {
    pub fn insert_node_after(&mut self, new_node: T) {
        self.insert_nodes_after.push(vec![new_node]);
    }

    pub fn insert_node_before(&mut self, new_node: T) {
        self.insert_nodes_before.push(vec![new_node]);
    }
}

impl<'a> VisitorContext<'a, ast::Value<ast::Instr>> {
    pub fn stop_traversal(&mut self) {
        self.traverse_stop = true;
    }

    pub fn insert_node_after(&mut self, new_node: ast::Instr) {
        self.insert_nodes_after.push(ast::Value::new(new_node));
    }

    pub fn insert_node_before(&mut self, new_node: ast::Instr) {
        self.insert_nodes_before.push(ast::Value::new(new_node));
    }

    pub fn replace_node(&mut self, new_node: ast::Instr) {
        self.replace_node = Some(ast::Value::new(new_node));
    }
}

pub trait Visitor {
    fn visit_instr<'a>(&self, _ctx: &'_ mut VisitorContext<'a, ast::Value<ast::Instr>>) {}
    fn visit_type<'a>(&self, _ctx: &'_ mut VisitorContext<'a, ast::Type>, _typeidx: u32) {}
    fn visit_code_section<'a>(&self, _ctx: &'_ mut VisitorContext<'a, Vec<ast::Code>>) {}
    fn visit_import_section<'a>(&self, _ctx: &'_ mut VisitorContext<'a, Vec<ast::Import>>) {}
    fn visit_func_section<'a>(&self, _ctx: &'_ mut VisitorContext<'a, Vec<u32>>) {}
    fn visit_data_section<'a>(&self, _ctx: &'_ mut VisitorContext<'a, Vec<ast::DataSegment>>) {}
    fn visit_table<'a>(&self, _ctx: &'_ mut VisitorContext<'a, ast::Table>) {}
    fn visit_export<'a>(&self, _ctx: &'_ mut VisitorContext<'a, ast::Export>) {}
    fn visit_element<'a>(&self, _ctx: &'_ mut VisitorContext<'a, ast::Element>) {}
}

pub fn traverse(module: Arc<ast::Module>, visitor: Arc<dyn Visitor + Send + Sync>) {
    let pool = ThreadPool::new(num_cpus::get());

    let mut curr_funcidx = 0;

    let module_ast = Arc::new(WasmModule::new(Arc::clone(&module)));

    for section in module.sections.lock().unwrap().iter() {
        match &section.value {
            ast::Section::Func((_section_size, funcs)) => {
                let nodes = funcs.lock().unwrap().clone();
                let mut ctx = VisitorContext::new(Arc::clone(&module_ast), &nodes);
                Arc::clone(&visitor).visit_func_section(&mut ctx);
                assert!(ctx.insert_nodes_before.is_empty());

                {
                    let mut new_nodes = ctx.insert_nodes_after;
                    new_nodes.reverse();

                    for new_node in new_nodes {
                        debug!("inject new func: {:?}", new_node);
                        funcs.lock().unwrap().extend_from_slice(&new_node);
                    }
                }
            }
            ast::Section::Export((_section_size, exports)) => {
                for export in exports.lock().unwrap().iter() {
                    let mut ctx = VisitorContext::new(Arc::clone(&module_ast), export);
                    visitor.visit_export(&mut ctx);
                    assert!(ctx.insert_nodes_before.is_empty());
                    assert!(ctx.insert_nodes_after.is_empty());
                }
            }
            ast::Section::Element((_section_size, elements)) => {
                for element in elements.lock().unwrap().iter() {
                    let mut ctx = VisitorContext::new(Arc::clone(&module_ast), element);
                    visitor.visit_element(&mut ctx);
                    assert!(ctx.insert_nodes_before.is_empty());
                    assert!(ctx.insert_nodes_after.is_empty());
                }
            }
            ast::Section::Table((_section_size, tables)) => {
                let module_ast = Arc::clone(&module_ast);
                for table in tables.lock().unwrap().iter() {
                    let mut ctx = VisitorContext::new(Arc::clone(&module_ast), table);
                    visitor.visit_table(&mut ctx);
                    assert!(ctx.insert_nodes_before.is_empty());
                    assert!(ctx.insert_nodes_after.is_empty());
                }
            }
            ast::Section::Type((_section_size, types)) => {
                let mut typeidx = 0;
                let types_copy = types.lock().unwrap().clone();
                for t in types_copy {
                    let mut ctx = VisitorContext::new(Arc::clone(&module_ast), &t);
                    visitor.visit_type(&mut ctx, typeidx);
                    typeidx += 1;

                    assert!(ctx.insert_nodes_before.is_empty());
                    assert!(ctx.insert_nodes_after.is_empty());
                }
            }
            ast::Section::Import((_section_size, content)) => {
                let nodes = content.lock().unwrap().clone();
                let mut ctx = VisitorContext::new(Arc::clone(&module_ast), &nodes);
                Arc::clone(&visitor).visit_import_section(&mut ctx);
                assert!(ctx.insert_nodes_before.is_empty());

                {
                    for new_node in ctx.insert_nodes_after {
                        debug!("inject new import: {:?}", new_node);
                        content.lock().unwrap().extend_from_slice(&new_node);
                    }
                }

                curr_funcidx += content.lock().unwrap().len() as u32;
            }
            ast::Section::Code((_section_size, codes)) => {
                {
                    let nodes = codes.lock().unwrap().clone().value;
                    let mut ctx = VisitorContext::new(Arc::clone(&module_ast), &nodes);
                    Arc::clone(&visitor).visit_code_section(&mut ctx);
                    assert!(ctx.insert_nodes_before.is_empty());

                    let mut new_nodes = ctx.insert_nodes_after;
                    new_nodes.reverse();

                    for new_node in new_nodes {
                        debug!("inject new code: {:?}", new_node);
                        codes.lock().unwrap().value.extend_from_slice(&new_node);
                    }
                }

                let codes = codes.lock().unwrap().clone();
                for code in codes.value {
                    {
                        let visitor = Arc::clone(&visitor);
                        let module_ast = Arc::clone(&module_ast);
                        pool.execute(move || {
                            visit_expr(
                                Arc::clone(&module_ast),
                                Arc::clone(&code.body),
                                Arc::clone(&visitor),
                                curr_funcidx,
                            );
                        });
                    }

                    curr_funcidx += 1;
                }
            }
            ast::Section::Data((_section_size, segments)) => {
                let nodes = segments.lock().unwrap().clone();
                let mut ctx = VisitorContext::new(Arc::clone(&module_ast), &nodes);
                Arc::clone(&visitor).visit_data_section(&mut ctx);
                assert!(ctx.insert_nodes_before.is_empty());

                let mut new_nodes = ctx.insert_nodes_after;
                new_nodes.reverse();

                for new_node in new_nodes {
                    debug!("inject new data: {:?}", new_node);
                    segments.lock().unwrap().extend_from_slice(&new_node);
                }
            }
            _ => {}
        }
    }

    // TODO: add barrier
    pool.join();
}

fn visit_expr(
    module_ast: Arc<WasmModule>,
    expr: ast::MutableValue<Vec<ast::Value<ast::Instr>>>,
    visitor: Arc<dyn Visitor + Send + Sync>,
    curr_funcidx: u32,
) {
    let expr_copy = expr.lock().unwrap().clone();

    // Keep track of many nodes we injected since we started iterating, so that
    // subsequent inserts are at the right place.
    // The iterator is a copy of the array of nodes.
    let mut added = 0;

    for i in 0..expr_copy.value.len() {
        let instr = expr_copy.value[i].clone();
        if let ast::Instr::Block(_, body) = instr.value {
            visit_expr(Arc::clone(&module_ast), body, visitor.clone(), curr_funcidx);
        } else if let ast::Instr::If(_, body) = instr.value {
            visit_expr(Arc::clone(&module_ast), body, visitor.clone(), curr_funcidx);
        } else if let ast::Instr::Loop(_, body) = instr.value {
            visit_expr(Arc::clone(&module_ast), body, visitor.clone(), curr_funcidx);
        } else {
            let mut ctx = VisitorContext::new(Arc::clone(&module_ast), &instr);
            ctx.curr_funcidx = Some(curr_funcidx);
            visitor.visit_instr(&mut ctx);

            if let Some(replace_node) = ctx.replace_node {
                debug!("replace instr: {:?}", replace_node);
                expr.lock().unwrap().value[i + added] = replace_node;
            }

            if ctx.insert_nodes_after.len() > 0 {
                debug!("insert instr(s): {:?}", ctx.insert_nodes_after);
                expr.lock().unwrap().value.splice(
                    (i + added + 1)..(i + added + 1),
                    ctx.insert_nodes_after.clone(),
                );
                added += ctx.insert_nodes_after.len();
            }

            if ctx.insert_nodes_before.len() > 0 {
                debug!("insert instr(s): {:?}", ctx.insert_nodes_before);

                expr.lock()
                    .unwrap()
                    .value
                    .splice((i + added)..(i + added), ctx.insert_nodes_before.clone());
                added += ctx.insert_nodes_before.len();
            }

            if ctx.traverse_stop {
                break;
            }
        }
    }
}