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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
use parity_wasm::elements::{External, FunctionType, ImportSection, Module, Type};

use super::{
    imports::{ImportList, ImportType},
    ChiselModule, ModuleError, ModuleKind, ModulePreset, ModuleValidator,
};

/// Enum representing the state of an import in a module.
#[derive(PartialEq)]
pub enum ImportStatus {
    Good,
    NotFound,
    Malformed,
}

/// Trait over ImportType that lets a caller check if it is imported in a given module, and
/// verifies its type signature is correct.
trait IsImported {
    fn is_imported(&self, module: &Module) -> bool;
}

/// Trait over ImportType that checks an import's type signature in the case that it is imported.
trait ImportCheck {
    fn check(&self, module: &Module) -> ImportStatus;
}

/// Struct on which ModuleValidator is implemented.
pub struct VerifyImports<'a> {
    /// List of function signatures to check.
    list: ImportList<'a>,
    /// Option to require the presence of all listed imports in the module. When false, only the
    /// validity of existing imports on the list is checked.
    require_all: bool,
    /// Option to allow imports that are not listed in `entries`.
    allow_unlisted: bool,
}

impl<'a> ChiselModule<'a> for VerifyImports<'a> {
    type ObjectReference = &'a dyn ModuleValidator;

    fn id(&'a self) -> String {
        "verifyimports".to_string()
    }

    fn kind(&'a self) -> ModuleKind {
        ModuleKind::Validator
    }

    fn as_abstract(&'a self) -> Self::ObjectReference {
        self as Self::ObjectReference
    }
}

impl<'a> ModulePreset for VerifyImports<'a> {
    fn with_preset(preset: &str) -> Result<Self, ModuleError> {
        let mut import_set = ImportList::new();
        let presets: String = preset
            .chars()
            .filter(|c| *c != ' ' && *c != '_' && *c != '\n' && *c != '\t')
            .collect();

        for preset_individual in presets.split(',') {
            let to_append = ImportList::with_preset(preset_individual)?;
            import_set.concatenate(to_append);
        }

        Ok(VerifyImports {
            list: import_set,
            require_all: false, //FIXME: How should require_all and allow_unlisted be handled in the case of multiple presets?
            allow_unlisted: false,
        })
    }
}

// Utility functions used in tests to get more coverage
#[cfg(test)]
impl<'a> VerifyImports<'a> {
    pub fn set_require_all(&mut self, arg: bool) {
        self.require_all = arg;
    }

    pub fn set_allow_unlisted(&mut self, arg: bool) {
        self.allow_unlisted = arg;
    }
}

impl<'a> ModuleValidator for VerifyImports<'a> {
    fn validate(&self, module: &Module) -> Result<bool, ModuleError> {
        let import_section_len = if let Some(section) = module.import_section() {
            section.entries().len()
        } else {
            0
        };

        Ok(match (self.require_all, self.allow_unlisted) {
            // Check that all listed imports exist and are correct.
            (true, true) => self
                .list
                .entries()
                .iter()
                .map(|e| e.is_imported(module))
                .find(|e| *e == false)
                .is_none(),
            // Check that all listed imports exist, are correct, and are the only imports in the
            // module.
            (true, false) => {
                self.list
                    .entries()
                    .iter()
                    .map(|e| e.is_imported(module))
                    .find(|e| *e == false)
                    .is_none()
                    && (self.list.entries().len() == import_section_len)
            }
            // Check that the imports which are both listed and imported are of correct type.
            (false, true) => self
                .list
                .entries()
                .iter()
                .map(|e| e.check(module))
                .find(|e| *e == ImportStatus::Malformed)
                .is_none(),
            (false, false) => {
                // Check that all existent imports are listed and correct.
                let checklist: Vec<ImportStatus> = self
                    .list
                    .entries()
                    .iter()
                    .map(|e| e.check(module))
                    .collect();
                let valid_entries_count = checklist
                    .iter()
                    .filter(|e| **e == ImportStatus::Good)
                    .count();

                // Proof: If the number of valid entries is equal to the number of existing entries, all
                // entries are valid.
                //
                // If an import entry is valid, it exists; If the number of existent imports is
                // equal to the number of valid imports, all existent imports are valid; If all
                // existent imports are valid, there are no existent invalid imports; qed
                valid_entries_count == import_section_len
            }
        })
    }
}

