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
//! Module for parsing [WebAssembly script format] \(a.k.a. wast).
//!
//! These scripts might be useful to integrate the official spec [testsuite] into implementations
//! of the wasm execution engines (such as [wasmi]) and for developing fine-grained tests of
//! runtimes or/and if it isn't desired to use full fledged compilers.
//!
//! # Example
//!
//! ```rust
//! use wabt::script::{ScriptParser, Command, CommandKind, Action, Value};
//! # use wabt::script::Error;
//!
//! # fn try_main() -> Result<(), Error> {
//! let wast = r#"
//! ;; Define anonymous module with function export named `sub`.
//! (module
//!   (func (export "sub") (param $x i32) (param $y i32) (result i32)
//!     ;; return x - y;
//!     (i32.sub
//!       (get_local $x) (get_local $y)
//!     )
//!   )
//! )
//!
//! ;; Assert that invoking export `sub` with parameters (8, 3)
//! ;; should return 5.
//! (assert_return
//!   (invoke "sub"
//!     (i32.const 8) (i32.const 3)
//!   )
//!   (i32.const 5)
//! )
//! "#;
//!
//! let mut parser = ScriptParser::<f32, f64>::from_str(wast)?;
//! while let Some(Command { kind, .. }) = parser.next()? {
//!     match kind {
//!         CommandKind::Module { module, name } => {
//!             // The module is declared as annonymous.
//!             assert_eq!(name, None);
//!
//!             // Convert the module into the binary representation and check the magic number.
//!             let module_binary = module.into_vec();
//!             assert_eq!(&module_binary[0..4], &[0, 97, 115, 109]);
//!         }
//!         CommandKind::AssertReturn { action, expected } => {
//!             assert_eq!(action, Action::Invoke {
//!                 module: None,
//!                 field: "sub".to_string(),
//!                 args: vec![
//!                     Value::I32(8),
//!                     Value::I32(3)
//!                 ],
//!             });
//!             assert_eq!(expected, vec![Value::I32(5)]);
//!         },
//!         _ => panic!("there are no other commands apart from that defined above"),
//!     }
//! }
//! # Ok(())
//! # }
//! #
//! # fn main() {
//! #     try_main().unwrap();
//! # }
//! ```
//! [WebAssembly script format]: https://github.com/WebAssembly/spec/blob/a25083ac7076b05e3f304ec9e093ef1b1ee09422/interpreter/README.md#scripts
//! [testsuite]: https://github.com/WebAssembly/testsuite
//! [wasmi]: https://github.com/pepyakin/wasmi

use std::collections::HashMap;
use std::error;
use std::ffi::CString;
use std::fmt;
use std::io;
use std::str;
use std::vec;

use serde_json;

use super::{Error as WabtError, Features, Script, WabtBuf, WabtWriteScriptResult};

mod json;

/// Error that can happen when parsing spec.
#[derive(Debug)]
pub enum Error {
    /// IO error happened during parsing or preparing to parse.
    IoError(io::Error),
    /// WABT reported an error while converting wast to json.
    WabtError(WabtError),
    /// Other error represented by String.
    Other(String),
    /// Not a different kind of an error but just a wrapper for a error
    /// which we have a line number information.
    WithLineInfo {
        /// Line number of the script on which just error happen.
        line: u64,
        /// Box with actual error.
        error: Box<Error>,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::IoError(ref io_err) => write!(f, "IO error: {}", io_err),
            Error::WabtError(ref wabt_err) => write!(f, "wabt error: {:?}", wabt_err),
            Error::Other(ref message) => write!(f, "{}", message),
            Error::WithLineInfo { line, ref error } => write!(f, "At line {}: {}", line, error),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::IoError(ref e) => e.description(),
            Error::WabtError(_) => "wabt error",
            Error::Other(ref msg) => &msg,
            Error::WithLineInfo { ref error, .. } => error.description(),
        }
    }

    fn cause(&self) -> Option<&dyn error::Error> {
        match *self {
            Error::IoError(ref io_err) => Some(io_err),
            Error::WabtError(ref wabt_err) => Some(wabt_err),
            Error::Other(_) => None,
            Error::WithLineInfo { ref error, .. } => Some(error),
        }
    }
}

