Skip to content

Commit

Permalink
Add std feature for gating the jit code alongside windows cfg option.
Browse files Browse the repository at this point in the history
This was done to prepare the foundation for adding no_std compatibility.
The std feature can be disabled by the users of this library which will
compile rbpf in a way that doesn't require the standard library.

Signed-off-by: SzymonKubica <[email protected]>
  • Loading branch information
SzymonKubica authored and qmonnet committed Jun 17, 2024
1 parent 17353e3 commit 9061ae8
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 43 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ json = "0.11"
hex = "0.4.3"

[features]
default = []
default = ["std"]
std = []
cranelift = [
"dep:cranelift-codegen",
"dep:cranelift-frontend",
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,10 @@ fn main() {
// directly reads from packet data)
let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();

#[cfg(windows)] {
#[cfg(any(windows, not(feature = "std")))] {
assert_eq!(vm.execute_program(mem).unwrap(), 0x11);
}
#[cfg(not(windows))] {
#[cfg(all(not(windows), feature = "std"))] {
// This time we JIT-compile the program.
vm.jit_compile().unwrap();

Expand Down Expand Up @@ -352,10 +352,10 @@ fn main() {
// This eBPF VM is for program that use a metadata buffer.
let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();

#[cfg(windows)] {
#[cfg(any(windows, not(feature = "std")))] {
assert_eq!(vm.execute_program(mem, mbuff).unwrap(), 0x2211);
}
#[cfg(not(windows))] {
#[cfg(all(not(windows), feature = "std"))] {
// Here again we JIT-compile the program.
vm.jit_compile().unwrap();

Expand Down
12 changes: 6 additions & 6 deletions examples/rbpf_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ fn main() {
return;
},
"--jit" => {
#[cfg(windows)] {
println!("JIT not supported on Windows");
#[cfg(any(windows, not(feature = "std")))] {
println!("JIT not supported");
return;
}
#[cfg(not(windows))] {
#[cfg(all(not(windows), feature = "std"))] {
jit = true;
}
},
Expand Down Expand Up @@ -93,11 +93,11 @@ fn main() {

let result : u64;
if jit {
#[cfg(windows)] {
println!("JIT not supported on Windows");
#[cfg(any(windows, not(feature = "std")))] {
println!("JIT not supported");
return;
}
#[cfg(not(windows))] {
#[cfg(all(not(windows), feature = "std"))] {
unsafe {
vm.jit_compile().unwrap();
result = vm.execute_program_jit(&mut memory).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/uptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ fn main() {

let time;

#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
{
vm.jit_compile().unwrap();

time = unsafe { vm.execute_program_jit().unwrap() };
}

#[cfg(windows)]
#[cfg(any(windows, not(feature = "std")))]
{
time = vm.execute_program().unwrap();
}
Expand Down
38 changes: 19 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub mod ebpf;
pub mod helpers;
pub mod insn_builder;
mod interpreter;
#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
mod jit;
mod verifier;

Expand Down Expand Up @@ -118,7 +118,7 @@ struct MetaBuff {
pub struct EbpfVmMbuff<'a> {
prog: Option<&'a [u8]>,
verifier: Verifier,
#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
jit: Option<jit::JitMemory<'a>>,
#[cfg(feature = "cranelift")]
cranelift_prog: Option<cranelift::CraneliftProgram>,
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<'a> EbpfVmMbuff<'a> {
Ok(EbpfVmMbuff {
prog,
verifier: verifier::check,
#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
jit: None,
#[cfg(feature = "cranelift")]
cranelift_prog: None,
Expand Down Expand Up @@ -319,7 +319,7 @@ impl<'a> EbpfVmMbuff<'a> {
///
/// vm.jit_compile();
/// ```
#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
pub fn jit_compile(&mut self) -> Result<(), Error> {
let prog = match self.prog {
Some(prog) => prog,
Expand Down Expand Up @@ -374,17 +374,17 @@ impl<'a> EbpfVmMbuff<'a> {
/// // Instantiate a VM.
/// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
///
/// # #[cfg(not(windows))]
/// # #[cfg(all(not(windows), feature = "std"))]
/// vm.jit_compile();
///
/// // Provide both a reference to the packet data, and to the metadata buffer.
/// # #[cfg(not(windows))]
/// # #[cfg(all(not(windows), feature = "std"))]
/// unsafe {
/// let res = vm.execute_program_jit(mem, &mut mbuff).unwrap();
/// assert_eq!(res, 0x2211);
/// }
/// ```
#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
pub unsafe fn execute_program_jit(
&self,
mem: &mut [u8],
Expand Down Expand Up @@ -835,7 +835,7 @@ impl<'a> EbpfVmFixedMbuff<'a> {
///
/// vm.jit_compile();
/// ```
#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
pub fn jit_compile(&mut self) -> Result<(), Error> {
let prog = match self.parent.prog {
Some(prog) => prog,
Expand Down Expand Up @@ -884,19 +884,19 @@ impl<'a> EbpfVmFixedMbuff<'a> {
/// // Instantiate a VM. Note that we provide the start and end offsets for mem pointers.
/// let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
///
/// # #[cfg(not(windows))]
/// # #[cfg(all(not(windows), feature = "std"))]
/// vm.jit_compile();
///
/// // Provide only a reference to the packet data. We do not manage the metadata buffer.
/// # #[cfg(not(windows))]
/// # #[cfg(all(not(windows), feature = "std"))]
/// unsafe {
/// let res = vm.execute_program_jit(mem).unwrap();
/// assert_eq!(res, 0xdd);
/// }
/// ```
// This struct redefines the `execute_program_jit()` function, in order to pass the offsets
// associated with the fixed mbuff.
#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
pub unsafe fn execute_program_jit(&mut self, mem: &'a mut [u8]) -> Result<u64, Error> {
// If packet data is empty, do not send the address of an empty slice; send a null pointer
// as first argument instead, as this is uBPF's behavior (empty packet should not happen
Expand Down Expand Up @@ -1240,7 +1240,7 @@ impl<'a> EbpfVmRaw<'a> {
///
/// vm.jit_compile();
/// ```
#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
pub fn jit_compile(&mut self) -> Result<(), Error> {
let prog = match self.parent.prog {
Some(prog) => prog,
Expand Down Expand Up @@ -1286,16 +1286,16 @@ impl<'a> EbpfVmRaw<'a> {
///
/// let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
///
/// # #[cfg(not(windows))]
/// # #[cfg(all(not(windows), feature = "std"))]
/// vm.jit_compile();
///
/// # #[cfg(not(windows))]
/// # #[cfg(all(not(windows), feature = "std"))]
/// unsafe {
/// let res = vm.execute_program_jit(mem).unwrap();
/// assert_eq!(res, 0x22cc);
/// }
/// ```
#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
pub unsafe fn execute_program_jit(&self, mem: &'a mut [u8]) -> Result<u64, Error> {
let mut mbuff = vec![];
self.parent.execute_program_jit(mem, &mut mbuff)
Expand Down Expand Up @@ -1555,7 +1555,7 @@ impl<'a> EbpfVmNoData<'a> {
///
/// vm.jit_compile();
/// ```
#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
pub fn jit_compile(&mut self) -> Result<(), Error> {
self.parent.jit_compile()
}
Expand Down Expand Up @@ -1604,16 +1604,16 @@ impl<'a> EbpfVmNoData<'a> {
///
/// let mut vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
///
/// # #[cfg(not(windows))]
/// # #[cfg(all(not(windows), feature = "std"))]
/// vm.jit_compile();
///
/// # #[cfg(not(windows))]
/// # #[cfg(all(not(windows), feature = "std"))]
/// unsafe {
/// let res = vm.execute_program_jit().unwrap();
/// assert_eq!(res, 0x1122);
/// }
/// ```
#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
pub unsafe fn execute_program_jit(&self) -> Result<u64, Error> {
self.parent.execute_program_jit(&mut [])
}
Expand Down
20 changes: 10 additions & 10 deletions tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fn test_vm_block_port() {
assert_eq!(res, 0xffffffff);
}

#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
#[test]
fn test_jit_block_port() {
// To load the bytecode from an object file instead of using the hardcoded instructions,
Expand Down Expand Up @@ -309,7 +309,7 @@ fn test_vm_mbuff_with_rust_api() {
}

// Program and memory come from uBPF test ldxh.
#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
#[test]
fn test_jit_mbuff() {
let prog = &[
Expand Down Expand Up @@ -338,7 +338,7 @@ fn test_jit_mbuff() {
}
}

#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
#[test]
fn test_vm_jit_ldabsb() {
let prog = &[
Expand All @@ -358,7 +358,7 @@ fn test_vm_jit_ldabsb() {
};
}

#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
#[test]
fn test_vm_jit_ldabsh() {
let prog = &[
Expand All @@ -378,7 +378,7 @@ fn test_vm_jit_ldabsh() {
};
}

#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
#[test]
fn test_vm_jit_ldabsw() {
let prog = &[
Expand All @@ -398,7 +398,7 @@ fn test_vm_jit_ldabsw() {
};
}

#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
#[test]
fn test_vm_jit_ldabsdw() {
let prog = &[
Expand Down Expand Up @@ -448,7 +448,7 @@ fn test_vm_err_ldabsb_nomem() {
// Memory check not implemented for JIT yet.
}

#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
#[test]
fn test_vm_jit_ldindb() {
let prog = &[
Expand All @@ -469,7 +469,7 @@ fn test_vm_jit_ldindb() {
};
}

#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
#[test]
fn test_vm_jit_ldindh() {
let prog = &[
Expand All @@ -490,7 +490,7 @@ fn test_vm_jit_ldindh() {
};
}

#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
#[test]
fn test_vm_jit_ldindw() {
let prog = &[
Expand All @@ -511,7 +511,7 @@ fn test_vm_jit_ldindw() {
};
}

#[cfg(not(windows))]
#[cfg(all(not(windows), feature = "std"))]
#[test]
fn test_vm_jit_ldinddw() {
let prog = &[
Expand Down
2 changes: 1 addition & 1 deletion tests/ubpf_jit_x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// These are unit tests for the eBPF JIT compiler.

#![cfg_attr(feature = "cargo-clippy", allow(clippy::unreadable_literal))]
#![cfg(not(windows))]
#![cfg(all(not(windows), feature = "std"))]

extern crate rbpf;
mod common;
Expand Down

0 comments on commit 9061ae8

Please sign in to comment.