use bpf_ins::{Instruction, Opcode};
type Optimizer = fn(&mut &[Instruction], &mut Vec<Instruction>) -> bool;
fn optimize_mov_add_load(inp: &mut &[Instruction], out: &mut Vec<Instruction>) -> bool {
const NEEDED: usize = 3;
if inp.len() < NEEDED {
return false;
}
let (ins, rem) = inp.split_at(NEEDED);
let load_size = if let Opcode::Memory(memory) = ins[2].get_opcode() {
*memory.get_size()
} else {
return false;
};
let offset1: i16 = match ins[1].get_imm().try_into() {
Ok(offset) => offset,
Err(_) => return false,
};
let check0 = Instruction::movx64(ins[0].get_dst_reg(), ins[0].get_src_reg());
let check1 = Instruction::add64(ins[1].get_dst_reg(), offset1.into());
let check2 = Instruction::loadx(ins[2].get_dst_reg(), ins[2].get_src_reg(), 0, load_size);
if check0 != ins[0] || check1 != ins[1] || check2 != ins[2] {
return false;
}
*inp = rem;
out.push(Instruction::loadx(
ins[0].get_dst_reg(),
ins[0].get_src_reg(),
offset1,
load_size,
));
true
}
fn optimize_add_load(inp: &mut &[Instruction], out: &mut Vec<Instruction>) -> bool {
const NEEDED: usize = 2;
if inp.len() < NEEDED {
return false;
}
let (ins, rem) = inp.split_at(NEEDED);
let load_size = if let Opcode::Memory(memory) = ins[1].get_opcode() {
*memory.get_size()
} else {
return false;
};
let offset0: i16 = match ins[0].get_imm().try_into() {
Ok(offset) => offset,
Err(_) => return false,
};
let check0 = Instruction::add64(ins[0].get_dst_reg(), offset0.into());
let check1 = Instruction::loadx(ins[1].get_dst_reg(), ins[1].get_src_reg(), 0, load_size);
if check0 != ins[0] || check1 != ins[1] {
return false;
}
*inp = rem;
out.push(Instruction::loadx(
ins[0].get_dst_reg(),
ins[0].get_dst_reg(),
offset0,
load_size,
));
true
}
fn no_optimization(inp: &mut &[Instruction], out: &mut Vec<Instruction>) -> bool {
let (ins, rem) = match inp.split_first() {
Some((ins, rem)) => (ins, rem),
None => return false,
};
out.push(*ins);
*inp = rem;
true
}
static OPTIMIZERS: [Optimizer; 3] = [optimize_mov_add_load, optimize_add_load, no_optimization];
pub fn optimize(mut instructions: &[Instruction]) -> Vec<Instruction> {
let mut optimized = vec![];
let instructions = &mut instructions;
while !instructions.is_empty() {
for optimizer in OPTIMIZERS {
optimizer(instructions, &mut optimized);
}
}
optimized
}