cbe_rbpf/
lib.rs

1// Derived from uBPF <https://github.com/iovisor/ubpf>
2// Copyright 2015 Big Switch Networks, Inc
3//      (uBPF: VM architecture, parts of the interpreter, originally in C)
4// Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>
5//      (Translation to Rust, MetaBuff/multiple classes addition, hashmaps for syscalls)
6// Copyright 2022 Cartallum CBE Maintainers <maintainers@cartallum.com>
7//
8// Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
9// the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
10// copied, modified, or distributed except according to those terms.
11
12//! Virtual machine and JIT compiler for eBPF programs.
13#![warn(missing_docs)]
14#![doc(
15    html_logo_url = "https://raw.githubusercontent.com/qmonnet/rbpf/master/misc/rbpf.png",
16    html_favicon_url = "https://raw.githubusercontent.com/qmonnet/rbpf/master/misc/rbpf.ico"
17)]
18#![deny(clippy::integer_arithmetic)]
19
20extern crate byteorder;
21extern crate combine;
22extern crate hash32;
23extern crate log;
24extern crate rand;
25extern crate thiserror;
26
27pub mod aligned_memory;
28mod asm_parser;
29pub mod assembler;
30#[cfg(feature = "debugger")]
31pub mod debugger;
32pub mod disassembler;
33pub mod ebpf;
34pub mod elf;
35pub mod elf_parser;
36pub mod elf_parser_glue;
37pub mod error;
38pub mod fuzz;
39pub mod insn_builder;
40pub mod interpreter;
41#[cfg(all(feature = "jit", not(target_os = "windows"), target_arch = "x86_64"))]
42mod jit;
43#[cfg(feature = "jit")]
44mod memory_management;
45pub mod memory_region;
46pub mod static_analysis;
47pub mod syscalls;
48pub mod verifier;
49pub mod vm;
50#[cfg(all(feature = "jit", not(target_os = "windows"), target_arch = "x86_64"))]
51mod x86;
52
53trait ErrCheckedArithmetic: Sized {
54    fn err_checked_add(self, other: Self) -> Result<Self, ArithmeticOverflow>;
55    fn err_checked_sub(self, other: Self) -> Result<Self, ArithmeticOverflow>;
56    fn err_checked_mul(self, other: Self) -> Result<Self, ArithmeticOverflow>;
57    fn err_checked_div(self, other: Self) -> Result<Self, ArithmeticOverflow>;
58}
59struct ArithmeticOverflow;
60
61macro_rules! impl_err_checked_arithmetic {
62    ($($ty:ty),*) => {
63        $(
64            impl ErrCheckedArithmetic for $ty {
65                fn err_checked_add(self, other: $ty) -> Result<Self, ArithmeticOverflow> {
66                    self.checked_add(other).ok_or(ArithmeticOverflow)
67                }
68
69                fn err_checked_sub(self, other: $ty) -> Result<Self, ArithmeticOverflow> {
70                    self.checked_sub(other).ok_or(ArithmeticOverflow)
71                }
72
73                fn err_checked_mul(self, other: $ty) -> Result<Self, ArithmeticOverflow> {
74                    self.checked_mul(other).ok_or(ArithmeticOverflow)
75                }
76
77                fn err_checked_div(self, other: $ty) -> Result<Self, ArithmeticOverflow> {
78                    self.checked_div(other).ok_or(ArithmeticOverflow)
79                }
80            }
81        )*
82    }
83}
84
85impl_err_checked_arithmetic!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);