diff --git a/tests/failing_tests/loops/range_step_too_wide.py b/tests/failing_tests/loops/range_step_too_wide.py new file mode 100644 index 0000000..4e1fab6 --- /dev/null +++ b/tests/failing_tests/loops/range_step_too_wide.py @@ -0,0 +1,23 @@ +# A step of 2**64 is a valid Python range() step (it yields just 0), but it +# does not fit the 64-bit counter: it truncates to 0 and the loop never ends. +# It should be a compile error. +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64 + + +@bpf +@section("tracepoint/syscalls/sys_enter_execve") +def hello(ctx: c_void_p) -> c_int64: + total: c_int64 = 0 + for i in range(0, 10, 18446744073709551616): + total = total + 1 + return total + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/framework/host_jit.py b/tests/framework/host_jit.py new file mode 100644 index 0000000..e865c1d --- /dev/null +++ b/tests/framework/host_jit.py @@ -0,0 +1,54 @@ +"""Run a BPF function's IR on the host, to check what it computes. + +Only for helper-free programs: the IR is retargeted to the host and JIT +compiled, and the function is called with a NULL ctx. A helper call is an +inttoptr to a BPF helper id, which on the host would jump to a bogus address. + +Run as a script (`python -m tests.framework.host_jit file.ll func`), so the +caller can put a timeout on a program that never terminates. +""" + +import ctypes +import re +import subprocess +import sys +from pathlib import Path + +import llvmlite.binding as llvm + + +def run_function(ll_text: str, func_name: str) -> int: + ll_text = re.sub(r'^target (triple|datalayout) = ".*"$', "", ll_text, flags=re.M) + llvm.initialize_native_target() + llvm.initialize_native_asmprinter() + mod = llvm.parse_assembly(ll_text) + mod.verify() + tm = llvm.Target.from_default_triple().create_target_machine() + engine = llvm.create_mcjit_compiler(mod, tm) + engine.finalize_object() + + ret = re.search(rf'define [^\n]*?\bi(\d+) @"?{re.escape(func_name)}"?\(', ll_text) + ret_ty = {64: ctypes.c_int64, 32: ctypes.c_int32}[int(ret.group(1))] + func = ctypes.CFUNCTYPE(ret_ty, ctypes.c_void_p)( + engine.get_function_address(func_name) + ) + return func(None) + + +def run_in_subprocess(ll_path, func_name: str, timeout: float) -> int: + """Return value of func_name(NULL). Raises subprocess.TimeoutExpired if it + does not return within timeout, CalledProcessError if it fails to run.""" + out = subprocess.run( + [sys.executable, "-m", "tests.framework.host_jit", str(ll_path), func_name], + cwd=Path(__file__).parents[2], + capture_output=True, + text=True, + timeout=timeout, + check=True, + ) + return int(out.stdout.strip()) + + +if __name__ == "__main__": + with open(sys.argv[1]) as f: + print(run_function(f.read(), sys.argv[2])) diff --git a/tests/passing_tests/loops/loop_var_shared.py b/tests/passing_tests/loops/loop_var_shared.py new file mode 100644 index 0000000..13eaab6 --- /dev/null +++ b/tests/passing_tests/loops/loop_var_shared.py @@ -0,0 +1,29 @@ +# Where a loop variable is visible outside its loop it stays one local, as in +# Python: the outer body reads the `i` the inner loop left behind, and the +# return reads the last loop's final `i`. Each outer iteration adds 0 + 1 from +# the inner loop and 1 after it, the last loop adds 50, and the return adds 4: +# 6 + 50 + 4 = 60. +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64 + + +@bpf +@section("tracepoint/syscalls/sys_enter_execve") +def hello(ctx: c_void_p) -> c_int64: + total: c_int64 = 0 + for i in range(3): + for i in range(2): + total = total + i + total = total + i + for i in range(5): + total = total + 10 + return total + i + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/loops/pass_body.py b/tests/passing_tests/loops/pass_body.py new file mode 100644 index 0000000..35c4ce4 --- /dev/null +++ b/tests/passing_tests/loops/pass_body.py @@ -0,0 +1,25 @@ +# `pass` is the natural body of a loop run only for its side effects, or of an +# else-branch kept for symmetry. Returns 3: the loop variable keeps its last +# value after the loop, as in Python. +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64 + + +@bpf +@section("tracepoint/syscalls/sys_enter_execve") +def hello(ctx: c_void_p) -> c_int64: + i: c_int64 = 0 + for i in range(4): + pass + else: + pass + return i + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/loops/range_step_overflow_signed.py b/tests/passing_tests/loops/range_step_overflow_signed.py new file mode 100644 index 0000000..9dc603f --- /dev/null +++ b/tests/passing_tests/loops/range_step_overflow_signed.py @@ -0,0 +1,31 @@ +# range() stops once the next value would pass stop; it never wraps. Here the +# first step overshoots INT64_MAX, so the counter must not wrap to a negative +# value that is still below stop; the second loop does the same below +# INT64_MIN. Leaving that way is a normal exit, so each else-branch runs. +# Returns 1 + 10 + 1 + 10 = 22. +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64 + + +@bpf +@section("tracepoint/syscalls/sys_enter_execve") +def hello(ctx: c_void_p) -> c_int64: + total: c_int64 = 0 + for i in range(9223372036854775806, 9223372036854775807, 2): + total = total + 1 + else: + total = total + 10 + for j in range(-9223372036854775807, -9223372036854775808, -3): + total = total + 1 + else: + total = total + 10 + return total + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/loops/range_step_overflow_unsigned.py b/tests/passing_tests/loops/range_step_overflow_unsigned.py new file mode 100644 index 0000000..32063ef --- /dev/null +++ b/tests/passing_tests/loops/range_step_overflow_unsigned.py @@ -0,0 +1,23 @@ +# The unsigned version of range_step_overflow_signed: the step goes past +# UINT64_MAX, and the counter must not wrap to 0, which is below stop. Returns 1. +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64, c_uint64 + + +@bpf +@section("tracepoint/syscalls/sys_enter_execve") +def hello(ctx: c_void_p) -> c_int64: + stop: c_uint64 = 18446744073709551615 + total: c_int64 = 0 + for i in range(18446744073709551614, stop, 2): + total = total + 1 + return total + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/loops/range_u32_stop_negative_start.py b/tests/passing_tests/loops/range_u32_stop_negative_start.py new file mode 100644 index 0000000..56b2e22 --- /dev/null +++ b/tests/passing_tests/loops/range_u32_stop_negative_start.py @@ -0,0 +1,25 @@ +# The counter is 64-bit, and a 64-bit signed value holds every u32, so by +# C's own rules a u32 stop does not make the loop unsigned: in +# `for (__s64 idx = -1; idx < n; idx++)` n widens to __s64. Returns 4, as +# range(-1, 3) does in Python. +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64, c_uint32 + + +@bpf +@section("tracepoint/syscalls/sys_enter_execve") +def hello(ctx: c_void_p) -> c_int64: + n: c_uint32 = 3 + total: c_int64 = 0 + for i in range(-1, n): + total = total + 1 + return total + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/loops/reuse_loop_var_signedness.py b/tests/passing_tests/loops/reuse_loop_var_signedness.py new file mode 100644 index 0000000..1e4a193 --- /dev/null +++ b/tests/passing_tests/loops/reuse_loop_var_signedness.py @@ -0,0 +1,28 @@ +# Two unrelated loops that reuse `i`. The first counts over an unsigned +# bound, the second over a signed one. The second loop's `i < 0` has to see +# -3, -2 and -1, not the unsigned slot the first loop left behind. +# Returns 3 + 3 * 100 = 303. +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64, c_uint64 + + +@bpf +@section("tracepoint/syscalls/sys_enter_execve") +def hello(ctx: c_void_p) -> c_int64: + n: c_uint64 = 3 + total: c_int64 = 0 + for i in range(n): + total = total + 1 + for i in range(-3, 3): + if i < 0: + total = total + 100 + return total + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/passing_tests/loops/while_true_return_i32.py b/tests/passing_tests/loops/while_true_return_i32.py new file mode 100644 index 0000000..7cb30e9 --- /dev/null +++ b/tests/passing_tests/loops/while_true_return_i32.py @@ -0,0 +1,24 @@ +# The only way out of `while True` is the return in its body, so the block +# after the loop is unreachable. Whatever closes it off has to match the +# function's c_int32 return type. Returns 6. +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int32 + + +@bpf +@section("tracepoint/syscalls/sys_enter_execve") +def hello(ctx: c_void_p) -> c_int32: + i: c_int32 = 0 + while True: + i = i + 1 + if i > 5: + return i + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/test_config.toml b/tests/test_config.toml index 309caff..453650f 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -44,6 +44,8 @@ "failing_tests/loops/range_variable_step.py" = {reason = "range() step must be an integer literal: its sign decides the loop test", level = "ir"} +"failing_tests/loops/range_step_too_wide.py" = {reason = "range() step must fit the 64-bit counter: 2**64 truncates to 0 and never terminates", level = "ir"} + "kernel_selftest_equivalent/ringbuf/reserve_submit_discard.py" = {reason = "RingBuffer reserve/typed record/discard workflow is planned but not implemented yet", level = "ir"} "kernel_selftest_equivalent/vmlinux/perf_skip.py" = {reason = "Nested struct field access (ctx.regs.ip) not supported; one level such as ctx.sample_period works", level = "ir"} diff --git a/tests/test_loops_run.py b/tests/test_loops_run.py new file mode 100644 index 0000000..72e21d4 --- /dev/null +++ b/tests/test_loops_run.py @@ -0,0 +1,71 @@ +""" +Loops: what they compute. + +Levels 1 and 2 only prove the passing_tests/loops cases compile. A loop that +compiles can still run the wrong number of times, or forever, so this test +JIT-compiles each helper-free case on the host and checks the value it returns +against what CPython returns for the same function body. Every expected value +below was checked that way. +""" + +import subprocess +from pathlib import Path + +import pytest + +from tests.framework.compiler import run_ir_generation +from tests.framework.host_jit import run_in_subprocess + +LOOPS_DIR = Path(__file__).parent / "passing_tests" / "loops" + +# file -> return value of hello(ctx) under CPython +CASES = { + "for_break.py": 5, + "for_continue.py": 25, + "for_range_start_stop_step.py": 20, + "for_range_sum.py": 10, + "loop_else.py": 104, + "nested_for.py": 9, + "range_negative_step.py": 22, + "rebind_loop_var.py": 10, + "while_basic.py": 10, + "while_true_break.py": 10, + "pass_body.py": 3, + "while_true_return_i32.py": 6, + "range_step_overflow_signed.py": 22, + "range_step_overflow_unsigned.py": 1, + "range_u32_stop_negative_start.py": 4, + "reuse_loop_var_signedness.py": 303, + "loop_var_shared.py": 60, +} + +# file -> why it returns something else today (strict: a fix shows as XPASS) +XFAIL = { + # Every local gets one slot typed by its first binding, so the second + # loop's `i` is unsigned and `i < 0` never holds. Not a loop bug: + # `x = n_u64; x = -3` does the same, so the fix is general (one variable + # per web of reaching definitions), not anything loop-specific. + "reuse_loop_var_signedness.py": "one slot per name, typed by its first binding", +} + + +@pytest.mark.parametrize( + "name", + [ + pytest.param(name, marks=pytest.mark.xfail(reason=XFAIL[name], strict=True)) + if name in XFAIL + else name + for name in CASES + ], +) +def test_loop_result(name, tmp_path): + expected = CASES[name] + ll_path = tmp_path / name.replace(".py", ".ll") + run_ir_generation(LOOPS_DIR / name, ll_path) + try: + got = run_in_subprocess(ll_path, "hello", timeout=10) + except subprocess.TimeoutExpired: + pytest.fail(f"{name}: did not terminate (expected to return {expected})") + except subprocess.CalledProcessError as e: + pytest.fail(f"{name}: could not run on the host:\n{e.stderr.strip()}") + assert got == expected, f"{name}: returned {got}, CPython returns {expected}"