impl<'a> IsImported for ImportType<'a> {
    fn is_imported(&self, module: &Module) -> bool {
        if let Some(section) = module.import_section() {
            match self {
                ImportType::Function(namespace, field, sig) => {
                    has_func_import(module, namespace, field, sig)
                }
                ImportType::Global(namespace, field) => {
                    has_global_import(section, namespace, field)
                }
                ImportType::Memory(namespace, field) => {
                    has_memory_import(section, namespace, field)
                }
                ImportType::Table(namespace, field) => has_table_import(section, namespace, field),
            }
        } else {
            false
        }
    }
}

impl<'a> ImportCheck for ImportType<'a> {
    fn check(&self, module: &Module) -> ImportStatus {
        // Destructure self here so that it is easier to manipulate individual fields later.
        let (module_str, field_str, func_sig) = match self {
            ImportType::Function(namespace, field, sig) => (namespace, field, Some(sig)),
            ImportType::Global(namespace, field) => (namespace, field, None),
            ImportType::Memory(namespace, field) => (namespace, field, None),
            ImportType::Table(namespace, field) => (namespace, field, None),
        };

        if let Some(section) = module.import_section() {
            // Find an entry that matches self. If the name matches, check the namespace and/or
            // signature.
            if let Some(entry) = section
                .entries()
                .iter()
                .find(|e| e.field() == *field_str && *module_str == e.module())
            {
                match entry.external() {
                    External::Function(idx) => {
                        let sig = func_sig.expect("Function entry missing signature!");
                        if *sig == imported_func_sig_by_index(module, *idx as usize) {
                            ImportStatus::Good
                        } else {
                            ImportStatus::Malformed
                        }
                    }
                    // NOTE: There may be a better way to do mappings between enum variants.
                    // Just check import variant here.
                    External::Global(_idx) => {
                        if let ImportType::Global(_n, _f) = self {
                            ImportStatus::Good
                        } else {
                            ImportStatus::Malformed
                        }
                    }
                    External::Memory(_idx) => {
                        if let ImportType::Memory(_n, _f) = self {
                            ImportStatus::Good
                        } else {
                            ImportStatus::Malformed
                        }
                    }
                    External::Table(_idx) => {
                        if let ImportType::Table(_n, _f) = self {
                            ImportStatus::Good
                        } else {
                            ImportStatus::Malformed
                        }
                    }
                }
            } else {
                ImportStatus::NotFound
            }
        } else {
            ImportStatus::NotFound
        }
    }
}

fn has_global_import(section: &ImportSection, namespace: &str, field: &str) -> bool {
    if let Some(import) = section
        .entries()
        .iter()
        .find(|e| e.module() == namespace && e.field() == field)
    {
        match import.external() {
            External::Global(_globaltype) => true,
            _ => false,
        }
    } else {
        false
    }
}

fn has_memory_import(section: &ImportSection, namespace: &str, field: &str) -> bool {
    if let Some(import) = section
        .entries()
        .iter()
        .find(|e| e.module() == namespace && e.field() == field)
    {
        match import.external() {
            External::Memory(_memorytype) => true,
            _ => false,
        }
    } else {
        false
    }
}

fn has_table_import(section: &ImportSection, namespace: &str, field: &str) -> bool {
    if let Some(import) = section
        .entries()
        .iter()
        .find(|e| e.module() == namespace && e.field() == field)
    {
        match import.external() {
            External::Table(_tabletype) => true,
            _ => false,
        }
    } else {
        false
    }
}

fn has_func_import(module: &Module, namespace: &str, field: &str, sig: &FunctionType) -> bool {
    if let Some(section) = module.import_section() {
        if let Some(import) = section
            .entries()
            .iter()
            .find(|e| e.module() == namespace && e.field() == field)
        {
            match import.external() {
                External::Function(index) => {
                    imported_func_sig_by_index(module, *index as usize) == *sig
                }
                _ => false,
            }
        } else {
            false
        }
    } else {
        false
    }
}

/// Resolves an imported function's signature from its callable index.
pub fn imported_func_sig_by_index(module: &Module, index: usize) -> FunctionType {
    module.import_section().expect("No function section found");
    let type_section = module.type_section().expect("No type section found");

    match type_section.types()[index] {
        Type::Function(ref func_type) => func_type.clone(),
    }
}

