insn.bend checks
raw source on the hub · import bend-osdev-bpf@0.1.0.0/insn.bend as Insn
bend-osdev-bpf: classic BPF instructions, raw and decoded.
A classic BPF program is a list of struct sock_filter {code: u16, jt: u8, jf: u8, k: u32} (the BSD bpf(4) manual page; McCanne and Jacobson, "The BSD Packet Filter", USENIX 1993). BpfRaw is that struct, field for field. BpfOp is the same instruction decoded into what it does, one constructor per instruction and addressing mode, so code that builds or inspects a program never handles opcode bits.
The opcode is class | size | mode for loads, class | op | source for ALU and jumps, class | rval for returns: class LD 0, LDX 1, ST 2, STX 3, ALU 4, JMP 5, RET 6, MISC 7 size W 0x00, H 0x08, B 0x10 mode IMM 0x00, ABS 0x20, IND 0x40, MEM 0x60, LEN 0x80, MSH 0xa0 ALU ADD 0x00, SUB 0x10, MUL 0x20, DIV 0x30, OR 0x40, AND 0x50, LSH 0x60, RSH 0x70, NEG 0x80, MOD 0x90, XOR 0xa0 JMP JA 0x00, JEQ 0x10, JGT 0x20, JGE 0x30, JSET 0x40 source K 0x00, X 0x08; rval K 0x00, A 0x10; MISC TAX 0x00, TXA 0x80
2 imports
import Base import bend-osdev-bytes@0.1.0.0/bytes.bend as B
Types
type BpfSize source · line 24 · raw
Data
BpfWBpfSize
BpfHBpfSize
BpfBBpfSize
type BpfAluOp source · line 29 · raw
Data
BpfAddBpfAluOp
BpfSubBpfAluOp
BpfMulBpfAluOp
BpfDivBpfAluOp
BpfOrBpfAluOp
BpfAndBpfAluOp
BpfLshBpfAluOp
BpfRshBpfAluOp
BpfModBpfAluOp
BpfXorBpfAluOp
type BpfCond source · line 41 · raw
Data
BpfJeqBpfCond
BpfJgtBpfCond
BpfJgeBpfCond
BpfJsetBpfCond
type BpfSrc source · line 48 · raw
Data
the second operand of an ALU or jump instruction: the constant k, or X
BpfK@k:U32 -> BpfSrc
BpfXBpfSrc
type BpfRval source · line 53 · raw
Data
what a return instruction answers: the constant k, or A
BpfRetK@k:U32 -> BpfRval
BpfRetABpfRval
type BpfOp source · line 59 · raw
Data
A decoded instruction. Jump offsets count the instructions skipped: a jump at index i by n goes to index i + 1 + n, so jumps only move forward.
BpfLdAbs@size:BpfSize -> @k:U32 -> BpfOp
BpfLdInd@size:BpfSize -> @k:U32 -> BpfOp
BpfLdImm@k:U32 -> BpfOp
BpfLdLenBpfOp
BpfLdMem@k:U32 -> BpfOp
BpfLdxImm@k:U32 -> BpfOp
BpfLdxMem@k:U32 -> BpfOp
BpfLdxLenBpfOp
BpfLdxMsh@k:U32 -> BpfOp
BpfSt@k:U32 -> BpfOp
BpfStx@k:U32 -> BpfOp
BpfAlu@op:BpfAluOp -> @src:BpfSrc -> BpfOp
BpfNegBpfOp
BpfJa@k:U32 -> BpfOp
BpfJmp@cond:BpfCond -> @src:BpfSrc -> @jt:U32 -> @jf:U32 -> BpfOp
BpfRet@rval:BpfRval -> BpfOp
BpfTaxBpfOp
BpfTxaBpfOp
type BpfRaw source · line 80 · raw
Data
struct sock_filter; code is 16 bits and jt, jf are 8 bits when stored
BpfRaw@code:U32 -> @jt:U32 -> @jf:U32 -> @k:U32 -> BpfRaw
type BpfOrder source · line 84 · raw
Data
the byte order of a stored program, and of a filter's loads
BpfBigBpfOrder
BpfLittleBpfOrder
type BpfError source · line 91 · raw
Data
Every failure: decoding (BadCode, Truncated, StoredTooLong), validation (Empty to SeccompInsn), installing seccomp filters (PathTooLong), and running a program that was not validated (FellOff, BadScratch).
BpfErrBadCode@pc:Nat -> @code:U32 -> BpfError
BpfErrTruncated@len:Nat -> BpfError
BpfErrStoredTooLong@max:Nat -> BpfError
BpfErrEmptyBpfError
BpfErrTooLong@len:Nat -> BpfError
BpfErrJump@pc:Nat -> BpfError
BpfErrJumpField@pc:Nat -> BpfError
BpfErrNoReturn@pc:Nat -> BpfError
BpfErrScratch@pc:Nat -> BpfError
BpfErrDivZero@pc:Nat -> BpfError
BpfErrMemInit@pc:Nat -> BpfError
BpfErrSeccompLoad@pc:Nat -> BpfError
BpfErrSeccompInsn@pc:Nat -> BpfError
BpfErrPathTooLong@total:Nat -> BpfError
BpfErrFellOffBpfError
BpfErrBadScratch@k:U32 -> BpfError
Definitions
def bpf_memwords source · line 110 · raw
U32
BPF_MEMWORDS: the scratch memory is M[0] to M[15]
def bpf_stmt source · line 115 · raw
@code:U32 -> @k:U32 -> BpfRaw
def bpf_jump source · line 118 · raw
@code:U32 -> @k:U32 -> @jt:U32 -> @jf:U32 -> BpfRaw
def BpfRaw.code source · line 121 · raw
@r:BpfRaw -> U32
def BpfSize.bits source · line 128 · raw
@s:BpfSize -> U32
def BpfAluOp.bits source · line 137 · raw
@o:BpfAluOp -> U32
def BpfCond.bits source · line 160 · raw
@c:BpfCond -> U32
def BpfSrc.bits source · line 171 · raw
@s:BpfSrc -> U32
def BpfSrc.k source · line 178 · raw
@s:BpfSrc -> U32
def BpfRval.encode source · line 185 · raw
@r:BpfRval -> BpfRaw
def BpfOp.encode source · line 193 · raw
@op:BpfOp -> BpfRaw
the sock_filter of an instruction; unused fields are 0
def BpfRaw.decode.pick source · line 234 · raw
@hit:Bool -> @op:BpfOp -> @rest:Maybe<&2, BpfOp> -> Maybe<&2, BpfOp>
def BpfRaw.decode source · line 243 · raw
@r:BpfRaw -> Maybe<&2, BpfOp>
the instruction a sock_filter holds, or None for an opcode classic BPF does not define; fields the instruction does not use are ignored
def bpf_encode.ref source · line 302 · raw
@ops:List<&2, BpfOp> -> List<&2, BpfRaw>
def bpf_encode.go source · line 310 · raw
@ops:List<&2, BpfOp> -> @acc:List<&2, BpfRaw> -> List<&2, BpfRaw>
the encoded instructions, last first, onto acc
def bpf_encode source · line 317 · raw
@ops:List<&2, BpfOp> -> List<&2, BpfRaw>
def bpf_decode.cons source · line 320 · raw
@m:Maybe<&2, BpfOp> -> @e:BpfError -> @rest:Result<&2, &2, BpfError, List<&2, BpfOp>> -> Result<&2, &2, BpfError, List<&2, BpfOp>>
def bpf_decode.ref.go source · line 331 · raw
@rs:List<&2, BpfRaw> -> @+pc:Nat -> Result<&2, &2, BpfError, List<&2, BpfOp>>
def bpf_decode.ref source · line 338 · raw
@rs:List<&2, BpfRaw> -> Result<&2, &2, BpfError, List<&2, BpfOp>>
def bpf_decode.push source · line 342 · raw
@m:Maybe<&2, BpfOp> -> @e:BpfError -> @acc:List<&2, BpfOp> -> Result<&2, &2, BpfError, List<&2, BpfOp>>
one decoded instruction pushed onto the instructions so far (last first)
def bpf_decode.go source · line 351 · raw
@rs:List<&2, BpfRaw> -> @+pc:Nat -> @a:Result<&2, &2, BpfError, List<&2, BpfOp>> -> Result<&2, &2, BpfError, List<&2, BpfOp>>
a: the instructions before pc, last first, or the first failure; the loop stops at the instruction after a failure
def bpf_decode source · line 367 · raw
@rs:List<&2, BpfRaw> -> Result<&2, &2, BpfError, List<&2, BpfOp>>
every instruction decoded, or the first one that is not classic BPF
def BpfRaw.to_octets source · line 372 · raw
@order:BpfOrder -> @r:BpfRaw -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
def bpf_to_octets.go source · line 384 · raw
@rs:List<&2, BpfRaw> -> @+order:BpfOrder -> @acc:List<&2, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets> -> List<&2, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
each instruction's 8 bytes, last first, onto acc
def bpf_to_octets source · line 391 · raw
@+order:BpfOrder -> @rs:List<&2, BpfRaw> -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
def bpf_field source · line 394 · raw
@m:Maybe<&2, U32> -> U32
def BpfRaw.of_octets source · line 397 · raw
@order:BpfOrder -> @+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> BpfRaw
def bpf_of_octets.go source · line 405 · raw
@n:Nat -> @+order:BpfOrder -> @+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @acc:List<&2, BpfRaw> -> List<&2, BpfRaw>
the next n instructions, onto acc (last first), then all of them in order
def bpf_of_octets.rem source · line 412 · raw
@r:Nat -> @q:Nat -> @+order:BpfOrder -> @+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @+len:Nat -> Result<&2, &2, BpfError, List<&2, BpfRaw>>
def bpf_of_octets.fin source · line 419 · raw
@+order:BpfOrder -> @+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @+len:Nat -> @qr:Pair(Nat, Nat) -> Result<&2, &2, BpfError, List<&2, BpfRaw>>
def bpf_of_octets.fits source · line 424 · raw
@over:Maybe<&2, U32> -> @max:Nat -> @+order:BpfOrder -> @+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, BpfError, List<&2, BpfRaw>>
over: the input's byte at offset max, if it has one
def bpf_octets_max source · line 434 · raw
Nat
8 * BPF_MAXINSNS: the bytes of the longest program validation accepts by default (LAWS.bend: octets_max_default ties it to bpf_maxinsns)
def bpf_of_octets_with source · line 440 · raw
@+max:Nat -> @+order:BpfOrder -> @+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, BpfError, List<&2, BpfRaw>>
a stored program's instructions: at most max bytes, a multiple of 8. The limit is checked first, by looking for the byte at offset max, so a longer input costs max steps whatever its length
def bpf_of_octets source · line 444 · raw
@+order:BpfOrder -> @o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, BpfError, List<&2, BpfRaw>>
the same, with the default limit, bpf_octets_max()
def BpfError.show source · line 449 · raw
@e:BpfError -> String