Skip to content

Commit

Permalink
Call register uses imm (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmay authored May 14, 2019
1 parent 0063b44 commit 0cd7254
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn encode(inst_type: InstructionType, opc: u8, operands: &[Operand]) -> Result<I
insn(opc | ebpf::BPF_K, dst, 0, off, imm)
}
(CallImm, Integer(imm), Nil, Nil) => insn(opc, 0, 0, 0, imm),
(CallReg, Integer(src), Nil, Nil) => insn(opc, 0, src, 0, 0),
(CallReg, Integer(imm), Nil, Nil) => insn(opc, 0, 0, 0, imm),
(Endian(size), Register(dst), Nil, Nil) => insn(opc, dst, 0, 0, size),
(LoadImm, Register(dst), Integer(imm), Nil) => insn(opc, dst, 0, 0, (imm << 32) >> 32),
_ => Err(format!("Unexpected operands: {:?}", operands)),
Expand Down
2 changes: 1 addition & 1 deletion src/disassembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ pub fn to_insn_vec(prog: &[u8]) -> Vec<HLInsn> {
ebpf::JSLE_IMM => { name = "jsle"; desc = jmp_imm_str(name, &insn); },
ebpf::JSLE_REG => { name = "jsle"; desc = jmp_reg_str(name, &insn); },
ebpf::CALL_IMM => { name = "call"; desc = format!("{} {:#x}", name, insn.imm); },
ebpf::CALL_REG => { name = "callx"; desc = format!("{} {:#x}", name, insn.src); },
ebpf::CALL_REG => { name = "callx"; desc = format!("{} {:#x}", name, insn.imm); },
ebpf::EXIT => { name = "exit"; desc = name.to_string(); },

_ => {
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,8 +807,9 @@ impl<'a> EbpfVmMbuff<'a> {
ebpf::JSLE_REG => if (reg[_dst] as i64) <= reg[_src] as i64 { pc = (pc as i16 + insn.off) as usize; },

ebpf::CALL_REG => {
println!("CALL_REG R{:?}[{:?}]", _src, reg[_src]);
pc = reg[_src] as usize;
let base_address = &prog[0] as *const _ as usize;
let target_address = reg[insn.imm as usize] as usize;
pc = (target_address - base_address) / ebpf::INSN_SIZE;
},

// Do not delegate the check to the verifier, since registered functions can be
Expand Down
2 changes: 1 addition & 1 deletion tests/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn test_jeq() {

#[test]
fn test_call_reg() {
assert_eq!(asm("callx 3"), Ok(vec![insn(ebpf::CALL_REG, 0, 3, 0, 0)]));
assert_eq!(asm("callx 3"), Ok(vec![insn(ebpf::CALL_REG, 0, 0, 0, 3)]));
}

// Example for InstructionType::Call.
Expand Down

0 comments on commit 0cd7254

Please sign in to comment.