impl From<WabtError> for Error {
    fn from(e: WabtError) -> Error {
        Error::WabtError(e)
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Error {
        Error::IoError(e)
    }
}

/// Bitwise conversion from T
pub trait FromBits<T> {
    /// Convert `other` to `Self`, preserving bitwise representation
    fn from_bits(other: T) -> Self;
}

impl<T> FromBits<T> for T {
    fn from_bits(other: T) -> Self {
        other
    }
}

impl FromBits<u32> for f32 {
    fn from_bits(other: u32) -> Self {
        Self::from_bits(other)
    }
}

impl FromBits<u64> for f64 {
    fn from_bits(other: u64) -> Self {
        Self::from_bits(other)
    }
}

/// Wasm value
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
pub enum Value<F32 = f32, F64 = f64> {
    /// 32-bit signed or unsigned integer.
    I32(i32),
    /// 64-bit signed or unsigned integer.
    I64(i64),
    /// 32-bit floating point number.
    F32(F32),
    /// 64-bit floating point number.
    F64(F64),
    /// 128-bit vector.
    V128(u128),
}

impl<F32: FromBits<u32>, F64: FromBits<u64>> Value<F32, F64> {
    fn decode_f32(val: u32) -> Self {
        Value::F32(F32::from_bits(val))
    }

    fn decode_f64(val: u64) -> Self {
        Value::F64(F64::from_bits(val))
    }
}

/// Description of action that should be performed on a wasm module.
#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub enum Action<F32 = f32, F64 = f64> {
    /// Invoke a specified function.
    Invoke {
        /// Name of the module. If `None`, last defined module should be
        /// used.
        module: Option<String>,
        /// Field name on which action should be performed.
        field: String,
        /// Arguments that should be passed to the invoked function.
        args: Vec<Value<F32, F64>>,
    },
    /// Read the specified global variable.
    Get {
        /// Name of the module. If `None`, last defined module should be
        /// used.
        module: Option<String>,
        /// Field name on which action should be performed.
        field: String,
    },
}

fn parse_value<F32: FromBits<u32>, F64: FromBits<u64>>(
    test_val: &json::RuntimeValue,
) -> Result<Value<F32, F64>, Error> {
    fn parse_val<P: str::FromStr>(str_val: &str, str_ty: &str) -> Result<P, Error> {
        str_val
            .parse()
            .map_err(|_| Error::Other(format!("can't parse '{}' as '{}'", str_val, str_ty)))
    }
    let value = match test_val.value_type.as_ref() {
        "i32" => {
            let unsigned: u32 = parse_val(&test_val.value, &test_val.value_type)?;
            Value::I32(unsigned as i32)
        }
        "i64" => {
            let unsigned: u64 = parse_val(&test_val.value, &test_val.value_type)?;
            Value::I64(unsigned as i64)
        }
        "f32" => {
            let unsigned: u32 = parse_val(&test_val.value, &test_val.value_type)?;
            Value::decode_f32(unsigned)
        }
        "f64" => {
            let unsigned: u64 = parse_val(&test_val.value, &test_val.value_type)?;
            Value::decode_f64(unsigned)
        }
        "v128" => {
            let unsigned: u128 = parse_val(&test_val.value, &test_val.value_type)?;
            Value::V128(unsigned)
        }
        other_ty => {
            return Err(Error::Other(format!("Unknown type '{}'", other_ty)));
        }
    };
    Ok(value)
}

fn parse_value_list<F32: FromBits<u32>, F64: FromBits<u64>>(
    test_vals: &[json::RuntimeValue],
) -> Result<Vec<Value<F32, F64>>, Error> {
    test_vals.iter().map(parse_value).collect()
}

