Commit Graph

663 Commits

Author SHA1 Message Date
c4c0c586b3 Merge pull request #94 from pythonbpf/fix/probe-read-ctx-not-found
register ctx in the local_sym_tab with the type `void *` if no struct annotation is given
2026-08-30 19:34:25 +05:30
30c0e6b562 Merge pull request #92 from pythonbpf/fix/ringbuf-map-type
Fix auto testing by adding the verifier testing mode as well, suppress deperacation warnings
2026-08-14 03:10:27 +05:30
180c35f83e just return the keys instead of all the processors in maps_utils,py 2026-08-14 03:06:06 +05:30
e1b40d2c77 Fix comment in maps_pass.py 2026-08-14 03:04:28 +05:30
79c8a1e194 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>
2026-08-07 17:07:32 +05:30
1c349c87b4 Core: Return the function-pointer check directly in _is_flattenable_scalar
Style only, no behaviour change and no IR change. Keeps `ruff check --select
SIM` clean on the files this branch touches.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 05:30:13 +05:30
013907758f Core: Describe anonymous members in DWARF so their relocations can resolve
The previous commits emit `llvm.pt_regs:0:136$0:17:0` for `ctx.cs`, which llc
happily turns into a CO-RE relocation, but the local BTF it is resolved
against described member 17 as a NAMED, EMPTY composite:

    '_0' type_id=4 bits_offset=1088
    [4] STRUCT '(anon)' size=8 vlen=0

libbpf walks a CO-RE access string by member index into the local type and
then matches by field NAME in the target type, so both halves of that break
it: index 0 into a vlen=0 composite has nothing to land on, and a local
member called `_0` cannot be paired with the kernel's anonymous one. The
object would build cleanly and then fail to load.

Emit anonymous members the way a C compiler does: unnamed, with a real
DW_TAG_union_type (or structure, for an anonymous struct) carrying their
members in declaration order. The emitted BTF now reads

    '(anon)' type_id=4 bits_offset=1088
    [4] UNION '(anon)' size=8 vlen=3
            'cs' ... 'csx' ... 'fred_cs' ...

which is the same shape clang produces for the equivalent C and the same
shape `bpftool btf dump file /sys/kernel/btf/vmlinux` reports.

Gated on the struct declaring `_anonymous_`, and it bails back to the old
opaque member for anything it cannot describe faithfully. Across every
vmlinux/xdp test program the set of emitted globals, every relocation string
and every instruction are unchanged; the only diffs are the new DWARF nodes
and the DWARF node renumbering they cause, in the two pt_regs programs.

This is the one part of the fix that cannot be verified without loading a
program, so it is kept as its own commit.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 05:19:13 +05:30
9959862154 Core: Widen any sub-64-bit context field instead of special-casing xdp_md
Reading a context field narrower than a register was hardcoded to
struct_xdp_md's i32 fields in three places that had to agree with each other:
the destination alloca in allocation_pass, the zext in load_ctx_field, and
the store in assign_pass. struct pt_regs' cs and ss are 2 bytes, so a second
special case would have been needed.

Replace all three with the general rule: a context field is loaded at its
natural width and zero-extended to i64, so its destination is i64.
allocation_pass gains the same context discriminator load_ctx_field uses
(a context argument has no alloca of its own), so non-context field reads,
which go through load_struct_field and bpf_probe_read_kernel, keep their
natural width and are untouched.

struct_xdp_md's generated IR is byte-identical, as is every other vmlinux/xdp
test program's; the only diff in the whole corpus remains the four new globals
from the previous commit.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 05:09:45 +05:30
4ff3ce26c6 Core: Emit nested CO-RE access strings for anonymous members
A field lifted out of an anonymous member needs an access string that walks
into that member, so `struct pt_regs.cs` is `llvm.pt_regs:0:136$0:17:0`
(member 0 of anonymous member 17) rather than the flat `$0:<index>` form.
This is byte-for-byte what clang emits for the equivalent C, verified against
`__builtin_preserve_access_index(ctx->cs)` on a struct with the same shape,
and the indices and offsets agree with `bpftool btf dump file
/sys/kernel/btf/vmlinux`.

Only fields carrying an access path take the new path; every other field
keeps the existing flat and indexed/array forms unchanged. The only .ll diff
across all vmlinux/xdp test programs is four brand new globals for cs, csx,
ss and ssx. No pre-existing relocation string changes.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 04:59:08 +05:30
f2ec70759e Core: Flatten anonymous struct/union members during vmlinux parsing
On this kernel `struct pt_regs` wraps `cs` and `ss` in anonymous unions.
ctypes lifts those names onto the parent class, exactly as C does, but the
parser only walked the top-level `_fields_`, so `cs` and `ss` were unknown to
the compiler and `ctx.cs` blew up.

Register the members of an anonymous member as fields of the parent, one
level deep, taking the ABSOLUTE offset ctypes reports on the parent (no
arithmetic) and recording the access path (anonymous member index, member
index). The anonymous member itself stays registered as before.

Deliberately conservative: only scalar ctypes members are flattened. Nested
vmlinux structs (`fred_cs`, `fred_ss`) and bitfields are skipped with a
warning rather than half-registered, and a flattened member never overwrites
an existing top-level field of the same name. Overlapping members are kept as
distinct fields, since `cs` and `csx` both legitimately live at offset 136.

Flattened fields are not yet emitted: IR generation and DWARF generation skip
them, and critically they do not consume a top-level field index, so existing
CO-RE names are untouched. Every vmlinux/xdp test program still compiles to a
byte-identical .ll. Entirely inert for any struct without `_anonymous_`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 04:54:17 +05:30
aa51d123de Core: Add an optional CO-RE access path to Field
Carries the chain of member indices from the enclosing struct down to a
field, so a field that does not sit at the top level of the struct can still
describe where it lives. Defaults to None, which keeps meaning "this field is
a plain top-level member".

