Skip to content

Commit

Permalink
cranelift: Use the default libcall names
Browse files Browse the repository at this point in the history
Some operations may not work in some older CPU's and may need to
be lowered to a libcall.

Signed-off-by: Afonso Bordado <afonsobordado@az8.co>
  • Loading branch information
afonso360 authored and qmonnet committed Sep 6, 2023
1 parent ea1c556 commit 4812c52
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 29 deletions.
11 changes: 3 additions & 8 deletions src/cranelift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
34 changes: 13 additions & 21 deletions tests/cranelift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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]
Expand All @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);
}

0 comments on commit 4812c52

Please sign in to comment.