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
//! The error module contains all the data structures that represent
//! an error.

use crate::IType;
use crate::IValue;
use crate::{ast::TypeKind, interpreter::Instruction};
use std::{
    error::Error,
    fmt::{self, Display, Formatter},
    num::TryFromIntError,
    result::Result,
    string::{self, ToString},
};

use it_lilo::lifter::LiError;
use it_lilo::lowerer::LoError;
use thiserror::Error as ThisError;

pub use fluence_it_types::WasmValueNativeCastError;

/// A type alias for instruction's results.
pub type InstructionResult<T> = Result<T, InstructionError>;

/// A type alias for the interpreter result.
pub type InterpreterResult<T> = Result<T, InstructionError>;

/// Structure to represent the errors for instructions.
#[derive(Debug)]
pub struct InstructionError {
    /// The instruction that raises the error.
    pub instruction: Instruction,

    /// The error kind.
    pub error_kind: InstructionErrorKind,
}

impl InstructionError {
    pub(crate) fn from_error_kind(
        instruction: Instruction,
        error_kind: InstructionErrorKind,
    ) -> Self {
        Self {
            instruction,
            error_kind,
        }
    }

    pub(crate) fn from_li(instruction: Instruction, li: LiError) -> Self {
        let error_kind = InstructionErrorKind::LiError(li);
        Self::from_error_kind(instruction, error_kind)
    }

    pub(crate) fn from_lo(instruction: Instruction, lo: LoError) -> Self {
        let error_kind = InstructionErrorKind::LoError(lo);
        Self::from_error_kind(instruction, error_kind)
    }
}

impl Error for InstructionError {}

/// Allows you to shorten the expression creates a new InstructionError.
#[macro_export]
macro_rules! instr_error {
    ($instruction:expr, $error_kind:expr) => {
        Err(crate::errors::InstructionError::from_error_kind(
            $instruction,
            $error_kind,
        ))
    };
}

impl Display for InstructionError {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        write!(
            formatter,
            "`{}` {}",
            (&self.instruction).to_string(),
            self.error_kind
        )
    }
}

/// The kind of instruction errors.
#[derive(ThisError, Debug)]
pub enum InstructionErrorKind {
    /// The instruction needs to read an invocation input at index `index`, but it's missing.
    #[error("cannot access invocation inputs #{index} because it doesn't exist")]
    InvocationInputIsMissing {
        /// The invocation input index.
        index: u32,
    },

    /// Failed to cast from a WIT value to a native value.
    #[error("failed to cast the WIT value `{0}` to its native type")]
    ToNative(#[from] WasmValueNativeCastError),

    /// Failed to cast from `from` to `to`.
    #[error("failed to cast `{from:?}` to `{to:?}`")]
    LoweringLifting {
        /// The initial type.
        from: IType,

        /// The targeted type.
        to: IType,
    },

    /// Read a value from the stack, but it doesn't have the expected
    /// type.
    #[error("read a value `{expected_type:?}` from the stack, that can't be converted to `{received_value:?}`")]
    InvalidValueOnTheStack {
        /// The expected type.
        expected_type: IType,

        /// The received type.
        received_value: IValue,
    },

    /// Need to read some values from the stack, but it doesn't
    /// contain enough data.
    #[error(
        "needed to read `{needed}` value(s) from the stack, but it doesn't contain enough data"
    )]
    StackIsTooSmall {
        /// The number of values that were needed.
        needed: usize,
    },

    /// The local or import function doesn't exist.
    #[error("the local or import function `{function_index}` doesn't exist")]
    LocalOrImportIsMissing {
        /// The local or import function index.
        function_index: u32,
    },

    /// Values given to a local or import function doesn't match the
    /// function signature.
    #[error(
        "the local or import function `{function_index}` has the signature\
             `{:?} -> {:?}`\
             but it received values of kind `{:?} -> {:?}`",
        .expected.0, .expected.1, .received.0, .received.1,
    )]
    LocalOrImportSignatureMismatch {
        /// The local or import function index.
        function_index: u32,

        /// The expected signature.
        expected: (Vec<IType>, Vec<IType>),

        /// The received signature.
        received: (Vec<IType>, Vec<IType>),
    },

    /// Failed to call a local or import function.
    #[error("failed while calling the local or import function `{function_name}`")]
    LocalOrImportCall {
        /// The local or import function name that has been called.
        function_name: String,
    },

    /// The memory doesn't exist.
    #[error("memory `{memory_index}` does not exist")]
    MemoryIsMissing {
        /// The memory index.
        memory_index: usize,
    },

    /// Tried to read out of bounds of the memory.
    #[error("read out of the memory bounds (index {index} > memory length {length})")]
    MemoryOutOfBoundsAccess {
        /// The access index.
        index: usize,

        /// The memory length.
        length: usize,
    },

    /// The string contains invalid UTF-8 encoding.
    #[error("{0}")]
    String(string::FromUtf8Error),

    /// Out of range integral type conversion attempted.
    #[error("attempted to convert `{subject}`, but it appears to be a negative value")]
    NegativeValue {
        /// The variable name that triggered the error.
        subject: &'static str,
    },

    /// The type doesn't exist.
    #[error("the type `{type_index}` doesn't exist")]
    TypeIsMissing {
        /// The type index.
        type_index: u32,
    },

    /// The searched by id type doesn't exist.
    #[error("type with `{record_type_id}` is missing in a Wasm binary")]
    RecordTypeByNameIsMissing {
        /// The record type name.
        record_type_id: u64,
    },

    /// Corrupted array's been popped from the stack.
    #[error("{0}")]
    CorruptedArray(String),

    /// Corrupted record's been popped from the stack.
    #[error("{0}")]
    CorruptedRecord(String),

    /// Read a type that has an unexpected type.
    #[error(
        "read a type of kind `{received_kind:?}`,\
             but the kind `{expected_kind:?}` was expected"
    )]
    InvalidTypeKind {
        /// The expected kind.
        expected_kind: TypeKind,

        /// The received kind.
        received_kind: TypeKind,
    },

    /// Errors related to Serialization/deserialization of record.
    #[error("serde error: {0}")]
    SerdeError(String),

    /// Errors related to lifting/lowering records.
    #[error("{0}")]
    LiError(#[from] LiError),

    /// Errors related to incorrect writing to memory.
    #[error("{0}")]
    LoError(#[from] LoError),
}

impl From<(TryFromIntError, &'static str)> for InstructionErrorKind {
    fn from((_, subject): (TryFromIntError, &'static str)) -> Self {
        InstructionErrorKind::NegativeValue { subject }
    }
}