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
//! Runtime interpreter error implementation

use fuel_asm::{
    PanicInstruction,
    PanicReason,
    RawInstruction,
    Word,
};
use fuel_tx::ValidityError;

use crate::checked_transaction::CheckError;
use alloc::{
    format,
    string::{
        String,
        ToString,
    },
};
use core::{
    convert::Infallible,
    fmt,
};

use crate::storage::predicate;

/// Interpreter runtime error variants.
#[derive(Debug, derive_more::Display)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum InterpreterError<StorageError> {
    /// The instructions execution resulted in a well-formed panic, caused by an
    /// explicit instruction.
    #[display(fmt = "Execution error: {_0:?}")]
    PanicInstruction(PanicInstruction),
    /// The VM execution resulted in a well-formed panic. This panic wasn't
    /// caused by an instruction contained in the transaction or a called
    /// contract.
    #[display(fmt = "Execution error: {_0:?}")]
    Panic(PanicReason),
    /// Failed while checking the transaction.
    #[display(fmt = "Failed to check the transaction: {_0:?}")]
    CheckError(CheckError),
    /// No transaction was initialized in the interpreter. It cannot provide
    /// state transitions.
    #[display(fmt = "Execution error")]
    NoTransactionInitialized,
    #[display(fmt = "Execution error")]
    /// The debug state is not initialized; debug routines can't be called.
    DebugStateNotInitialized,
    /// Storage I/O error
    #[display(fmt = "Storage error: {}", _0)]
    Storage(StorageError),
    /// Encountered a bug
    #[display(fmt = "Bug: {_0}")]
    Bug(Bug),
    /// The `Ready` transaction provided to `Interpreter` doesn't have expected gas price
    #[display(
        fmt = "The transaction's gas price is wrong: expected {expected}, got {actual}"
    )]
    ReadyTransactionWrongGasPrice {
        /// Expected gas price
        expected: Word,
        /// Actual gas price
        actual: Word,
    },
}

impl<StorageError> InterpreterError<StorageError> {
    /// Describe the error as recoverable or halt.
    pub fn from_runtime(
        error: RuntimeError<StorageError>,
        instruction: RawInstruction,
    ) -> Self {
        match error {
            RuntimeError::Recoverable(reason) => {
                Self::PanicInstruction(PanicInstruction::error(reason, instruction))
            }
            _ => Self::from(error),
        }
    }

    /// Return the specified panic reason that caused this error, if applicable.
    pub const fn panic_reason(&self) -> Option<PanicReason> {
        match self {
            Self::PanicInstruction(result) => Some(*result.reason()),
            Self::Panic(reason) => Some(*reason),
            _ => None,
        }
    }

    /// Return the instruction that caused this error, if applicable.
    pub const fn instruction(&self) -> Option<&RawInstruction> {
        match self {
            Self::PanicInstruction(result) => Some(result.instruction()),
            _ => None,
        }
    }

    /// Return the underlying `InstructionResult` if this instance is
    /// `PanicInstruction`; returns `None` otherwise.
    pub fn instruction_result(&self) -> Option<PanicInstruction> {
        match self {
            Self::PanicInstruction(r) => Some(*r),
            _ => None,
        }
    }
}

impl<StorageError> InterpreterError<StorageError>
where
    StorageError: fmt::Debug,
{
    /// Make non-generic by converting the storage error to a string.
    pub fn erase_generics(&self) -> InterpreterError<String> {
        match self {
            Self::Storage(e) => InterpreterError::Storage(format!("{e:?}")),
            Self::PanicInstruction(e) => InterpreterError::PanicInstruction(*e),
            Self::Panic(e) => InterpreterError::Panic(*e),
            Self::NoTransactionInitialized => InterpreterError::NoTransactionInitialized,
            Self::DebugStateNotInitialized => InterpreterError::DebugStateNotInitialized,
            Self::Bug(e) => InterpreterError::Bug(e.clone()),
            Self::CheckError(e) => InterpreterError::CheckError(e.clone()),
            InterpreterError::ReadyTransactionWrongGasPrice { expected, actual } => {
                InterpreterError::ReadyTransactionWrongGasPrice {
                    expected: *expected,
                    actual: *actual,
                }
            }
        }
    }
}

impl<StorageError> From<RuntimeError<StorageError>> for InterpreterError<StorageError> {
    fn from(error: RuntimeError<StorageError>) -> Self {
        match error {
            RuntimeError::Recoverable(e) => Self::Panic(e),
            RuntimeError::Bug(e) => Self::Bug(e),
            RuntimeError::Storage(e) => Self::Storage(e),
        }
    }
}

