From 3de98327926a5d2914ea51976e62cfc0dcfffe3a Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 25 Sep 2026 01:18:52 +0530 Subject: [PATCH] Core: Implement ArrayMap as BPF_MAP_TYPE_ARRAY process_array_map was a stub raising NotImplementedError while ArrayMap was exported from pythonbpf.maps as if usable. The lowering is the hash map's with a different type constant: the same lookup/update/delete helpers, and the kernel fixes the key at a 4-byte index. The lookup allocation path accepts ARRAY alongside HASH, the audit tool lists ARRAY as supported, and the array_map_lookup_update case is a passing test now instead of a strict xfail. Co-Authored-By: Claude Fable 5.1 --- pythonbpf/allocation_pass.py | 2 +- pythonbpf/maps/maps_pass.py | 18 +++++++++++++++--- .../PORTING-NOTES.md | 6 +++--- tests/test_config.toml | 1 - tools/selftest-audit.py | 4 ++-- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/pythonbpf/allocation_pass.py b/pythonbpf/allocation_pass.py index aff095a..c7dcc93 100644 --- a/pythonbpf/allocation_pass.py +++ b/pythonbpf/allocation_pass.py @@ -208,7 +208,7 @@ def _allocate_for_map_method( return map_params = map_sym_tab[map_name].params - if map_params["type"] != BPFMapType.HASH: + if map_params["type"] not in (BPFMapType.HASH, BPFMapType.ARRAY): logger.warning( "Map method lookup used on non-hash map, using fallback allocation" ) diff --git a/pythonbpf/maps/maps_pass.py b/pythonbpf/maps/maps_pass.py index 108600b..ae0d220 100644 --- a/pythonbpf/maps/maps_pass.py +++ b/pythonbpf/maps/maps_pass.py @@ -145,10 +145,22 @@ def process_hash_map(map_name, rval, compilation_context): @MapProcessorRegistry.register("ArrayMap") def process_array_map(map_name, rval, compilation_context): - """Document the planned BPF_ARRAY map support with an explicit failure.""" - raise NotImplementedError( - "ArrayMap is not implemented yet; add BPF_MAP_TYPE_ARRAY metadata support" + """Process a BPF_ARRAY map declaration: the same lowering as a hash map + with BPF_MAP_TYPE_ARRAY, through the same lookup/update/delete helpers; + the kernel requires a 4-byte key (an index).""" + logger.info(f"Processing ArrayMap: {map_name}") + map_params = _parse_map_params(rval, expected_args=["key", "value", "max_entries"]) + map_params["type"] = BPFMapType.ARRAY + + logger.info(f"Map parameters: {map_params}") + map_global = create_bpf_map(compilation_context, map_name, map_params) + create_map_debug_info( + compilation_context, + map_global.var, + map_name, + map_params, ) + return map_global @MapProcessorRegistry.register("PerfEventArray") diff --git a/tests/kernel_selftest_equivalent/PORTING-NOTES.md b/tests/kernel_selftest_equivalent/PORTING-NOTES.md index a5929e3..d09f299 100644 --- a/tests/kernel_selftest_equivalent/PORTING-NOTES.md +++ b/tests/kernel_selftest_equivalent/PORTING-NOTES.md @@ -74,9 +74,9 @@ cursor, which together constrain the design more than anything here does. `.bss`, `.data` and `.rodata` become internal array maps at load time. Two consequences: - A one-element **`ArrayMap`** is the structurally faithful stand-in for a global, not a - `HashMap`. `HashMap` is used here only because `ArrayMap` is still a placeholder that - raises `NotImplementedError`. Landing `ArrayMap` first would make the eventual migration - to real globals close to mechanical. + `HashMap`. The `HashMap` stand-ins predate `ArrayMap`, which lowers now + (`BPF_MAP_TYPE_ARRAY`, the same helpers as `HashMap`); real `@bpfglobal` scalars have + landed since, so the migration is to those. - **Most of the ELF work is already done.** `@bpfglobal` is vestigial — a metadata carrier for `LICENSE` — but the machinery behind it already emits globals that LLVM places into `.bss` and `.data` correctly, and that libbpf already recognises: diff --git a/tests/test_config.toml b/tests/test_config.toml index fa8c9c8..719137f 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -61,7 +61,6 @@ "failing_tests/loops/for_map_items.py" = {reason = "for/while loops not implemented: no sugar over bpf_for_each_map_elem()-style map iteration exists yet", level = "ir"} -"kernel_selftest_equivalent/maps/array_map_lookup_update.py" = {reason = "ArrayMap / BPF_MAP_TYPE_ARRAY support is planned but not implemented yet", level = "ir"} "kernel_selftest_equivalent/ringbuf/reserve_submit_discard.py" = {reason = "RingBuffer reserve/typed record/discard workflow is planned but not implemented yet", level = "ir"} diff --git a/tools/selftest-audit.py b/tools/selftest-audit.py index 051a519..26f6798 100755 --- a/tools/selftest-audit.py +++ b/tools/selftest-audit.py @@ -52,8 +52,8 @@ SUPPORTED_HELPERS = { "bpf_skb_store_bytes", } -# BPF_MAP_TYPE_* that pythonbpf/maps lowers (ArrayMap is still a stub). -SUPPORTED_MAP_TYPES = {"HASH", "PERF_EVENT_ARRAY", "RINGBUF"} +# BPF_MAP_TYPE_* that pythonbpf/maps lowers. +SUPPORTED_MAP_TYPES = {"ARRAY", "HASH", "PERF_EVENT_ARRAY", "RINGBUF"} # Things that look like helper calls but are libbpf macros, not helpers. NOT_HELPERS = {