Function solana_rbpf::disassembler::to_insn_vec[][src]

pub fn to_insn_vec(prog: &[u8]) -> Vec<HlInsn>

Return a vector of struct HlInsn built from an eBPF program.

This is made public to provide a way to manipulate a program as a vector of instructions, in a high-level format, for example for dumping the program instruction after instruction with a custom format.

Note that the two parts of LD_DW_IMM instructions (that have the size of two standard instructions) are considered as making a single immediate value. As a consequence, the number of instructions stored in the vector may not be equal to the size in bytes of the program divided by the length of an instructions.

To do so, the immediate value operand is stored as an i64 instead as an i32, so be careful when you use it (see example examples/to_json.rs).

This is to oppose to ebpf::to_insn_vec() function, that treats instructions on a low-level ground and do not merge the parts of LD_DW_IMM. Also, the version in ebpf module does not use names or descriptions when storing the instructions.

Examples

use solana_rbpf::disassembler;

let prog = &[
    0x18, 0x00, 0x00, 0x00, 0x88, 0x77, 0x66, 0x55,
    0x00, 0x00, 0x00, 0x00, 0x44, 0x33, 0x22, 0x11,
    0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
];

let v = disassembler::to_insn_vec(prog);
assert_eq!(v, vec![
    disassembler::HlInsn {
        ptr: 0,
        opc: 0x18,
        name: "lddw".to_string(),
        desc: "lddw r0, 0x1122334455667788".to_string(),
        dst: 0,
        src: 0,
        off: 0,
        imm: 0x1122334455667788
    },
    disassembler::HlInsn {
        ptr: 2,
        opc: 0x95,
        name: "exit".to_string(),
        desc: "exit".to_string(),
        dst: 0,
        src: 0,
        off: 0,
        imm: 0
    },
]);