mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
Core: Default to a GPL license, with a warning, when none is defined
Without a LICENSE, @llvm.compiler.used still named one, and opt stopped on "use of undefined value @LICENSE". A program with no LICENSE now gets "GPL", the kernel's own license, and a warning that says so; an explicit LICENSE is emitted as written. The old failing license test becomes passing_tests/license_default.py: it calls print, which lowers to the GPL-only bpf_trace_printk, so the verifier accepts it only if the default reaches the kernel. license_non_gpl.py is the counterpart, a verifier-level negative fixture pinned to the kernel's GPL rejection, and test_license_ir.py checks both license sections and the warning. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@ -5,6 +5,11 @@ import logging
|
||||
|
||||
logger: Logger = logging.getLogger(__name__)
|
||||
|
||||
# The license a program gets when it declares none. The kernel is GPL, and a
|
||||
# program whose license is not GPL-compatible may not call GPL-only helpers
|
||||
# (bpf_trace_printk, which `print` lowers to, is one).
|
||||
DEFAULT_LICENSE = "GPL"
|
||||
|
||||
|
||||
def emit_license(module: ir.Module, license_str: str):
|
||||
license_bytes = license_str.encode("utf8") + b"\x00"
|
||||
@ -24,7 +29,10 @@ def emit_license(module: ir.Module, license_str: str):
|
||||
|
||||
|
||||
def license_processing(tree, compilation_context):
|
||||
"""Process the LICENSE function decorated with @bpf and @bpfglobal and return the section name"""
|
||||
"""Process the LICENSE function decorated with @bpf and @bpfglobal and return the section name.
|
||||
|
||||
A program without one gets DEFAULT_LICENSE, with a warning.
|
||||
"""
|
||||
count = 0
|
||||
for node in tree.body:
|
||||
if isinstance(node, ast.FunctionDef) and node.name == "LICENSE":
|
||||
@ -52,4 +60,10 @@ def license_processing(tree, compilation_context):
|
||||
)
|
||||
else:
|
||||
raise SyntaxError("ERROR: Multiple LICENSE globals defined")
|
||||
return None
|
||||
|
||||
logger.warning(
|
||||
f'No LICENSE defined; defaulting to "{DEFAULT_LICENSE}". Define '
|
||||
"@bpf @bpfglobal def LICENSE() -> str to choose another license."
|
||||
)
|
||||
emit_license(compilation_context.module, DEFAULT_LICENSE)
|
||||
return "LICENSE"
|
||||
|
||||
@ -1,14 +0,0 @@
|
||||
from pythonbpf import compile, bpf, section
|
||||
from ctypes import c_void_p, c_int64
|
||||
|
||||
|
||||
# FAILS WHEN THERE IS NO LICENSE. which is wrong.
|
||||
@bpf
|
||||
@section("sometag1")
|
||||
def sometag(ctx: c_void_p) -> c_int64:
|
||||
a = 1 + 2
|
||||
print(f"{a}")
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
compile()
|
||||
23
tests/failing_tests/license_non_gpl.py
Normal file
23
tests/failing_tests/license_non_gpl.py
Normal file
@ -0,0 +1,23 @@
|
||||
# A license that is not GPL-compatible may not call GPL-only helpers: `print`
|
||||
# lowers to bpf_trace_printk, so the verifier rejects this program. The
|
||||
# counterpart of passing_tests/license_default.py, which proves the default
|
||||
# license is GPL.
|
||||
|
||||
from pythonbpf import compile, bpf, bpfglobal, section
|
||||
from ctypes import c_void_p, c_int64
|
||||
|
||||
|
||||
@bpf
|
||||
@section("tracepoint/syscalls/sys_enter_getpid")
|
||||
def sometag(ctx: c_void_p) -> c_int64:
|
||||
print("hello")
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "Proprietary"
|
||||
|
||||
|
||||
compile()
|
||||
17
tests/passing_tests/license_default.py
Normal file
17
tests/passing_tests/license_default.py
Normal file
@ -0,0 +1,17 @@
|
||||
# No LICENSE: the program gets "GPL", with a warning. `print` lowers to
|
||||
# bpf_trace_printk, which is GPL-only, so the verifier only accepts this
|
||||
# program if the default license reaches the kernel.
|
||||
|
||||
from pythonbpf import compile, bpf, section
|
||||
from ctypes import c_void_p, c_int64
|
||||
|
||||
|
||||
@bpf
|
||||
@section("tracepoint/syscalls/sys_enter_getpid")
|
||||
def sometag(ctx: c_void_p) -> c_int64:
|
||||
a = 1 + 2
|
||||
print(f"{a}")
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
compile()
|
||||
@ -19,7 +19,7 @@
|
||||
|
||||
"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/license_non_gpl.py" = {reason = "Negative fixture: a non-GPL license may not call the GPL-only bpf_trace_printk that print lowers to", level = "verifier", match = "cannot call GPL-restricted function from non-GPL compatible program"}
|
||||
|
||||
"failing_tests/undeclared_values.py" = {reason = "Undeclared variable used in f-string — should raise SyntaxError (correct behaviour, test documents it)", level = "ir"}
|
||||
|
||||
|
||||
42
tests/test_license_ir.py
Normal file
42
tests/test_license_ir.py
Normal file
@ -0,0 +1,42 @@
|
||||
"""
|
||||
The license section: a program without LICENSE gets "GPL", with a warning,
|
||||
and an explicit LICENSE is emitted as written, with no warning.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from tests.framework.compiler import run_ir_generation
|
||||
|
||||
TESTS_DIR = Path(__file__).parent
|
||||
|
||||
|
||||
def _license_global(text):
|
||||
"""The LICENSE global as compile_to_ir emits it: NUL-terminated bytes."""
|
||||
data = text.encode() + b"\x00"
|
||||
elems = ", ".join(f"i8 {b}" for b in data)
|
||||
return (
|
||||
f'@"LICENSE" = dso_local global [{len(data)} x i8] [{elems}], section "license"'
|
||||
)
|
||||
|
||||
|
||||
def _license_ir(name, tmp_path, caplog):
|
||||
ll_path = tmp_path / "output.ll"
|
||||
with caplog.at_level(logging.WARNING, logger="pythonbpf.license_pass"):
|
||||
run_ir_generation(TESTS_DIR / name, ll_path)
|
||||
warnings = [
|
||||
r.getMessage() for r in caplog.records if r.name == "pythonbpf.license_pass"
|
||||
]
|
||||
return ll_path.read_text(), warnings
|
||||
|
||||
|
||||
def test_missing_license_defaults_to_gpl(tmp_path, caplog):
|
||||
ir, warnings = _license_ir("passing_tests/license_default.py", tmp_path, caplog)
|
||||
assert _license_global("GPL") in ir
|
||||
assert any('defaulting to "GPL"' in w for w in warnings), warnings
|
||||
|
||||
|
||||
def test_explicit_license_is_kept(tmp_path, caplog):
|
||||
ir, warnings = _license_ir("failing_tests/license_non_gpl.py", tmp_path, caplog)
|
||||
assert _license_global("Proprietary") in ir
|
||||
assert not warnings, warnings
|
||||
Reference in New Issue
Block a user