Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ebpf::RETURN instruction #607

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions benches/vm_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn bench_jit_vs_interpreter_address_translation(bencher: &mut Bencher) {
mov r0, r1
and r0, 0xFFFFFF
jlt r0, 0x20000, -5
exit",
return",
Config::default(),
655361,
&mut [0; 0x20000],
Expand All @@ -161,15 +161,15 @@ static ADDRESS_TRANSLATION_STACK_CODE: &str = "
add r3, -1
ldxb r4, [r3]
add r2, 1
jlt r2, 0x10000, -8
exit";
jlt r2, 0x10000, -8";

#[cfg(all(feature = "jit", not(target_os = "windows"), target_arch = "x86_64"))]
#[bench]
fn bench_jit_vs_interpreter_address_translation_stack_fixed(bencher: &mut Bencher) {
let asm = format!("{ADDRESS_TRANSLATION_STACK_CODE}\nexit");
bench_jit_vs_interpreter(
bencher,
ADDRESS_TRANSLATION_STACK_CODE,
asm.as_str(),
Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
Expand All @@ -182,9 +182,10 @@ fn bench_jit_vs_interpreter_address_translation_stack_fixed(bencher: &mut Benche
#[cfg(all(feature = "jit", not(target_os = "windows"), target_arch = "x86_64"))]
#[bench]
fn bench_jit_vs_interpreter_address_translation_stack_dynamic(bencher: &mut Bencher) {
let asm = format!("{ADDRESS_TRANSLATION_STACK_CODE}\nreturn");
bench_jit_vs_interpreter(
bencher,
ADDRESS_TRANSLATION_STACK_CODE,
asm.as_str(),
Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V2,
..Config::default()
Expand All @@ -204,7 +205,7 @@ fn bench_jit_vs_interpreter_empty_for_loop(bencher: &mut Bencher) {
and r1, 1023
add r2, 1
jlt r2, 0x10000, -4
exit",
return",
Config::default(),
262145,
&mut [0; 0],
Expand Down Expand Up @@ -252,7 +253,7 @@ fn bench_jit_vs_interpreter_call_depth_dynamic(bencher: &mut Bencher) {
mov r1, 18
call function_foo
jlt r6, 1024, -4
exit
return
function_foo:
add r11, -4
stw [r10-4], 0x11223344
Expand All @@ -262,7 +263,7 @@ fn bench_jit_vs_interpreter_call_depth_dynamic(bencher: &mut Bencher) {
add r1, -1
call function_foo
add r11, 4
exit",
return",
Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V2,
..Config::default()
Expand Down
3 changes: 2 additions & 1 deletion src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ fn make_instruction_map(sbpf_version: &SBPFVersion) -> HashMap<String, (Instruct
};

// Miscellaneous.
entry("return", NoOperand, ebpf::RETURN);
entry("exit", NoOperand, ebpf::EXIT);
entry("ja", JumpUnconditional, ebpf::JA);
entry("syscall", Syscall, ebpf::CALL_IMM);
Expand Down Expand Up @@ -313,7 +314,7 @@ pub fn assemble<C: ContextObject>(
src: &str,
loader: Arc<BuiltinProgram<C>>,
) -> Result<Executable<C>, String> {
let sbpf_version = loader.get_config().enabled_sbpf_versions.end().clone();
let sbpf_version = *loader.get_config().enabled_sbpf_versions.end();

let statements = parse(src)?;
let instruction_map = make_instruction_map(&sbpf_version);
Expand Down
1 change: 1 addition & 0 deletions src/disassembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ pub fn disassemble_instruction<C: ContextObject>(
},
ebpf::CALL_REG => { name = "callx"; desc = format!("{} r{}", name, if sbpf_version.callx_uses_src_reg() { insn.src } else { insn.imm as u8 }); },
ebpf::EXIT => { name = "exit"; desc = name.to_string(); },
ebpf::RETURN => { name = "return"; desc = name.to_string(); },

_ => { name = "unknown"; desc = format!("{} opcode={:#x}", name, insn.opc); },
};
Expand Down
4 changes: 3 additions & 1 deletion src/ebpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,10 @@ pub const JSLE_REG: u8 = BPF_JMP | BPF_X | BPF_JSLE;
pub const CALL_IMM: u8 = BPF_JMP | BPF_CALL;
/// BPF opcode: tail call.
pub const CALL_REG: u8 = BPF_JMP | BPF_X | BPF_CALL;
/// BPF opcode: `exit` /// `return r0`.
/// BPF opcode: `exit` /// `return r0`. /// Valid only for SBPFv1
pub const EXIT: u8 = BPF_JMP | BPF_EXIT;
/// BPF opcode: `return` /// `return r0`. /// Valid only for SBPFv2
pub const RETURN: u8 = BPF_JMP | BPF_X | BPF_EXIT;

// Used in JIT
/// Mask to extract the operation class from an operation code.
Expand Down
8 changes: 7 additions & 1 deletion src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,13 @@ impl<'a, 'b, C: ContextObject> Interpreter<'a, 'b, C> {
}
}

ebpf::EXIT => {
ebpf::EXIT
| ebpf::RETURN => {
if (insn.opc == ebpf::EXIT && self.executable.get_sbpf_version().static_syscalls())
|| (insn.opc == ebpf::RETURN && !self.executable.get_sbpf_version().static_syscalls()) {
throw_error!(self, EbpfError::UnsupportedInstruction);
}

if self.vm.call_depth == 0 {
if config.enable_instruction_meter && self.vm.due_insn_count > self.vm.previous_instruction_meter {
throw_error!(self, EbpfError::ExceededMaxInstructions);
Expand Down
20 changes: 13 additions & 7 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,13 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
};
self.emit_internal_call(Value::Register(target_pc));
},
ebpf::EXIT => {
ebpf::EXIT
| ebpf::RETURN => {
if (insn.opc == ebpf::EXIT && self.executable.get_sbpf_version().static_syscalls())
|| (insn.opc == ebpf::RETURN && !self.executable.get_sbpf_version().static_syscalls()) {
return Err(EbpfError::UnsupportedInstruction);
}

self.emit_validate_instruction_count(true, Some(self.pc));

let call_depth_access = X86IndirectAccess::Offset(self.slot_in_vm(RuntimeEnvironmentSlot::CallDepth));
Expand All @@ -772,7 +778,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
// and return
self.emit_profile_instruction_count(false, Some(0));
self.emit_ins(X86Instruction::return_near());
},
}

_ => return Err(EbpfError::UnsupportedInstruction),
}
Expand Down Expand Up @@ -1736,7 +1742,7 @@ mod tests {
}

fn create_mockup_executable(config: Config, program: &[u8]) -> Executable<TestContextObject> {
let sbpf_version = config.enabled_sbpf_versions.end().clone();
let sbpf_version = *config.enabled_sbpf_versions.clone().end();
let mut function_registry =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry
Expand Down Expand Up @@ -1769,7 +1775,7 @@ mod tests {
let empty_program_machine_code_length = {
let config = Config {
noop_instruction_rate: 0,
enabled_sbpf_versions: sbpf_version.clone()..=sbpf_version.clone(),
enabled_sbpf_versions: sbpf_version..=sbpf_version,
..Config::default()
};
let mut executable = create_mockup_executable(config, &prog[0..0]);
Expand All @@ -1780,7 +1786,7 @@ mod tests {
.machine_code_length()
};
assert!(empty_program_machine_code_length <= MAX_EMPTY_PROGRAM_MACHINE_CODE_LENGTH);
empty_program_machine_code_length_per_version[sbpf_version.clone() as usize] =
empty_program_machine_code_length_per_version[sbpf_version as usize] =
empty_program_machine_code_length;
}

Expand Down Expand Up @@ -1814,7 +1820,7 @@ mod tests {

for sbpf_version in [SBPFVersion::V1, SBPFVersion::V2] {
let empty_program_machine_code_length =
empty_program_machine_code_length_per_version[sbpf_version.clone() as usize];
empty_program_machine_code_length_per_version[sbpf_version as usize];

for mut opcode in 0x00..=0xFF {
let (registers, immediate) = match opcode {
Expand All @@ -1841,7 +1847,7 @@ mod tests {
}
let config = Config {
noop_instruction_rate: 0,
enabled_sbpf_versions: sbpf_version.clone()..=sbpf_version.clone(),
enabled_sbpf_versions: sbpf_version..=sbpf_version,
..Config::default()
};
let mut executable = create_mockup_executable(config, &prog);
Expand Down
2 changes: 1 addition & 1 deletion src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use {
};

/// Defines a set of sbpf_version of an executable
#[derive(Debug, PartialEq, PartialOrd, Eq, Clone)]
#[derive(Debug, PartialEq, PartialOrd, Eq, Clone, Copy)]
pub enum SBPFVersion {
/// The legacy format
V1,
Expand Down
5 changes: 3 additions & 2 deletions src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl Verifier for RequisiteVerifier {
function_range.end = *function_iter.peek().unwrap_or(&program_range.end);
let insn = ebpf::get_insn(prog, function_range.end.saturating_sub(1));
match insn.opc {
ebpf::JA | ebpf::EXIT => {},
ebpf::JA | ebpf::RETURN => {},
_ => return Err(VerifierError::InvalidFunction(
function_range.end.saturating_sub(1),
)),
Expand Down Expand Up @@ -390,7 +390,8 @@ impl Verifier for RequisiteVerifier {
ebpf::CALL_IMM if sbpf_version.static_syscalls() && insn.src == 0 => { check_call_target(insn.imm as u32, syscall_registry)?; },
ebpf::CALL_IMM => {},
ebpf::CALL_REG => { check_callx_register(&insn, insn_ptr, sbpf_version)?; },
ebpf::EXIT => {},
ebpf::EXIT if !sbpf_version.static_syscalls() => {},
ebpf::RETURN if sbpf_version.static_syscalls() => {},

_ => {
return Err(VerifierError::UnknownOpCode(insn.opc, insn_ptr));
Expand Down
2 changes: 1 addition & 1 deletion src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub struct CallFrame {
/// };
///
/// let prog = &[
/// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
/// 0x9d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
/// ];
/// let mem = &mut [
/// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
Expand Down
6 changes: 3 additions & 3 deletions test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub const PROG_TCP_PORT_80: &str = "
ldxh r1, [r1+0x0]
jne r1, 0x5000, +0x1
mov64 r0, 0x1
exit";
return";

pub const TCP_SACK_ASM: &str = "
ldxb r2, [r1+12]
Expand Down Expand Up @@ -84,7 +84,7 @@ pub const TCP_SACK_ASM: &str = "
jsgt r2, r3, -18
ja +1
mov r0, 0x1
exit";
return";

pub const TCP_SACK_BIN: [u8; 352] = [
0x2c, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, //
Expand Down Expand Up @@ -130,7 +130,7 @@ pub const TCP_SACK_BIN: [u8; 352] = [
0x6d, 0x32, 0xee, 0xff, 0x00, 0x00, 0x00, 0x00, //
0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, //
0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, //
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x9d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
];

pub const TCP_SACK_MATCH: [u8; 78] = [
Expand Down
5 changes: 5 additions & 0 deletions tests/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ fn test_exit() {
assert_eq!(asm("exit"), Ok(vec![insn(0, ebpf::EXIT, 0, 0, 0, 0)]));
}

#[test]
fn test_return() {
assert_eq!(asm("return"), Ok(vec![insn(0, ebpf::RETURN, 0, 0, 0, 0)]));
}

// Example for InstructionType::AluBinary.
#[test]
fn test_add64() {
Expand Down
5 changes: 5 additions & 0 deletions tests/disassembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ fn test_exit() {
disasm!("entrypoint:\n exit\n");
}

#[test]
fn test_return() {
disasm!("entrypoint:\n return\n");
}

// Example for InstructionType::AluBinary.
#[test]
fn test_add64() {
Expand Down
Loading