#[cfg(test)]
mod tests {
    use parity_wasm::elements::ValueType;

    use super::*;

    #[test]
    fn no_imports_ok_ewasm() {
        // wast:
        // (module
        //   (memory 1)
        //   (export "main" (func $main))
        //   (export "memory" (memory 0))
        //   (func $main)
        // )
        let wasm: Vec<u8> = vec![
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
            0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07, 0x11, 0x02, 0x04, 0x6d,
            0x61, 0x69, 0x6e, 0x00, 0x00, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00,
            0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
        ];

        let module = Module::from_bytes(&wasm).unwrap();
        let checker = VerifyImports::with_preset("ewasm").unwrap();
        let result = checker.validate(&module).unwrap();
        assert_eq!(true, result);
    }

    #[test]
    fn one_import_ok_ewasm() {
        // wast:
        // (module
        //   (import "ethereum" "storageStore" (func $storageStore (param i32 i32)))
        //   (memory 1)
        //   (export "main" (func $main))
        //   (export "memory" (memory 0))
        //   (func $main)
        // )
        let wasm: Vec<u8> = vec![
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x02, 0x7f,
            0x7f, 0x00, 0x60, 0x00, 0x00, 0x02, 0x19, 0x01, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72,
            0x65, 0x75, 0x6d, 0x0c, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x53, 0x74, 0x6f,
            0x72, 0x65, 0x00, 0x00, 0x03, 0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
            0x11, 0x02, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x01, 0x06, 0x6d, 0x65, 0x6d, 0x6f,
            0x72, 0x79, 0x02, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
        ];

        let module = Module::from_bytes(&wasm).unwrap();
        let checker = VerifyImports::with_preset("ewasm").unwrap();
        let result = checker.validate(&module).unwrap();
        assert_eq!(true, result);
    }

    #[test]
    fn one_import_bad_sig_ewasm() {
        // wast:
        // (module
        //   (import "ethereum" "storageStore" (func $storageStore (param i32)))
        //   (memory 1)
        //   (export "main" (func $main))
        //   (export "memory" (memory 0))
        //   (func $main)
        // )
        let wasm: Vec<u8> = vec![
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x60, 0x01, 0x7f,
            0x00, 0x60, 0x00, 0x00, 0x02, 0x19, 0x01, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65,
            0x75, 0x6d, 0x0c, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x53, 0x74, 0x6f, 0x72,
            0x65, 0x00, 0x00, 0x03, 0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07, 0x11,
            0x02, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x01, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72,
            0x79, 0x02, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
        ];

        let module = Module::from_bytes(&wasm).unwrap();
        let checker = VerifyImports::with_preset("ewasm").unwrap();
        let result = checker.validate(&module).unwrap();
        assert_eq!(false, result);
    }

    #[test]
    fn one_import_bad_namespace_ewasm() {
        // wast:
        // (module
        //   (import "env" "storageStore" (func $storageStore (param i32 i32)))
        //   (memory 1)
        //   (export "main" (func $main))
        //   (export "memory" (memory 0))
        //   (func $main)
        // )
        let wasm: Vec<u8> = vec![
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x02, 0x7f,
            0x7f, 0x00, 0x60, 0x00, 0x00, 0x02, 0x14, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x0c, 0x73,
            0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x53, 0x74, 0x6f, 0x72, 0x65, 0x00, 0x00, 0x03,
            0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07, 0x11, 0x02, 0x04, 0x6d, 0x61,
            0x69, 0x6e, 0x00, 0x01, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x0a,
            0x04, 0x01, 0x02, 0x00, 0x0b,
        ];

        let module = Module::from_bytes(&wasm).unwrap();
        let checker = VerifyImports::with_preset("ewasm").unwrap();
        let result = checker.validate(&module).unwrap();
        assert_eq!(false, result);
    }

