diff --git a/tests/failing_tests/return_struct.py b/tests/failing_tests/return_struct.py new file mode 100644 index 0000000..4d29a0b --- /dev/null +++ b/tests/failing_tests/return_struct.py @@ -0,0 +1,28 @@ +# A struct value has no integer to reach by dereferencing, so returning one +# from an integer function is a type error, raised by convert() with both +# types named rather than left for llc to reject with an IR line number. +from pythonbpf import bpf, struct, section, bpfglobal, compile +from ctypes import c_void_p, c_int64, c_uint64 + + +@bpf +@struct +class task_info: + pid: c_uint64 + + +@bpf +@section("tracepoint/raw_syscalls/sys_enter") +def prog(ctx: c_void_p) -> c_int64: + t = task_info() + t.pid = 1 + return t + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/return/map_value.py b/tests/passing_tests/return/map_value.py new file mode 100644 index 0000000..b866e44 --- /dev/null +++ b/tests/passing_tests/return/map_value.py @@ -0,0 +1,31 @@ +# `return` is a consumer of a value like any other: a pointer to one is +# dereferenced (null-checked) to reach it, so returning a map lookup result +# works the way `p + 0` already did. C shape: `if (p) return *p; return 0;` +from pythonbpf import bpf, map, section, bpfglobal, compile +from pythonbpf.maps import HashMap +from ctypes import c_void_p, c_int64, c_uint32, c_uint64 + + +@bpf +@map +def m() -> HashMap: + return HashMap(key=c_uint32, value=c_uint64, max_entries=4) + + +@bpf +@section("tracepoint/raw_syscalls/sys_enter") +def prog(ctx: c_void_p) -> c_int64: + k = c_uint32(1) + p = m.lookup(k) + if p: + return p + return c_int64(0) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/vmlinux/enum_rank.py b/tests/passing_tests/vmlinux/enum_rank.py new file mode 100644 index 0000000..ffab214 --- /dev/null +++ b/tests/passing_tests/vmlinux/enum_rank.py @@ -0,0 +1,24 @@ +# An enum constant has C's `int` type, whatever the enum's underlying type is +# (clang: `XDP_PASS - k` is `sub nsw i32` + sext; a *variable* of the enum +# type would be u32). So `XDP_PASS - k` with an unsigned k is a u32 operation, +# the same rank a literal that fits in int gets. +from ctypes import c_int64, c_uint32, c_void_p +from pythonbpf import bpf, section, bpfglobal, compile +from vmlinux import XDP_PASS + + +@bpf +@section("tracepoint/raw_syscalls/sys_enter") +def prog(ctx: c_void_p) -> c_int64: + k = c_uint32(3) + d = XDP_PASS - k + return c_int64(d) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/test_config.toml b/tests/test_config.toml index 77e068b..a8ba472 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -24,6 +24,8 @@ # in a form the verifier can follow. "failing_tests/xdp/xdp_test_1.py" = {reason = "XDP direct packet access: the data/data_end guard does not establish a packet range the verifier can use", level = "verifier"} +"failing_tests/return_struct.py" = {reason = "Returning a struct value from an integer function is a type error (nothing to dereference to an integer)", level = "ir"} + "failing_tests/license.py" = {reason = "Missing LICENSE global produces IR that llc rejects — should be caught earlier with a clear error message", level = "llc"} "failing_tests/undeclared_values.py" = {reason = "Undeclared variable used in f-string — should raise SyntaxError (correct behaviour, test documents it)", level = "ir"} diff --git a/tests/test_signedness_ir.py b/tests/test_signedness_ir.py index 0b0c113..f44eeed 100644 --- a/tests/test_signedness_ir.py +++ b/tests/test_signedness_ir.py @@ -8,6 +8,7 @@ is the whole point of the cases: zext vs sext on widening, udiv vs sdiv, icmp ugt vs sgt, lshr vs ashr, and the trunc/zext pair that wraps u32 * u32. """ +import importlib.util import re from pathlib import Path @@ -15,46 +16,62 @@ import pytest from tests.framework.compiler import run_ir_generation -SIGNEDNESS_DIR = Path(__file__).parent / "passing_tests" / "signedness" +PASSING_DIR = Path(__file__).parent / "passing_tests" +HAVE_VMLINUX = importlib.util.find_spec("vmlinux") is not None -# file -> (patterns that must appear, patterns that must not) +# path under passing_tests -> (patterns that must appear, patterns that must not) CASES = { - "widen_unsigned.py": ( + "signedness/widen_unsigned.py": ( [r"zext i32 .* to i64"], [r"sext i32 .* to i64"], ), - "widen_signed.py": ( + "signedness/widen_signed.py": ( [r"sext i32 .* to i64"], [r"zext i32 .* to i64"], ), - "mixed_division.py": ( + "signedness/mixed_division.py": ( [r"\budiv i64", r"\burem i64", r"\bsdiv i64"], [r"\bsrem i64"], ), - "unsigned_compare.py": ( + "signedness/unsigned_compare.py": ( [r"icmp ugt i64", r"icmp sgt i64"], [], ), - "narrow_wrap.py": ( + "signedness/narrow_wrap.py": ( # the product is computed at 64 bits, cut to 32, then zero-extended [r"\bmul i64", r"trunc i64 .* to i32", r"zext i32 .* to i64"], [r"sext i32 .* to i64"], ), - "right_shift.py": ( + "signedness/right_shift.py": ( [r"\blshr i64 .*, 4", r"\bashr i64 .*, 4"], [], ), - "literal_rank.py": ( + "signedness/literal_rank.py": ( [r"\budiv i64 .*, 4294967294"], [r"\bsdiv i64"], ), + # An enum constant is a C `int`, so `XDP_PASS - k` with k a c_uint32 is a + # u32 operation: the result is cut to 32 bits and zero-extended. Ranked + # as i64 it would be a signed 64-bit subtraction with no trunc at all. + "vmlinux/enum_rank.py": ( + [r"trunc i64 .* to i32", r"zext i32 .* to i64"], + [r"sext i32 .* to i64"], + ), + # `return p` on a map lookup dereferences through a null check and returns + # the i64, never the pointer. + "return/map_value.py": ( + [r"deref_0_not_null", r"ret i64 %"], + [r"ret i64\*"], + ), } @pytest.mark.parametrize("name", list(CASES)) def test_signedness_ir_shape(name, tmp_path): - ll_path = tmp_path / name.replace(".py", ".ll") - run_ir_generation(SIGNEDNESS_DIR / name, ll_path) + if name.startswith("vmlinux/") and not HAVE_VMLINUX: + pytest.skip("vmlinux.py not importable") + ll_path = tmp_path / Path(name).name.replace(".py", ".ll") + run_ir_generation(PASSING_DIR / name, ll_path) ir_text = ll_path.read_text() expected, forbidden = CASES[name]