Skip to content

Commit

Permalink
feat: Add nostd for no_std compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
code0xff committed Dec 5, 2024
1 parent c5b30c3 commit cb77fe3
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 140 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rbpf/src/aligned_memory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Aligned memory
use crate::lib::*;
use nostd::{io, mem, prelude::*, ptr};

/// Scalar types, aka "plain old data"
pub trait Pod {}
Expand Down
14 changes: 8 additions & 6 deletions rbpf/src/disassembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
//! Functions in this module are used to handle eBPF programs with a higher level representation,
//! for example to disassemble the code into a human-readable format.
use crate::{
ebpf,
lib::*,
program::{BuiltinProgram, FunctionRegistry, SBPFVersion},
static_analysis::CfgNode,
vm::ContextObject,
use {
crate::{
ebpf,
program::{BuiltinProgram, FunctionRegistry, SBPFVersion},
static_analysis::CfgNode,
vm::ContextObject,
},
nostd::{collections::BTreeMap, prelude::*},
};

fn resolve_label(cfg_nodes: &BTreeMap<usize, CfgNode>, pc: usize) -> &str {
Expand Down
9 changes: 6 additions & 3 deletions rbpf/src/ebpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
//! <https://www.kernel.org/doc/Documentation/networking/filter.txt>, or for a shorter version of
//! the list of the operation codes: <https://github.com/iovisor/bpf-docs/blob/master/eBPF.md>
use crate::lib::*;
use byteorder::{ByteOrder, LittleEndian};
use hash32::{Hash, Hasher, Murmur3Hasher};
use {
byteorder::{ByteOrder, LittleEndian},
core::panic,
hash32::{Hash, Hasher, Murmur3Hasher},
nostd::{fmt, prelude::*},
};

/// Solana BPF version flag
pub const EF_SBPF_V2: u32 = 0x20;
Expand Down
60 changes: 29 additions & 31 deletions rbpf/src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,33 @@
// where the section headers may be removed from the ELF. If that happens then
// this loader will need to be re-written to use the program headers instead.

use crate::{
aligned_memory::{is_memory_aligned, AlignedMemory},
ebpf::{self, EF_SBPF_V2, HOST_ALIGN, INSN_SIZE},
elf_parser::{
consts::{
ELFCLASS64, ELFDATA2LSB, ELFOSABI_NONE, EM_BPF, EM_SBPF, ET_DYN, R_X86_64_32,
R_X86_64_64, R_X86_64_NONE, R_X86_64_RELATIVE,
},
types::{Elf64Phdr, Elf64Shdr, Elf64Word},
Elf64, ElfParserError,
},
error::EbpfError,
lib::*,
memory_region::MemoryRegion,
program::{BuiltinProgram, FunctionRegistry, SBPFVersion},
verifier::Verifier,
vm::{Config, ContextObject},
};

#[cfg(all(feature = "jit", not(target_os = "windows"), target_arch = "x86_64"))]
use crate::jit::{JitCompiler, JitProgram};
use byteorder::{ByteOrder, LittleEndian};

#[cfg(all(feature = "std", not(feature = "shuttle-test")))]
use std::sync::Arc;

#[cfg(not(feature = "std"))]
use alloc::sync::Arc;

#[cfg(not(feature = "shuttle-test"))]
use nostd::sync::Arc;
#[cfg(feature = "shuttle-test")]
use shuttle::sync::Arc;
use {
crate::{
aligned_memory::{is_memory_aligned, AlignedMemory},
ebpf::{self, EF_SBPF_V2, HOST_ALIGN, INSN_SIZE},
elf_parser::{
consts::{
ELFCLASS64, ELFDATA2LSB, ELFOSABI_NONE, EM_BPF, EM_SBPF, ET_DYN, R_X86_64_32,
R_X86_64_64, R_X86_64_NONE, R_X86_64_RELATIVE,
},
types::{Elf64Phdr, Elf64Shdr, Elf64Word},
Elf64, ElfParserError,
},
error::EbpfError,
memory_region::MemoryRegion,
program::{BuiltinProgram, FunctionRegistry, SBPFVersion},
verifier::Verifier,
vm::{Config, ContextObject},
},
byteorder::{ByteOrder, LittleEndian},
nostd::{collections::BTreeMap, mem, ops::Range, prelude::*, str},
};

/// Error definitions
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
Expand Down Expand Up @@ -1194,7 +1190,6 @@ pub(crate) fn get_ro_region(ro_section: &Section, elf: &[u8]) -> MemoryRegion {

#[cfg(test)]
mod test {
use super::*;
use crate::{
elf_parser::{
// FIXME consts::{ELFCLASS32, ELFDATA2MSB, ET_REL},
Expand All @@ -1208,9 +1203,12 @@ mod test {
syscalls,
vm::TestContextObject,
};
use rand::{distributions::Uniform, Rng};
use std::{fs::File, io::Read};
use test_utils::assert_error;
use {
super::*,
rand::{distributions::Uniform, Rng},
std::{fs::File, io::Read},
test_utils::assert_error,
};
type ElfExecutable = Executable<TestContextObject>;

fn loader() -> Arc<BuiltinProgram<TestContextObject>> {
Expand Down
12 changes: 7 additions & 5 deletions rbpf/src/elf_parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
pub mod consts;
pub mod types;

use crate::{lib::*, ArithmeticOverflow, ErrCheckedArithmetic};
use {consts::*, types::*};
use {
crate::{ArithmeticOverflow, ErrCheckedArithmetic},
consts::*,
nostd::{fmt, mem, ops::Range, prelude::*, slice, str},
types::*,
};

/// Maximum length of section name allowed.
pub const SECTION_NAME_LENGTH_MAXIMUM: usize = 16;
Expand Down Expand Up @@ -563,9 +567,7 @@ impl fmt::Debug for Elf64<'_> {
section_header.sh_name,
SECTION_NAME_LENGTH_MAXIMUM,
)
.and_then(|name| {
str::from_utf8(name).map_err(|_| ElfParserError::InvalidString)
})
.and_then(|name| str::from_utf8(name).map_err(|_| ElfParserError::InvalidString))
.unwrap();
writeln!(f, "{section_name}")?;
writeln!(f, "{section_header:#X?}")?;
Expand Down
6 changes: 5 additions & 1 deletion rbpf/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

//! This module contains error and result types
use crate::{elf::ElfError, lib::*, memory_region::AccessType, verifier::VerifierError};
use {
crate::{elf::ElfError, memory_region::AccessType, verifier::VerifierError},
core::panic,
nostd::{error, fmt, prelude::*, ptr},
};

/// Error definitions
#[derive(Debug, thiserror::Error)]
Expand Down
39 changes: 0 additions & 39 deletions rbpf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,45 +60,6 @@ pub mod vm;
#[cfg(all(feature = "jit", not(target_os = "windows"), target_arch = "x86_64"))]
mod x86;

mod lib {
#[cfg(feature = "std")]
pub use std::{
array,
cell::{Cell, UnsafeCell},
cmp,
collections::{btree_map::Entry, BTreeMap, BTreeSet, HashMap, HashSet},
error, fmt,
fmt::Debug,
io, mem,
ops::{self, Range},
ptr::{self, copy_nonoverlapping},
slice, str,
};
#[cfg(not(feature = "std"))]
pub use {
alloc::{
boxed::Box,
collections::{btree_map::Entry, BTreeMap, BTreeSet},
format, slice,
string::{String, ToString},
vec,
vec::Vec,
},
core::{
array,
cell::{Cell, UnsafeCell},
cmp, error, fmt,
fmt::Debug,
mem,
ops::{self, Range},
ptr::{self, copy_nonoverlapping},
str,
},
nostd::io,
hashbrown::{HashMap, HashSet},
};
}

trait ErrCheckedArithmetic: Sized {
fn err_checked_add(self, other: Self) -> Result<Self, ArithmeticOverflow>;
fn err_checked_sub(self, other: Self) -> Result<Self, ArithmeticOverflow>;
Expand Down
32 changes: 21 additions & 11 deletions rbpf/src/memory_region.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
//! This module defines memory regions
use crate::{
aligned_memory::Pod,
ebpf,
error::{EbpfError, ProgramResult},
lib::*,
program::SBPFVersion,
vm::Config,
use {
crate::{
aligned_memory::Pod,
ebpf,
error::{EbpfError, ProgramResult},
program::SBPFVersion,
vm::Config,
},
nostd::{
array,
cell::{Cell, UnsafeCell},
cmp, fmt, mem,
ops::Range,
prelude::*,
ptr::{self, copy_nonoverlapping},
},
};

/* Explaination of the Gapped Memory
Expand Down Expand Up @@ -954,10 +963,11 @@ impl MappingCache {

#[cfg(test)]
mod test {
use std::{cell::RefCell, rc::Rc};
use test_utils::assert_error;

use super::*;
use {
super::*,
std::{cell::RefCell, rc::Rc},
test_utils::assert_error,
};

#[test]
fn test_mapping_cache() {
Expand Down
22 changes: 15 additions & 7 deletions rbpf/src/program.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
//! Common interface for built-in and user supplied programs
use crate::{
ebpf,
elf::ElfError,
lib::*,
vm::{Config, ContextObject, EbpfVm},
use {
crate::{
ebpf,
elf::ElfError,
vm::{Config, ContextObject, EbpfVm},
},
nostd::{
collections::{btree_map::Entry, BTreeMap},
fmt, mem,
prelude::*,
},
};

/// Defines a set of sbpf_version of an executable
Expand Down Expand Up @@ -346,8 +352,10 @@ macro_rules! declare_builtin_function {

#[cfg(test)]
mod tests {
use super::*;
use crate::{syscalls, vm::TestContextObject};
use {
super::*,
crate::{syscalls, vm::TestContextObject},
};

#[test]
fn test_builtin_program_eq() {
Expand Down
23 changes: 15 additions & 8 deletions rbpf/src/static_analysis.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
#![allow(clippy::arithmetic_side_effects)]
//! Static Byte Code Analysis
use crate::disassembler::disassemble_instruction;
use crate::{
ebpf,
elf::Executable,
error::EbpfError,
lib::*,
vm::{ContextObject, DynamicAnalysis, TestContextObject},
use {
crate::{
disassembler::disassemble_instruction,
ebpf,
elf::Executable,
error::EbpfError,
vm::{ContextObject, DynamicAnalysis, TestContextObject},
},
nostd::{
cmp,
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
io, mem, ops,
prelude::*,
},
rustc_demangle::demangle,
};
use rustc_demangle::demangle;

/// Register state recorded after executing one instruction
///
Expand Down
14 changes: 8 additions & 6 deletions rbpf/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
//!
//! Contrary to the verifier of the Linux kernel, this one does not modify the bytecode at all.
use crate::{
ebpf,
lib::*,
program::{FunctionRegistry, SBPFVersion},
vm::Config,
use {
crate::{
ebpf,
program::{FunctionRegistry, SBPFVersion},
vm::Config,
},
nostd::ops,
thiserror::Error,
};
use thiserror::Error;

/// Error definitions
#[derive(Debug, Error, Eq, PartialEq)]
Expand Down
Loading

0 comments on commit cb77fe3

Please sign in to comment.