Skip to content

Commit

Permalink
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 <[email protected]>
  • Loading branch information
afonso360 committed Aug 13, 2023
1 parent 602c1fa commit 1bb71ae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 33 deletions.
13 changes: 2 additions & 11 deletions src/cranelift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -284,16 +278,13 @@ 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,
ebpf::LD_ABS_W | ebpf::LD_IND_W => I32,
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);
Expand Down
36 changes: 14 additions & 22 deletions tests/cranelift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}

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

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

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

0 comments on commit 1bb71ae

Please sign in to comment.