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
use crate::value::Value;
use std::fmt;
use wain_ast::{Import, ValType};

#[cfg_attr(test, derive(Debug))]
pub enum TrapReason {
    UnknownImport {
        mod_name: String,
        name: String,
        kind: &'static str,
    },
    OutOfLimit {
        max: usize,
        idx: usize,
        kind: &'static str,
    },
    DataSegmentOutOfBuffer {
        segment_end: usize,
        buffer_size: usize,
    },
    ElemSegmentLargerThanTable {
        segment_end: usize,
        table_size: usize,
    },
    ReachUnreachable,
    IdxOutOfTable {
        idx: usize,
        table_size: usize,
    },
    UninitializedElem(usize),
    FuncSignatureMismatch {
        import: Option<(String, String)>,
        expected_params: Box<[ValType]>,
        expected_results: Box<[ValType]>,
        actual_params: Box<[ValType]>,
        actual_results: Box<[ValType]>,
    },
    // 10. https://webassembly.github.io/spec/core/exec/instructions.html#and
    LoadMemoryOutOfRange {
        max: usize,
        addr: usize,
        operation: &'static str,
        ty: &'static str,
    },
    ImportFuncCallFail {
        mod_name: String,
        name: String,
        msg: String,
    },
    WrongInvokeTarget {
        name: String,
        actual: Option<&'static str>,
    },
    InvokeInvalidArgs {
        name: String,
        args: Box<[Value]>,
        arg_types: Vec<ValType>,
    },
    RemZeroDivisor,
    DivByZeroOrOverflow,
}

#[cfg_attr(test, derive(Debug))]
pub struct Trap {
    pub reason: TrapReason,
    pub offset: usize,
}

impl Trap {
    pub(crate) fn unknown_import<'s>(
        import: &Import<'s>,
        kind: &'static str,
        offset: usize,
    ) -> Box<Self> {
        Self::new(
            TrapReason::UnknownImport {
                mod_name: import.mod_name.0.to_string(),
                name: import.name.0.to_string(),
                kind,
            },
            offset,
        )
    }

    pub(crate) fn new(reason: TrapReason, offset: usize) -> Box<Trap> {
        Box::new(Trap { reason, offset })
    }
}

struct JoinWritable<'a, D: fmt::Display>(&'a [D], &'static str);

impl<'a, D: fmt::Display> fmt::Display for JoinWritable<'a, D> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(d) = self.0.first() {
            d.fmt(f)?;
        }
        for d in self.0.iter().skip(1) {
            write!(f, "{}{}", self.1, d)?;
        }
        Ok(())
    }
}

impl fmt::Display for Trap {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use TrapReason::*;
        match &self.reason {
            UnknownImport {
                mod_name,
                name,
                kind,
            } => write!(
                f,
                "unknown module '{}' or unknown {} value '{}' imported from the module",
                mod_name, kind, name,
            )?,
            OutOfLimit { max, idx, kind } => write!(
                f,
                "specified {} index 0x{:x} is out of limit 0x{:x}",
                kind, idx, max,
            )?,
            DataSegmentOutOfBuffer {
                segment_end,
                buffer_size,
            } => write!(
                f,
                "'data' segment ends at address 0x{:x} but memory buffer size is 0x{:x}",
                segment_end, buffer_size,
            )?,
            ElemSegmentLargerThanTable {
                segment_end,
                table_size,
            } => write!(
                f,
                "'elem' segment ends at index {} but table length is {}",
                segment_end, table_size,
            )?,
            ReachUnreachable => f.write_str("reached unreachable code")?,
            IdxOutOfTable { idx, table_size } => write!(
                f,
                "cannot refer function because index {} is out of table size {}",
                idx, table_size
            )?,
            UninitializedElem(idx) => {
                write!(f, "element at index {} in table is uninitialized", idx,)?
            }
            FuncSignatureMismatch {
                import,
                expected_params,
                expected_results,
                actual_params,
                actual_results,
            } => {
                if let Some((mod_name, name)) = import {
                    write!(
                        f,
                        "function signature mismatch in imported function '{}' of module '{}'. ",
                        name, mod_name
                    )?;
                } else {
                    f.write_str("cannot invoke function due to mismatch of function signature. ")?;
                }
                write!(
                    f,
                    "expected '[{}] -> [{}]' but got '[{}] -> [{}]'",
                    JoinWritable(expected_params, " "),
                    JoinWritable(expected_results, " "),
                    JoinWritable(actual_params, " "),
                    JoinWritable(actual_results, " "),
                )?
            }
            LoadMemoryOutOfRange {
                max,
                addr,
                operation,
                ty,
            } => write!(
                f,
                "cannot {} {} value at 0x{:x} due to out of range of memory. memory size is 0x{:x}",
                operation, ty, addr, max,
            )?,
            ImportFuncCallFail {
                mod_name,
                name,
                msg,
            } => write!(
                f,
                "calling imported function '{}' in module '{}': {}",
                name, mod_name, msg,
            )?,
            WrongInvokeTarget { name, actual: None } => write!(f, "cannot invoke unknown function '{}'", name)?,
            WrongInvokeTarget { name, actual: Some(actual) } => write!(
                f,
                "cannot invoke '{name}': '{name}' is {actual}",
                name=name,
                actual=actual,
            )?,
            InvokeInvalidArgs { name, args, arg_types } => write!(
                f,
                "cannot invoke function '{}' since given values [{}] does not match to parameter types [{}]",
                name,
                JoinWritable(args, ", "),
                JoinWritable(arg_types, " "),
            )?,
            RemZeroDivisor => f.write_str("attempt to calculate reminder with zero divisor")?,
            DivByZeroOrOverflow => f.write_str("integer overflow or attempt to devide integer by zero")?,
        }
        write!(
            f,
            ": execution was trapped at byte offset 0x{:x}",
            self.offset
        )
    }
}

pub type Result<T> = ::std::result::Result<T, Box<Trap>>;