Skip to content

Commit 79c8a1e

Browse files
r41k0uclaude
andcommitted
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) <noreply@anthropic.com>
1 parent ecb2774 commit 79c8a1e

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

pythonbpf/maps/maps_pass.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,16 @@ def process_bpf_map(func_node, compilation_context):
175175

176176
if isinstance(rval, ast.Call) and isinstance(rval.func, ast.Name):
177177
handler = MapProcessorRegistry.get_processor(rval.func.id)
178-
if handler:
179-
return handler(map_name, rval, compilation_context)
180-
else:
181-
logger.warning(f"Unknown map type {rval.func.id}, defaulting to HashMap")
182-
return process_hash_map(map_name, rval, compilation_context)
178+
if handler is None:
179+
# Falling back to a hash map here used to produce an object that
180+
# compiled cleanly and was then rejected by the kernel, because the
181+
# map carried the wrong type and none of the real map type's
182+
# validation ran. A misspelled map type is a program error.
183+
known = ", ".join(sorted(MapProcessorRegistry.known_types()))
184+
raise ValueError(
185+
f"Unknown map type '{rval.func.id}' returned by '{map_name}'. "
186+
f"Known map types: {known}"
187+
)
188+
return handler(map_name, rval, compilation_context)
183189
else:
184190
raise ValueError("Function under @map must return a map")

pythonbpf/maps/maps_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ def decorator(func):
3333
def get_processor(cls, map_type_name):
3434
"""Get the processor function for a map type"""
3535
return cls._processors.get(map_type_name)
36+
37+
@classmethod
38+
def known_types(cls):
39+
"""Names of every registered map type, for error messages"""
40+
return list(cls._processors)

0 commit comments

Comments
 (0)