mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
A lookup local was always an i64*, whatever the map's value type, so a c_uint32 value was read with an 8-byte load (the verifier rejects an access past value_size) and its spare `v_tmp` slot, typed i32, could not take the i64 a rebind stored into it. The local now points at the value's own width for an integer value (a struct value keeps the generic pointer; field access casts it), and the generic pointer the lookup helper returns is cast to it on assignment. The sites that read the local by its pointer type follow: - A comparison reads it as get_typed_operand does, at its width and with the map's declared sign, so `u == 3` on a c_uint32 value is a zext and an unsigned compare where needed (on c_uint64 values it was a signed compare before). - print() takes the format from the declared value type and widens by its sign: a c_uint8 value was about to print as a string (%s on i8*), and integers narrower than 32 bits now print as %d, promoted as C varargs are. - A rebind converts the i64 result to the slot's width. holds_map_int_value names the predicate those sites share.
119 lines
4.2 KiB
Python
119 lines
4.2 KiB
Python
"""
|
|
Integer signedness: the shape of the IR.
|
|
|
|
The passing_tests/signedness cases mirror tests/c-form/signedness.bpf.c, and
|
|
the IR clang emits for that C file is the specification. Levels 1 and 2 only
|
|
prove these files compile; this test checks the operations themselves, which
|
|
is the whole point of the cases: zext vs sext on widening, udiv vs sdiv,
|
|
icmp ugt vs sgt, lshr vs ashr, and the trunc/zext pair that wraps u32 * u32.
|
|
"""
|
|
|
|
import importlib.util
|
|
import re
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from tests.framework.compiler import run_ir_generation
|
|
|
|
PASSING_DIR = Path(__file__).parent / "passing_tests"
|
|
HAVE_VMLINUX = importlib.util.find_spec("vmlinux") is not None
|
|
|
|
# path under passing_tests -> (patterns that must appear, patterns that must not)
|
|
CASES = {
|
|
"signedness/widen_unsigned.py": (
|
|
[r"zext i32 .* to i64"],
|
|
[r"sext i32 .* to i64"],
|
|
),
|
|
"signedness/widen_signed.py": (
|
|
[r"sext i32 .* to i64"],
|
|
[r"zext i32 .* to i64"],
|
|
),
|
|
"signedness/mixed_division.py": (
|
|
[r"\budiv i64", r"\burem i64", r"\bsdiv i64"],
|
|
[r"\bsrem i64"],
|
|
),
|
|
"signedness/unsigned_compare.py": (
|
|
[r"icmp ugt i64", r"icmp sgt i64"],
|
|
[],
|
|
),
|
|
"signedness/narrow_wrap.py": (
|
|
# the product is computed at 64 bits, cut to 32, then zero-extended
|
|
[r"\bmul i64", r"trunc i64 .* to i32", r"zext i32 .* to i64"],
|
|
[r"sext i32 .* to i64"],
|
|
),
|
|
"signedness/right_shift.py": (
|
|
[r"\blshr i64 .*, 4", r"\bashr i64 .*, 4"],
|
|
[],
|
|
),
|
|
"signedness/literal_rank.py": (
|
|
[r"\budiv i64 .*, 4294967294"],
|
|
[r"\bsdiv i64"],
|
|
),
|
|
"signedness/augassign_unsigned.py": (
|
|
[r"\blshr i64", r"\budiv i64", r"\burem i64"],
|
|
[r"\bashr i64", r"\bsdiv i64", r"\bsrem i64"],
|
|
),
|
|
"signedness/helper_results.py": (
|
|
[r"\blshr i64", r"\budiv i64"],
|
|
[r"\bashr i64", r"\bsdiv i64"],
|
|
),
|
|
"signedness/map_value_sign.py": (
|
|
[r"\blshr i64 [^,]*, 63"],
|
|
[r"\bashr i64"],
|
|
),
|
|
# u32 - int is a u32 operation: the result is cut to 32 bits and
|
|
# zero-extended; ranked as 64-bit there would be no trunc at all.
|
|
"vmlinux/ctx_field_rank.py": (
|
|
[r"\bsub i64", r"trunc i64 .* to i32", r"zext i32 .* to i64"],
|
|
[],
|
|
),
|
|
# A bool widens with zext and an integer narrows to it by != 0, never by
|
|
# trunc; sext of an i1 would return -1 for True.
|
|
"signedness/bool_int.py": (
|
|
[r"zext i1 .* to i(32|64)", r"icmp ne i64 .*, 0"],
|
|
[r"sext i1 ", r"trunc i64 .* to i1"],
|
|
),
|
|
# An enum constant is a C `int`, so `XDP_PASS - k` with k a c_uint32 is a
|
|
# u32 operation: the result is cut to 32 bits and zero-extended. Ranked
|
|
# as i64 it would be a signed 64-bit subtraction with no trunc at all.
|
|
"vmlinux/enum_rank.py": (
|
|
[r"trunc i64 .* to i32", r"zext i32 .* to i64"],
|
|
[r"sext i32 .* to i64"],
|
|
),
|
|
# A narrow map value is loaded at its width, widened per its sign, and
|
|
# never read with an 8-byte load; a c_uint8 value is printed by value.
|
|
"signedness/map_value_narrow.py": (
|
|
[
|
|
r"load i32, i32\* %\"deref",
|
|
r"load i8, i8\* %\"deref",
|
|
r"zext i32 %\"deref",
|
|
r"sext i32 %\"deref",
|
|
r"zext i8 %\"deref",
|
|
r"store i8 .*, i8\* %\"b_tmp\"",
|
|
],
|
|
[r"load i64, i64\* %\"deref", r"i8\* %\"deref[^\"]*\"\)$"],
|
|
),
|
|
# `return p` on a map lookup dereferences through a null check and returns
|
|
# the i64, never the pointer.
|
|
"return/map_value.py": (
|
|
[r"deref_0_not_null", r"ret i64 %"],
|
|
[r"ret i64\*"],
|
|
),
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize("name", list(CASES))
|
|
def test_signedness_ir_shape(name, tmp_path):
|
|
if name.startswith("vmlinux/") and not HAVE_VMLINUX:
|
|
pytest.skip("vmlinux.py not importable")
|
|
ll_path = tmp_path / Path(name).name.replace(".py", ".ll")
|
|
run_ir_generation(PASSING_DIR / name, ll_path)
|
|
ir_text = ll_path.read_text()
|
|
|
|
expected, forbidden = CASES[name]
|
|
for pattern in expected:
|
|
assert re.search(pattern, ir_text), f"{name}: expected /{pattern}/ in the IR"
|
|
for pattern in forbidden:
|
|
assert not re.search(pattern, ir_text), f"{name}: /{pattern}/ must not appear"
|