fn parse_action<F32: FromBits<u32>, F64: FromBits<u64>>(
    test_action: &json::Action,
) -> Result<Action<F32, F64>, Error> {
    let action = match *test_action {
        json::Action::Invoke {
            ref module,
            ref field,
            ref args,
        } => Action::Invoke {
            module: module.to_owned(),
            field: field.to_owned(),
            args: parse_value_list(args)?,
        },
        json::Action::Get {
            ref module,
            ref field,
        } => Action::Get {
            module: module.to_owned(),
            field: field.to_owned(),
        },
    };
    Ok(action)
}

fn wast2json(
    source: &[u8],
    test_filename: &str,
    features: Features,
) -> Result<WabtWriteScriptResult, Error> {
    let script = Script::parse(test_filename, source, features.clone())?;
    script.resolve_names()?;
    script.validate()?;
    let result = script.write_binaries(test_filename)?;
    Ok(result)
}

/// This is a handle to get the binary representation of the module.
#[derive(Clone, Debug)]
pub struct ModuleBinary {
    module: Vec<u8>,
}

impl Eq for ModuleBinary {}
impl PartialEq for ModuleBinary {
    fn eq(&self, rhs: &Self) -> bool {
        self.module == rhs.module
    }
}

impl ModuleBinary {
    fn from_vec(module: Vec<u8>) -> ModuleBinary {
        ModuleBinary { module }
    }

    /// Convert this object into wasm module binary representation.
    pub fn into_vec(self) -> Vec<u8> {
        self.module
    }
}

/// Script's command.
#[derive(Clone, Debug, PartialEq)]
pub enum CommandKind<F32 = f32, F64 = f64> {
    /// Define, validate and instantiate a module.
    Module {
        /// Wasm module binary to define, validate and instantiate.
        module: ModuleBinary,

        /// If the `name` is specified, the module should be registered under this name.
        name: Option<String>,
    },
    /// Assert that specified action should yield specified results.
    AssertReturn {
        /// Action to perform.
        action: Action<F32, F64>,
        /// Values that expected to be yielded by the action.
        expected: Vec<Value<F32, F64>>,
    },
    /// Assert that specified action should yield NaN in canonical form.
    AssertReturnCanonicalNan {
        /// Action to perform.
        action: Action<F32, F64>,
    },
    /// Assert that specified action should yield NaN with 1 in MSB of fraction field.
    AssertReturnArithmeticNan {
        /// Action to perform.
        action: Action<F32, F64>,
    },
    /// Assert that performing specified action must yield in a trap.
    AssertTrap {
        /// Action to perform.
        action: Action<F32, F64>,
        /// Expected failure should be with this message.
        message: String,
    },
    /// Assert that specified module is invalid.
    AssertInvalid {
        /// Module that should be invalid.
        module: ModuleBinary,
        /// Expected failure should be with this message.
        message: String,
    },
    /// Assert that specified module cannot be decoded.
    AssertMalformed {
        /// Module that should be malformed.
        module: ModuleBinary,
        /// Expected failure should be with this message.
        message: String,
    },
    /// Assert that specified module is uninstantiable.
    AssertUninstantiable {
        /// Module that should be uninstantiable.
        module: ModuleBinary,
        /// Expected failure should be with this message.
        message: String,
    },
    /// Assert that specified action should yield in resource exhaustion.
    AssertExhaustion {
        /// Action to perform.
        action: Action<F32, F64>,
        /// Expected failure should be with this message.
        message: String,
    },
    /// Assert that specified module fails to link.
    AssertUnlinkable {
        /// Module that should be unlinkable.
        module: ModuleBinary,
        /// Expected failure should be with this message.
        message: String,
    },
    /// Register a module under specified name (`as_name`).
    Register {
        /// Name of the module, which should be registered under different name.
        ///
        /// If `None` then the last defined [module][`Module`] should be used.
        ///
        /// [`Module`]: #variant.Module
        name: Option<String>,
        /// New name of the specified module.
        as_name: String,
    },
    /// Perform the specified [action].
    ///
    /// [action]: enum.Action.html
    PerformAction(Action<F32, F64>),
}