impl<StorageError> PartialEq for InterpreterError<StorageError>
where
    StorageError: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::PanicInstruction(s), Self::PanicInstruction(o)) => s == o,
            (Self::Panic(s), Self::Panic(o)) => s == o,
            (Self::NoTransactionInitialized, Self::NoTransactionInitialized) => true,
            (Self::Storage(a), Self::Storage(b)) => a == b,
            (Self::DebugStateNotInitialized, Self::DebugStateNotInitialized) => true,

            _ => false,
        }
    }
}

impl<StorageError> From<Bug> for InterpreterError<StorageError> {
    fn from(bug: Bug) -> Self {
        Self::Bug(bug)
    }
}

impl<StorageError> From<Infallible> for InterpreterError<StorageError> {
    fn from(_: Infallible) -> Self {
        unreachable!()
    }
}

impl<StorageError> From<ValidityError> for InterpreterError<StorageError> {
    fn from(err: ValidityError) -> Self {
        Self::CheckError(CheckError::Validity(err))
    }
}

/// Runtime error description that should either be specified in the protocol or
/// halt the execution.
#[derive(Debug)]
#[must_use]
pub enum RuntimeError<StorageError> {
    /// Specified error with well-formed fallback strategy, i.e. vm panics.
    Recoverable(PanicReason),
    /// Invalid interpreter state reached unexpectedly, this is a bug
    Bug(Bug),
    /// Storage io error
    Storage(StorageError),
}

impl<StorageError> RuntimeError<StorageError> {
    /// Flag whether the error is recoverable.
    pub const fn is_recoverable(&self) -> bool {
        matches!(self, Self::Recoverable(_))
    }

    /// Flag whether the error must halt the execution.
    pub const fn must_halt(&self) -> bool {
        !self.is_recoverable()
    }
}

impl<StorageError: PartialEq> PartialEq for RuntimeError<StorageError> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (RuntimeError::Recoverable(a), RuntimeError::Recoverable(b)) => a == b,
            (RuntimeError::Bug(a), RuntimeError::Bug(b)) => a == b,
            (RuntimeError::Storage(a), RuntimeError::Storage(b)) => a == b,
            _ => false,
        }
    }
}

impl<StorageError: core::fmt::Debug> fmt::Display for RuntimeError<StorageError> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Recoverable(reason) => write!(f, "Recoverable error: {}", reason),
            Self::Bug(err) => write!(f, "Bug: {}", err),
            Self::Storage(err) => write!(f, "Unrecoverable storage error: {:?}", err),
        }
    }
}

impl<StorageError> From<PanicReason> for RuntimeError<StorageError> {
    fn from(value: PanicReason) -> Self {
        Self::Recoverable(value)
    }
}

impl<StorageError> From<core::array::TryFromSliceError> for RuntimeError<StorageError> {
    fn from(value: core::array::TryFromSliceError) -> Self {
        Self::Recoverable(value.into())
    }
}

impl<StorageError> From<Bug> for RuntimeError<StorageError> {
    fn from(bug: Bug) -> Self {
        Self::Bug(bug)
    }
}

impl<StorageError> From<Infallible> for RuntimeError<StorageError> {
    fn from(_: Infallible) -> Self {
        unreachable!()
    }
}

/// Predicates checking failed
#[derive(Debug, Clone, PartialEq, derive_more::Display)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum PredicateVerificationFailed {
    /// The predicate did not use the amount of gas provided
    #[display(fmt = "Predicate used less than the required amount of gas")]
    GasMismatch,
    /// The transaction doesn't contain enough gas to evaluate the predicate
    #[display(fmt = "Insufficient gas available for single predicate")]
    OutOfGas,
    /// The predicate owner does not correspond to the predicate code
    #[display(fmt = "Predicate owner invalid, doesn't match code root")]
    InvalidOwner,
    /// The predicate wasn't successfully evaluated to true
    #[display(fmt = "Predicate failed to evaluate")]
    False,
    /// The predicate gas used was not specified before execution
    #[display(fmt = "Predicate failed to evaluate")]
    GasNotSpecified,
    /// The transaction's `max_gas` is greater than the global gas limit.
    #[display(fmt = "Transaction exceeds total gas allowance {_0:?}")]
    TransactionExceedsTotalGasAllowance(Word),
    /// The cumulative gas overflowed the u64 accumulator
    #[display(fmt = "Cumulative gas computation overflowed the u64 accumulator")]
    GasOverflow,
    /// Invalid interpreter state reached unexpectedly, this is a bug
    #[display(fmt = "Invalid interpreter state reached unexpectedly")]
    Bug(Bug),
    /// The VM execution resulted in a well-formed panic, caused by an instruction.
    #[display(fmt = "Execution error: {_0:?}")]
    PanicInstruction(PanicInstruction),
    /// The VM execution resulted in a well-formed panic not caused by an instruction.
    #[display(fmt = "Execution error: {_0:?}")]
    Panic(PanicReason),
    /// Predicate verification failed since it attempted to access storage
    #[display(
        fmt = "Predicate verification failed since it attempted to access storage"
    )]
    Storage,
}

