mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
ctx_field_into_slots.py stores 8-, 32- and 64-bit sk_buff fields into locals declared at every width and into struct fields of every width, with IR assertions on the slot widths and the truncation before each store. It is the compact form of a 52-case matrix (four field widths, nine local and four struct-field destinations) compiled on master and here: every case compiles, nothing sign-extends a field on the way in, and where master compiled the IR is identical except where master put a narrow local in a 64-bit slot. c_bool.py covers the local, struct field, map value and global. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
50 lines
981 B
Python
50 lines
981 B
Python
# c_bool is C's _Bool: a value narrows to it by comparing with zero (5 is
|
|
# true), and it widens to 0 or 1 (zero-extended, never sign-extended), as a
|
|
# local, a struct field, a map value and a global. In BTF it is a one-byte
|
|
# _Bool, so the map's value size is 1.
|
|
from ctypes import c_bool, c_int64, c_uint32, c_void_p
|
|
from pythonbpf import bpf, map, struct, section, bpfglobal, compile
|
|
from pythonbpf.maps import HashMap
|
|
|
|
|
|
@bpf
|
|
@struct
|
|
class flags:
|
|
on: c_bool
|
|
n: c_uint32
|
|
|
|
|
|
@bpf
|
|
@map
|
|
def seen() -> HashMap:
|
|
return HashMap(key=c_uint32, value=c_bool, max_entries=4)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def armed() -> c_bool:
|
|
return c_bool(1)
|
|
|
|
|
|
@bpf
|
|
@section("tracepoint/raw_syscalls/sys_enter")
|
|
def prog(ctx: c_void_p) -> c_int64:
|
|
global armed
|
|
b = c_bool(5)
|
|
f = flags()
|
|
f.on = 2
|
|
k = c_uint32(1)
|
|
seen.update(k, c_bool(1))
|
|
armed = 7
|
|
print(f"{b} {f.on} {armed}")
|
|
return c_int64(b)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def LICENSE() -> str:
|
|
return "GPL"
|
|
|
|
|
|
compile()
|