    #[test]
    fn one_import_bad_field_ewasm() {
        // wast:
        // (module
        //   (import "ethereum" "stoageStore" (func $storageStore (param i32 i32)))
        //   (memory 1)
        //   (export "main" (func $main))
        //   (export "memory" (memory 0))
        //   (func $main)
        // )
        let wasm: Vec<u8> = vec![
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x02, 0x7f,
            0x7f, 0x00, 0x60, 0x00, 0x00, 0x02, 0x18, 0x01, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72,
            0x65, 0x75, 0x6d, 0x0b, 0x73, 0x74, 0x6f, 0x61, 0x67, 0x65, 0x53, 0x74, 0x6f, 0x72,
            0x65, 0x00, 0x00, 0x03, 0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07, 0x11,
            0x02, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x01, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72,
            0x79, 0x02, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
        ];

        let module = Module::from_bytes(&wasm).unwrap();
        let checker = VerifyImports::with_preset("ewasm").unwrap();
        let result = checker.validate(&module).unwrap();
        assert_eq!(false, result);
    }

    #[test]
    fn state_test_case_good_ewasm() {
        // wast:
        // (module
        // (import "ethereum" "storageStore" (func $storageStore (param i32 i32)))
        //   (memory 1)
        //   (data (i32.const 0)  "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00") ;; Path
        //   (data (i32.const 32) "\cd\ab\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00") ;; Value
        //   (export "memory" (memory 0))
        //   (export "main" (func $main))
        //   (func $main
        //     ;; Write to storage
        //     (call $storageStore (i32.const 0) (i32.const 32))
        //   )
        // )
        let wasm: Vec<u8> = vec![
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x02, 0x7f,
            0x7f, 0x00, 0x60, 0x00, 0x00, 0x02, 0x19, 0x01, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72,
            0x65, 0x75, 0x6d, 0x0c, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x53, 0x74, 0x6f,
            0x72, 0x65, 0x00, 0x00, 0x03, 0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
            0x11, 0x02, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x04, 0x6d, 0x61,
            0x69, 0x6e, 0x00, 0x01, 0x0a, 0x0a, 0x01, 0x08, 0x00, 0x41, 0x00, 0x41, 0x20, 0x10,
            0x00, 0x0b, 0x0b, 0x4b, 0x02, 0x00, 0x41, 0x00, 0x0b, 0x20, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x41, 0x20, 0x0b, 0x20, 0xcd, 0xab, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        ];

        let module = Module::from_bytes(&wasm).unwrap();
        let checker = VerifyImports::with_preset("ewasm").unwrap();
        let result = checker.validate(&module).unwrap();
        assert_eq!(true, result);
    }

    #[test]
    fn unlisted_import_eth_namespace_ewasm() {
        // wast:
        // (module
        //   (import "ethereum" "storageStore" (func $storageStore (param i32 i32)))
        //   (import "ethereum" "foo" (func $foo))
        //   (memory 1)
        //   (export "main" (func $main))
        //   (export "memory" (memory 0))
        //   (func $main)
        // )
        let wasm: Vec<u8> = vec![
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x02, 0x7f,
            0x7f, 0x00, 0x60, 0x00, 0x00, 0x02, 0x2b, 0x02, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72,
            0x65, 0x75, 0x6d, 0x0c, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x53, 0x74, 0x6f,
            0x72, 0x65, 0x00, 0x00, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x06,
            0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72, 0x00, 0x01, 0x03, 0x02, 0x01, 0x01, 0x05, 0x03,
            0x01, 0x00, 0x01, 0x07, 0x11, 0x02, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x02, 0x06,
            0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
        ];

        let module = Module::from_bytes(&wasm).unwrap();
        let checker = VerifyImports::with_preset("ewasm").unwrap();
        let result = checker.validate(&module).unwrap();
        assert_eq!(false, result);
    }

    #[test]
    fn unlisted_import_eth_namespace_good_ewasm() {
        // wast:
        // (module
        //   (import "ethereum" "storageStore" (func $storageStore (param i32 i32)))
        //   (import "ethereum" "foo" (func $foo))
        //   (memory 1)
        //   (export "main" (func $main))
        //   (export "memory" (memory 0))
        //   (func $main)
        // )
        let wasm: Vec<u8> = vec![
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x02, 0x7f,
            0x7f, 0x00, 0x60, 0x00, 0x00, 0x02, 0x2b, 0x02, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72,
            0x65, 0x75, 0x6d, 0x0c, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x53, 0x74, 0x6f,
            0x72, 0x65, 0x00, 0x00, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x06,
            0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72, 0x00, 0x01, 0x03, 0x02, 0x01, 0x01, 0x05, 0x03,
            0x01, 0x00, 0x01, 0x07, 0x11, 0x02, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x02, 0x06,
            0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
        ];

        let module = Module::from_bytes(&wasm).unwrap();
        let mut checker = VerifyImports::with_preset("ewasm").unwrap();
        // Allow unlisted, just for this test case
        checker.set_allow_unlisted(true);
        let result = checker.validate(&module).unwrap();
        assert_eq!(true, result);
    }