/// Command in the script.
///
/// It consists of line number and [`CommandKind`].
///
/// [`CommandKind`]: enum.CommandKind.html
#[derive(Clone, Debug, PartialEq)]
pub struct Command<F32 = f32, F64 = f64> {
    /// Line number the command is defined on.
    pub line: u64,

    /// Kind of the command.
    pub kind: CommandKind<F32, F64>,
}

/// Parser which allows to parse WebAssembly script text format.
pub struct ScriptParser<F32 = f32, F64 = f64> {
    cmd_iter: vec::IntoIter<json::Command>,
    modules: HashMap<CString, WabtBuf>,
    _phantom: ::std::marker::PhantomData<(F32, F64)>,
}

impl<F32: FromBits<u32>, F64: FromBits<u64>> ScriptParser<F32, F64> {
    /// Create `ScriptParser` from the script in specified file.
    ///
    /// The `source` should contain valid wast.
    ///
    /// The `test_filename` must have a `.wast` extension.
    pub fn from_source_and_name(source: &[u8], test_filename: &str) -> Result<Self, Error> {
        ScriptParser::from_source_and_name_with_features(source, test_filename, Features::new())
    }

    /// Create `ScriptParser` from the script in specified file, parsing with
    /// the given features.
    ///
    /// The `source` should contain valid wast.
    ///
    /// The `test_filename` must have a `.wast` extension.
    pub fn from_source_and_name_with_features(
        source: &[u8],
        test_filename: &str,
        features: Features,
    ) -> Result<Self, Error> {
        if !test_filename.ends_with(".wast") {
            return Err(Error::Other(format!(
                "Provided {} should have .wast extension",
                test_filename
            )));
        }

        // Convert wasm script into json spec and binaries. The output artifacts
        // will be placed in result.

        let results = wast2json(source, test_filename, features.clone())?;
        let results = results.take_all().expect("Failed to release");

        let json_str = results.json_output_buffer.as_ref();

        let spec: json::Spec =
            serde_json::from_slice(json_str).expect("Failed to deserialize JSON buffer");

        let json::Spec { commands, .. } = spec;

        Ok(ScriptParser {
            cmd_iter: commands.into_iter(),
            modules: results.module_output_buffers,
            _phantom: Default::default(),
        })
    }

    /// Create `ScriptParser` from the script source.
    pub fn from_str(source: &str) -> Result<Self, Error> {
        ScriptParser::from_source_and_name(source.as_bytes(), "test.wast")
    }

