mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 18:08:36 +00:00
Core: Resolve plain-ctypes context parameter in helper args
process_func_body only entered the context parameter into local_sym_tab when its annotation was a vmlinux struct, so a program declared as `def prog(ctx: c_void_p)` had no `ctx` symbol at all. Passing `ctx` to a helper that takes a raw pointer -- probe_read(dst, size, ctx) -- failed with "Variable 'ctx' not found in local symbol table". Register the parameter for plain ctypes annotations too, keeping `var` as None: that is the established sentinel meaning "this symbol is the context parameter, read it from func.args[0]" (see VmlinuxHandler.handle_vmlinux_struct_field). Metadata holds the annotated Python type, mirroring the vmlinux branch. Teach the consumer side accordingly: get_or_create_ptr_from_arg now recognises the sentinel and hands back the function's first argument instead of an alloca. The special case lives at the call site rather than inside get_var_ptr_from_name so that helper stays a pure symbol table lookup -- the call site already has `func` and `builder` in scope, and get_var_ptr_from_name has no other callers to update. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@ -50,8 +50,20 @@ def get_or_create_ptr_from_arg(
|
||||
logger.info(f"Getting pointer from arg: {ast.dump(arg)}")
|
||||
sz = None
|
||||
if isinstance(arg, ast.Name):
|
||||
# Stack space is already allocated
|
||||
ptr = get_var_ptr_from_name(arg.id, local_sym_tab)
|
||||
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)
|
||||
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):
|
||||
|
||||
Reference in New Issue
Block a user