From 4812c52fe1009ee1e3c2307662d175bdd07b5c71 Mon Sep 17 00:00:00 2001 From: Afonso Bordado Date: Thu, 27 Jul 2023 19:45:42 +0100 Subject: [PATCH] cranelift: 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 | 11 +++-------- tests/cranelift.rs | 34 +++++++++++++--------------------- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/cranelift.rs b/src/cranelift.rs index f43ce0c7..7ea26908 100644 --- a/src/cranelift.rs +++ b/src/cranelift.rs @@ -12,7 +12,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, @@ -29,12 +29,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 @@ -84,7 +78,8 @@ 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); diff --git a/tests/cranelift.rs b/tests/cranelift.rs index cfb164d6..d0175219 100644 --- a/tests/cranelift.rs +++ b/tests/cranelift.rs @@ -2121,8 +2121,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); @@ -2138,8 +2137,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); @@ -2155,10 +2153,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); } @@ -2172,10 +2169,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); } @@ -2190,11 +2186,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] @@ -2208,8 +2203,7 @@ 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); @@ -2226,10 +2220,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); } @@ -2244,9 +2237,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); }