mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
`v = m.lookup(k); v += 1` was rejected with "augmented assignment needs an integer target, got i64*": the Name branch took the local's slot type, the pointer, as the target type. `v = v + 1` already compiles, and to a rebinding: the value is read through the pointer, null-checked, and the local is pointed at its spare `v_tmp` slot holding the result; the map is not written. `v += 1` now does the same. The read is get_typed_operand's, so the operation takes the map's declared value type and sign (u64 >>= is an lshr); the write is a rebind_map_value_local helper, now shared with plain assignment so the two cannot drift apart. A struct-valued lookup local is refused with a pointer to its fields.
33 lines
719 B
Python
33 lines
719 B
Python
# Augmented assignment to a map-lookup local: `v += 1` is `v = v + 1`. It reads
|
|
# the value through the pointer (null-checked) and rebinds v to the result; the
|
|
# map itself is not written.
|
|
from pythonbpf import bpf, map, section, bpfglobal, compile
|
|
from pythonbpf.maps import HashMap
|
|
from ctypes import c_void_p, c_int64, c_uint64
|
|
|
|
|
|
@bpf
|
|
@map
|
|
def m() -> HashMap:
|
|
return HashMap(key=c_int64, value=c_uint64, max_entries=4)
|
|
|
|
|
|
@bpf
|
|
@section("tracepoint/raw_syscalls/sys_enter")
|
|
def prog(ctx: c_void_p) -> c_int64:
|
|
v = m.lookup(0)
|
|
if v:
|
|
v += 1
|
|
v >>= 1 # c_uint64 value: a logical shift
|
|
return v
|
|
return c_int64(0)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def LICENSE() -> str:
|
|
return "GPL"
|
|
|
|
|
|
compile()
|