From d4f1b5a9b4ec7035e2e7226038843f05824fcd4a Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Sun, 31 May 2026 12:46:11 +0530 Subject: [PATCH 1/6] Handle nested map lookup helper args Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- pythonbpf/helper/bpf_helper_handler.py | 21 ++++++++++++++++++--- pythonbpf/helper/helper_utils.py | 8 ++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/pythonbpf/helper/bpf_helper_handler.py b/pythonbpf/helper/bpf_helper_handler.py index f44f05f..245c34d 100644 --- a/pythonbpf/helper/bpf_helper_handler.py +++ b/pythonbpf/helper/bpf_helper_handler.py @@ -113,7 +113,12 @@ def bpf_map_lookup_elem_emitter( f"Map lookup expects exactly one argument (key), got {len(call.args)}" ) key_ptr = get_or_create_ptr_from_arg( - func, compilation_context, call.args[0], builder, local_sym_tab + func, + compilation_context, + call.args[0], + builder, + local_sym_tab, + expected_type=ir.IntType(64), ) map_void_ptr = builder.bitcast(map_ptr, ir.PointerType()) @@ -215,10 +220,20 @@ def bpf_map_update_elem_emitter( flags_arg = call.args[2] if len(call.args) > 2 else None key_ptr = get_or_create_ptr_from_arg( - func, compilation_context, key_arg, builder, local_sym_tab + func, + compilation_context, + key_arg, + builder, + local_sym_tab, + expected_type=ir.IntType(64), ) value_ptr = get_or_create_ptr_from_arg( - func, compilation_context, value_arg, builder, local_sym_tab + func, + compilation_context, + value_arg, + builder, + local_sym_tab, + expected_type=ir.IntType(64), ) flags_val = get_flags_val(flags_arg, builder, local_sym_tab) diff --git a/pythonbpf/helper/helper_utils.py b/pythonbpf/helper/helper_utils.py index 211a925..655608d 100644 --- a/pythonbpf/helper/helper_utils.py +++ b/pythonbpf/helper/helper_utils.py @@ -6,6 +6,8 @@ eval_expr, access_struct_field, ) +from pythonbpf.expr.ir_ops import deref_to_depth +from pythonbpf.expr.type_normalization import get_base_type_and_depth logger = logging.getLogger(__name__) @@ -104,6 +106,12 @@ def get_or_create_ptr_from_arg( if val is None: raise ValueError("Failed to evaluate expression for helper arg.") + if expected_type and isinstance(val.type, ir.PointerType): + _, val_depth = get_base_type_and_depth(val.type) + _, expected_depth = get_base_type_and_depth(expected_type) + if val_depth > expected_depth: + val = deref_to_depth(func, builder, val, val_depth - expected_depth) + ptr, temp_name = compilation_context.scratch_pool.get_next_temp( local_sym_tab, expected_type ) From 0482aa2be9d7f30c83728aeb0b014f9e4bc90b90 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Sun, 31 May 2026 15:31:43 +0530 Subject: [PATCH 2/6] Fix deref_to_depth loop type check to use cur_type Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- pythonbpf/expr/ir_ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonbpf/expr/ir_ops.py b/pythonbpf/expr/ir_ops.py index df6f503..3f10d19 100644 --- a/pythonbpf/expr/ir_ops.py +++ b/pythonbpf/expr/ir_ops.py @@ -11,7 +11,7 @@ def deref_to_depth(func, builder, val, target_depth): cur_type = val.type for depth in range(target_depth): - if not isinstance(val.type, ir.PointerType): + if not isinstance(cur_type, ir.PointerType): logger.error("Cannot dereference further, non-pointer type") return None From 7cdb9cb2f567097d4fd6e7cd778cc7dab11eb7f0 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Sun, 31 May 2026 15:31:53 +0530 Subject: [PATCH 3/6] Add null-safety check after deref_to_depth in helper_utils Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- pythonbpf/helper/helper_utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pythonbpf/helper/helper_utils.py b/pythonbpf/helper/helper_utils.py index 655608d..2036d60 100644 --- a/pythonbpf/helper/helper_utils.py +++ b/pythonbpf/helper/helper_utils.py @@ -111,6 +111,8 @@ def get_or_create_ptr_from_arg( _, expected_depth = get_base_type_and_depth(expected_type) if val_depth > expected_depth: val = deref_to_depth(func, builder, val, val_depth - expected_depth) + if val is None: + raise ValueError("Failed to dereference pointer to expected depth") ptr, temp_name = compilation_context.scratch_pool.get_next_temp( local_sym_tab, expected_type From 0dea1836dfb6905e68a07c1999d8f1747a32be99 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Sun, 31 May 2026 15:32:03 +0530 Subject: [PATCH 4/6] Pass expected_type to bpf_map_delete_elem key arg Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- pythonbpf/helper/bpf_helper_handler.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pythonbpf/helper/bpf_helper_handler.py b/pythonbpf/helper/bpf_helper_handler.py index 245c34d..9fde71f 100644 --- a/pythonbpf/helper/bpf_helper_handler.py +++ b/pythonbpf/helper/bpf_helper_handler.py @@ -282,7 +282,12 @@ def bpf_map_delete_elem_emitter( f"Map delete expects exactly one argument (key), got {len(call.args)}" ) key_ptr = get_or_create_ptr_from_arg( - func, compilation_context, call.args[0], builder, local_sym_tab + func, + compilation_context, + call.args[0], + builder, + local_sym_tab, + expected_type=ir.IntType(64), ) map_void_ptr = builder.bitcast(map_ptr, ir.PointerType()) From d0f7a9dcec48a28bd27722f2336ef100d3c265b2 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Mon, 1 Jun 2026 11:57:48 +0530 Subject: [PATCH 5/6] Add DEBUG log in case of no expected type or target is not a pointer --- pythonbpf/helper/helper_utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pythonbpf/helper/helper_utils.py b/pythonbpf/helper/helper_utils.py index 2036d60..95ab34f 100644 --- a/pythonbpf/helper/helper_utils.py +++ b/pythonbpf/helper/helper_utils.py @@ -113,6 +113,8 @@ def get_or_create_ptr_from_arg( val = deref_to_depth(func, builder, val, val_depth - expected_depth) if val is None: raise ValueError("Failed to dereference pointer to expected depth") + else: + logger.debug("Expected Type not known / Not a pointer, skipping dereference") ptr, temp_name = compilation_context.scratch_pool.get_next_temp( local_sym_tab, expected_type From 5a9c77f0160bc32cd60351d511856df72413772f Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Mon, 1 Jun 2026 12:09:06 +0530 Subject: [PATCH 6/6] janitorial --- pythonbpf/helper/helper_utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pythonbpf/helper/helper_utils.py b/pythonbpf/helper/helper_utils.py index 95ab34f..dd00228 100644 --- a/pythonbpf/helper/helper_utils.py +++ b/pythonbpf/helper/helper_utils.py @@ -114,7 +114,9 @@ def get_or_create_ptr_from_arg( if val is None: raise ValueError("Failed to dereference pointer to expected depth") else: - logger.debug("Expected Type not known / Not a pointer, skipping dereference") + logger.debug( + "Expected Type not known / Not a pointer, skipping dereference" + ) ptr, temp_name = compilation_context.scratch_pool.get_next_temp( local_sym_tab, expected_type