瀏覽代碼

clippy clean

Jack May 6 年之前
父節點
當前提交
4bbf5f1811
共有 3 個文件被更改,包括 23 次插入10 次删除
  1. 2 2
      src/asm_parser.rs
  2. 8 7
      src/jit.rs
  3. 13 1
      src/lib.rs

+ 2 - 2
src/asm_parser.rs

@@ -110,7 +110,7 @@ fn format_error(error: &Error<char, &str>) -> String {
     }
 }
 
-fn format_parse_error(parse_error: ParseError<State<&str>>) -> String {
+fn format_parse_error(parse_error: &ParseError<State<&str>>) -> String {
     format!("Parse error at line {} column {}: {}",
             parse_error.position.line,
             parse_error.position.column,
@@ -123,7 +123,7 @@ fn format_parse_error(parse_error: ParseError<State<&str>>) -> String {
 pub fn parse(input: &str) -> Result<Vec<Instruction>, String> {
     match spaces().with(many(parser(instruction)).skip(eof())).parse(State::new(input)) {
         Ok((insts, _)) => Ok(insts),
-        Err(err) => Err(format_parse_error(err)),
+        Err(err) => Err(format_parse_error(&err)),
     }
 }
 

+ 8 - 7
src/jit.rs

@@ -80,7 +80,8 @@ macro_rules! emit_bytes {
         let size = mem::size_of::<$t>() as usize;
         assert!($jit.offset + size <= $jit.contents.len());
         unsafe {
-            let mut ptr = $jit.contents.as_ptr().offset($jit.offset as isize) as *mut $t;
+            #[allow(cast_ptr_alignment)]
+            let mut ptr = $jit.contents.as_ptr().add($jit.offset) as *mut $t;
             *ptr = $data as $t;
         }
         $jit.offset += size;
@@ -348,9 +349,9 @@ fn emit_store_imm32(jit: &mut JitMemory, size: OperandSize, dst: u8, offset: i32
 }
 
 #[inline]
-fn emit_call(jit: &mut JitMemory, target: i64) {
+fn emit_call(jit: &mut JitMemory, target: usize) {
     // TODO use direct call when possible
-    emit_load_imm(jit, RAX, target);
+    emit_load_imm(jit, RAX, target as i64);
     // callq *%rax
     emit1(jit, 0xff);
     emit1(jit, 0xd0);
@@ -439,7 +440,7 @@ impl<'a> JitMemory<'a> {
             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(mem::transmute(raw), num_pages * PAGE_SIZE);
+            contents = std::slice::from_raw_parts_mut(raw as *mut u8, num_pages * PAGE_SIZE);
         }
 
         JitMemory {
@@ -775,7 +776,7 @@ impl<'a> JitMemory<'a> {
                     if let Some(helper) = helpers.get(&(insn.imm as u32)) {
                         // We reserve RCX for shifts
                         emit_mov(self, R9, RCX);
-                        emit_call(self, *helper as i64);
+                        emit_call(self, *helper as usize);
                     } else {
                         panic!("[JIT] Error: unknown helper function (id: {:#x})",
                                insn.imm as u32);
@@ -832,7 +833,7 @@ impl<'a> JitMemory<'a> {
             pc as i64 // Just to prevent warnings
         };
         emit_mov(self, RCX, RDI); // muldivmod stored pc in RCX
-        emit_call(self, log as i64);
+        emit_call(self, log as usize);
         emit_load_imm(self, map_register(0), -1);
         emit_jmp(self, TARGET_PC_EXIT);
     }
@@ -850,7 +851,7 @@ impl<'a> JitMemory<'a> {
                 let offset_loc = jump.offset_loc as i32 + std::mem::size_of::<i32>() as i32;
                 let rel = &(target_loc as i32 - offset_loc) as *const i32;
 
-                let offset_ptr = self.contents.as_ptr().offset(jump.offset_loc as isize);
+                let offset_ptr = self.contents.as_ptr().add(jump.offset_loc);
 
                 libc::memcpy(offset_ptr as *mut libc::c_void, rel as *const libc::c_void,
                              std::mem::size_of::<i32>());

+ 13 - 1
src/lib.rs

@@ -16,8 +16,10 @@
 #![warn(missing_docs)]
 // There are unused mut warnings due to unsafe code.
 #![allow(unused_mut)]
+// Allows old-style clippy
+#![allow(renamed_and_removed_lints)]
 
-#![cfg_attr(feature = "cargo-clippy", allow(cast_lossless, doc_markdown, match_same_arms, unreadable_literal))]
+#![cfg_attr(feature = "cargo-clippy", allow(redundant_field_names, single_match, cast_lossless, doc_markdown, match_same_arms, unreadable_literal))]
 
 extern crate byteorder;
 extern crate combine;
@@ -318,21 +320,25 @@ impl<'a> EbpfVmMbuff<'a> {
 
                 // BPF_LDX class
                 ebpf::LD_B_REG   => reg[_dst] = unsafe {
+                    #[allow(cast_ptr_alignment)]
                     let x = (reg[_src] as *const u8).offset(insn.off as isize) as *const u8;
                     check_mem_load(x as u64, 1, insn_ptr);
                     *x as u64
                 },
                 ebpf::LD_H_REG   => reg[_dst] = unsafe {
+                    #[allow(cast_ptr_alignment)]
                     let x = (reg[_src] as *const u8).offset(insn.off as isize) as *const u16;
                     check_mem_load(x as u64, 2, insn_ptr);
                     *x as u64
                 },
                 ebpf::LD_W_REG   => reg[_dst] = unsafe {
+                    #[allow(cast_ptr_alignment)]
                     let x = (reg[_src] as *const u8).offset(insn.off as isize) as *const u32;
                     check_mem_load(x as u64, 4, insn_ptr);
                     *x as u64
                 },
                 ebpf::LD_DW_REG  => reg[_dst] = unsafe {
+                    #[allow(cast_ptr_alignment)]
                     let x = (reg[_src] as *const u8).offset(insn.off as isize) as *const u64;
                     check_mem_load(x as u64, 8, insn_ptr);
                     *x as u64
@@ -345,16 +351,19 @@ impl<'a> EbpfVmMbuff<'a> {
                     *x = insn.imm as u8;
                 },
                 ebpf::ST_H_IMM   => unsafe {
+                    #[allow(cast_ptr_alignment)]
                     let x = (reg[_dst] as *const u8).offset(insn.off as isize) as *mut u16;
                     check_mem_store(x as u64, 2, insn_ptr);
                     *x = insn.imm as u16;
                 },
                 ebpf::ST_W_IMM   => unsafe {
+                    #[allow(cast_ptr_alignment)]
                     let x = (reg[_dst] as *const u8).offset(insn.off as isize) as *mut u32;
                     check_mem_store(x as u64, 4, insn_ptr);
                     *x = insn.imm as u32;
                 },
                 ebpf::ST_DW_IMM  => unsafe {
+                    #[allow(cast_ptr_alignment)]
                     let x = (reg[_dst] as *const u8).offset(insn.off as isize) as *mut u64;
                     check_mem_store(x as u64, 8, insn_ptr);
                     *x = insn.imm as u64;
@@ -367,16 +376,19 @@ impl<'a> EbpfVmMbuff<'a> {
                     *x = reg[_src] as u8;
                 },
                 ebpf::ST_H_REG   => unsafe {
+                    #[allow(cast_ptr_alignment)]
                     let x = (reg[_dst] as *const u8).offset(insn.off as isize) as *mut u16;
                     check_mem_store(x as u64, 2, insn_ptr);
                     *x = reg[_src] as u16;
                 },
                 ebpf::ST_W_REG   => unsafe {
+                    #[allow(cast_ptr_alignment)]
                     let x = (reg[_dst] as *const u8).offset(insn.off as isize) as *mut u32;
                     check_mem_store(x as u64, 4, insn_ptr);
                     *x = reg[_src] as u32;
                 },
                 ebpf::ST_DW_REG  => unsafe {
+                    #[allow(cast_ptr_alignment)]
                     let x = (reg[_dst] as *const u8).offset(insn.off as isize) as *mut u64;
                     check_mem_store(x as u64, 8, insn_ptr);
                     *x = reg[_src] as u64;