mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
Compare commits
9 Commits
fix/verifi
...
releasescr
| Author | SHA1 | Date | |
|---|---|---|---|
| 40eead72a1 | |||
| 5311a16c21 | |||
| 8c5069f424 | |||
| 5a9c77f016 | |||
| d0f7a9dcec | |||
| 0dea1836df | |||
| 7cdb9cb2f5 | |||
| 0482aa2be9 | |||
| d4f1b5a9b4 |
7
Makefile
7
Makefile
@ -10,13 +10,12 @@ test:
|
|||||||
pytest tests/ -W ignore::DeprecationWarning -v --tb=short -m "not verifier"
|
pytest tests/ -W ignore::DeprecationWarning -v --tb=short -m "not verifier"
|
||||||
|
|
||||||
test-cov:
|
test-cov:
|
||||||
pytest tests/ -W ignore::DeprecationWarning -v --tb=short -m "not verifier" \
|
pytest tests/ -v --tb=short -m "not verifier" \
|
||||||
--cov=pythonbpf --cov-report=term-missing --cov-report=html
|
--cov=pythonbpf --cov-report=term-missing --cov-report=html
|
||||||
|
|
||||||
test-verifier:
|
test-verifier:
|
||||||
@echo "NOTE: verifier tests shell out to 'sudo bpftool'; run 'sudo -v' first so"
|
@echo "NOTE: verifier tests require sudo and bpftool. Uses sudo .venv/bin/python3."
|
||||||
@echo " the timestamp does not lapse mid-run. bpftool must be installed."
|
pytest tests/test_verifier.py -v --tb=short -m verifier
|
||||||
pytest tests/test_verifier.py -W ignore::DeprecationWarning -v --tb=short -m verifier
|
|
||||||
|
|
||||||
all: clean install
|
all: clean install
|
||||||
|
|
||||||
|
|||||||
@ -15,8 +15,8 @@ sys.path.insert(0, os.path.abspath(".."))
|
|||||||
project = "PythonBPF"
|
project = "PythonBPF"
|
||||||
copyright = "2026, Pragyansh Chaturvedi, Varun Mallya"
|
copyright = "2026, Pragyansh Chaturvedi, Varun Mallya"
|
||||||
author = "Pragyansh Chaturvedi, Varun Mallya"
|
author = "Pragyansh Chaturvedi, Varun Mallya"
|
||||||
release = "0.1.8"
|
release = "0.1.9"
|
||||||
version = "0.1.8"
|
version = "0.1.9"
|
||||||
|
|
||||||
# -- General configuration ---------------------------------------------------
|
# -- General configuration ---------------------------------------------------
|
||||||
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
|
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
|
||||||
|
|||||||
@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "pythonbpf"
|
name = "pythonbpf"
|
||||||
version = "0.1.8"
|
version = "0.1.9"
|
||||||
description = "Reduced Python frontend for eBPF"
|
description = "Reduced Python frontend for eBPF"
|
||||||
authors = [
|
authors = [
|
||||||
{ name = "r41k0u", email="pragyanshchaturvedi18@gmail.com" },
|
{ name = "r41k0u", email="pragyanshchaturvedi18@gmail.com" },
|
||||||
|
|||||||
@ -26,7 +26,7 @@ import re
|
|||||||
|
|
||||||
logger: Logger = logging.getLogger(__name__)
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
VERSION = "v0.1.8"
|
VERSION = "v0.1.9"
|
||||||
|
|
||||||
|
|
||||||
def finalize_module(original_str):
|
def finalize_module(original_str):
|
||||||
|
|||||||
@ -11,7 +11,7 @@ def deref_to_depth(func, builder, val, target_depth):
|
|||||||
cur_type = val.type
|
cur_type = val.type
|
||||||
|
|
||||||
for depth in range(target_depth):
|
for depth in range(target_depth):
|
||||||
if not isinstance(val.type, ir.PointerType):
|
if not isinstance(cur_type, ir.PointerType):
|
||||||
logger.error("Cannot dereference further, non-pointer type")
|
logger.error("Cannot dereference further, non-pointer type")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@ -113,7 +113,12 @@ def bpf_map_lookup_elem_emitter(
|
|||||||
f"Map lookup expects exactly one argument (key), got {len(call.args)}"
|
f"Map lookup expects exactly one argument (key), got {len(call.args)}"
|
||||||
)
|
)
|
||||||
key_ptr = get_or_create_ptr_from_arg(
|
key_ptr = get_or_create_ptr_from_arg(
|
||||||
func, compilation_context, call.args[0], builder, local_sym_tab
|
func,
|
||||||
|
compilation_context,
|
||||||
|
call.args[0],
|
||||||
|
builder,
|
||||||
|
local_sym_tab,
|
||||||
|
expected_type=ir.IntType(64),
|
||||||
)
|
)
|
||||||
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
||||||
|
|
||||||
@ -215,10 +220,20 @@ def bpf_map_update_elem_emitter(
|
|||||||
flags_arg = call.args[2] if len(call.args) > 2 else None
|
flags_arg = call.args[2] if len(call.args) > 2 else None
|
||||||
|
|
||||||
key_ptr = get_or_create_ptr_from_arg(
|
key_ptr = get_or_create_ptr_from_arg(
|
||||||
func, compilation_context, key_arg, builder, local_sym_tab
|
func,
|
||||||
|
compilation_context,
|
||||||
|
key_arg,
|
||||||
|
builder,
|
||||||
|
local_sym_tab,
|
||||||
|
expected_type=ir.IntType(64),
|
||||||
)
|
)
|
||||||
value_ptr = get_or_create_ptr_from_arg(
|
value_ptr = get_or_create_ptr_from_arg(
|
||||||
func, compilation_context, value_arg, builder, local_sym_tab
|
func,
|
||||||
|
compilation_context,
|
||||||
|
value_arg,
|
||||||
|
builder,
|
||||||
|
local_sym_tab,
|
||||||
|
expected_type=ir.IntType(64),
|
||||||
)
|
)
|
||||||
flags_val = get_flags_val(flags_arg, builder, local_sym_tab)
|
flags_val = get_flags_val(flags_arg, builder, local_sym_tab)
|
||||||
|
|
||||||
@ -267,7 +282,12 @@ def bpf_map_delete_elem_emitter(
|
|||||||
f"Map delete expects exactly one argument (key), got {len(call.args)}"
|
f"Map delete expects exactly one argument (key), got {len(call.args)}"
|
||||||
)
|
)
|
||||||
key_ptr = get_or_create_ptr_from_arg(
|
key_ptr = get_or_create_ptr_from_arg(
|
||||||
func, compilation_context, call.args[0], builder, local_sym_tab
|
func,
|
||||||
|
compilation_context,
|
||||||
|
call.args[0],
|
||||||
|
builder,
|
||||||
|
local_sym_tab,
|
||||||
|
expected_type=ir.IntType(64),
|
||||||
)
|
)
|
||||||
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
||||||
|
|
||||||
|
|||||||
@ -6,6 +6,8 @@ from pythonbpf.expr import (
|
|||||||
eval_expr,
|
eval_expr,
|
||||||
access_struct_field,
|
access_struct_field,
|
||||||
)
|
)
|
||||||
|
from pythonbpf.expr.ir_ops import deref_to_depth
|
||||||
|
from pythonbpf.expr.type_normalization import get_base_type_and_depth
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -104,6 +106,18 @@ def get_or_create_ptr_from_arg(
|
|||||||
if val is None:
|
if val is None:
|
||||||
raise ValueError("Failed to evaluate expression for helper arg.")
|
raise ValueError("Failed to evaluate expression for helper arg.")
|
||||||
|
|
||||||
|
if expected_type and isinstance(val.type, ir.PointerType):
|
||||||
|
_, val_depth = get_base_type_and_depth(val.type)
|
||||||
|
_, expected_depth = get_base_type_and_depth(expected_type)
|
||||||
|
if val_depth > expected_depth:
|
||||||
|
val = deref_to_depth(func, builder, val, val_depth - expected_depth)
|
||||||
|
if val is None:
|
||||||
|
raise ValueError("Failed to dereference pointer to expected depth")
|
||||||
|
else:
|
||||||
|
logger.debug(
|
||||||
|
"Expected Type not known / Not a pointer, skipping dereference"
|
||||||
|
)
|
||||||
|
|
||||||
ptr, temp_name = compilation_context.scratch_pool.get_next_temp(
|
ptr, temp_name = compilation_context.scratch_pool.get_next_temp(
|
||||||
local_sym_tab, expected_type
|
local_sym_tab, expected_type
|
||||||
)
|
)
|
||||||
|
|||||||
@ -69,16 +69,8 @@ Known-broken tests are declared in `tests/test_config.toml`:
|
|||||||
"failing_tests/my_test.py" = {reason = "...", level = "ir"}
|
"failing_tests/my_test.py" = {reason = "...", level = "ir"}
|
||||||
```
|
```
|
||||||
|
|
||||||
- `level = "ir"` — fails during IR generation.
|
- `level = "ir"` — fails during IR generation; both IR and LLC tests are marked xfail.
|
||||||
- `level = "llc"` — IR generates fine but `llc` rejects it.
|
- `level = "llc"` — IR generates fine but `llc` rejects it; only the LLC test is marked xfail.
|
||||||
- `level = "verifier"` — IR and `llc` both succeed, but the kernel verifier rejects the object.
|
|
||||||
|
|
||||||
A failure at one level implies failure at every later one, so the declared level marks
|
|
||||||
that level **and all later ones** xfail. An `"ir"` entry is xfail at all three levels; a
|
|
||||||
`"verifier"` entry is xfail at level 3 only and must still pass levels 1 and 2.
|
|
||||||
|
|
||||||
Every test file runs at every level, including the ones declared here — level 3 does not
|
|
||||||
skip declared failures, it reports them as expected ones.
|
|
||||||
|
|
||||||
All xfails use `strict = True`: if a test starts **passing** it shows up as **XPASS** and is treated as a test failure. This is intentional — it means the bug was fixed and the test should be promoted to `passing_tests/`.
|
All xfails use `strict = True`: if a test starts **passing** it shows up as **XPASS** and is treated as a test failure. This is intentional — it means the bug was fixed and the test should be promoted to `passing_tests/`.
|
||||||
|
|
||||||
|
|||||||
@ -19,7 +19,6 @@ import logging
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from tests.framework.bpf_test_case import level_index
|
|
||||||
from tests.framework.collector import collect_all_test_files
|
from tests.framework.collector import collect_all_test_files
|
||||||
|
|
||||||
# ── vmlinux availability ────────────────────────────────────────────────────
|
# ── vmlinux availability ────────────────────────────────────────────────────
|
||||||
@ -71,19 +70,14 @@ def pytest_collection_modifyitems(items):
|
|||||||
|
|
||||||
# xfail (strict: XPASS counts as a test failure, alerting us to fixed bugs)
|
# xfail (strict: XPASS counts as a test failure, alerting us to fixed bugs)
|
||||||
if case.is_expected_fail:
|
if case.is_expected_fail:
|
||||||
# A failure at one level implies failure at every later one, so mark
|
# Level "ir" → fails at IR generation: xfail both IR and LLC tests
|
||||||
# this item xfail whenever the declared level is at or before it:
|
# Level "llc" → IR succeeds but LLC fails: only xfail the LLC test
|
||||||
# "ir" → IR, LLC and verifier
|
is_llc_test = item.nodeid.startswith("tests/test_llc_compilation.py")
|
||||||
# "llc" → LLC and verifier (IR is expected to succeed)
|
|
||||||
# "verifier" → verifier only
|
|
||||||
if item.nodeid.startswith("tests/test_verifier.py"):
|
|
||||||
item_level = "verifier"
|
|
||||||
elif item.nodeid.startswith("tests/test_llc_compilation.py"):
|
|
||||||
item_level = "llc"
|
|
||||||
else:
|
|
||||||
item_level = "ir"
|
|
||||||
|
|
||||||
if level_index(case.xfail_level) <= level_index(item_level):
|
apply_xfail = (case.xfail_level == "ir") or (
|
||||||
|
case.xfail_level == "llc" and is_llc_test
|
||||||
|
)
|
||||||
|
if apply_xfail:
|
||||||
item.add_marker(
|
item.add_marker(
|
||||||
pytest.mark.xfail(
|
pytest.mark.xfail(
|
||||||
reason=case.xfail_reason,
|
reason=case.xfail_reason,
|
||||||
|
|||||||
@ -3,7 +3,7 @@ from ctypes import c_void_p, c_int64
|
|||||||
|
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
@section("tracepoint/syscalls/sys_enter_execve")
|
@section("sometag1")
|
||||||
def sometag(ctx: c_void_p) -> c_int64:
|
def sometag(ctx: c_void_p) -> c_int64:
|
||||||
if 3 + 2 == 5:
|
if 3 + 2 == 5:
|
||||||
return c_int64(5)
|
return c_int64(5)
|
||||||
|
|||||||
@ -1,20 +1,6 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# The three test levels, in pipeline order. A test declared as failing at one
|
|
||||||
# level is also expected to fail at every later level: a program that cannot
|
|
||||||
# generate IR cannot reach llc, and one that llc rejects never reaches the
|
|
||||||
# kernel. Used by conftest to decide which items to mark xfail.
|
|
||||||
LEVELS = ("ir", "llc", "verifier")
|
|
||||||
|
|
||||||
|
|
||||||
def level_index(level: str) -> int:
|
|
||||||
"""Position of a level in the pipeline. Unknown levels sort first ("ir")."""
|
|
||||||
try:
|
|
||||||
return LEVELS.index(level)
|
|
||||||
except ValueError:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class BpfTestCase:
|
class BpfTestCase:
|
||||||
@ -22,7 +8,7 @@ class BpfTestCase:
|
|||||||
rel_path: str
|
rel_path: str
|
||||||
is_expected_fail: bool = False
|
is_expected_fail: bool = False
|
||||||
xfail_reason: str = ""
|
xfail_reason: str = ""
|
||||||
xfail_level: str = "ir" # one of LEVELS
|
xfail_level: str = "ir" # "ir" or "llc"
|
||||||
needs_vmlinux: bool = False
|
needs_vmlinux: bool = False
|
||||||
skip_reason: str = ""
|
skip_reason: str = ""
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@ from ctypes import c_void_p, c_int64
|
|||||||
|
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
@section("tracepoint/syscalls/sys_enter_execve")
|
@section("sometag1")
|
||||||
def sometag(ctx: c_void_p) -> c_int64:
|
def sometag(ctx: c_void_p) -> c_int64:
|
||||||
return c_int64(1 - 1)
|
return c_int64(1 - 1)
|
||||||
|
|
||||||
|
|||||||
@ -5,7 +5,7 @@ from ctypes import c_void_p, c_int64
|
|||||||
|
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
@section("tracepoint/syscalls/sys_enter_execve")
|
@section("sometag1")
|
||||||
def sometag(ctx: c_void_p) -> c_int64:
|
def sometag(ctx: c_void_p) -> c_int64:
|
||||||
a = 1 - 1
|
a = 1 - 1
|
||||||
return c_int64(a)
|
return c_int64(a)
|
||||||
|
|||||||
@ -24,12 +24,12 @@ from tests.framework.compiler import run_ir_generation, run_llc
|
|||||||
from tests.framework.verifier import verify_object
|
from tests.framework.verifier import verify_object
|
||||||
|
|
||||||
|
|
||||||
def _verifier_test_files():
|
def _passing_test_files():
|
||||||
return [c.path for c in collect_all_test_files()]
|
return [c.path for c in collect_all_test_files() if not c.is_expected_fail]
|
||||||
|
|
||||||
|
|
||||||
def _verifier_test_ids():
|
def _passing_test_ids():
|
||||||
return [c.rel_path for c in collect_all_test_files()]
|
return [c.rel_path for c in collect_all_test_files() if not c.is_expected_fail]
|
||||||
|
|
||||||
|
|
||||||
def _get_rejection_reason(verifier_test_file: Path, output) -> str:
|
def _get_rejection_reason(verifier_test_file: Path, output) -> str:
|
||||||
@ -43,16 +43,11 @@ def _get_rejection_reason(verifier_test_file: Path, output) -> str:
|
|||||||
return errstr
|
return errstr
|
||||||
|
|
||||||
|
|
||||||
# Every test file runs at this level, including the ones declared in
|
|
||||||
# test_config.toml. conftest marks those xfail, so an "ir"- or "llc"-level
|
|
||||||
# failure is still reported as an expected failure rather than being silently
|
|
||||||
# dropped from the level-3 run — and a "verifier"-level entry becomes possible
|
|
||||||
# at all.
|
|
||||||
@pytest.mark.verifier
|
@pytest.mark.verifier
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"verifier_test_file",
|
"verifier_test_file",
|
||||||
_verifier_test_files(),
|
_passing_test_files(),
|
||||||
ids=_verifier_test_ids(),
|
ids=_passing_test_ids(),
|
||||||
)
|
)
|
||||||
def test_kernel_verifier(verifier_test_file: Path, tmp_path, caplog):
|
def test_kernel_verifier(verifier_test_file: Path, tmp_path, caplog):
|
||||||
"""Compile the BPF test and verify it passes the kernel verifier."""
|
"""Compile the BPF test and verify it passes the kernel verifier."""
|
||||||
|
|||||||
80
tools/release.sh
Executable file
80
tools/release.sh
Executable file
@ -0,0 +1,80 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
RED='\033[0;31m'
|
||||||
|
GREEN='\033[0;32m'
|
||||||
|
YELLOW='\033[1;33m'
|
||||||
|
NC='\033[0m'
|
||||||
|
|
||||||
|
log() { echo -e "${GREEN}[release]${NC} $1"; }
|
||||||
|
warn() { echo -e "${YELLOW}[release]${NC} $1"; }
|
||||||
|
err() { echo -e "${RED}[release]${NC} $1"; exit 1; }
|
||||||
|
|
||||||
|
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||||
|
cd "$REPO_ROOT"
|
||||||
|
|
||||||
|
git diff --quiet || err "Working tree is dirty — commit or stash changes first."
|
||||||
|
|
||||||
|
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "none")
|
||||||
|
log "Latest tag: $LATEST_TAG"
|
||||||
|
|
||||||
|
CURRENT_VERSION=$(grep -E '^version\s*=' pyproject.toml | head -1 | sed 's/version\s*=\s*"\([^"]*\)"/\1/')
|
||||||
|
log "Current version in pyproject.toml: $CURRENT_VERSION"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
read -r -p "Enter new version (e.g. 0.1.9): " NEW_VERSION
|
||||||
|
|
||||||
|
if [[ ! "$NEW_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
|
||||||
|
err "Invalid version format — use X.Y.Z (e.g. 0.1.9)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
TAG="v${NEW_VERSION}"
|
||||||
|
|
||||||
|
if git rev-parse "$TAG" >/dev/null 2>&1; then
|
||||||
|
err "Tag $TAG already exists."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# --- Apply version bumps across all files ---
|
||||||
|
|
||||||
|
sed -i "s/^version = \"$CURRENT_VERSION\"/version = \"$NEW_VERSION\"/" pyproject.toml
|
||||||
|
sed -i "s/VERSION = \"v$CURRENT_VERSION\"/VERSION = \"v$NEW_VERSION\"/" pythonbpf/codegen.py
|
||||||
|
sed -i "s/release = \"$CURRENT_VERSION\"/release = \"$NEW_VERSION\"/" docs/conf.py
|
||||||
|
sed -i "s/version = \"$CURRENT_VERSION\"/version = \"$NEW_VERSION\"/" docs/conf.py
|
||||||
|
|
||||||
|
log "Bumped version $CURRENT_VERSION → $NEW_VERSION in:"
|
||||||
|
log " pyproject.toml"
|
||||||
|
log " pythonbpf/codegen.py"
|
||||||
|
log " docs/conf.py"
|
||||||
|
|
||||||
|
# --- Show diff and confirm ---
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
log "Changes to be committed:"
|
||||||
|
git diff --stat
|
||||||
|
echo ""
|
||||||
|
git diff
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
read -r -p "Commit these changes and create tag $TAG? [y/N]: " CONFIRM
|
||||||
|
if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
|
||||||
|
git checkout -- pyproject.toml pythonbpf/codegen.py docs/conf.py
|
||||||
|
warn "Reverted changes. Aborted."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
git add pyproject.toml pythonbpf/codegen.py docs/conf.py
|
||||||
|
git commit -m "Bump version to $NEW_VERSION"
|
||||||
|
log "Committed version bump."
|
||||||
|
|
||||||
|
git tag "$TAG"
|
||||||
|
log "Created tag $TAG."
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
read -r -p "Push commit and tag to origin? [y/N]: " PUSH
|
||||||
|
if [[ "$PUSH" =~ ^[Yy]$ ]]; then
|
||||||
|
git push origin HEAD
|
||||||
|
git push origin "$TAG"
|
||||||
|
log "Pushed commit and tag $TAG. CI will build and publish to PyPI."
|
||||||
|
else
|
||||||
|
warn "Push skipped. Run: git push origin HEAD && git push origin $TAG"
|
||||||
|
fi
|
||||||
Reference in New Issue
Block a user