impl From<InterpreterError<predicate::StorageUnavailable>>
    for PredicateVerificationFailed
{
    fn from(error: InterpreterError<predicate::StorageUnavailable>) -> Self {
        match error {
            error if error.panic_reason() == Some(PanicReason::OutOfGas) => {
                PredicateVerificationFailed::OutOfGas
            }
            InterpreterError::Panic(reason) => PredicateVerificationFailed::Panic(reason),
            InterpreterError::PanicInstruction(result) => {
                PredicateVerificationFailed::PanicInstruction(result)
            }
            InterpreterError::Bug(bug) => PredicateVerificationFailed::Bug(bug),
            InterpreterError::Storage(_) => PredicateVerificationFailed::Storage,
            _ => PredicateVerificationFailed::False,
        }
    }
}

impl From<Bug> for PredicateVerificationFailed {
    fn from(bug: Bug) -> Self {
        Self::Bug(bug)
    }
}

impl From<PanicReason> for PredicateVerificationFailed {
    fn from(reason: PanicReason) -> Self {
        Self::Panic(reason)
    }
}

impl From<PanicOrBug> for PredicateVerificationFailed {
    fn from(err: PanicOrBug) -> Self {
        match err {
            PanicOrBug::Panic(reason) => Self::from(reason),
            PanicOrBug::Bug(bug) => Self::Bug(bug),
        }
    }
}

/// Traceable bug variants
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum::EnumMessage)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum BugVariant {
    /// Context gas increase has overflow
    #[strum(
        message = "The context gas cannot overflow since it was created by a valid transaction and the total gas does not increase - hence, it always fits a word."
    )]
    ContextGasOverflow,

    /// Context gas increase has underflow
    #[strum(
        message = "The context gas cannot underflow since any script should halt upon gas exhaustion."
    )]
    ContextGasUnderflow,

    /// Global gas subtraction has underflow
    #[strum(
        message = "The gas consumption cannot exceed the gas context since it is capped by the transaction gas limit."
    )]
    GlobalGasUnderflow,

    /// The global gas is less than the context gas.
    #[strum(message = "The global gas cannot ever be less than the context gas. ")]
    GlobalGasLessThanContext,

    /// The stack point has overflow
    #[strum(message = "The stack pointer cannot overflow under checked operations.")]
    StackPointerOverflow,

    /// Code size of a contract doesn't fit into a Word. This is prevented by tx size
    /// limit.
    #[strum(message = "Contract size doesn't fit into a word.")]
    CodeSizeOverflow,

    /// Refund cannot be computed in the current vm state.
    #[strum(message = "Refund cannot be computed in the current vm state.")]
    UncomputableRefund,

    /// Receipts context is full, but there's an attempt to add more receipts.
    #[strum(message = "Receipts context is full, cannot add new receipts.")]
    ReceiptsCtxFull,

    /// Witness index is out of bounds.
    #[strum(message = "Witness index is out of bounds.")]
    WitnessIndexOutOfBounds,

    /// The witness subsection index is higher than the total number of parts.
    #[strum(
        message = "The witness subsection index is higher than the total number of parts."
    )]
    NextSubsectionIndexIsHigherThanTotalNumberOfParts,
}

impl fmt::Display for BugVariant {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use strum::EnumMessage;
        if let Some(msg) = self.get_message() {
            write!(f, "{}", msg)
        } else {
            write!(f, "{:?}", self)
        }
    }
}

/// VM encountered unexpected state. This is a bug.
/// The execution must terminate since the VM is in an invalid state.
///
/// The bug it self is identified by the caller location.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[must_use]
pub struct Bug {
    /// Source code location of the bug, in `path/to/file.rs:line:column` notation
    location: String,

    /// Type of bug
    variant: BugVariant,

    /// Additional error message for the bug, if it's caused by a runtime error
    inner_message: Option<String>,