Pure data, nothing reads it yet, generated IR is unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 04:48:06 +05:30
ce222afa65 Core: Make vmlinux field lookups agree on one source of truth
has_field() consulted hasattr() on the ctypes class while get_field_type()
indexed the parsed members dict. For any member ctypes exposes but the
vmlinux parser never registered, has_field() said True and get_field_type()
then died with a raw KeyError.

Both now consult the parsed members dict, and a lookup that misses raises a
ValueError that distinguishes "no such field" from "field exists in
vmlinux.py but the parser does not support it yet".

get_field_index() additionally derives the index from the ctypes _fields_
list (the C declaration order) instead of the insertion order of the parsed
members dict, so it no longer depends on the two happening to coincide.

No change to generated IR: every vmlinux/xdp test program compiles to a
byte-identical .ll.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 04:43:49 +05:30
d7a763639a Core: Guard vmlinux struct lookup against non-class metadata
_allocate_for_attribute reached for `metadata.__name__` whenever the
base symbol was not a known user struct, but `metadata` is only a struct
class for struct-typed symbols. For a symbol carrying None (any plainly
typed local) or a ctypes class (a `c_void_p` context parameter), that
raised AttributeError instead of reporting the unsupported access.

Resolve the name via getattr and fall through to the existing
"struct type not found" diagnostic when there is none.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 04:39:01 +05:30
b47063c49c 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>
2026-08-07 04:38:49 +05:30
5311a16c21 Bump version to 0.1.9 2026-06-05 22:19:18 +05:30
5a9c77f016 janitorial 2026-06-01 12:09:06 +05:30
d0f7a9dcec Add DEBUG log in case of no expected type or target is not a pointer 2026-06-01 12:06:33 +05:30
0dea1836df Pass expected_type to bpf_map_delete_elem key arg
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-31 15:32:03 +05:30
7cdb9cb2f5 Add null-safety check after deref_to_depth in helper_utils
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-31 15:31:53 +05:30
0482aa2be9 Fix deref_to_depth loop type check to use cur_type
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-31 15:31:43 +05:30
d4f1b5a9b4 Handle nested map lookup helper args
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-31 12:46:11 +05:30
3f4f95115f Core: Pass compilation_context to _prepare_expr_args as it calls eval_expr 2026-03-29 13:28:38 +05:30
f2b9767098 Core: Fix args in helper/printk_formatter 2026-03-29 13:11:44 +05:30
0e087b9ea5 Core: Revert unnecessary changes to allocation_pass 2026-03-29 02:48:16 +05:30
ccbdfee9de Core: Remove unsused args in assign_pass 2026-03-29 02:44:42 +05:30
61bca6bad9 Core: Fix global pass to divide internal functions 2026-03-29 02:40:55 +05:30
305a8ba9e3 Core: Fix unnecessary args and changes in maps pass 2026-03-29 01:36:46 +05:30
bdcfe47601 Core: Fix unnecessary arg in structs/structs_pass 2026-03-28 20:44:07 +05:30
3396d84e26 Core: Fix incompatible logic in helper/helper_utils 2026-03-28 19:58:26 +05:30
c22911daaf Core: FIx inconsistent compilation_context usage in helper/printk_formatter 2026-03-28 19:02:16 +05:30
c04e32bd24 Core: Remove spurious comments from functions_pass 2026-03-28 12:15:41 +05:30
ec4a6852ec PythonBPF: Add Compilation Context to allow parallel compilation of multiple bpf programs 2026-02-21 18:59:33 +05:30
049ec55e85 bump version
Signed-off-by: varun-r-mallya <varunrmallya@gmail.com>
2025-11-30 05:55:22 +05:30
704b0d8cd3 Fix debug info generation of PerfEventArray maps 2025-11-28 21:02:27 +05:30
4ea02745b3 Janitorial: Remove useless comments 2025-11-28 21:02:26 +05:30
6f017a9176 Unify struct and pointer to struct handling, abstract null check in ir_ops 2025-11-28 21:02:25 +05:30
24e5829b80 format chore 2025-11-28 14:52:45 +05:30
536ea4855e add cgroup helper 2025-11-28 14:47:01 +05:30
de8c486461 fix: remove deref_to_depth on single depth pointers 2025-11-27 22:59:34 +05:30
f135cdbcc0 format chore 2025-11-27 14:03:12 +05:30
a8595ff1d2 feat: allocate tmp variable for pointer to vmlinux struct field access. 2025-11-27 14:02:00 +05:30
9becee8f77 add expected type check 2025-11-27 12:44:12 +05:30
189526d5ca format chore 2025-11-27 12:42:10 +05:30
1593b7bcfe feat:user defined struct casting 2025-11-27 12:41:57 +05:30
4905649700 feat:non struct field values can be cast 2025-11-26 14:18:40 +05:30
fed6af1ed6 bump version and prepare for release 2025-11-22 13:54:41 +05:30
18886816fb Merge pull request #68 from pythonbpf/request-struct
Support enough machinery to make request struct work
2025-11-22 13:48:06 +05:30
a2de15fb1e add c_int to type_deducer.py 2025-11-22 13:36:21 +05:30
9def969592 Make map val struct type allocation work by fixing pointer deref and debuginfogen: WIP 2025-11-22 13:20:09 +05:30
a91c3158ad sort fields in debug info by offset order 2025-11-22 12:35:47 +05:30