finite-wasm 0.6.0

Guarantee deterministic limits on execution time and space resources made available to the WebAssembly programs in a runtime-agnostic way.
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
use crate::{max_stack, Analysis, Fee};
use std::error;
use std::ffi::OsString;
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::process::ExitStatus;
use std::time::Duration;

pub fn write_error(mut to: impl io::Write, error: impl error::Error) -> std::io::Result<()> {
    writeln!(to, "error: {}", error)?;
    let mut source = error.source();
    while let Some(error) = source {
        writeln!(to, "caused by: {}", error)?;
        source = error.source();
    }
    Ok(())
}

#[derive(Debug, PartialEq)]
enum Line {
    Equal(String),
    Delete(String),
    Insert(String),
}

#[derive(Debug)]
pub(crate) struct DiffError {
    diff: Vec<Line>,
    path: Option<PathBuf>,
}

impl DiffError {
    fn diff(old: &str, new: &str) -> Option<Self> {
        if old == new {
            return None;
        }
        let result = dissimilar::diff(old, new);
        let diff = result
            .into_iter()
            .map(|s| match s {
                dissimilar::Chunk::Delete(s) => Line::Delete(s.into()),
                dissimilar::Chunk::Insert(s) => Line::Insert(s.into()),
                dissimilar::Chunk::Equal(s) => Line::Equal(s.into()),
            })
            .collect::<Vec<_>>();
        if diff.iter().all(|l| matches!(l, Line::Equal(_))) {
            None
        } else {
            Some(Self { diff, path: None })
        }
    }
    fn with_path(self, path: PathBuf) -> Self {
        Self {
            diff: self.diff,
            path: Some(path),
        }
    }
}
impl std::error::Error for DiffError {}
impl std::fmt::Display for DiffError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(path) = &self.path {
            f.write_fmt(format_args!(
                "non-empty differential against {}",
                path.display()
            ))?;
        } else {
            f.write_str("non-empty differential")?;
        }
        for line in self.diff.iter() {
            match line {
                Line::Delete(s) => {
                    f.write_str("\n      - ")?;
                    f.write_str(&s)?;
                }
                Line::Insert(s) => {
                    f.write_str("\n      + ")?;
                    f.write_str(&s)?;
                }
                Line::Equal(s) => {
                    f.write_str("\n        ")?;
                    f.write_str(&s)?;
                }
            }
        }
        f.write_str("\n")
    }
}

