Explorar el Código

Add std feature for gating the jit code alongside windows cfg option.

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 <szymo.kubica@gmail.com>
SzymonKubica hace 1 año
padre
commit
9061ae823d
Se han modificado 7 ficheros con 44 adiciones y 43 borrados
  1. 2 1
      Cargo.toml
  2. 4 4
      README.md
  3. 6 6
      examples/rbpf_plugin.rs
  4. 2 2
      examples/uptime.rs
  5. 19 19
      src/lib.rs
  6. 10 10
      tests/misc.rs
  7. 1 1
      tests/ubpf_jit_x86_64.rs

+ 2 - 1
Cargo.toml

@@ -43,7 +43,8 @@ json = "0.11"
 hex = "0.4.3"
 
 [features]
-default = []
+default = ["std"]
+std = []
 cranelift = [
     "dep:cranelift-codegen",
     "dep:cranelift-frontend",

+ 4 - 4
README.md

@@ -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();
 
@@ -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();
 

+ 6 - 6
examples/rbpf_plugin.rs

@@ -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;
                 }
             },
@@ -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();

+ 2 - 2
examples/uptime.rs

@@ -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();
     }

+ 19 - 19
src/lib.rs

@@ -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;
 
@@ -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>,
@@ -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,
@@ -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,
@@ -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],
@@ -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,
@@ -884,11 +884,11 @@ 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);
@@ -896,7 +896,7 @@ impl<'a> EbpfVmFixedMbuff<'a> {
     /// ```
     // 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
@@ -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,
@@ -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)
@@ -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()
     }
@@ -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 [])
     }

+ 10 - 10
tests/misc.rs

@@ -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,
@@ -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 = &[
@@ -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 = &[
@@ -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 = &[
@@ -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 = &[
@@ -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 = &[
@@ -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 = &[
@@ -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 = &[
@@ -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 = &[
@@ -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 = &[

+ 1 - 1
tests/ubpf_jit_x86_64.rs

@@ -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;