mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
Four programs from prog_tests/bpf_verif_scale.c that loops alone kept out. The loop lowering matches clang's for the same C, but volatile, barrier() and the no-unroll pragma have no spelling here: loop2 folds to a constant and loop4 unrolls, so those two no longer make the verifier walk a loop. Their headers and PORTING-NOTES.md say so. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
# Ported from Linux tools/testing/selftests/bpf/progs/loop4.c
|
|
#
|
|
# A verifier-scale test (prog_tests/bpf_verif_scale.c, test_verif_scale_loop4):
|
|
# a 20-iteration loop whose body branches on a context field, so the verifier
|
|
# sees 2^20 paths unless it prunes states. Upstream:
|
|
#
|
|
# SEC("socket")
|
|
# int combinations(volatile struct __sk_buff* skb)
|
|
# {
|
|
# int ret = 0, i;
|
|
#
|
|
# __pragma_loop_no_unroll
|
|
# for (i = 0; i < 20; i++)
|
|
# if (skb->len)
|
|
# ret |= 1 << i;
|
|
# return ret;
|
|
# }
|
|
#
|
|
# NOTE: this port does not test what upstream tests. `volatile` and the
|
|
# no-unroll pragma have no PythonBPF spelling, so skb->len is loaded once and
|
|
# LLVM fully unrolls the loop (97 instructions against upstream's 14): the
|
|
# verifier walks straight-line code instead of pruning states across a loop.
|
|
# Clang does the same to the C with `volatile` and the pragma removed. Revisit
|
|
# when volatile reads land.
|
|
|
|
from pythonbpf import bpf, section, bpfglobal, compile
|
|
from vmlinux import struct___sk_buff
|
|
from ctypes import c_int32
|
|
|
|
|
|
@bpf
|
|
@section("socket")
|
|
def combinations(skb: struct___sk_buff) -> c_int32:
|
|
ret: c_int32 = 0
|
|
for i in range(20):
|
|
if skb.len:
|
|
ret |= 1 << i
|
|
return ret
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def LICENSE() -> str:
|
|
return "GPL"
|
|
|
|
|
|
compile()
|