mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
Tests: Load without -d in the verifier tests, and report a timeout properly
-d asks the kernel for the instruction-level verifier log on every load. For loop1, which makes the verifier walk nested loops, that log outgrows libbpf's buffer; the kernel fails the load with ENOSPC, libbpf doubles the buffer and verifies again, and on CI the load ran past the 30s timeout. A rejection still comes with its log without -d, since libbpf retries a failed load at log level 1 and prints it as a warning. The timeout path also returned a bare string where callers expect stdout and stderr, so a timeout surfaced as an AttributeError instead of the timeout. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@ -3,25 +3,34 @@ import uuid
|
||||
from collections import namedtuple
|
||||
from pathlib import Path
|
||||
|
||||
Output = namedtuple("Output", ["stdout", "stderr"])
|
||||
|
||||
def verify_object(obj_path: Path) -> tuple[bool, str]:
|
||||
"""Run bpftool prog load -d to verify a BPF object file against the kernel verifier.
|
||||
|
||||
def verify_object(obj_path: Path) -> tuple[bool, Output]:
|
||||
"""Run bpftool prog load to verify a BPF object file against the kernel verifier.
|
||||
|
||||
Pins the program temporarily at /sys/fs/bpf/bpf_prog_test_<uuid>, then removes it.
|
||||
Returns (success, combined_output). Requires sudo / root.
|
||||
Returns (success, Output(stdout, stderr)). Requires sudo / root.
|
||||
|
||||
No -d: it asks the kernel for the full instruction-level log (log level
|
||||
1+2+4) on every load. For a program that makes the verifier walk a loop that
|
||||
log outgrows libbpf's buffer, the kernel then fails the load with ENOSPC, and
|
||||
libbpf doubles the buffer and verifies the whole program again, until the
|
||||
load takes longer than the timeout. A rejection still comes with its log:
|
||||
libbpf retries a failed load at log level 1 and prints the log as a warning.
|
||||
"""
|
||||
pin_path = f"/sys/fs/bpf/bpf_prog_test_{uuid.uuid4().hex[:8]}"
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["sudo", "bpftool", "prog", "load", "-d", str(obj_path), pin_path],
|
||||
["sudo", "bpftool", "prog", "load", str(obj_path), pin_path],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=30,
|
||||
)
|
||||
Output = namedtuple("Output", ["stdout", "stderr"])
|
||||
output = Output(stdout=result.stdout, stderr=result.stderr)
|
||||
return result.returncode == 0, output
|
||||
return result.returncode == 0, Output(
|
||||
stdout=result.stdout, stderr=result.stderr
|
||||
)
|
||||
except subprocess.TimeoutExpired:
|
||||
return False, "bpftool timed out after 30s"
|
||||
return False, Output(stdout="", stderr="bpftool timed out after 30s")
|
||||
finally:
|
||||
subprocess.run(["sudo", "rm", "-f", pin_path], check=False, capture_output=True)
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
Level 3 — Kernel verifier tests.
|
||||
|
||||
For every passing BPF test file, compiles to a .o and runs:
|
||||
sudo bpftool prog load -d <file.o> /sys/fs/bpf/bpf_prog_test_<id>
|
||||
sudo bpftool prog load <file.o> /sys/fs/bpf/bpf_prog_test_<id>
|
||||
|
||||
These tests are opt-in: they require sudo and kernel access, and are gated
|
||||
behind the `verifier` pytest mark. Run with:
|
||||
|
||||
Reference in New Issue
Block a user