    #[test]
    fn one_import_but_all_required_ewasm() {
        // wast:
        // (module
        //   (import "ethereum" "storageStore" (func $storageStore (param i32 i32)))
        //   (memory 1)
        //   (export "main" (func $main))
        //   (export "memory" (memory 0))
        //   (func $main)
        // )
        let wasm: Vec<u8> = vec![
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x02, 0x7f,
            0x7f, 0x00, 0x60, 0x00, 0x00, 0x02, 0x19, 0x01, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72,
            0x65, 0x75, 0x6d, 0x0c, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x53, 0x74, 0x6f,
            0x72, 0x65, 0x00, 0x00, 0x03, 0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
            0x11, 0x02, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x01, 0x06, 0x6d, 0x65, 0x6d, 0x6f,
            0x72, 0x79, 0x02, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
        ];

        let module = Module::from_bytes(&wasm).unwrap();
        let mut checker = VerifyImports::with_preset("ewasm").unwrap();
        // Require all, just for this test case
        checker.set_require_all(true);
        let result = checker.validate(&module).unwrap();
        assert_eq!(false, result);
    }

    #[test]
    fn all_required_imports() {
        // wast:
        // (module
        //   (import "ethereum" "storageStore" (func $storageStore (param i32 i32)))
        //   (memory 1)
        //   (export "main" (func $main))
        //   (export "memory" (memory 0))
        //   (func $main)
        // )
        let wasm: Vec<u8> = vec![
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x02, 0x7f,
            0x7f, 0x00, 0x60, 0x00, 0x00, 0x02, 0x19, 0x01, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72,
            0x65, 0x75, 0x6d, 0x0c, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x53, 0x74, 0x6f,
            0x72, 0x65, 0x00, 0x00, 0x03, 0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
            0x11, 0x02, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x01, 0x06, 0x6d, 0x65, 0x6d, 0x6f,
            0x72, 0x79, 0x02, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
        ];

        let module = Module::from_bytes(&wasm).unwrap();
        let checker = VerifyImports {
            list: ImportList::with_entries(vec![ImportType::Function(
                "ethereum",
                "storageStore",
                FunctionType::new(vec![ValueType::I32, ValueType::I32], None),
            )]),
            require_all: true,
            allow_unlisted: false,
        };
        let result = checker.validate(&module).unwrap();
        assert_eq!(true, result);
    }

    #[test]
    fn all_required_imports_but_one_unlisted_same_namespace_ok() {
        // wast:
        // (module
        //   (import "ethereum" "storageStore" (func $storageStore (param i32 i32)))
        //   (import "ethereum" "foo" (func $foo))
        //   (memory 1)
        //   (export "main" (func $main))
        //   (export "memory" (memory 0))
        //   (func $main)
        // )
        let wasm: Vec<u8> = vec![
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x02, 0x7f,
            0x7f, 0x00, 0x60, 0x00, 0x00, 0x02, 0x28, 0x02, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72,
            0x65, 0x75, 0x6d, 0x0c, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x53, 0x74, 0x6f,
            0x72, 0x65, 0x00, 0x00, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x03,
            0x66, 0x6f, 0x6f, 0x00, 0x01, 0x03, 0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01,
            0x07, 0x11, 0x02, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x02, 0x06, 0x6d, 0x65, 0x6d,
            0x6f, 0x72, 0x79, 0x02, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
        ];

        let module = Module::from_bytes(&wasm).unwrap();
        let checker = VerifyImports {
            list: ImportList::with_entries(vec![ImportType::Function(
                "ethereum",
                "storageStore",
                FunctionType::new(vec![ValueType::I32, ValueType::I32], None),
            )]),
            allow_unlisted: true,
            require_all: true,
        };
        let result = checker.validate(&module).unwrap();
        assert_eq!(true, result);
    }

