From 7af54df7c05936fe3460b0b6ac473261c997ad28 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Wed, 19 Nov 2025 00:17:01 +0530 Subject: [PATCH] Add passing test hash_map_struct.py for using structs as hashmap key/val types --- tests/passing_tests/hash_map_struct.py | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/passing_tests/hash_map_struct.py diff --git a/tests/passing_tests/hash_map_struct.py b/tests/passing_tests/hash_map_struct.py new file mode 100644 index 0000000..9f6cbac --- /dev/null +++ b/tests/passing_tests/hash_map_struct.py @@ -0,0 +1,42 @@ +from pythonbpf import bpf, section, struct, bpfglobal, compile, map +from pythonbpf.maps import HashMap +from pythonbpf.helper import pid +from ctypes import c_void_p, c_int64 + + +@bpf +@struct +class val_type: + counter: c_int64 + shizzle: c_int64 + + +@bpf +@map +def last() -> HashMap: + return HashMap(key=val_type, value=c_int64, max_entries=16) + + +@bpf +@section("tracepoint/syscalls/sys_enter_clone") +def hello_world(ctx: c_void_p) -> c_int64: + obj = val_type() + obj.counter, obj.shizzle = 42, 96 + t = last.lookup(obj) + if t: + print(f"Found existing entry: counter={obj.counter}, pid={t}") + last.delete(obj) + return 0 # type: ignore [return-value] + val = pid() + last.update(obj, val) + print(f"Map updated!, {obj.counter}, {obj.shizzle}, {val}") + return 0 # type: ignore [return-value] + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile()