giit_rbpf/
error.rs

1// Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>
2//
3// Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
4// the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
5// copied, modified, or distributed except according to those terms.
6
7//! This module contains all the definitions related to eBPF, and some functions permitting to
8//! manipulate eBPF instructions.
9//!
10//! The number of bytes in an instruction, the maximum number of instructions in a program, and
11//! also all operation codes are defined here as constants.
12//!
13//! The structure for an instruction used by this crate, as well as the function to extract it from
14//! a program, is also defined in the module.
15//!
16//! To learn more about these instructions, see the Linux kernel documentation:
17//! <https://www.kernel.org/doc/Documentation/networking/filter.txt>, or for a shorter version of
18//! the list of the operation codes: <https://github.com/iovisor/bpf-docs/blob/master/eBPF.md>
19
20use crate::{elf::ElfError, memory_region::AccessType, verifier::VerifierError};
21
22/// User defined errors must implement this trait
23pub trait UserDefinedError: 'static + std::error::Error {}
24
25/// Error definitions
26#[derive(Debug, thiserror::Error, PartialEq, Eq)]
27pub enum EbpfError<E: UserDefinedError> {
28    /// User defined error
29    #[error("{0}")]
30    UserError(#[from] E),
31    /// ELF error
32    #[error("ELF error: {0}")]
33    ElfError(#[from] ElfError),
34    /// Syscall was already registered before
35    #[error("syscall #{0} was already registered before")]
36    SycallAlreadyRegistered(usize),
37    /// Syscall was not registered before bind
38    #[error("syscall #{0} was not registered before bind")]
39    SyscallNotRegistered(usize),
40    /// Syscall already has a bound context object
41    #[error("syscall #{0} already has a bound context object")]
42    SyscallAlreadyBound(usize),
43    /// Exceeded max BPF to BPF call depth
44    #[error("exceeded max BPF to BPF call depth of {1} at instruction #{0}")]
45    CallDepthExceeded(usize, usize),
46    /// Attempt to exit from root call frame
47    #[error("attempted to exit root call frame")]
48    ExitRootCallFrame,
49    /// Divide by zero"
50    #[error("divide by zero at instruction {0}")]
51    DivideByZero(usize),
52    /// Exceeded max instructions allowed
53    #[error("attempted to execute past the end of the text segment at instruction #{0}")]
54    ExecutionOverrun(usize),
55    /// Attempt to call to an address outside the text segment
56    #[error(
57        "callx at instruction {0} attempted to call outside of the text segment to addr 0x{1:x}"
58    )]
59    CallOutsideTextSegment(usize, u64),
60    /// Exceeded max instructions allowed
61    #[error("exceeded maximum number of instructions allowed ({1}) at instruction #{0}")]
62    ExceededMaxInstructions(usize, u64),
63    /// Program has not been JIT-compiled
64    #[error("program has not been JIT-compiled")]
65    JitNotCompiled,
66    /// Invalid virtual address
67    #[error("invalid virtual address {0:x?}")]
68    InvalidVirtualAddress(u64),
69    /// Memory region index or virtual address space is invalid
70    #[error("Invalid memory region at index {0}")]
71    InvalidMemoryRegion(usize),
72    /// Access violation (general)
73    #[error("Access violation in {4} section at address {2:#x} of size {3:?} by instruction #{0}")]
74    AccessViolation(usize, AccessType, u64, u64, &'static str),
75    /// Access violation (stack specific)
76    #[error(
77        "Access violation in stack frame {4} at address {2:#x} of size {3:?} by instruction #{0}"
78    )]
79    StackAccessViolation(usize, AccessType, u64, u64, i64),
80    /// Invalid instruction
81    #[error("invalid instruction at {0}")]
82    InvalidInstruction(usize),
83    /// Unsupported instruction
84    #[error("unsupported instruction at instruction {0}")]
85    UnsupportedInstruction(usize),
86    /// Compilation is too big to fit
87    #[error("Compilation exhaused text segment at instruction {0}")]
88    ExhausedTextSegment(usize),
89    /// Libc function call returned an error
90    #[error("Libc calling {0} {1:?} returned error code {2}")]
91    LibcInvocationFailed(&'static str, Vec<String>, i32),
92    /// ELF error
93    #[error("Verifier error: {0}")]
94    VerifierError(#[from] VerifierError),
95}