    /// Returns the next [`Command`] from the script.
    ///
    /// Returns `Err` if an error occurred while parsing the script,
    /// or returns `None` if the parser reached end of script.
    ///
    /// [`Command`]: struct.Command.html
    pub fn next(&mut self) -> Result<Option<Command<F32, F64>>, Error> {
        let command = match self.cmd_iter.next() {
            Some(cmd) => cmd,
            None => return Ok(None),
        };

        let get_module = |filename: String, s: &Self| {
            let filename = CString::new(filename).unwrap();
            s.modules
                .get(&filename)
                .map(|module| ModuleBinary::from_vec(module.as_ref().to_owned()))
                .expect("Module referenced in JSON does not exist.")
        };

        let (line, kind) = match command {
            json::Command::Module {
                line,
                name,
                filename,
            } => (
                line,
                CommandKind::Module {
                    module: get_module(filename, self),
                    name,
                },
            ),
            json::Command::AssertReturn {
                line,
                action,
                expected,
            } => (
                line,
                CommandKind::AssertReturn {
                    action: parse_action(&action)?,
                    expected: parse_value_list(&expected)?,
                },
            ),
            json::Command::AssertReturnCanonicalNan { line, action } => (
                line,
                CommandKind::AssertReturnCanonicalNan {
                    action: parse_action(&action)?,
                },
            ),
            json::Command::AssertReturnArithmeticNan { line, action } => (
                line,
                CommandKind::AssertReturnArithmeticNan {
                    action: parse_action(&action)?,
                },
            ),
            json::Command::AssertExhaustion { line, action, text } => (
                line,
                CommandKind::AssertExhaustion {
                    action: parse_action(&action)?,
                    message: text,
                },
            ),
            json::Command::AssertTrap { line, action, text } => (
                line,
                CommandKind::AssertTrap {
                    action: parse_action(&action)?,
                    message: text,
                },
            ),
            json::Command::AssertInvalid {
                line,
                filename,
                text,
            } => (
                line,
                CommandKind::AssertInvalid {
                    module: get_module(filename, self),
                    message: text,
                },
            ),
            json::Command::AssertMalformed {
                line,
                filename,
                text,
            } => (
                line,
                CommandKind::AssertMalformed {
                    module: get_module(filename, self),
                    message: text,
                },
            ),
            json::Command::AssertUnlinkable {
                line,
                filename,
                text,
            } => (
                line,
                CommandKind::AssertUnlinkable {
                    module: get_module(filename, self),
                    message: text,
                },
            ),
            json::Command::AssertUninstantiable {
                line,
                filename,
                text,
            } => (
                line,
                CommandKind::AssertUninstantiable {
                    module: get_module(filename, self),
                    message: text,
                },
            ),
            json::Command::Register {
                line,
                name,
                as_name,
            } => (line, CommandKind::Register { name, as_name }),
            json::Command::Action { line, action } => {
                (line, CommandKind::PerformAction(parse_action(&action)?))
            }
        };

        Ok(Some(Command { line, kind }))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::wat2wasm;

    #[test]
    fn assert_exhaustion() {
        const EXHAUSTION: &str = r#"
(module
  (func (export "foo")
    call 0))

(assert_exhaustion (invoke "foo") "so exhausted")
"#;
        let mut script = ScriptParser::<f32, f64>::from_str(EXHAUSTION).unwrap();
        assert_eq!(
            script.next().unwrap().unwrap(),
            Command {
                line: 2,
                kind: CommandKind::Module {
                    module: ModuleBinary::from_vec(
                        wat2wasm(r#"(func (export "foo") call 0)"#).unwrap()
                    ),
                    name: None,
                },
            }
        );
        assert_eq!(
            script.next().unwrap().unwrap(),
            Command {
                line: 6,
                kind: CommandKind::AssertExhaustion {
                    action: Action::Invoke {
                        module: None,
                        field: "foo".into(),
                        args: vec![],
                    },
                    message: "so exhausted".into(),
                },
            }
        );
    }

    #[test]
    fn utf8_handling() {
        // See https://github.com/pepyakin/wabt-rs/issues/50
        let wast = r#"
            (module
                ;; Test that names are case-sensitive.
                (func (export "a") (result i32) (i32.const 13))
                (func (export "A") (result i32) (i32.const 14))

                ;; Test that UTF-8 BOM code points can appear in identifiers.
                (func (export "") (result i32) (i32.const 15))

                ;; Test that Unicode normalization is not applied. These function names
                ;; contain different codepoints which normalize to the same thing under
                ;; NFC or NFD.
                (func (export "Å") (result i32) (i32.const 16))
                (func (export "Å") (result i32) (i32.const 17))
                (func (export "Å") (result i32) (i32.const 18))
            )

            (assert_return (invoke "a") (i32.const 13))
            (assert_return (invoke "A") (i32.const 14))
            (assert_return (invoke "") (i32.const 15))
            (assert_return (invoke "Å") (i32.const 16))
            (assert_return (invoke "Å") (i32.const 17))
            (assert_return (invoke "Å") (i32.const 18))
        "#;

        let mut parser: ScriptParser = ScriptParser::from_str(wast).unwrap();
        while let Some(Command { .. }) = parser.next().unwrap() {}
    }
}