Quellcode durchsuchen

src/(lib,jit).rs: use functions from std::ptr when relevant

We have functions for null pointers and for calling memset() in std::ptr
module, better call them than resort on libc or hand-made null pointers…
Quentin Monnet vor 8 Jahren
Ursprung
Commit
e6d68af67a
2 geänderte Dateien mit 9 neuen und 12 gelöschten Zeilen
  1. 1 4
      src/jit.rs
  2. 8 8
      src/lib.rs

+ 1 - 4
src/jit.rs

@@ -19,9 +19,6 @@ use ebpf;
 
 extern crate libc;
 
-extern {
-    fn memset(s: *mut libc::c_void, c: libc::uint32_t, n: libc::size_t) -> *mut libc::c_void;
-}
 const PAGE_SIZE: usize = 4096;
 
 // Special values for target_pc in struct Jump
@@ -438,7 +435,7 @@ impl<'a> JitMemory<'a> {
             let mut raw: *mut libc::c_void = mem::uninitialized();
             libc::posix_memalign(&mut raw, PAGE_SIZE, size);
             libc::mprotect(raw, size, libc::PROT_EXEC | libc::PROT_READ | libc::PROT_WRITE);
-            memset(raw, 0xc3, size);  // for now, prepopulate with 'RET' calls
+            std::ptr::write_bytes(raw, 0xc3, size);  // for now, prepopulate with 'RET' calls
             contents = std::slice::from_raw_parts_mut(mem::transmute(raw), num_pages * PAGE_SIZE);
         }
 

+ 8 - 8
src/lib.rs

@@ -567,11 +567,11 @@ impl<'a> EbpfVmMbuff<'a> {
     /// ```
     pub unsafe fn prog_exec_jit(&self, mem: &mut [u8], mbuff: &'a mut [u8]) -> u64 {
         // If packet data is empty, do not send the address of an empty slice; send a null pointer
-        // (zero value) as first argument instead, as this is uBPF's behavior (empty packet should
-        // not happen in the kernel; anyway the verifier would prevent the use of uninitialized
-        // registers). See `mul_loop` test.
+        //  as first argument instead, as this is uBPF's behavior (empty packet should not happen
+        //  in the kernel; anyway the verifier would prevent the use of uninitialized registers).
+        //  See `mul_loop` test.
         let mem_ptr = match mem.len() {
-            0 => 0 as *mut u8,
+            0 => std::ptr::null_mut(),
             _ => mem.as_ptr() as *mut u8
         };
         // The last two arguments are not used in this function. They would be used if there was a
@@ -920,11 +920,11 @@ impl<'a> EbpfVmFixedMbuff<'a> {
     // associated with the fixed mbuff.
     pub unsafe fn prog_exec_jit(&mut self, mem: &'a mut [u8]) -> u64 {
         // If packet data is empty, do not send the address of an empty slice; send a null pointer
-        // (zero value) as first argument instead, as this is uBPF's behavior (empty packet should
-        // not happen in the kernel; anyway the verifier would prevent the use of uninitialized
-        // registers). See `mul_loop` test.
+        //  as first argument instead, as this is uBPF's behavior (empty packet should not happen
+        //  in the kernel; anyway the verifier would prevent the use of uninitialized registers).
+        //  See `mul_loop` test.
         let mem_ptr = match mem.len() {
-            0 => 0 as *mut u8,
+            0 => std::ptr::null_mut(),
             _ => mem.as_ptr() as *mut u8
         };
         (self.parent.jit)(self.mbuff.buffer.as_ptr() as *mut u8, self.mbuff.buffer.len(),