From 1bb71ae72195130792a08ce6df391b545de10197 Mon Sep 17 00:00:00 2001 From: Afonso Bordado Date: Thu, 27 Jul 2023 19:45:42 +0100 Subject: [PATCH] Use the default libcall names Some operations may not work in some older CPU's and may need to be lowered to a libcall. Signed-off-by: Afonso Bordado --- src/cranelift.rs | 13 ++----------- tests/cranelift.rs | 36 ++++++++++++++---------------------- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/cranelift.rs b/src/cranelift.rs index 983c67f57..c03c48563 100644 --- a/src/cranelift.rs +++ b/src/cranelift.rs @@ -8,7 +8,7 @@ use cranelift_codegen::{ ir::{ condcodes::IntCC, types::{I16, I32, I64, I8}, - AbiParam, Block, Endianness, FuncRef, Function, InstBuilder, LibCall, MemFlags, Signature, + AbiParam, Block, Endianness, FuncRef, Function, InstBuilder, MemFlags, Signature, SourceLoc, StackSlotData, StackSlotKind, TrapCode, Type, UserFuncName, Value, }, isa::OwnedTargetIsa, @@ -25,12 +25,6 @@ use crate::ebpf::{ use super::Error; -fn libcall_names(libcall: LibCall) -> String { - match libcall { - _ => unimplemented!(), - } -} - pub type JittedFunction = extern "C" fn( *mut u8, // mem_ptr usize, // mem_len @@ -80,7 +74,7 @@ impl CraneliftCompiler { .finish(settings::Flags::new(flag_builder)) .unwrap(); - let mut jit_builder = JITBuilder::with_isa(isa.clone(), Box::new(libcall_names)); + let mut jit_builder = JITBuilder::with_isa(isa.clone(), cranelift_module::default_libcall_names()); // Register all the helpers for (k, v) in helpers.iter() { let name = format!("helper_{}", k); @@ -284,7 +278,6 @@ impl CraneliftCompiler { | ebpf::LD_IND_H | ebpf::LD_IND_W | ebpf::LD_IND_DW => { - dbg!(&insn); let ty = match insn.opc { ebpf::LD_ABS_B | ebpf::LD_IND_B => I8, ebpf::LD_ABS_H | ebpf::LD_IND_H => I16, @@ -292,8 +285,6 @@ impl CraneliftCompiler { ebpf::LD_ABS_DW | ebpf::LD_IND_DW => I64, _ => unreachable!(), }; - dbg!(ty); - dbg!(insn.imm as u32 as i64); // Both instructions add the imm part of the instruction to the pointer let ptr = bcx.use_var(self.mem_start); diff --git a/tests/cranelift.rs b/tests/cranelift.rs index da1d4ff9a..d3203aec5 100644 --- a/tests/cranelift.rs +++ b/tests/cranelift.rs @@ -2118,8 +2118,7 @@ fn test_cranelift_ldabsb() { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, ]; - let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap(); - assert_eq!(vm.execute_program(mem).unwrap(), 0x33); + let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap(); vm.cranelift_compile().unwrap(); assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x33); @@ -2135,8 +2134,7 @@ fn test_cranelift_ldabsh() { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, ]; - let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap(); - assert_eq!(vm.execute_program(mem).unwrap(), 0x4433); + let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap(); vm.cranelift_compile().unwrap(); assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x4433); @@ -2152,10 +2150,9 @@ fn test_cranelift_ldabsw() { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, ]; - let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap(); - assert_eq!(vm.execute_program(mem).unwrap(), 0x66554433); - vm.cranelift_compile().unwrap(); + let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap(); + vm.cranelift_compile().unwrap(); assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x66554433); } @@ -2169,10 +2166,9 @@ fn test_cranelift_ldabsdw() { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, ]; - let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap(); - assert_eq!(vm.execute_program(mem).unwrap(), 0xaa99887766554433); - vm.cranelift_compile().unwrap(); + let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap(); + vm.cranelift_compile().unwrap(); assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0xaa99887766554433); } @@ -2187,11 +2183,10 @@ fn test_cranelift_ldindb() { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, ]; - let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap(); - assert_eq!(vm.execute_program(mem).unwrap(), 0x88); + let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap(); vm.cranelift_compile().unwrap(); - assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x88); + assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x88); } #[test] @@ -2205,11 +2200,10 @@ fn test_cranelift_ldindh() { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, ]; - let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap(); - assert_eq!(vm.execute_program(mem).unwrap(), 0x9988); + let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap(); vm.cranelift_compile().unwrap(); - assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x9988); + assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x9988); } #[test] @@ -2223,10 +2217,9 @@ fn test_cranelift_ldindw() { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, ]; - let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap(); - assert_eq!(vm.execute_program(mem).unwrap(), 0x88776655); - vm.cranelift_compile().unwrap(); + let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap(); + vm.cranelift_compile().unwrap(); assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x88776655); } @@ -2241,9 +2234,8 @@ fn test_cranelift_ldinddw() { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, ]; - let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap(); - assert_eq!(vm.execute_program(mem).unwrap(), 0xccbbaa9988776655); - vm.cranelift_compile().unwrap(); + let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap(); + vm.cranelift_compile().unwrap(); assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0xccbbaa9988776655); }