C's rules over the ctypes types: what a declaration means, literal rank,
source-driven widening with the destination typing the stored value, the
usual arithmetic conversions applied per operation, which operators and
predicates the sign selects, and the list of deliberate divergences from
Python (/ and // truncate, % takes the dividend's sign, fixed width,
unsigned types exist).
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
Seven programs under passing_tests/signedness, one per case in
tests/c-form/signedness.bpf.c: unsigned and signed widening, mixed
division and remainder, unsigned comparison, u32 * u32 wrapping before
the widening, right shifts, and literal rank. Levels 1 and 2 compile
them like any other case; test_signedness_ir.py asserts the shape of the
IR against what clang emits for the C (zext vs sext, udiv vs sdiv,
icmp ugt vs sgt, lshr vs ashr, the trunc/zext pair around the wrap).
The XDP xfail note no longer claims the guard is a signed compare.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
A comparison's descriptor was a plain i1, which reads as signed, so
storing or returning a truth value would sign-extend true to -1. C's
relational operators yield int 0 or 1; give the results an unsigned
descriptor so any widening zero-extends.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
-2 parses as USub(Constant 2), so u32 / -2 reached the divider as a
mul-by-minus-one instruction rather than the constant clang folds it to.
Fold it at the unary operator; the result is a literal like any other,
with a literal's C rank, so the usual arithmetic conversions still make
u32 / -2 an unsigned division by 0xFFFFFFFE.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
The operator table now carries both variants for the sign-sensitive
operations: / and // -> sdiv/udiv, % -> srem/urem, >> -> ashr/lshr. The
ring operations are unchanged since two's complement makes their low bits
sign-blind. apply_binop picks the variant from the promoted type computed
by the usual arithmetic conversions, so uint32(10) / int32(-2) is a udiv
in u32 (0, as C) rather than a signed division (-5).
Comparisons between two integers now go through the same promotion and
pick icmp_signed or icmp_unsigned from the promoted type, so
uint64(10) > int64(-1) is icmp ugt (false, as C). Pointer and struct
comparisons keep the depth-normalising path unchanged.
Corpus: only an XDP test comparing c_uint context fields changes, to an
unsigned predicate.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
Arithmetic is now typed bottom-up through the expression tree the way C does
it. get_typed_operand evaluates an operand to (value, IntTy); a binary node
computes its type with usual_arithmetic_conversions, converts each operand to
that type per the operand's own sign (source-driven, folding literals), runs
the operation in the i64 working register, and narrows the result back to the
node's type. That last step is what makes a u32 * u32 wrap at 32 bits before
it is widened into a u64, exactly as clang emits (mul i32, zext) -- verified
against tests/c-form/signedness.bpf.c. There is no expression-wide signed or
unsigned mode; each node decides from its operands, and the assignment target
only converts the finished result.
Augmented assignment is typed identically (x op= v is x = x op v), unary minus
negates in the operand's promoted type (2^N - x for unsigned), and return
statements now convert the value to the declared return type instead of
emitting whatever width the expression happened to have.
Corpus: 13 programs change; with SSA numbering normalised every changed line
is a trunc/sext/zext inserted by canonicalisation where an operation's type is
narrower than 64 bits (int-typed literals and c_int32/c_uint32 operands).
No program changes compile status.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
Sign now originates wherever a type is declared or inferred, and emitted IR
changes only where a value is widened out of a type now known to be unsigned:
- Helper return types are typed per the kernel signature: ktime_get_ns,
get_current_pid_tgid, get_current_uid_gid, get_current_cgroup_id,
get_prandom_u32 and get_smp_processor_id are unsigned; the probe_read
family, perf_event_output and get_stack return long.
- Undeclared locals are 64-bit with the sign of their initializer -- from the
helper registry for helper results, and for binary operations from a small
static inference (expr/type_inference.py) applying the same usual arithmetic
conversions the code generator will.
- Integer literals are typed as C types them (int if it fits, else long long)
while remaining 64-bit constants; this rank is what makes u32 / -2 promote
to an unsigned 32-bit division.
- signedness() understands vmlinux Field descriptors, and load_ctx_field
sign-extends signed sub-64-bit context fields instead of always
zero-extending them.
- printk widening goes through convert.
Two sites decided whether to emit a conversion from descriptor *equality*.
With literal descriptors now narrower than the constants that carry them, that
skipped required truncations (storing an i64 into an i32 slot). Assignment and
the ctypes cast handler now always run convert, which is a no-op when the
physical widths already agree.
Corpus: the following programs change, every changed line a sext->zext swap on
a value of known unsigned type:
failing_tests/xdp/xdp_test_1.py.ll
passing_tests/assign/augassign_struct_field.py.ll
passing_tests/helpers/smp_processor_id.py.ll
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
The same widen-to-i64 / truncate-to-slot logic was written out by hand in
six places: local assignment, struct-field assignment, augmented assignment,
helper-argument temporaries, binary-operation operands, and the comparison
normaliser. Each now calls type_normalization.convert, which is the single
point where the sign decides between zext and sext.
Across the 67-program corpus this changes exactly one emitted instruction, and
it is a correction rather than a regression: augmented assignment on a
c_uint32 struct field now widens the field's current value with zext instead
of sext, because that slot's descriptor already carries its sign from
ctypes_to_ir. Every other site still sees plain (signed-by-default) descriptors
and is byte-identical. The remaining hand-written widenings -- the printk
formatter, the vmlinux field loader, return handling -- have their own rules
and are migrated together with the sign sources.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
LLVM integer types are sign-agnostic; the sign lives in the operations. The
frontend therefore has to carry it, and until now it did not: c_uint32 became
a bare i32 at declaration and every later choice (sext, sdiv, icmp_signed)
assumed signed.
This commit adds the pieces without changing any emitted IR:
- type_deducer.IntTy, an ir.IntType subclass carrying 'signed'. It renders,
compares and hashes as the plain type, so the 40 isinstance checks and every
== ir.IntType(64) comparison keep working; signedness() reads it and treats a
plain IntType as signed (today's behaviour). Constructed outside IntType's
per-width instance cache, which is shared with subclasses and would have
merged the two signs of a width into one object.
- ctypes_to_ir now returns IntTy, which types five declaration kinds at once:
locals from ctypes constructors, struct fields, globals, context
annotations, and map key/value types. is_signed_ctype replaces the private
copy in globals_pass.
- operators.usual_arithmetic_conversions: C's rule for the type a binary
operation is performed in.
- type_normalization.convert (source-driven zext/sext when widening, trunc
when narrowing) and canonicalise (bring a value to the working width holding
exactly a given type's value).
Nothing consults the sign yet. The corpus of 67 compilable test programs
produces byte-identical .ll before and after.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
Eight cases, each reading operands from .bss globals (so clang cannot fold)
and writing its result to a .bss global (so the runtime proof is a bpftool
map dump). The IR clang emits is the specification for the signedness work:
s64 = u32 zext i32 -> i64 (widening is source-driven)
u64 = s32 sext i32 -> i64
u32 / s32 udiv i32 (usual arithmetic conversions)
u64 > s64 icmp ugt i64
u32 * u32 -> u64 mul i32, then zext (wraps at 32 before widening)
u32 >> 4 lshr i32
s32 >> 4 ashr i32
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
Levels 1 and 2 only prove the globals cases compile; what they are about
is whether a name resolves to the @bpfglobal symbol or to a stack slot
that shadows it. test_globals_ir.py reads the IR of each BPF function
and counts loads and stores per operand, exactly: `global counter` is
load/add/store on @counter with no slot, a read needs no declaration and
hits the symbol, and a write without `global` binds a slot for the whole
body of that function while a second function still reads the symbol.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
globals_shadowing.py asserted the old rule, so it moves to
passing_tests/globals/shadowing.py and asserts the new one: the write
without `global` binds a local, and a second program still reads the
global's initializer. copy_to_local.py covers `x = a_global`, the read
shape that used to fail in the allocation pass.
globals_read_before_shadow.py takes over as the failing case, since a
read above the binding is what Python rejects, and the augassign test
keeps its xfail with the reason reworded to the UnboundLocalError it is.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Three review notes from the globals PR, all about being explicit:
- Augmented assignment on a struct field handles one level of
dereference. Say so where the target is unpacked, and give
`obj.inner.field += v` its own message instead of an ast.dump.
- The ast.Global branch of process_stmt is a no-op for a reason
(declarations are collected before anything is lowered, because the
binding is function-wide). Spell that out, and log the names at debug
level rather than leaving a bare pass.
- process_stmt had no else, so any statement the compiler does not
handle was dropped without a word, which makes a program mean
something other than what it says. Warn, with the line number.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
`x = a_global` logged three errors and emitted nothing: the Name source
path in the allocation pass only looked in the local symbol table, so a
read that needs no declaration failed anyway. Resolve local first, then
the BPF globals, and give the copy the source's type as the local-source
case already does.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
`global x` covers the whole function body, so a body that reads x above
the declaration has two incompatible readings of that line. Python
rejects it with "name 'x' is used prior to global declaration"; say the
same thing rather than silently picking the global.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Assigning a global's name without declaring it `global` was a compile
error. Python makes it a local instead: the name is local for the whole
function body, the global keeps its value, and only a `global` statement
writes through to it. Do that, so the same program means the same thing
in Python and in BPF.
Python's scoping is function-wide and static, which the compiler gets
for free: allocation runs as a pre-pass over the whole body, so a name
bound anywhere is in local_sym_tab before the first statement is
lowered, and reads resolve local-first. What does not come for free is
the read above the binding, which Python answers with UnboundLocalError
rather than the global's value. There is no runtime here in which to
raise that, so the local records where its binding ends and a read above
it is rejected, with the message Python would have given. Silence there
would load an uninitialised stack slot through a name the author
expected to be the global.
`counter += 1` without `global counter` stays an error for the same
reason: it binds a local and reads it in the same statement. Only its
message changes, from a shadowing refusal to the UnboundLocalError it
actually is.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
LocalSymbol, BpfGlobalSymbol and MapSymbol each named the storage behind a
name differently (var, var, sym) and shared no type. They now all derive from
symbols.Symbol, which carries what every symbol has -- the storage pointer
(alloca, GlobalVariable, or None for the context parameter) and its IR type
-- and each subclass adds only what its kind needs: LocalSymbol its metadata
and declared_global flag, BpfGlobalSymbol its ctypes name, MapSymbol its map
type and params. MapSymbol.sym becomes var to match.
local_symbol.py is folded into the new symbols.py. LocalSymbol keeps its
three-field __iter__ for the call sites that tuple-unpack a symbol.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
A 'global x' statement is a fact about one function's scope, but it was
tracked as current_func_globals on the compilation-wide context -- per-function
state living at the wrong level, which had to be set before each body and
reset after, and which every read and write site then had to consult in a
specific order relative to local_sym_tab (the source of the parameter
collision fixed in f4ec12d).
The declaration now does what it means: it binds the name in local_sym_tab to
the @bpfglobal's storage, as a LocalSymbol whose var is the GlobalVariable and
whose declared_global flag records why. A GlobalVariable is a pointer to
storage exactly as an alloca is, so every existing path that loads from or
stores through a symbol's var works unchanged. That deletes the dedicated
global-store branch in handle_variable_assignment, the global branch in
handle_aug_assign, the allocation-pass skip, and the reset -- one table, one
lookup, no order to get wrong. Reads of an undeclared global (legal in Python)
still fall back to bpf_globals after local_sym_tab.
Python's rules fall out naturally: a parameter is already in the table when
the declarations are processed, so 'global ctx' on a parameter is detected by
the same membership check. LocalSymbol.__iter__ deliberately still yields
three fields, since several sites tuple-unpack a symbol.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
Reads resolve a name local-first (_handle_name_expr, get_operand_value, the
printk formatter), but both write paths checked the declared-global set
before local_sym_tab. Normally harmless because the two tables are disjoint
by construction -- except for a parameter whose name matches a @bpfglobal.
With 'global ctx' in a function taking ctx, the write path stored to the
global while a read of the same name picked the parameter and crashed on its
None slot: the same name resolving to different storage depending on which
side of an assignment it sat.
Python forbids the construct outright, and now so does the compiler, with
Python's own wording: SyntaxError: name 'ctx' is parameter and global. Both
write paths now resolve local-first like the reads, so all name lookups read
the same way; and an augmented assignment to the context parameter itself
gets a clear error instead of a store to None.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
Operator knowledge was spread across three files: the binary-op table (an
op_map dict rebuilt inline on every _handle_binary_op_impl call, then
extracted to ir_ops.apply_binop for augmented assignment), COMPARISON_OPS in
type_normalization.py, and the unary/boolean operators as isinstance chains
in expr_pass.py. Anyone asking "which operators does PythonBPF support" had
to read all three.
They now live in one module. BINOP_METHODS and COMPARISON_OPS are the tables
themselves; UNARY_OPS and BOOL_OPS list the operators whose lowering is
structural (convert_to_bool, short-circuit control flow) so the registry is
complete even where the emission stays in expr_pass. apply_binop moves here
from ir_ops, which goes back to being purely about IR operations. No
behaviour change: 133 passed, 19 xfailed before and after.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
Captures the trade-off behind reworking augmented assignment: desugaring into
synthetic AST is a legitimate compiler technique with real benefits, and it is
still the wrong shape for this compiler, whose passes communicate through the
source tree. Records both sides so the next person tempted by it declines for
the right reasons, and states the idiom to reach for instead -- shared
value-level helpers, not shared AST.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
A struct-field test with a non-add operator and a sub-64-bit field (the
narrow-on-store path), and a strict expected failure for counter += 1 without
a global statement, which now fails as loudly as plain assignment does. Ruff
flags that line as F823 (referenced before assignment) -- the same
UnboundLocalError Python itself would raise, which is exactly the semantics
the global statement requirement mirrors.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
handle_struct_field_assignment stored the evaluated value as-is, and since
expressions evaluate in i64 that meant any assignment to a sub-64-bit field
(dat.hits = 1 with hits: c_uint32) died in llvmlite with "cannot store i64 to
i32*". Apply the same implicit widening/truncation the local-assignment path
already has. Surfaced by the augmented-assignment struct-field test.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
The first version synthesised an Assign(BinOp(...)) node and fed it back
through handle_assign. That is a standard compiler move, but it is not how
this compiler works: every pass walks the tree the user wrote, and a node
invented mid-codegen is invisible to the allocation pass that already ran,
carries no source location for diagnostics, and shows up in logs as a
statement nobody typed.
handle_aug_assign now resolves the target slot (declared global, local, or
struct field), loads it, evaluates the right-hand side, applies the operator,
and stores back. Semantic agreement with "x = x op v" no longer rests on
shared AST but on shared value-level helpers: the RHS goes through
get_operand_value like any other read, and the operator table is extracted
from binary-op evaluation into apply_binop so it exists exactly once.
Two behavioural refinements fall out: augmented assignment to a global name
without a global statement is now the same loud SyntaxError plain assignment
gives (previously it slipped past the allocation pass, which only walks
Assign), and struct-field targets compute their GEP once rather than twice.
Target read precedes RHS evaluation, matching Python.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
Two tests locking the shadowing semantics: a local named XDP_PASS returned
from a program (the exact case the return fast path used to hijack), and a
vmlinux-importing program where a local shadows one enum constant while a
@bpfglobal shadows another, read in expressions and f-strings.
The vmlinux test needs noqa on its import and both collision sites: ruff
rightly flags unused imports and redefinitions in normal Python, and this file
is deliberately made of them.
These pin compilation; the value-level proof (ret of 55, not 2) was verified
against the emitted IR and the clang reference for the same collision shapes.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
handle_return consulted the hardcoded XDP action table before any symbol
table, so with a local XDP_PASS = 55 in scope, "return XDP_PASS" silently
returned 2. clang treats the equivalent C (a local shadowing an enum constant)
as the local winning, and every other resolution site in the compiler already
resolves local, then global, then vmlinux -- the return fast path was the one
place with the order inverted. It now applies only when the name is bound in
neither table.
Also warn when a @bpfglobal shadows a vmlinux enum constant. C rejects that
outright as a redefinition; PythonBPF follows the rebinding semantics the
Python file itself has (the global wins, consistently), but the collision is
worth a compile-time warning rather than silence.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
Five passing tests: read in condition and print; the get_cgroup_id_kern.c
read-one-write-one shape; the counter += 1 idiom; two programs sharing global
state (the test_autoattach.c shape); and mixed .bss/.data placement with mixed
widths and signedness.
Three strict expected failures documenting deliberate errors: assignment
without the global statement (shadowing), a global statement naming a
non-global, and a non-integer global type.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
Each integer global gets a DIGlobalVariableExpression with a C-named basic
type (matching signedness and width), attached via !dbg -- the same mechanism
map_debug_info.py already uses. llc's BPF backend manufactures the BTF VAR and
DATASEC '.bss'/'.data' entries from it:
[2] VAR 'expected_pid' type_id=1, linkage=global
[3] VAR 'cg_id' type_id=1, linkage=global
[4] DATASEC '.bss' size=0 vlen=2
Verified against clang's output for the same shapes: the in-object DATASEC
var offsets read 0 in both (libbpf patches them from the ELF symbol table at
load), and the ELF symbols carry the real layout, identical to the reference
(expected_pid at .bss+0, cg_id at .bss+8).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
AugAssign previously raised 'not supported' outright. Desugaring to an
ordinary assignment with a BinOp reproduces its Python semantics for Name and
Attribute targets and reuses the whole existing assignment path -- locals,
struct fields, and the new globals all work, so 'counter += 1' (the canonical
global idiom in the kernel selftest corpus) compiles to load/add/store on
@counter.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
Python's own scoping marks the write: 'global cg_id' declares that assignments
to cg_id in this function mean the BPF global, and the assignment then emits
the plain 'store i64 %v, ptr @cg_id' of the C reference, with the same implicit
widening/truncation rules as local assignments.
Without the declaration, an assignment to a global's name is refused:
SyntaxError: assignment to 'cg_id' shadows the BPF global of the same
name -- add 'global cg_id' to write to it
That is deliberate. In real Python such an assignment creates a shadowing
local; silently compiling it as either a local or a global store would be wrong
in one direction or the other, so it is a loud error instead. 'global' naming
something that is not a @bpfglobal is also a compile error.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
Name resolution tried local_sym_tab, then vmlinux enums, then failed. Globals
now resolve between the two, in _handle_name_expr, get_operand_value, and the
printk f-string formatter, each emitting the plain 'load i64, ptr @counter'
form the C reference produces. A global therefore works in conditions, binops,
print arguments and as a helper scalar argument with no further changes --
those paths all funnel through eval_expr/get_operand_value.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
globals_processing emitted each global and forgot it, which is why nothing
could ever read one back. Each integer-scalar global now lands in
CompilationContext.bpf_globals as a BpfGlobalSymbol; non-scalar globals get a
clear NotImplementedError instead of emitting something unusable. LICENSE stays
special-cased and unaffected.
Alignment becomes natural (width/8) instead of a blanket 8, matching what clang
emits for the same declarations in tests/c-form/global_vars.bpf.c -- llc
derives the BTF DATASEC layout from these symbols.
Also adds current_func_globals to the context for the upcoming write support.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
Captures the development loop this project has always used for new compiler
features: write a minimal C eBPF program exercising only the feature, compile
it to LLVM IR and read that as the specification, diff against what PythonBPF
currently emits, stop for a human decision on the Python syntax, implement
against the reference, and test at the right tier. Recorded as a project skill
so agents follow it too; the syntax step is an explicit hard stop.
tests/c-form/global_vars.bpf.c is the loop's step 1 for global variables: the
four C global classes and nothing else. Its IR established that every global is
an independent symbol accessed by plain load/store, that llc manufactures the
BTF VAR/DATASEC entries from DIGlobalVariable metadata, and that clang folds
const but not const volatile -- the facts the globals design rests on.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
* widen bitfields whose declared width exceeds their base type
clang2py can emit a bitfield typed as a smaller ctype than its own
declared width (e.g. a 15-bit field typed ctypes.c_ubyte, which only
has 8 bits) - ctypes rejects these with 'ValueError: number of bits
invalid for bit field'. Surfaced by struct_vmbus_channel_offer_channel
on a kernel with Hyper-V support enabled (not present locally, but
present on GitHub Actions' Azure-hosted runners).
Generalizes the existing c_bool-specific workaround to all integer
ctypes, widening to the smallest standard type that fits.
* fix ruff-format violation in vmlinux-gen.py
* Add CI workflow to run the test suite (#96)
* add CI workflow to run the test suite
Runs make test on every push/PR. Kernel verifier tests need passwordless
sudo and a kernel with BTF/BPF enabled, which isn't guaranteed on every
runner, so the workflow probes for working sudo first and only attempts
them if it's available.
Also add ctypeslib2 to the test extra: make test now regenerates
vmlinux.py via tools/vmlinux-gen.py, which needs clang2py.
* fix CI: bpftool is a virtual package on Ubuntu, install linux-tools instead
* fix CI: locate bpftool binary and add it to PATH manually
linux-tools-generic's update-alternatives symlink for bpftool doesn't
fire on GitHub-hosted runners, since their kernel version has no
matching linux-tools-<version> package.
* pin clang bindings to 16.0.6 to match Ubuntu's default libclang
pip installs the latest 'clang' release by default, whose libclang API
surface is newer than Ubuntu 24.04's apt libclang-16, causing a
LibclangError about an undefined symbol. Older bindings against a
newer libclang stay compatible, so pin the bindings low rather than
the system library.
* DEBUG: dump struct_vmbus_channel_offer_channel on test failure
* remove debug step now that the bitfield issue is fixed
* fix CI: install LLVM 19 from apt.llvm.org, Ubuntu's default is too old
llvmlite>=0.49's ArgumentAttributes only recognizes the LLVM 19+
'captures(none)' spelling of the renamed 'nocapture' attribute.
Ubuntu noble's default llvm/clang packages are LLVM 18, whose llc
can't parse that attribute in the emitted .ll text.
* DEBUG: show real llc stderr on failure
* fix YAML syntax in debug step
* fix CI: install LLVM 22 (matching llvmlite's bundled version), not 19
Verified on CI: LLVM 19's llc still rejects 'captures(none)' as a
parse error ('expected ) at end of argument list'). llvmlite 0.49
bundles LLVM 22.1.0 internally; match that generation instead.
* remove debug step, LLVM 22 fix confirmed working on CI
* avoid running CI twice per push: scope push trigger to master
Branches live in this repo, not forks, so a push to a branch with an
open PR fired both push and pull_request for the same commit. push
now only fires for master (a post-merge check); pull_request already
covers every commit on a feature branch.
- make test/test-cov/test-verifier now depend on a 'vmlinux' target that
regenerates vmlinux.py from the running kernel's BTF and symlinks it
into every directory under tests/, replacing the stale, duplicated
vmlinux.py fixtures that used to be committed there.
- collector.py now skips the 'vmlinux.py' filename when collecting BPF
test cases, since it's a fixture module, not a test program - it was
previously being mis-collected and compiled as a bogus test.
- functions_pass.py: llvmlite >=0.49 renamed the 'nocapture' argument
attribute to 'captures(none)'.
Prevents ctypes _fields_ assignment collisions when multiple anonymous
struct/union members across different structs get the same generic
'_N' name from clang2py.
It generates IR and compiles, and the CO-RE relocations against struct xdp_md
resolve correctly, so it must keep passing levels 1 and 2. The kernel rejects
it: the 'data + 34 < data_end' guard is emitted as a signed compare over values
round-tripped through the stack, so the verifier never narrows the packet range
and the later iph.saddr read fails with
invalid access to packet, off=26 size=4, R1(id=0,off=26,r=0)
R1 offset is outside of the packet
Direct packet access needs bounds checks in a form the verifier can follow.
strict = True, so this flags up if that ever lands.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>