    /// Optionally include a backtrace for the instruction triggering this bug.
    /// This is only available when the `backtrace` feature is enabled.
    #[cfg(feature = "backtrace")]
    bt: backtrace::Backtrace,
}

impl Bug {
    /// Construct a new bug with the specified variant, using caller location for
    /// idenitfying the bug.
    #[track_caller]
    pub fn new(variant: BugVariant) -> Self {
        let caller = core::panic::Location::caller();
        let location = format!("{}:{}:{}", caller.file(), caller.line(), caller.column());
        Self {
            location,
            variant,
            inner_message: None,
            #[cfg(feature = "backtrace")]
            bt: backtrace::Backtrace::new(),
        }
    }

    /// Set an additional error message.
    pub fn with_message<E: ToString>(mut self, error: E) -> Self {
        self.inner_message = Some(error.to_string());
        self
    }
}

impl PartialEq for Bug {
    fn eq(&self, other: &Self) -> bool {
        self.location == other.location
    }
}

#[cfg(feature = "backtrace")]
mod bt {
    use super::*;
    use backtrace::Backtrace;

    impl Bug {
        /// Backtrace data
        pub const fn bt(&self) -> &Backtrace {
            &self.bt
        }
    }
}

impl fmt::Display for Bug {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use percent_encoding::{
            utf8_percent_encode,
            NON_ALPHANUMERIC,
        };

        let issue_title = format!("Bug report: {:?} in {}", self.variant, self.location);

        let issue_body = format!(
            "Error: {:?} {}\nLocation: {}\nVersion: {} {}\n",
            self.variant,
            self.inner_message
                .as_ref()
                .map(|msg| format!("({msg})"))
                .unwrap_or_default(),
            self.location,
            env!("CARGO_PKG_NAME"),
            env!("CARGO_PKG_VERSION")
        );

        write!(
            f,
            concat!(
                "Encountered a bug! Please report this using the following link: ",
                "https://github.com/FuelLabs/fuel-vm/issues/new",
                "?title={}",
                "&body={}",
                "\n\n",
                "{:?} error in {}: {} {}\n",
            ),
            utf8_percent_encode(&issue_title, NON_ALPHANUMERIC),
            utf8_percent_encode(&issue_body, NON_ALPHANUMERIC),
            self.variant,
            self.location,
            self.variant,
            self.inner_message
                .as_ref()
                .map(|msg| format!("({msg})"))
                .unwrap_or_default(),
        )?;

        #[cfg(feature = "backtrace")]
        {
            write!(f, "\nBacktrace:\n{:?}\n", self.bt)?;
        }

        Ok(())
    }
}

/// Runtime error description that should either be specified in the protocol or
/// halt the execution.
#[derive(Debug, Clone, PartialEq)]
#[must_use]
pub enum PanicOrBug {
    /// VM panic
    Panic(PanicReason),
    /// Invalid interpreter state reached unexpectedly, this is a bug
    Bug(Bug),
}

impl From<PanicReason> for PanicOrBug {
    fn from(panic: PanicReason) -> Self {
        Self::Panic(panic)
    }
}

impl From<Bug> for PanicOrBug {
    fn from(bug: Bug) -> Self {
        Self::Bug(bug)
    }
}

impl<StorageError> From<PanicOrBug> for RuntimeError<StorageError> {
    fn from(value: PanicOrBug) -> Self {
        match value {
            PanicOrBug::Panic(reason) => Self::Recoverable(reason),
            PanicOrBug::Bug(bug) => Self::Bug(bug),
        }
    }
}

impl<StorageError> From<PanicOrBug> for InterpreterError<StorageError> {
    fn from(value: PanicOrBug) -> Self {
        match value {
            PanicOrBug::Panic(reason) => Self::Panic(reason),
            PanicOrBug::Bug(bug) => Self::Bug(bug),
        }
    }
}

/// Result of a operation that doesn't access storage
pub type SimpleResult<T> = Result<T, PanicOrBug>;

/// Result of a operation that accesses storage
pub type IoResult<T, S> = Result<T, RuntimeError<S>>;

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

    #[test]
    fn bug_report_message() {
        let bug = Bug::new(BugVariant::ContextGasOverflow).with_message("Test message");
        let text = format!("{}", bug);
        assert!(text.contains(file!()));
        assert!(text.contains("https://github.com/FuelLabs/fuel-vm/issues/new"));
        assert!(text.contains("ContextGasOverflow"));
        assert!(text.contains("Test message"));
    }
}