Files
python-bpf/tests/property/known_gaps.py
Pragyansh Chaturvedi 32c1afff36 Tests: Make the integer properties cover every type on every run
Review of the first version of the suite found four holes.

- The conversion sites returned the converted value, which shows its
  bits but not the type it was stored as. The untyped_local site passed
  over a real bug that way. Each site now reads the value back through
  an expression whose result depends on its sign and width.
- With the type drawn at random, 25 examples per site left a single type
  with about 3. A deliberately broken c_bool conversion went undetected
  under some seeds. The conversion property is now parametrized by
  destination type and the operator property by left operand type, a
  sixteenth of the profile's examples each.
- The global_init site was a strict xfail. Hypothesis shrank its
  expected failures, which took 115 of the property's 187 seconds. Its
  two bugs are now pinned in test_known_gaps.py, and the site steps
  around them.
- Gaps are now stepped around by drawing only inputs that avoid them,
  not by assume(). Filtering tripped Hypothesis's filter_too_much health
  check.

untyped-copy-keeps-narrow-type is renamed untyped-copy-keeps-source-type.
`t = a` keeps a's type for any source that is not 64-bit, c_uint32 and
c_int32 included, not only narrow ones.

Under the ci profile, each of these deliberate compiler bugs fails a
property:
- >> always logical
- % always unsigned
- / always unsigned
- equal-width mixed signs going signed
- promotion keeping an unsigned sign
- widening always by zero-extension
- c_bool conversion by truncation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 21:55:59 +05:30

134 lines
4.6 KiB
Python

"""
Known divergences from C that the property tests step around.
Each gap has a predicate that recognises a generated program hitting it, so
the properties keep looking for *new* bugs instead of re-finding this one, and
a pinned program that test_known_gaps.py runs as a strict xfail. When a fix
lands, that test XPASSes and fails the run: delete the gap here then, and the
properties start generating those programs again.
"""
from dataclasses import dataclass
from typing import Callable
from tests.property.cints import (
C_INT8,
C_INT32,
C_INT64,
C_UINT8,
C_UINT32,
C_UINT64,
COMPARE_OPS,
SHIFT_OPS,
common_type,
promote,
untyped_local_type,
)
from tests.property.programs import Bin, ExpressionProgram, Lit, Neg, Not, Var
def walk(expr):
yield expr
for child in ("operand", "left", "right"):
if hasattr(expr, child):
yield from walk(getattr(expr, child))
def nodes(program: ExpressionProgram):
for _, _, expr in program.temps:
yield from walk(expr)
yield from walk(program.result)
@dataclass(frozen=True)
class Gap:
name: str
reason: str
hits: Callable[[ExpressionProgram], bool]
# The smallest program showing it, run by test_known_gaps.py as a strict xfail
example: ExpressionProgram
GAPS: list[Gap] = [
Gap(
"negated-literal-rank",
"`-2147483648` folds to a literal ranked by its negated value (int); "
"C ranks -x by x, and 2147483648 is a long long "
"(pythonbpf/expr/expr_pass.py, the USub constant fold)",
lambda p: any(
isinstance(n, Neg)
and isinstance(n.operand, Lit)
and n.operand.value == 2**31
for n in nodes(p)
),
# C: a 64-bit signed compare, 1. PythonBPF: an unsigned 32-bit one, 0.
ExpressionProgram(
[("a0", C_UINT32, 1)], [], Bin("<", Neg(Lit(2**31)), Var("a0", C_UINT32))
),
),
Gap(
"untyped-local-unary-or-compare",
'`t = -x`, `t = not x` and `t = a < b` fail with "Undefined variable '
't": the allocation pass has no case for a UnaryOp or Compare '
"right-hand side, so the local never gets a slot "
"(pythonbpf/allocation_pass.py, _allocate_for_value)",
lambda p: any(
isinstance(e, (Neg, Not)) or (isinstance(e, Bin) and e.op in COMPARE_OPS)
for _, _, e in p.temps
),
ExpressionProgram(
[("a0", C_INT32, 3)],
[("t0", C_INT64, Neg(Var("a0", C_INT32)))],
Var("t0", C_INT64),
),
),
Gap(
"shift-result-type",
"a shift is typed by the usual arithmetic conversions of both operands; "
"C types it by the promoted left operand alone, so `int << s64` must "
"wrap at 32 bits and `s64 >> u64` must be arithmetic "
"(pythonbpf/expr/expr_pass.py _handle_binary_op_impl, "
"type_inference.py infer_int_type, and for `<<=`/`>>=` "
"functions/functions_pass.py's augmented assignment)",
lambda p: any(
isinstance(n, Bin)
and n.op in SHIFT_OPS
and promote(n.left.ty) != common_type(n.left.ty, n.right.ty)
for n in nodes(p)
),
# C: -8 >> 1 in s64, -4. PythonBPF: a logical shift in u64.
ExpressionProgram(
[("a0", C_INT64, -8), ("a1", C_UINT64, 1)],
[],
Bin(">>", Var("a0", C_INT64), Var("a1", C_UINT64)),
),
),
Gap(
"untyped-copy-keeps-source-type",
"`t = a` gives t a's own type, whatever it is: a c_uint32 copy stays "
"u32 and a c_int8 copy stays narrow and promotes to int, while "
"integers.md says an undeclared local is always 64 bits with the "
"expression's sign. Only a 64-bit source is unaffected. Code or doc: "
"a decision (pythonbpf/allocation_pass.py, _allocate_for_name)",
lambda p: any(
isinstance(e, Var) and e.ty != untyped_local_type(e.ty)
for _, _, e in p.temps
),
# C, by integers.md: -128 + (u64)1 is a u64, so 0 < it holds. PythonBPF:
# t1 stays a u8, the sum is the int -127, and the compare is false.
ExpressionProgram(
[("a0", C_INT8, -128), ("a1", C_UINT8, 1)],
[("t1", untyped_local_type(C_UINT8), Var("a1", C_UINT8))],
Bin(
"<",
Lit(0),
Bin("+", Var("a0", C_INT8), Var("t1", untyped_local_type(C_UINT8))),
),
),
),
]
def gap_of(program: ExpressionProgram) -> "Gap | None":
return next((g for g in GAPS if g.hits(program)), None)