bpfvm

Crates.iobpfvm
lib.rsbpfvm
version0.1.2
sourcesrc
created_at2022-10-16 01:01:16.075521
updated_at2022-10-28 23:09:03.971672
description`bpfvm` is a small BPF VM implementation and cBPF token 'assembler'. It is intended for testing cBPF functionality before deployment, e.g. seccomp BPF filters.
homepagehttps://github.com/tarka/libpledge/bpfvm/
repositoryhttps://github.com/tarka/libpledge/bpfvm/
max_upload_size
id689264
size48,302
Steve Smith (tarka)

documentation

README

bpfvm: A cBPF 'assembler' and virtual machine

bpfvm is a small BPF VM implementation and cBPF token 'assembler'. It is intended for testing cBPF functionality before deployment, e.g. seccomp BPF filters.

Example

// Simple BPF opcode list
//
#[test_log::test]
fn test_alu_mask() {
    let prog = vec![
        bpf_ld(Mode::ABS, 2*WORDS),
        bpf_stmt(libc::BPF_ALU | libc::BPF_AND | libc::BPF_K, 0xF0),
        bpf_ret(Src::Acc, 0),
    ];
    let mut vm = BpfVM::new(&prog).unwrap();

    let data = vec![0, 0, 0xFF, 0];
    let ret = vm.run(&data).unwrap();
    assert!(ret == 0xF0);

    let data = vec![0, 0, 0x80, 0];
    let ret = vm.run(&data).unwrap();
    assert!(ret == 0x80);
}

// With tokens and assembler...
//
#[test_log::test]
fn test_ld_gt_ret() {
    let asm = vec![
        Load(Mode::IMM, 99),
        Jump(JmpOp::JGT, 98, Some("ret_acc"), Some("ret999")),
        // Should skip this one
        Label("ret999"),
        Load(Mode::IMM, 999),
        Label("ret_acc"),
        Return(Src::Acc, 0),
    ];
    let prog = compile(&asm).unwrap();

    let mut vm = BpfVM::new(&prog).unwrap();
    let data = vec![];
    let ret = vm.run(&data).unwrap();
    assert!(ret == 99);
}

Commit count: 86

cargo fmt