solana_sbpf/
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 2020 Solana Maintainers <maintainers@solana.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 for SBPF programs.
13#![warn(missing_docs)]
14#![allow(clippy::literal_string_with_formatting_args)]
15#![deny(clippy::arithmetic_side_effects)]
16#![deny(clippy::ptr_as_ptr)]
17
18extern crate byteorder;
19extern crate combine;
20extern crate hash32;
21extern crate log;
22#[cfg(feature = "jit")]
23extern crate rand;
24extern crate thiserror;
25
26pub mod aligned_memory;
27mod asm_parser;
28pub mod assembler;
29#[cfg(feature = "debugger")]
30pub mod debugger;
31pub mod disassembler;
32pub mod ebpf;
33pub mod elf;
34pub mod elf_parser;
35pub mod error;
36pub mod insn_builder;
37pub mod interpreter;
38#[cfg(all(feature = "jit", not(target_os = "windows"), target_arch = "x86_64"))]
39pub mod jit;
40#[cfg(all(feature = "jit", not(target_os = "windows"), target_arch = "x86_64"))]
41mod memory_management;
42pub mod memory_region;
43pub mod program;
44pub mod static_analysis;
45pub mod verifier;
46pub mod vm;
47#[cfg(all(feature = "jit", not(target_os = "windows"), target_arch = "x86_64"))]
48mod x86;
49
50trait ErrCheckedArithmetic: Sized {
51    fn err_checked_add(self, other: Self) -> Result<Self, ArithmeticOverflow>;
52    fn err_checked_sub(self, other: Self) -> Result<Self, ArithmeticOverflow>;
53    fn err_checked_mul(self, other: Self) -> Result<Self, ArithmeticOverflow>;
54    #[allow(dead_code)]
55    fn err_checked_div(self, other: Self) -> Result<Self, ArithmeticOverflow>;
56}
57struct ArithmeticOverflow;
58
59macro_rules! impl_err_checked_arithmetic {
60    ($($ty:ty),*) => {
61        $(
62            impl ErrCheckedArithmetic for $ty {
63                fn err_checked_add(self, other: $ty) -> Result<Self, ArithmeticOverflow> {
64                    self.checked_add(other).ok_or(ArithmeticOverflow)
65                }
66
67                fn err_checked_sub(self, other: $ty) -> Result<Self, ArithmeticOverflow> {
68                    self.checked_sub(other).ok_or(ArithmeticOverflow)
69                }
70
71                fn err_checked_mul(self, other: $ty) -> Result<Self, ArithmeticOverflow> {
72                    self.checked_mul(other).ok_or(ArithmeticOverflow)
73                }
74
75                fn err_checked_div(self, other: $ty) -> Result<Self, ArithmeticOverflow> {
76                    self.checked_div(other).ok_or(ArithmeticOverflow)
77                }
78            }
79        )*
80    }
81}
82
83impl_err_checked_arithmetic!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);