mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
`s = m.lookup(k); s.count = 5` (and `s.count += 1`) compiled without an error to IR llc rejects: "invalid getelementptr indices" on `getelementptr inbounds i64*, i64** %s, i32 0, i32 0`. Both the assignment and augmented-assignment paths indexed into the local's own slot, which for a lookup or cast local holds a pointer to the struct, not the struct. Reads already got this right in access_struct_field. with_struct_field_ptr now resolves a field for a write the same way: directly for a struct local; for a pointer local, by loading the pointer, casting it to the struct type, and emitting the store only on the non-null path (emit_if_not_null, the write-side counterpart of _null_checked_operation). The write lands in the map entry itself, so `s.count += 1` is an in-place update with no update() call, and the maps/structs guide examples that did update(k, s) after a field assignment now just assign.
42 lines
875 B
Python
42 lines
875 B
Python
# Assignment to a field of a struct map value writes through the lookup
|
|
# pointer into the map, null-checked: `stats.count += 1` updates the entry in
|
|
# place, with no update() call. Also a sub-64-bit field and a plain store.
|
|
from pythonbpf import bpf, map, section, bpfglobal, compile, struct
|
|
from pythonbpf.maps import HashMap
|
|
from ctypes import c_void_p, c_int64, c_uint64, c_uint32
|
|
|
|
|
|
@bpf
|
|
@struct
|
|
class stats_t:
|
|
count: c_uint64
|
|
flags: c_uint32
|
|
|
|
|
|
@bpf
|
|
@map
|
|
def stats() -> HashMap:
|
|
return HashMap(key=c_uint32, value=stats_t, max_entries=16)
|
|
|
|
|
|
@bpf
|
|
@section("tracepoint/raw_syscalls/sys_enter")
|
|
def prog(ctx: c_void_p) -> c_int64:
|
|
k = c_uint32(0)
|
|
s = stats.lookup(k)
|
|
if s:
|
|
s.count += 1
|
|
s.flags = 3
|
|
s.flags <<= 1
|
|
return s.count
|
|
return c_int64(0)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def LICENSE() -> str:
|
|
return "GPL"
|
|
|
|
|
|
compile()
|