From 79c8a1e1948d654d8bdad8082d591535b94927eb Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 7 Aug 2026 16:57:51 +0530 Subject: [PATCH] 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)