Browse Source

src/assembler.rs: disallow positive immediates with the high bit set

Rich Lane 8 năm trước cách đây
mục cha
commit
f1ff025735
3 tập tin đã thay đổi với 4 bổ sung4 xóa
  1. 1 1
      src/assembler.rs
  2. 2 2
      tests/assembler.rs
  3. 1 1
      tests/common.rs

+ 1 - 1
src/assembler.rs

@@ -126,7 +126,7 @@ fn insn(opc: u8, dst: i64, src: i64, off: i64, imm: i64) -> Result<Insn, String>
     if off < -32768 || off >= 32768 {
         return Err(format!("Invalid offset {}", off));
     }
-    if imm < -2147483648 || imm >= 4294967296 {
+    if imm < -2147483648 || imm >= 2147483648 {
         return Err(format!("Invalid immediate {}", imm));
     }
     Ok(Insn {

+ 2 - 2
tests/assembler.rs

@@ -428,8 +428,6 @@ fn test_endian() {
 fn test_large_immediate() {
     assert_eq!(asm("add64 r1, 2147483647"),
                Ok(vec![insn(ebpf::ADD64_IMM, 1, 0, 0, 2147483647)]));
-    assert_eq!(asm("add64 r1, 0xffffffff"),
-               Ok(vec![insn(ebpf::ADD64_IMM, 1, 0, 0, -1)]));
     assert_eq!(asm("add64 r1, -2147483648"),
                Ok(vec![insn(ebpf::ADD64_IMM, 1, 0, 0, -2147483648)]));
 }
@@ -469,6 +467,8 @@ fn test_error_operands_out_of_range() {
                Err("Failed to encode ja: Invalid offset 32768".to_string()));
     assert_eq!(asm("add r1, 4294967296"),
                Err("Failed to encode add: Invalid immediate 4294967296".to_string()));
+    assert_eq!(asm("add r1, 2147483648"),
+               Err("Failed to encode add: Invalid immediate 2147483648".to_string()));
     assert_eq!(asm("add r1, -2147483649"),
                Err("Failed to encode add: Invalid immediate -2147483649".to_string()));
 }

+ 1 - 1
tests/common.rs

@@ -29,7 +29,7 @@ pub const TCP_SACK_ASM: &'static str = "
     rsh r4, 0x2
     and r4, 0x3c
     mov r2, r4
-    add r2, 0xffffffec
+    add r2, -20
     mov r5, 0x15
     mov r3, 0x0
     jgt r5, r4, +20