Compare commits

..

8 Commits

8 changed files with 48 additions and 48 deletions

View File

@ -15,8 +15,8 @@ sys.path.insert(0, os.path.abspath(".."))
project = "PythonBPF"
copyright = "2026, Pragyansh Chaturvedi, Varun Mallya"
author = "Pragyansh Chaturvedi, Varun Mallya"
release = "0.1.8"
version = "0.1.8"
release = "0.1.9"
version = "0.1.9"
# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

View File

@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "pythonbpf"
version = "0.1.8"
version = "0.1.9"
description = "Reduced Python frontend for eBPF"
authors = [
{ name = "r41k0u", email="pragyanshchaturvedi18@gmail.com" },

View File

@ -340,15 +340,9 @@ def _allocate_for_attribute(
struct_type: type = local_sym_tab[struct_var].metadata
if not struct_type or struct_type not in structs_sym_tab:
# `metadata` only names a struct for struct-typed symbols. For anything
# else (None, an IR type, or a plain ctypes class such as a `c_void_p`
# context parameter) there is no vmlinux struct name to look up, so guard
# the attribute access instead of blowing up with an AttributeError.
vmlinux_struct_name = getattr(struct_type, "__name__", None)
if vmlinux_struct_name and VmlinuxHandlerRegistry.is_vmlinux_struct(
vmlinux_struct_name
):
if VmlinuxHandlerRegistry.is_vmlinux_struct(struct_type.__name__):
# Handle vmlinux struct field access
vmlinux_struct_name = struct_type.__name__
if not VmlinuxHandlerRegistry.has_field(vmlinux_struct_name, field_name):
logger.error(
f"Field '{field_name}' not found in vmlinux struct '{vmlinux_struct_name}'"

View File

@ -26,7 +26,7 @@ import re
logger: Logger = logging.getLogger(__name__)
VERSION = "v0.1.8"
VERSION = "v0.1.9"
def finalize_module(original_str):

View File

@ -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

View File

@ -1,12 +1,11 @@
from llvmlite import ir
import ast
import ctypes
import logging
from pythonbpf.helper import (
HelperHandlerRegistry,
)
from pythonbpf.type_deducer import ctypes_to_ir, is_ctypes
from pythonbpf.type_deducer import ctypes_to_ir
from pythonbpf.expr import (
eval_expr,
handle_expr,
@ -326,9 +325,6 @@ def process_func_body(
f"Unsupported annotation type: {ast.dump(context_arg.annotation)}"
)
# NOTE: `var` is None for the context parameter. It is the sentinel
# that tells consumers the symbol is an incoming function argument
# (`func.args[0]`) rather than a stack slot they can load from.
if VmlinuxHandlerRegistry.is_vmlinux_struct(context_type_name):
resolved_type = VmlinuxHandlerRegistry.get_struct_type(
context_type_name
@ -336,18 +332,6 @@ def process_func_body(
context_type = LocalSymbol(None, None, resolved_type)
local_sym_tab[context_name] = context_type
logger.info(f"Added argument '{context_name}' to local symbol table")
elif is_ctypes(context_type_name):
# Plain ctypes annotation, e.g. `ctx: c_void_p`. Register it so
# helpers that take the raw context (probe_read, ...) can resolve
# the name. Metadata is the annotated Python type, mirroring the
# vmlinux branch above.
context_type = LocalSymbol(
None,
ir.PointerType(),
getattr(ctypes, context_type_name, None),
)
local_sym_tab[context_name] = context_type
logger.info(f"Added argument '{context_name}' to local symbol table")
# pre-allocate dynamic variables
local_sym_tab = allocate_mem(

View File

@ -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)
@ -267,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())

View File

@ -6,6 +6,8 @@ from pythonbpf.expr import (
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__)
@ -50,20 +52,8 @@ def get_or_create_ptr_from_arg(
logger.info(f"Getting pointer from arg: {ast.dump(arg)}")
sz = None
if isinstance(arg, ast.Name):
symbol = local_sym_tab.get(arg.id) if local_sym_tab else None
if symbol is not None and symbol.var is None:
# A None `var` marks the context parameter (see process_func_body):
# it arrives as the function's first argument, not as a stack slot,
# so there is no alloca to hand back. Use the argument itself.
if not func.args:
raise ValueError(
f"'{arg.id}' is the context parameter but "
f"'{func.name}' takes no arguments"
)
ptr = builder.bitcast(func.args[0], ir.PointerType())
else:
# Stack space is already allocated
ptr = get_var_ptr_from_name(arg.id, local_sym_tab)
# Stack space is already allocated
ptr = get_var_ptr_from_name(arg.id, local_sym_tab)
elif isinstance(arg, ast.Constant) and isinstance(arg.value, int):
int_width = 64 # Default to i64
if expected_type and isinstance(expected_type, ir.IntType):
@ -116,6 +106,18 @@ 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)
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
)