From ecb277469439db526a86d9b08e2a96a304312fe6 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 7 Aug 2026 16:55:39 +0530 Subject: [PATCH 1/6] Tests: Declare the ringbuf map with its real type and a legal size The map was declared as RingBuf, which is not an exported name -- the class is RingBuffer. maps_pass has no processor registered under RingBuf, so it fell through to the 'unknown map type, defaulting to HashMap' path and was emitted as BPF_MAP_TYPE_HASH with no key or value size. The kernel rejected it: libbpf: map 'mymap': found type = 1. libbpf: map 'mymap': failed to create: -EINVAL Nothing caught this earlier because the test framework compiles the file's AST and never imports it, so the bogus 'from pythonbpf.maps import RingBuf' never raised ImportError. Using the real name also subjects the map to process_ringbuf_map's validation, which max_entries=1024 does not survive: a ringbuf needs a power of two at least as large as the page size. Raised to 4096. libbpf now reports type = 27 with max_entries = 4096. Co-Authored-By: Claude Opus 5 (1M context) --- tests/passing_tests/ringbuf.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/passing_tests/ringbuf.py b/tests/passing_tests/ringbuf.py index 0566d85..05652cf 100644 --- a/tests/passing_tests/ringbuf.py +++ b/tests/passing_tests/ringbuf.py @@ -1,13 +1,13 @@ from pythonbpf import bpf, BPF, map, bpfglobal, section, compile, compile_to_ir -from pythonbpf.maps import RingBuf, HashMap +from pythonbpf.maps import RingBuffer, HashMap from ctypes import c_int32, c_void_p # Define a map @bpf @map -def mymap() -> RingBuf: - return RingBuf(max_entries=(1024)) +def mymap() -> RingBuffer: + return RingBuffer(max_entries=4096) @bpf From 79c8a1e1948d654d8bdad8082d591535b94927eb Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 7 Aug 2026 16:57:51 +0530 Subject: [PATCH 2/6] Core: Reject an unknown map type instead of silently emitting a hash map process_bpf_map logged a warning and fell back to process_hash_map for any map type it did not recognise. That turns a typo into an object file that compiles cleanly, carries the wrong BPF_MAP_TYPE, skips the intended map type's parameter validation entirely, and is only rejected once it reaches the kernel -- as tests/passing_tests/ringbuf.py was, with 'failed to create: -EINVAL'. A warning was not enough to surface it either: the test framework only fails a test on logging.ERROR records. Raise instead, naming the registered map types so the fix is obvious: ValueError: Unknown map type 'RingBuf' returned by 'm'. Known map types: HashMap, PerfEventArray, RingBuffer Co-Authored-By: Claude Opus 5 (1M context) --- pythonbpf/maps/maps_pass.py | 16 +++++++++++----- pythonbpf/maps/maps_utils.py | 5 +++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pythonbpf/maps/maps_pass.py b/pythonbpf/maps/maps_pass.py index 083b24a..7e3b1ed 100644 --- a/pythonbpf/maps/maps_pass.py +++ b/pythonbpf/maps/maps_pass.py @@ -175,10 +175,16 @@ def process_bpf_map(func_node, compilation_context): if isinstance(rval, ast.Call) and isinstance(rval.func, ast.Name): handler = MapProcessorRegistry.get_processor(rval.func.id) - if handler: - return handler(map_name, rval, compilation_context) - else: - logger.warning(f"Unknown map type {rval.func.id}, defaulting to HashMap") - return process_hash_map(map_name, rval, compilation_context) + if handler is None: + # Falling back to a hash map here used to produce an object that + # compiled cleanly and was then rejected by the kernel, because the + # map carried the wrong type and none of the real map type's + # validation ran. A misspelled map type is a program error. + known = ", ".join(sorted(MapProcessorRegistry.known_types())) + raise ValueError( + f"Unknown map type '{rval.func.id}' returned by '{map_name}'. " + f"Known map types: {known}" + ) + return handler(map_name, rval, compilation_context) else: raise ValueError("Function under @map must return a map") diff --git a/pythonbpf/maps/maps_utils.py b/pythonbpf/maps/maps_utils.py index 194b408..decf04b 100644 --- a/pythonbpf/maps/maps_utils.py +++ b/pythonbpf/maps/maps_utils.py @@ -33,3 +33,8 @@ class MapProcessorRegistry: def get_processor(cls, map_type_name): """Get the processor function for a map type""" return cls._processors.get(map_type_name) + + @classmethod + def known_types(cls): + """Names of every registered map type, for error messages""" + return list(cls._processors) From d66b468f39f43613cd8cd1547314dca03a4f41c8 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 7 Aug 2026 16:57:54 +0530 Subject: [PATCH 3/6] Tests: Promote if.py to passing_tests It generates IR, compiles, and now passes the kernel verifier, so it is no longer a failing test. It sat in failing_tests/ with no test_config.toml entry, which meant the framework already required it to pass at every level -- the directory was merely misleading. Co-Authored-By: Claude Opus 5 (1M context) --- tests/{failing_tests => passing_tests}/if.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{failing_tests => passing_tests}/if.py (100%) diff --git a/tests/failing_tests/if.py b/tests/passing_tests/if.py similarity index 100% rename from tests/failing_tests/if.py rename to tests/passing_tests/if.py From fd2412e733f11fd9b7137d9c6f662b8c59d9d8ba Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 7 Aug 2026 17:00:23 +0530 Subject: [PATCH 4/6] Tests: Declare xdp_test_1 as a verifier-level expected failure It generates IR and compiles, and the CO-RE relocations against struct xdp_md resolve correctly, so it must keep passing levels 1 and 2. The kernel rejects it: the 'data + 34 < data_end' guard is emitted as a signed compare over values round-tripped through the stack, so the verifier never narrows the packet range and the later iph.saddr read fails with invalid access to packet, off=26 size=4, R1(id=0,off=26,r=0) R1 offset is outside of the packet Direct packet access needs bounds checks in a form the verifier can follow. strict = True, so this flags up if that ever lands. Co-Authored-By: Claude Opus 5 (1M context) --- tests/test_config.toml | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/test_config.toml b/tests/test_config.toml index edcd90c..8a6255a 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -2,15 +2,27 @@ # # [xfail] — tests expected to fail. # key = path relative to tests/ -# value = {reason = "...", level = "ir" | "llc"} -# level "ir" = fails during pythonbpf IR generation (exception or ERROR log) -# level "llc" = IR generates but llc rejects it +# value = {reason = "...", level = "ir" | "llc" | "verifier"} +# level "ir" = fails during pythonbpf IR generation (exception or ERROR log) +# level "llc" = IR generates but llc rejects it +# level "verifier" = IR and llc both succeed, but the kernel verifier rejects it +# +# A failure at one level implies failure at every later one, so the declared +# level marks that level and all later ones xfail. # [xfail] "failing_tests/conditionals/struct_ptr.py" = {reason = "Struct pointer used directly as boolean condition not supported", level = "ir"} +# Compiles cleanly; rejected by the kernel. The `data + 34 < data_end` guard is +# emitted as a signed compare over values round-tripped through the stack, so the +# verifier never narrows the packet range (it stays r=0) and the later +# `iph.saddr` read fails with "invalid access to packet, off=26 size=4" / +# "R1 offset is outside of the packet". Direct packet access needs bounds checks +# in a form the verifier can follow. +"failing_tests/xdp/xdp_test_1.py" = {reason = "XDP direct packet access: the data/data_end guard does not establish a packet range the verifier can use", level = "verifier"} + "failing_tests/license.py" = {reason = "Missing LICENSE global produces IR that llc rejects — should be caught earlier with a clear error message", level = "llc"} "failing_tests/undeclared_values.py" = {reason = "Undeclared variable used in f-string — should raise SyntaxError (correct behaviour, test documents it)", level = "ir"} From e1b40d2c778712af171315e0605fe75256011459 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi <76248539+r41k0u@users.noreply.github.com> Date: Fri, 14 Aug 2026 03:04:28 +0530 Subject: [PATCH 5/6] Fix comment in maps_pass.py --- pythonbpf/maps/maps_pass.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pythonbpf/maps/maps_pass.py b/pythonbpf/maps/maps_pass.py index 7e3b1ed..ca07845 100644 --- a/pythonbpf/maps/maps_pass.py +++ b/pythonbpf/maps/maps_pass.py @@ -176,10 +176,9 @@ def process_bpf_map(func_node, compilation_context): if isinstance(rval, ast.Call) and isinstance(rval.func, ast.Name): handler = MapProcessorRegistry.get_processor(rval.func.id) if handler is None: - # Falling back to a hash map here used to produce an object that - # compiled cleanly and was then rejected by the kernel, because the - # map carried the wrong type and none of the real map type's - # validation ran. A misspelled map type is a program error. + # Raise an exception and fail the build because the + # map carried the wrong type. A misspelled map type + # is a program error. known = ", ".join(sorted(MapProcessorRegistry.known_types())) raise ValueError( f"Unknown map type '{rval.func.id}' returned by '{map_name}'. " From 180c35f83ed6d7c5d0044b6c75398c86336e0db9 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi <76248539+r41k0u@users.noreply.github.com> Date: Fri, 14 Aug 2026 03:06:06 +0530 Subject: [PATCH 6/6] just return the keys instead of all the processors in maps_utils,py --- pythonbpf/maps/maps_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonbpf/maps/maps_utils.py b/pythonbpf/maps/maps_utils.py index decf04b..a271697 100644 --- a/pythonbpf/maps/maps_utils.py +++ b/pythonbpf/maps/maps_utils.py @@ -37,4 +37,4 @@ class MapProcessorRegistry: @classmethod def known_types(cls): """Names of every registered map type, for error messages""" - return list(cls._processors) + return list(cls._processors.keys())