    #[test]
    fn all_required_imports_but_one_unlisted_same_namespace() {
        // wast:
        // (module
        //   (import "ethereum" "storageStore" (func $storageStore (param i32 i32)))
        //   (import "ethereum" "foo" (func $foo))
        //   (memory 1)
        //   (export "main" (func $main))
        //   (export "memory" (memory 0))
        //   (func $main)
        // )
        let wasm: Vec<u8> = vec![
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x02, 0x7f,
            0x7f, 0x00, 0x60, 0x00, 0x00, 0x02, 0x28, 0x02, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72,
            0x65, 0x75, 0x6d, 0x0c, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x53, 0x74, 0x6f,
            0x72, 0x65, 0x00, 0x00, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x03,
            0x66, 0x6f, 0x6f, 0x00, 0x01, 0x03, 0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01,
            0x07, 0x11, 0x02, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x02, 0x06, 0x6d, 0x65, 0x6d,
            0x6f, 0x72, 0x79, 0x02, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
        ];

        let module = Module::from_bytes(&wasm).unwrap();
        let checker = VerifyImports {
            list: ImportList::with_preset("ewasm").unwrap(),
            allow_unlisted: false,
            require_all: true,
        };
        let result = checker.validate(&module).unwrap();
        assert_eq!(false, result);
    }

    #[test]
    fn all_required_imports_but_one_unlisted_diff_namespace() {
        // wast:
        // (module
        //   (import "ethereum" "storageStore" (func $storageStore (param i32 i32)))
        //   (import "env" "foo" (func $foo))
        //   (memory 1)
        //   (export "main" (func $main))
        //   (export "memory" (memory 0))
        //   (func $main)
        // )
        let wasm: Vec<u8> = vec![
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x02, 0x7f,
            0x7f, 0x00, 0x60, 0x00, 0x00, 0x02, 0x23, 0x02, 0x08, 0x65, 0x74, 0x68, 0x65, 0x72,
            0x65, 0x75, 0x6d, 0x0c, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x53, 0x74, 0x6f,
            0x72, 0x65, 0x00, 0x00, 0x03, 0x65, 0x6e, 0x76, 0x03, 0x66, 0x6f, 0x6f, 0x00, 0x01,
            0x03, 0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07, 0x11, 0x02, 0x04, 0x6d,
            0x61, 0x69, 0x6e, 0x00, 0x02, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00,
            0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
        ];

        let module = Module::from_bytes(&wasm).unwrap();
        let checker = VerifyImports {
            list: ImportList::with_preset("ewasm").unwrap(),
            allow_unlisted: false,
            require_all: true,
        };
        let result = checker.validate(&module).unwrap();
        assert_eq!(false, result);
    }

    #[test]
    fn verify_with_dynamic_dispatch_before_imports_good() {
        // NOTE: This is important for binaries utilizing dynamic dispatch.
        // For example, rustc will place a type for vtable lookups before the imports in the type
        // section, causing OOB.
        // wast:
        // (module
        //   (type (;0;) (func (param i32 i32 i32) (result i32))) ; this is the problem
        //   (type (;1;) (func (param i64)))
        //
        //   (import "ethereum" "useGas" (func $useGas (type 1)))
        //
        //   (memory 1)
        //   (export "memory" (memory 0))
        //   (export "main" (func $main))
        //
        //   (func $main)
        // )
        let wasm: Vec<u8> = vec![
            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0f, 0x03, 0x60, 0x03, 0x7f,
            0x7f, 0x7f, 0x01, 0x7f, 0x60, 0x01, 0x7e, 0x00, 0x60, 0x00, 0x00, 0x02, 0x13, 0x01,
            0x08, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x06, 0x75, 0x73, 0x65, 0x47,
            0x61, 0x73, 0x00, 0x01, 0x03, 0x02, 0x01, 0x02, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
            0x11, 0x02, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x04, 0x6d, 0x61,
            0x69, 0x6e, 0x00, 0x01, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
        ];

        let module = Module::from_bytes(&wasm).unwrap();
        let checker = VerifyImports::with_preset("ewasm").unwrap();

        let result = checker.validate(&module).unwrap();

        assert_eq!(true, result);
    }
}