#[derive(thiserror::Error, Debug)]
pub(crate) enum Error {
    #[error("could not read the test file `{1}`")]
    ReadTestContents(#[source] std::io::Error, PathBuf),
    #[error("could not interpret test file `{1}` as UTF-8")]
    FromUtf8(#[source] std::str::Utf8Error, PathBuf),
    #[error("could not create the parse buffer for the test file")]
    ParseBuffer(#[source] wast::Error),
    #[error("could not parse the test file")]
    ParseWast(#[source] wast::Error),
    #[error("could not encode the wast module `{1}`")]
    EncodeModule(#[source] wast::Error, String),

    #[error("could not open the snapshot file {1:?}")]
    OpenSnap(#[source] std::io::Error, PathBuf),
    #[error("could not read the snapshot file {1:?}")]
    ReadSnap(#[source] std::io::Error, PathBuf),
    #[error("could not truncate the snapshot file {1:?}")]
    TruncateSnap(#[source] std::io::Error, PathBuf),
    #[error("could not seek the snapshot file {1:?}")]
    SeekSnap(#[source] std::io::Error, PathBuf),
    #[error("could not write the snapshot file {1:?}")]
    WriteSnap(#[source] std::io::Error, PathBuf),
    #[error("snapshot comparison failed")]
    DiffSnap(#[source] DiffError),

    #[error("could not execute the interpreter with arguments: {1:?}")]
    InterpreterLaunch(#[source] std::io::Error, Vec<OsString>),
    #[error("could not wait on the interpreter")]
    InterpreterWait(#[source] std::io::Error),
    #[error("reference interpreter failed with exit code {1:?} (arguments: {2:?})")]
    InterpreterExit(
        #[source] Box<dyn std::error::Error + Send + Sync>,
        ExitStatus,
        Vec<OsString>,
    ),
    #[error("interpreter output wasn’t valid UTF-8")]
    InterpreterOutput(#[source] std::string::FromUtf8Error),
    #[error("could not analyze the module {1} at {2:?}")]
    AnalyseModule(#[source] crate::Error, String, PathBuf),
    #[error("could not analyze the module {0} at {1:?}, analysis panicked")]
    AnalyseModulePanic(String, PathBuf),
    #[error("could not instrument the module {0} at {1:?}, instrumentation code panicked")]
    InstrumentModulePanic(String, PathBuf),
    #[error("could not instrument the test module")]
    Instrument(#[source] crate::InstrumentError),
    #[error("could not write out the instrumented test module to a temporary file at {1:?}")]
    WriteTempTest(#[source] std::io::Error, PathBuf),
    #[error("interpreter (= {0}) and analysis (= {1}) disagree on gas consumption")]
    GasMismatch(i64, i64),
    #[error("interpreter analysis ({0} !< {1} !< {2}) disagree on aggregate gas consumption")]
    AggregateGasMismatch(i64, i64, i64),
    #[error("processing the interpreter output panicked")]
    InterpreterOutputProcessingPanic,
}

impl Error {
    pub(crate) fn set_path(&mut self, path: &Path) {
        match self {
            Error::ReadTestContents(_, _)
            | Error::FromUtf8(_, _)
            | Error::OpenSnap(_, _)
            | Error::SeekSnap(_, _)
            | Error::TruncateSnap(_, _)
            | Error::ReadSnap(_, _)
            | Error::WriteSnap(_, _)
            | Error::InterpreterLaunch(_, _)
            | Error::InterpreterWait(_)
            | Error::InterpreterExit(_, _, _)
            | Error::InterpreterOutput(_)
            | Error::DiffSnap(_)
            | Error::AnalyseModule(_, _, _)
            | Error::AnalyseModulePanic(_, _)
            | Error::InstrumentModulePanic(_, _)
            | Error::Instrument(_)
            | Error::GasMismatch(_, _)
            | Error::AggregateGasMismatch(_, _, _)
            | Error::InterpreterOutputProcessingPanic
            | Error::WriteTempTest(_, _) => {}
            Error::ParseBuffer(s) => set_wast_path(s, path),
            Error::ParseWast(s) => set_wast_path(s, path),
            Error::EncodeModule(s, _) => set_wast_path(s, path),
        }
    }
}

fn set_wast_path(error: &mut wast::Error, path: &Path) {
    error.set_path(path);
    if let Ok(content) = std::fs::read_to_string(path) {
        error.set_text(&content);
    }
}

pub(crate) fn read(storage: &mut String, path: &Path) -> Result<(), Error> {
    let test_contents = std::fs::read(path).map_err(|e| Error::ReadTestContents(e, path.into()))?;
    let test_string =
        std::str::from_utf8(&test_contents).map_err(|e| Error::FromUtf8(e, path.into()))?;
    storage.push_str(test_string);
    Ok(())
}

#[derive(Debug, PartialEq)]
enum Status {
    None,
    Passed,
    Failed,
}

#[derive(Debug)]
pub struct TestContext {
    test_name: String,
    test_path: PathBuf,
    snap_base: PathBuf,
    tmp_base: PathBuf,
    pub output: Vec<u8>,
    status: Status,
    analysis_duration: Duration,
    interpreter_duration: Duration,
    check_snapshot: bool,
}

impl<'a> TestContext {
    pub fn new(
        test_name: String,
        test_path: PathBuf,
        snap_base: PathBuf,
        tmp_base: PathBuf,
        check_snapshot: bool,
    ) -> Self {
        Self {
            test_name,
            test_path,
            snap_base,
            tmp_base,
            output: vec![],
            status: Status::None,
            analysis_duration: Duration::default(),
            interpreter_duration: Duration::default(),
            check_snapshot,
        }
    }

    pub fn failed(&self) -> bool {
        self.status == Status::Failed
    }

    fn output_time(&mut self) {
        self.output.extend_from_slice(
            format!(
                " (analysis: {:?}; interpreter: {:?})",
                self.analysis_duration, self.interpreter_duration
            )
            .as_bytes(),
        );
    }

    fn pass(&mut self) {
        assert!(self.status == Status::None);
        self.status = Status::Passed;
        self.output.extend_from_slice(b"[PASS] ");
        self.output.extend_from_slice(self.test_name.as_bytes());
        self.output_time();
        self.output.extend_from_slice(b"\n");
    }

    fn fail(&mut self, error: impl error::Error) {
        assert!([Status::None, Status::Failed].contains(&self.status));
        if let Status::None = self.status {
            self.output.extend_from_slice(b"[FAIL] ");
            self.output.extend_from_slice(self.test_name.as_bytes());
            self.output_time();
            self.output.extend_from_slice(b"\n");
            self.status = Status::Failed;
        }
        write_error(&mut self.output, error).expect("this should be infallible");
    }

    fn fail_test_error(&mut self, error: &mut Error) {
        error.set_path(&self.test_path);
        self.fail(&*error)
    }

    pub fn run(&mut self) {
        // There are some interesting properties of the interpreter that we want to verify, but
        // those properties are hard/infeasible to validate with our wast test runner
        // infrastructure.
        //
        // Instead we implement an escape hatch via the test name and allow the test suite to run
        // arbitrary rust code within the test context.
        if "!internal-self-test-interpreter" == self.test_name {
            if let Err(mut e) = self.self_test_interpreter() {
                self.fail_test_error(&mut e);
            } else {
                self.pass();
            }
            return;
        }

        let instrumented_wast = match self.run_analysis() {
            Ok(instrumented_wast) => instrumented_wast,
            Err(mut e) => return self.fail_test_error(&mut e),
        };

        if self.check_snapshot {
            if let Err(mut e) = self.compare_snapshot(&instrumented_wast, "instrumented") {
                return self.fail_test_error(&mut e);
            }
        }

        // Run the interpreter here with the wast file in some sort of a tracing mode (needs to
        // be implemented inside the interpreter).
        //
        // The output is probably going to be extremely verbose, but hey, it doesn’t result in
        // excessive effort at least, does it?
        let output = match self.exec_interpreter(instrumented_wast) {
            Ok(o) => o,
            Err(mut e) => return self.fail_test_error(&mut e),
        };
        if let Err(mut e) = self.validate_interpreter_output(&output) {
            return self.fail_test_error(&mut e);
        }
        self.pass();
    }

    fn validate_interpreter_output(&mut self, interpreter_out: &str) -> Result<(), Error> {
        let (interpreter_gas, instrumentation_gas) =
            std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                self.process_interpreter_output(interpreter_out)
            }))
            .map_err(|_| Error::InterpreterOutputProcessingPanic)??;
        if interpreter_gas != instrumentation_gas {
            return Err(Error::GasMismatch(interpreter_gas, instrumentation_gas));
        }
        Ok(())
    }

    fn process_interpreter_output(&mut self, interpreter_out: &str) -> Result<(i64, i64), Error> {
        fn parse_prefix_num(s: &[u8]) -> Option<i64> {
            match <i64 as atoi::FromRadix10Signed>::from_radix_10_signed(s) {
                (_, 0) => None,
                (n, _) => Some(n),
            }
        }

        let mut interpreter_gas = 0;
        let mut instrumentation_gas = 0;
        let mut instrumentation_aggregate_gas = 0;
        let mut total_stack_used = 0;
        let mut max_stack_used = 0;
        for line in interpreter_out.lines() {
            let Some((kind, rest)) = line.split_once(": ") else {
                continue;
            };
            match kind {
                // When a trap occurs, we want to verify that the gas falls somewhere within the
                // budget of the last `aggregate_charge`.
                "aggregate_trap" => {
                    let max_allowed_gas = instrumentation_gas + instrumentation_aggregate_gas;
                    if interpreter_gas < instrumentation_gas || interpreter_gas > max_allowed_gas {
                        return Err(Error::AggregateGasMismatch(
                            instrumentation_gas,
                            interpreter_gas,
                            max_allowed_gas,
                        ));
                    } else {
                        interpreter_gas = 0;
                        instrumentation_aggregate_gas = 0;
                        instrumentation_gas = 0;
                        total_stack_used = 0;
                    }
                }
                "gas" => {
                    let count = parse_prefix_num(rest.as_bytes()).expect("TODO");
                    interpreter_gas += count;
                }
                "charge_aggregate" => {
                    instrumentation_gas += std::mem::replace(&mut instrumentation_aggregate_gas, 0);
                    let count = parse_prefix_num(rest.as_bytes()).expect("TODO");
                    instrumentation_aggregate_gas = count;
                }
                "charge_gas" => {
                    instrumentation_gas += std::mem::replace(&mut instrumentation_aggregate_gas, 0);
                    let count = parse_prefix_num(rest.as_bytes()).expect("TODO");
                    instrumentation_gas += count;
                }
                "reserve_stack" => {
                    let Some((l, r)) = rest.split_once(" ") else {
                        continue;
                    };
                    let ops_size = parse_prefix_num(l.as_bytes()).expect("TODO");
                    let frame_size = parse_prefix_num(r.as_bytes()).expect("TODO");
                    // The reference interpreter charges 1 gas for each local and argument.
                    instrumentation_gas += frame_size;
                    total_stack_used += ops_size + frame_size;
                    max_stack_used = std::cmp::max(total_stack_used, max_stack_used);
                }
                "return_stack" => {
                    let Some((l, r)) = rest.split_once(" ") else {
                        continue;
                    };
                    let ops_size = parse_prefix_num(l.as_bytes()).expect("TODO");
                    let frame_size = parse_prefix_num(r.as_bytes()).expect("TODO");
                    total_stack_used -= ops_size + frame_size;
                }
                _ => {
                    continue;
                }
            }
        }
        instrumentation_gas += std::mem::replace(&mut instrumentation_aggregate_gas, 0);
        Ok((interpreter_gas, instrumentation_gas))
    }

    fn run_analysis(&mut self) -> Result<String, Error> {
        let mut test_contents = String::new();
        read(&mut test_contents, &self.test_path)?;
        let mut lexer = wast::lexer::Lexer::new(&test_contents);
        lexer.allow_confusing_unicode(true);
        let buf = wast::parser::ParseBuffer::new_with_lexer(lexer).map_err(Error::ParseBuffer)?;
        let wast: wast::Wast = wast::parser::parse(&buf).map_err(Error::ParseWast)?;
        let mut output_wast = String::new();
        let mut iter = wast.directives.into_iter().enumerate().peekable();
        while let Some((directive_index, directive)) = iter.next() {
            // saturating_sub here grabs the preceding parenthesis if any.
            let start_offset = directive.span().offset().saturating_sub(1);
            let end_offset = iter
                .peek()
                // and this one ensures that we don't include the initial parenthesis of the next
                // directive.
                .map(|(_, d)| d.span().offset().saturating_sub(1))
                .unwrap_or(test_contents.len());
            match directive {
                wast::WastDirective::Module(wast::QuoteWat::Wat(wast::Wat::Module(mut module))) => {
                    let id = module.id.map_or_else(
                        || format!("[directive {directive_index}]"),
                        |id| format!("{id:?}"),
                    );
                    let module = module
                        .encode()
                        .map_err(|e| Error::EncodeModule(e, id.clone()))?;
                    let instrumented = self.instrument_module(&id, &module)?;
                    let mut printer = ModulePrint(&mut output_wast);
                    wasmprinter::Config::new()
                        .print(&instrumented, &mut printer)
                        .expect("print");
                    output_wast.push_str("\n");
                }
                wast::WastDirective::Module(wast::QuoteWat::QuoteModule(_, _)) => {
                    unreachable!("doesn’t actually occur in our test suite");
                }
                wast::WastDirective::Module(wast::QuoteWat::Wat(wast::Wat::Component(_))) => {
                    // These are difficult and I would rather skip them for now...
                    continue;
                }
                wast::WastDirective::Module(wast::QuoteWat::QuoteComponent(_, _)) => {
                    // Same
                    continue;
                }
                wast::WastDirective::ModuleDefinition(_) => {
                    unreachable!("doesn’t actually occur in our test suite");
                }
                wast::WastDirective::ModuleInstance { .. } => {
                    unreachable!("doesn’t actually occur in our test suite");
                }

                // Ignore the “operations”, we only care about module analysis results.
                wast::WastDirective::Register { .. } => {
                    output_wast.push_str(&test_contents[start_offset..end_offset]);
                    continue;
                }
                wast::WastDirective::Invoke(i) => {
                    if self.check_snapshot {
                        output_wast.push_str(&test_contents[start_offset..end_offset]);
                    } else {
                        // When there’s no snapshot (ie. when fuzzing) we reuse
                        // the invoke instruction to pass the wast crate’s parsing
                        // while actually ignoring traps.
                        // The saturating_add(7) removes the "invoke " string added by
                        // the fuzzer. Grep XREF:INVOKE-FOR-JUST-RUN for more details.
                        output_wast.push_str("\n(just_run ");
                        output_wast.push_str(
                            &test_contents[i.span.offset().saturating_add(7)..end_offset],
                        );
                    }
                    continue;
                }
                wast::WastDirective::AssertTrap { exec, message, .. } => {
                    match exec {
                        wast::WastExecute::Invoke(_) => {
                            output_wast.push_str(&test_contents[start_offset..end_offset]);
                        }
                        wast::WastExecute::Wat(wast::Wat::Module(mut module)) => {
                            let id = module.id.map_or_else(
                                || format!("[directive {directive_index}]"),
                                |id| format!("{id:?}"),
                            );
                            let module = module
                                .encode()
                                .map_err(|e| Error::EncodeModule(e, id.clone()))?;
                            let instrumented = self.instrument_module(&id, &module)?;
                            output_wast.push_str("\n(assert_trap ");
                            let mut printer = ModulePrint(&mut output_wast);
                            wasmprinter::Config::new()
                                .print(&instrumented, &mut printer)
                                .expect("print");
                            output_wast.push_str(" \"");
                            output_wast.push_str(message);
                            output_wast.push_str("\")\n");
                        }
                        wast::WastExecute::Wat(wast::Wat::Component(_)) => {
                            unreachable!("components are not supported");
                        }
                        wast::WastExecute::Get { .. } => {
                            output_wast.push_str(&test_contents[start_offset..end_offset]);
                        }
                    }
                    continue;
                }
                wast::WastDirective::AssertReturn { .. } => {
                    output_wast.push_str(&test_contents[start_offset..end_offset]);
                    continue;
                }
                wast::WastDirective::AssertExhaustion { .. } => continue,
                wast::WastDirective::AssertException { .. } => continue,
                // Do not attempt to process invalid modules.
                wast::WastDirective::AssertMalformed { .. } => continue,
                wast::WastDirective::AssertInvalid { .. } => continue,
                wast::WastDirective::AssertUnlinkable { .. } => continue,
                wast::WastDirective::AssertSuspension { .. } => {
                    unreachable!("doesn’t actually occur in our test suite");
                }
                wast::WastDirective::Thread(_) => {
                    unreachable!("doesn’t actually occur in our test suite");
                }
                wast::WastDirective::Wait { .. } => {
                    unreachable!("doesn’t actually occur in our test suite");
                }
            };
        }
        Ok(output_wast)
    }

    fn instrument_module(&mut self, id: &str, code: &[u8]) -> Result<Vec<u8>, Error> {
        let start = std::time::Instant::now();
        let results = std::panic::catch_unwind(|| {
            Analysis::new()
                .with_stack(DefaultStackConfig)
                .with_gas(DefaultGasConfig)
                .analyze(code)
        })
        .map_err(|_| Error::AnalyseModulePanic(id.into(), self.test_path.clone()))?
        .map_err(|e| Error::AnalyseModule(e, id.into(), self.test_path.clone()))?;
        self.analysis_duration += start.elapsed();

        std::panic::catch_unwind(|| results.instrument("spectest", code))
            .map_err(|_| Error::InstrumentModulePanic(id.into(), self.test_path.clone()))?
            .map_err(Error::Instrument)
    }

    fn self_test_interpreter(&mut self) -> Result<(), Error> {
        // Validate that the gas and stack intrinsics are considered by the interpreter to be free
        // and charge the expected amount of gas.
        let charge_for_stack = 10;
        let charge_for_gas = 100;
        let module = format!(
            r#"
            (module
              (type $gas_ty (func (param i64)))
              (type $stack_ty (func (param i64 i64)))
              (import "spectest" "finite_wasm_gas" (func $finite_wasm_gas (type $gas_ty)))
              (import "spectest" "finite_wasm_stack" (func $finite_wasm_stack (type $stack_ty)))
              (import "spectest" "finite_wasm_unstack" (func $finite_wasm_unstack (type $stack_ty)))
              (func $main (export "main")
                block
                    (call $finite_wasm_stack (i64.const 1) (i64.const {charge_for_stack}))
                    (call $finite_wasm_gas (i64.const {charge_for_gas}))
                end
                (call $finite_wasm_unstack (i64.const 1) (i64.const {charge_for_stack}))
              )
            )
            (assert_return (invoke "main"))
            "#
        );
        let output = self.exec_interpreter(module.into())?;
        let (interpreter_gas, instrumentation_gas) = self.process_interpreter_output(&output)?;
        let expected_instrumentation_gas = charge_for_stack + charge_for_gas;
        if interpreter_gas != 0 || instrumentation_gas != expected_instrumentation_gas {
            return Err(Error::GasMismatch(interpreter_gas, instrumentation_gas));
        }
        Ok(())
    }

    fn exec_interpreter(&mut self, code: String) -> Result<String, Error> {
        let mut test_path = self.tmp_base.join(&self.test_name);
        if test_path.extension().is_none() {
            test_path.set_extension("wast");
        }
        let _ = std::fs::create_dir_all(&test_path.parent().unwrap());
        std::fs::write(&test_path, code).map_err(|e| Error::WriteTempTest(e, test_path.clone()))?;

        let mut args = vec!["-tg".into(), "-i".into(), test_path.into()];
        if !self.check_snapshot {
            // Limit gas pool size, to avoid infinite loops
            args.push("-g".into());
            args.push("1000".into());
        }
        static INTERPRETER_BYTES: &[u8] = include_bytes!("../../interpreter/wasm");
        extern "C" fn delete_directory() {
            // lazy_static tempdirs aren’t actually deleted at the end of the program
            let _ = INTERPRETER
                .0
                .lock()
                .map(|mut o| o.take().map(|d| d.close()));
        }
        lazy_static::lazy_static! {
            static ref INTERPRETER: (std::sync::Mutex<Option<tempfile::TempDir>>, std::path::PathBuf) = {
                let d = tempfile::Builder::new()
                    .prefix("finite-wasm-spec-interpreter.")
                    .tempdir()
                    .expect("failed creating spec interpreter binary directory");
                let path = d.path().join("wasm");
                let mut f = std::fs::File::create(&path)
                    .expect("failed creating spec interpreter binary");
                f.write_all(INTERPRETER_BYTES)
                    .expect("failed writing spec interpreter binary");
                f.set_permissions(std::os::unix::fs::PermissionsExt::from_mode(0o700))
                    .expect("failed making spec interpreter binary executable");
                unsafe { libc::atexit(delete_directory) };
                (std::sync::Mutex::new(Some(d)), path)
            };
        }
        let start = std::time::Instant::now();
        let process = std::process::Command::new(&INTERPRETER.1)
            .stdin(std::process::Stdio::null())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .args(&args)
            .spawn()
            .map_err(|e| Error::InterpreterLaunch(e, args.clone()))?;
        let output = process
            .wait_with_output()
            .map_err(|e| Error::InterpreterWait(e))?;
        self.interpreter_duration += start.elapsed();
        let stdoutput = String::from_utf8(output.stdout).map_err(Error::InterpreterOutput)?;
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            if !self.check_snapshot
                // Likely infinite recursion
                && (stderr.contains("call stack exhausted")
                    // Likely infinite looping, or somesuch case.
                    || stderr.contains("gas pool is empty")
                    // Likely module instantiation failed due to a runtime error of some sort.
                    || !stdoutput.contains("charge_gas"))
            {
                return Ok(String::new());
            }
            return Err(Error::InterpreterExit(stderr.into(), output.status, args));
        }
        Ok(stdoutput)
    }

    fn compare_snapshot(&mut self, directive_output: &str, index: &str) -> Result<(), Error> {
        let should_update = std::env::var_os("SNAP_UPDATE").is_some();
        let snap_filename = format!("{}@{}.snap", self.test_name, index);
        let snap_path = self.snap_base.join(snap_filename);
        if let Some(parent) = snap_path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }
        let mut snap_file = std::fs::OpenOptions::new()
            .create(true)
            .read(true)
            .write(true)
            .open(&snap_path)
            .map_err(|e| Error::OpenSnap(e, snap_path.clone()))?;
        let mut snap_contents = String::with_capacity(1024);
        snap_file
            .read_to_string(&mut snap_contents)
            .map_err(|e| Error::ReadSnap(e, snap_path.clone()))?;
        if !should_update {
            match DiffError::diff(&snap_contents, &directive_output) {
                None => Ok(()),
                Some(error) => {
                    self.output.extend_from_slice(
                        "note: run with SNAP_UPDATE environment variable to update\n".as_bytes(),
                    );
                    Err(Error::DiffSnap(error.with_path(snap_path)))
                }
            }
        } else {
            snap_file
                .set_len(0)
                .map_err(|e| Error::TruncateSnap(e, snap_path.clone()))?;
            // TRICKY: If we don’t seek, the file will be filled with 0-bytes up to the
            // current position (we read the file’s contents just now!) before the data is
            // written...
            snap_file
                .seek(SeekFrom::Start(0))
                .map_err(|e| Error::SeekSnap(e, snap_path.clone()))?;
            snap_file
                .write_all(directive_output.as_bytes())
                .map_err(|e| Error::WriteSnap(e, snap_path))?;
            Ok(())
        }
    }
}

struct ModulePrint<'a>(&'a mut String);
impl<'a> wasmprinter::Print for ModulePrint<'a> {
    fn write_str(&mut self, s: &str) -> io::Result<()> {
        Ok(self.0.push_str(s))
    }
    fn print_custom_section(&mut self, _n: &str, _bo: usize, _d: &[u8]) -> io::Result<bool> {
        Ok(true)
    }
}

struct DefaultStackConfig;
impl max_stack::SizeConfig for DefaultStackConfig {
    fn size_of_value(&self, ty: wasmparser::ValType) -> u8 {
        use wasmparser::ValType::*;
        match ty {
            I32 => 4,
            I64 => 8,
            F32 => 4,
            F64 => 8,
            V128 => 16,
            Ref(_) => 32,
        }
    }

    fn size_of_function_activation(
        &self,
        locals: &prefix_sum_vec::PrefixSumVec<wasmparser::ValType, u32>,
    ) -> u64 {
        u64::from(locals.max_index().map(|&v| v + 1).unwrap_or(0))
    }
}

pub(crate) struct DefaultGasConfig;

macro_rules! gas_visit {
    (visit_end => $({ $($arg:ident: $argty:ty),* })?) => {};
    (visit_else => $({ $($arg:ident: $argty:ty),* })?) => {};
    (visit_memory_copy => $({ $($arg:ident: $argty:ty),* })?) => {};
    (visit_memory_fill => $({ $($arg:ident: $argty:ty),* })?) => {};
    (visit_memory_init => $({ $($arg:ident: $argty:ty),* })?) => {};
    (visit_table_copy => $({ $($arg:ident: $argty:ty),* })?) => {};
    (visit_table_fill => $({ $($arg:ident: $argty:ty),* })?) => {};
    (visit_table_init => $({ $($arg:ident: $argty:ty),* })?) => {};
    ($visit:ident => $({ $($arg:ident: $argty:ty),* })?) => {
        fn $visit(&mut self $($(,$arg: $argty)*)?) -> Self::Output {
            Fee::constant(1)
        }
    };

    ($( @$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*))*) => {
        $(gas_visit!{ $visit => $({ $($arg: $argty),* })? })*
    }
}

impl<'a> wasmparser::VisitOperator<'a> for DefaultGasConfig {
    type Output = Fee;
    fn visit_end(&mut self) -> Fee {
        Fee::ZERO
    }
    fn visit_else(&mut self) -> Fee {
        Fee::ZERO
    }
    fn visit_memory_copy(&mut self, _: u32, _: u32) -> Fee {
        Fee {
            constant: 1,
            linear: 1,
        }
    }
    fn visit_memory_fill(&mut self, _: u32) -> Fee {
        Fee {
            constant: 1,
            linear: 1,
        }
    }
    fn visit_memory_init(&mut self, _: u32, _: u32) -> Fee {
        Fee {
            constant: 1,
            linear: 1,
        }
    }
    fn visit_table_copy(&mut self, _: u32, _: u32) -> Fee {
        Fee {
            constant: 1,
            linear: 1,
        }
    }
    fn visit_table_fill(&mut self, _: u32) -> Fee {
        Fee {
            constant: 1,
            linear: 1,
        }
    }
    fn visit_table_init(&mut self, _: u32, _: u32) -> Fee {
        Fee {
            constant: 1,
            linear: 1,
        }
    }

    wasmparser::for_each_visit_operator!(gas_visit);

    fn simd_visitor(
        &mut self,
    ) -> Option<&mut dyn wasmparser::VisitSimdOperator<'a, Output = Self::Output>> {
        Some(self)
    }
}

impl<'a> wasmparser::VisitSimdOperator<'a> for DefaultGasConfig {
    wasmparser::for_each_visit_simd_operator!(gas_visit);
}