4
0
Эх сурвалжийг харах

src: update deprecated syntax

When compiling with the latest toolchain (currently 1.42 as nightly),
the compiler complains about some items being deprecated. Let's update
the syntax. This includes:

- Using "..=" instead of deprecated "..."
- Using "?" instead of deprecated "try!"
- Removing unnecessary parenthesis around types
- Using "mem::MaybeUninit instead of "mem::uninitialized()"

Not 100% sure of my changes for the pointers to "raw" in regard with the
mem::MaybeUninit change, but the JIT tests are all passing so presumably
I didn't get it all wrong.
Quentin Monnet 5 жил өмнө
parent
commit
88fd75eae6
5 өөрчлөгдсөн 14 нэмэгдсэн , 13 устгасан
  1. 3 3
      src/assembler.rs
  2. 7 6
      src/jit.rs
  3. 2 2
      src/lib.rs
  4. 1 1
      src/verifier.rs
  5. 1 1
      tests/assembler.rs

+ 3 - 3
src/assembler.rs

@@ -154,7 +154,7 @@ fn operands_tuple(operands: &[Operand]) -> Result<(Operand, Operand, Operand), S
 }
 
 fn encode(inst_type: InstructionType, opc: u8, operands: &[Operand]) -> Result<Insn, String> {
-    let (a, b, c) = try!(operands_tuple(operands));
+    let (a, b, c) = (operands_tuple(operands))?;
     match (inst_type, a, b, c) {
         (AluBinary, Register(dst), Register(src), Nil) => insn(opc | ebpf::BPF_X, dst, src, 0, 0),
         (AluBinary, Register(dst), Integer(imm), Nil) => insn(opc | ebpf::BPF_K, dst, 0, 0, imm),
@@ -236,8 +236,8 @@ fn assemble_internal(parsed: &[Instruction]) -> Result<Vec<Insn>, String> {
 ///     0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
 /// ```
 pub fn assemble(src: &str) -> Result<Vec<u8>, String> {
-    let parsed = try!(parse(src));
-    let insns = try!(assemble_internal(&parsed));
+    let parsed = (parse(src))?;
+    let insns = (assemble_internal(&parsed))?;
     let mut result: Vec<u8> = vec![];
     for insn in insns {
         result.extend_from_slice(&insn.to_array());

+ 7 - 6
src/jit.rs

@@ -437,13 +437,14 @@ struct JitMemory<'a> {
 impl<'a> JitMemory<'a> {
     fn new(num_pages: usize) -> JitMemory<'a> {
         let contents: &mut[u8];
+        let mut raw: mem::MaybeUninit<*mut libc::c_void> = mem::MaybeUninit::uninit();
         unsafe {
             let size = num_pages * PAGE_SIZE;
-            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);
-            std::ptr::write_bytes(raw, 0xc3, size);  // for now, prepopulate with 'RET' calls
-            contents = std::slice::from_raw_parts_mut(raw as *mut u8, num_pages * PAGE_SIZE);
+            libc::posix_memalign(raw.as_mut_ptr(), PAGE_SIZE, size);
+            libc::mprotect(*raw.as_mut_ptr(), size, libc::PROT_EXEC | libc::PROT_READ | libc::PROT_WRITE);
+            std::ptr::write_bytes(*raw.as_mut_ptr(), 0xc3, size);  // for now, prepopulate with 'RET' calls
+            contents = std::slice::from_raw_parts_mut(*raw.as_mut_ptr() as *mut u8, num_pages * PAGE_SIZE);
+            raw.assume_init();
         }
 
         JitMemory {
@@ -901,7 +902,7 @@ impl<'a> std::fmt::Debug for JitMemory<'a> {
 pub fn compile(prog: &[u8],
                helpers: &HashMap<u32, ebpf::Helper>,
                use_mbuff: bool, update_data_ptr: bool)
-    -> Result<(JitProgram), Error> {
+    -> Result<JitProgram, Error> {
 
     // TODO: check how long the page must be to be sure to support an eBPF program of maximum
     // possible length

+ 2 - 2
src/lib.rs

@@ -1536,7 +1536,7 @@ impl<'a> EbpfVmNoData<'a> {
     /// let res = vm.execute_program().unwrap();
     /// assert_eq!(res, 0x1122);
     /// ```
-    pub fn execute_program(&self) -> Result<(u64), Error> {
+    pub fn execute_program(&self) -> Result<u64, Error> {
         self.parent.execute_program(&mut [])
     }
 
@@ -1572,7 +1572,7 @@ impl<'a> EbpfVmNoData<'a> {
     ///     assert_eq!(res, 0x1122);
     /// }
     /// ```
-    pub unsafe fn execute_program_jit(&self) -> Result<(u64), Error> {
+    pub unsafe fn execute_program_jit(&self) -> Result<u64, Error> {
         self.parent.execute_program_jit(&mut [])
     }
 }

+ 1 - 1
src/verifier.rs

@@ -103,7 +103,7 @@ fn check_registers(insn: &ebpf::Insn, store: bool, insn_ptr: usize) -> Result<()
     }
 
     match (insn.dst, store) {
-        (0 ... 9, _) | (10, true) => Ok(()),
+        (0 ..= 9, _) | (10, true) => Ok(()),
         (10, false) => reject(format!("cannot write into register r10 (insn #{:?})", insn_ptr)),
         (_, _)      => reject(format!("invalid destination register (insn #{:?})", insn_ptr))
     }

+ 1 - 1
tests/assembler.rs

@@ -14,7 +14,7 @@ use rbpf::assembler::assemble;
 use rbpf::ebpf;
 
 fn asm(src: &str) -> Result<Vec<ebpf::Insn>, String> {
-    Ok(ebpf::to_insn_vec(&try!(assemble(src))))
+    Ok(ebpf::to_insn_vec(&(assemble(src))?))
 }
 
 fn insn(opc: u8, dst: u8, src: u8, off: i16, imm: i32) -> ebpf::Insn {