Commit Graph

21 Commits

Author SHA1 Message Date
4cb5f01374 Merge origin/master (#105) into fix/packet-pointer-arith
Four keep-both conflicts: PktPtrTy and byte_size added at the same spot
in type_deducer, the allocation pass's type_deducer import, and
neighbouring entries in test_config.toml and test_signedness_ir.py.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 23:09:31 +05:30
20a1e2f3a1 Core: Accept c_bool as a ctypes type
c_bool was missing from the ctypes table, so c_bool(5) was an unknown
call, its slot was never allocated, and the error read "Undefined
variable". It is C's _Bool: one bit, like the True/False locals, never
negative, and a value narrows to it by comparing with zero, which the
signedness rules already apply to 1-bit types. It works as a local, a
struct field, a map value and a global; in BTF it is a one-byte _Bool.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 22:20:07 +05:30
4e8f767e6d Core: Size and describe integers by their real width
Two places took an integer's width as 32 or 64 bits. Ten sites computed
byte sizes as width // 8 (alignment, struct layout, globals), which is
zero for a 1-bit type; they now share byte_size(), which rounds up to
whole bytes. And debug info described every map key or value, and every
struct field, that was not 32 bits wide as unsigned long long: a c_uint8
or c_uint16 map value was declared 8 bytes wide in BTF, so the kernel
copied 8 bytes from a smaller slot. get_int_type() now gives the real
width and sign everywhere, globals included.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 22:20:05 +05:30
b9b582a7d8 Core: Carry the packet-pointer kind on the type descriptor
The previous commit recognised a packet-pointer field only where it was
read directly in a binary operation, so d = ctx.data made d an ordinary
integer again and comparisons such as data + 34 > data_end were still
ranked u32 and emitted as 32-bit compares, which the verifier rejects.

PktPtrTy is a 64-bit unsigned IntTy tagged with the verifier's kind
(pkt, pkt_meta, pkt_end). It originates at the field read and travels
as the descriptor does: into a local's slot, through pointer +/- offset
(which yields the same kind), and into comparisons, which compare at
64 bits unsigned whenever either side is a packet pointer. It is handled
before the usual arithmetic conversions, which would re-rank it.

Two rules the verifier enforces become compile errors: a packet-end
pointer takes no offset, and convert() refuses to narrow a packet
pointer below 64 bits.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 13:03:30 +05:30
532dc0f808 Core: Rank ctx fields and map values by their declared types
Two descriptors were built from the physical value instead of the
declaration. A sub-register vmlinux field is widened to i64 by
load_ctx_field, and its descriptor took that width, so ctx.data - k with
a c_int32 k was a 64-bit subtraction where C makes it a u32 one. A
map-lookup local dereferenced past its slot took the physical pointee
(i64, signed by default) and dropped the map's declared value ctype kept
in the symbol's metadata, so p >> 63 on a c_uint64 value was an ashr.
Both now read the declaration; the physical width still comes from the
value, as everywhere else.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-24 19:30:37 +05:30
a230b440b9 Core: Give bools C's rules: widen to 0 or 1, narrow by comparing with zero
A bool is ir.IntType(1), so convert() handled it as a 1-bit integer: a
bool slot's plain descriptor read as signed and True sign-extended to
-1 on the way out, and an integer narrowed to a bool by truncation, so
2 became false. Both predate this branch. C widens a bool to 0 or 1 and
narrows to it with != 0, and Python's truthiness agrees on the second.

signedness() now answers unsigned for any 1-bit integer whatever it is
wrapped in, convert() narrows to width 1 with icmp ne 0, and a bool
constant's slot carries the unsigned descriptor like the comparison
results already do.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-24 16:52:59 +05:30
ca92f2ab77 Core: Rank an enum constant as C's int, not as i64
An enum constant has type int in C whatever the enum's underlying type
is; clang emits `sub nsw i32` + sext for `XDP_PASS - k` and reserves the
enum's u32 for variables of the enum type. The vmlinux handler was
typing constants as i64, which made `XDP_PASS - k` with an unsigned k a
signed 64-bit operation instead of C's u32 one. The constant's C rank
is now decided once, in type_deducer.int_literal_type, shared by
literals and enum constants, and the handler returns it as the
descriptor so both operand typing and local inference read it from
the source.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-18 17:52:28 +05:30
f266f1fce0 Core: Carry signedness from every declaration and inference site
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
2026-09-17 20:34:21 +05:30
daf429d463 Core: Add the signedness machinery (no behaviour change)
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
2026-09-17 20:34:21 +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
204ec26154 add i32 support and make it extensible 2025-11-01 14:44:39 +05:30
85a62d6cd8 add example and support unsigned i64 2025-11-01 08:13:22 +05:30
93285dbdd8 geenrate gep IR 2025-10-26 02:12:33 +05:30
f53ca3bd5b Add ctypes in eval_expr 2025-10-06 04:43:04 +05:30
8d5067996f format chore and pre commit hook addition 2025-10-01 00:41:00 +05:30
5adc6d75e9 janitorial 2025-09-09 00:26:06 +05:30
03404bb164 make license handling easier 2025-09-09 00:19:53 +05:30
7e47f8dcef Multi string and multi program support 2025-09-07 20:41:41 +05:30
1118e4fcd6 add naive unpythonic return type inference to function parsing 2025-09-07 20:12:39 +05:30
a8e7a42b72 WIP functions processing 2025-08-31 05:12:47 +05:30