mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
Compare commits
1 Commits
tests/pack
...
fix/augass
| Author | SHA1 | Date | |
|---|---|---|---|
| 13538a4042 |
@ -60,6 +60,10 @@ def trace_open(ctx: c_void_p) -> c_int64:
|
|||||||
return 0
|
return 0
|
||||||
```
|
```
|
||||||
|
|
||||||
|
An integer value behaves like a Python `int`: `value += 1`, like
|
||||||
|
`value = value + 1`, rebinds the local to the new number and leaves the map
|
||||||
|
unchanged. To store it, call `update`.
|
||||||
|
|
||||||
##### update(key, value, flags=None)
|
##### update(key, value, flags=None)
|
||||||
|
|
||||||
Update or insert a key-value pair.
|
Update or insert a key-value pair.
|
||||||
|
|||||||
@ -87,6 +87,24 @@ def _copy_string_to_char_array(
|
|||||||
emit_probe_read_kernel_str_call(builder, dst_i8_ptr, array_size, src_ptr)
|
emit_probe_read_kernel_str_call(builder, dst_i8_ptr, array_size, src_ptr)
|
||||||
|
|
||||||
|
|
||||||
|
def is_map_value_local(local_sym_tab, var_name):
|
||||||
|
"""Whether `var_name` holds a map-lookup result: a pointer to the value,
|
||||||
|
allocated with a spare `<name>_tmp` slot for values computed from it."""
|
||||||
|
return (
|
||||||
|
isinstance(local_sym_tab[var_name].ir_type, ir.PointerType)
|
||||||
|
and f"{var_name}_tmp" in local_sym_tab
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def rebind_map_value_local(builder, local_sym_tab, var_name, val):
|
||||||
|
"""Bind a map-lookup local to a computed value, the way Python rebinds a
|
||||||
|
name: the value goes into the local's `<name>_tmp` slot and the local is
|
||||||
|
pointed at it. The map itself is not written."""
|
||||||
|
tmp = local_sym_tab[f"{var_name}_tmp"].var
|
||||||
|
builder.store(val, tmp)
|
||||||
|
builder.store(tmp, local_sym_tab[var_name].var)
|
||||||
|
|
||||||
|
|
||||||
def _is_char_array(ir_type):
|
def _is_char_array(ir_type):
|
||||||
"""Check if type is [N x i8]."""
|
"""Check if type is [N x i8]."""
|
||||||
return (
|
return (
|
||||||
@ -223,12 +241,9 @@ def handle_variable_assignment(
|
|||||||
return False
|
return False
|
||||||
elif isinstance(val_type, ir.IntType) and isinstance(var_type, ir.PointerType):
|
elif isinstance(val_type, ir.IntType) and isinstance(var_type, ir.PointerType):
|
||||||
# NOTE: This is assignment to a PTR_TO_MAP_VALUE_OR_NULL
|
# NOTE: This is assignment to a PTR_TO_MAP_VALUE_OR_NULL
|
||||||
logger.info(
|
rebind_map_value_local(builder, local_sym_tab, var_name, val)
|
||||||
f"Creating temporary variable for pointer assignment to {var_name}"
|
logger.info(f"Rebound map-lookup local {var_name} to a computed value")
|
||||||
)
|
return True
|
||||||
var_ptr_tmp = local_sym_tab[f"{var_name}_tmp"].var
|
|
||||||
builder.store(val, var_ptr_tmp)
|
|
||||||
val = var_ptr_tmp
|
|
||||||
else:
|
else:
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Type mismatch for variable {var_name}: {val_type} vs {var_type}"
|
f"Type mismatch for variable {var_name}: {val_type} vs {var_type}"
|
||||||
|
|||||||
@ -22,6 +22,8 @@ from pythonbpf.expr import (
|
|||||||
from pythonbpf.assign_pass import (
|
from pythonbpf.assign_pass import (
|
||||||
handle_variable_assignment,
|
handle_variable_assignment,
|
||||||
handle_struct_field_assignment,
|
handle_struct_field_assignment,
|
||||||
|
is_map_value_local,
|
||||||
|
rebind_map_value_local,
|
||||||
)
|
)
|
||||||
from pythonbpf.allocation_pass import (
|
from pythonbpf.allocation_pass import (
|
||||||
handle_assign_allocation,
|
handle_assign_allocation,
|
||||||
@ -207,6 +209,9 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab):
|
|||||||
read, and the operator table is apply_binop, the same one binary-op
|
read, and the operator table is apply_binop, the same one binary-op
|
||||||
evaluation uses.
|
evaluation uses.
|
||||||
"""
|
"""
|
||||||
|
# A map-lookup local reads through its pointer and is rebound, not
|
||||||
|
# written through, exactly as `v = v + 1` does it.
|
||||||
|
map_value_local = None
|
||||||
if isinstance(stmt.target, ast.Name):
|
if isinstance(stmt.target, ast.Name):
|
||||||
name = stmt.target.id
|
name = stmt.target.id
|
||||||
# One table: a declared global is a local_sym_tab entry whose slot is
|
# One table: a declared global is a local_sym_tab entry whose slot is
|
||||||
@ -220,6 +225,13 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab):
|
|||||||
raise SyntaxError(
|
raise SyntaxError(
|
||||||
f"cannot assign to '{name}': it is the context parameter"
|
f"cannot assign to '{name}': it is the context parameter"
|
||||||
)
|
)
|
||||||
|
if is_map_value_local(local_sym_tab, name):
|
||||||
|
if local_sym_tab[name].metadata in compilation_context.structs_sym_tab:
|
||||||
|
raise SyntaxError(
|
||||||
|
f"augmented assignment to '{name}', a struct map value; "
|
||||||
|
f"update one of its fields instead ({name}.field += ...)"
|
||||||
|
)
|
||||||
|
map_value_local = name
|
||||||
elif name in compilation_context.bpf_globals:
|
elif name in compilation_context.bpf_globals:
|
||||||
# `x += v` binds x as a local and reads it unbound: UnboundLocalError.
|
# `x += v` binds x as a local and reads it unbound: UnboundLocalError.
|
||||||
raise SyntaxError(
|
raise SyntaxError(
|
||||||
@ -263,13 +275,20 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab):
|
|||||||
f"Unsupported augmented-assignment target: {ast.dump(stmt.target)}"
|
f"Unsupported augmented-assignment target: {ast.dump(stmt.target)}"
|
||||||
)
|
)
|
||||||
|
|
||||||
if not isinstance(slot_type, ir.IntType):
|
# Python evaluates the target's current value before the right-hand side.
|
||||||
|
if map_value_local is not None:
|
||||||
|
# The null-checked read every other use of the local gets; its
|
||||||
|
# descriptor is the map's declared value type, which is also the
|
||||||
|
# type the result is stored back as.
|
||||||
|
current, slot_type = get_typed_operand(
|
||||||
|
func, compilation_context, stmt.target, builder, local_sym_tab
|
||||||
|
)
|
||||||
|
elif isinstance(slot_type, ir.IntType):
|
||||||
|
current = builder.load(slot)
|
||||||
|
else:
|
||||||
raise SyntaxError(
|
raise SyntaxError(
|
||||||
f"augmented assignment needs an integer target, got {slot_type}"
|
f"augmented assignment needs an integer target, got {slot_type}"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Python evaluates the target's current value before the right-hand side.
|
|
||||||
current = builder.load(slot)
|
|
||||||
rhs, rhs_ty = get_typed_operand(
|
rhs, rhs_ty = get_typed_operand(
|
||||||
func, compilation_context, stmt.value, builder, local_sym_tab
|
func, compilation_context, stmt.value, builder, local_sym_tab
|
||||||
)
|
)
|
||||||
@ -287,7 +306,11 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab):
|
|||||||
apply_binop(builder, stmt.op, current, rhs, signedness(result_ty)),
|
apply_binop(builder, stmt.op, current, rhs, signedness(result_ty)),
|
||||||
result_ty,
|
result_ty,
|
||||||
)
|
)
|
||||||
builder.store(convert(builder, result, result_ty, slot_type), slot)
|
result = convert(builder, result, result_ty, slot_type)
|
||||||
|
if map_value_local is not None:
|
||||||
|
rebind_map_value_local(builder, local_sym_tab, map_value_local, result)
|
||||||
|
else:
|
||||||
|
builder.store(result, slot)
|
||||||
|
|
||||||
|
|
||||||
def handle_cond(func, compilation_context, builder, cond, local_sym_tab):
|
def handle_cond(func, compilation_context, builder, cond, local_sym_tab):
|
||||||
|
|||||||
@ -1,27 +0,0 @@
|
|||||||
# xdp_md.data is declared u32, so C's rules make ctx.data - k a 32-bit
|
|
||||||
# subtraction, and that is what the compiler emits (w0 += -1). The verifier
|
|
||||||
# tracks data as a packet pointer and rejects it: "R0 32-bit pointer
|
|
||||||
# arithmetic prohibited". C hits the same and casts through (void *)(long).
|
|
||||||
# The planned fix gives the packet-pointer fields 64-bit pointer rank so no
|
|
||||||
# cast is needed; see the TODO in expr_pass._descriptor. The same u32 rank on
|
|
||||||
# a scalar field is pinned as passing in passing_tests/vmlinux/ctx_field_rank.py.
|
|
||||||
from ctypes import c_int32, c_int64
|
|
||||||
from pythonbpf import bpf, section, bpfglobal, compile
|
|
||||||
from vmlinux import struct_xdp_md
|
|
||||||
|
|
||||||
|
|
||||||
@bpf
|
|
||||||
@section("xdp")
|
|
||||||
def prog(ctx: struct_xdp_md) -> c_int64:
|
|
||||||
k = c_int32(1)
|
|
||||||
d = ctx.data - k
|
|
||||||
return c_int64(d)
|
|
||||||
|
|
||||||
|
|
||||||
@bpf
|
|
||||||
@bpfglobal
|
|
||||||
def LICENSE() -> str:
|
|
||||||
return "GPL"
|
|
||||||
|
|
||||||
|
|
||||||
compile()
|
|
||||||
32
tests/passing_tests/assign/augassign_map_value.py
Normal file
32
tests/passing_tests/assign/augassign_map_value.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# Augmented assignment to a map-lookup local: `v += 1` is `v = v + 1`. It reads
|
||||||
|
# the value through the pointer (null-checked) and rebinds v to the result; the
|
||||||
|
# map itself is not written.
|
||||||
|
from pythonbpf import bpf, map, section, bpfglobal, compile
|
||||||
|
from pythonbpf.maps import HashMap
|
||||||
|
from ctypes import c_void_p, c_int64, c_uint64
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@map
|
||||||
|
def m() -> HashMap:
|
||||||
|
return HashMap(key=c_int64, value=c_uint64, max_entries=4)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@section("tracepoint/raw_syscalls/sys_enter")
|
||||||
|
def prog(ctx: c_void_p) -> c_int64:
|
||||||
|
v = m.lookup(0)
|
||||||
|
if v:
|
||||||
|
v += 1
|
||||||
|
v >>= 1 # c_uint64 value: a logical shift
|
||||||
|
return v
|
||||||
|
return c_int64(0)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
compile()
|
||||||
@ -60,7 +60,3 @@
|
|||||||
"failing_tests/loops/for_continue.py" = {reason = "for/while loops not implemented: continue is not supported", level = "ir"}
|
"failing_tests/loops/for_continue.py" = {reason = "for/while loops not implemented: continue is not supported", level = "ir"}
|
||||||
|
|
||||||
"failing_tests/loops/for_map_items.py" = {reason = "for/while loops not implemented: no sugar over bpf_for_each_map_elem()-style map iteration exists yet", level = "ir"}
|
"failing_tests/loops/for_map_items.py" = {reason = "for/while loops not implemented: no sugar over bpf_for_each_map_elem()-style map iteration exists yet", level = "ir"}
|
||||||
|
|
||||||
# 32-bit arithmetic on a packet-pointer context field. IR and llc succeed; the
|
|
||||||
# verifier rejects it with "R0 32-bit pointer arithmetic prohibited".
|
|
||||||
"failing_tests/vmlinux/ctx_data_arith.py" = {reason = "xdp_md.data is u32 in C but a packet pointer to the verifier; 32-bit arithmetic on it is prohibited until packet-pointer fields get 64-bit pointer rank", level = "verifier"}
|
|
||||||
|
|||||||
@ -81,6 +81,12 @@ CASES = {
|
|||||||
[r"trunc i64 .* to i32", r"zext i32 .* to i64"],
|
[r"trunc i64 .* to i32", r"zext i32 .* to i64"],
|
||||||
[r"sext i32 .* to i64"],
|
[r"sext i32 .* to i64"],
|
||||||
),
|
),
|
||||||
|
# `v += 1; v >>= 1` on a c_uint64 map value: read through the pointer,
|
||||||
|
# operate unsigned, rebind v to the lookup local's spare slot.
|
||||||
|
"assign/augassign_map_value.py": (
|
||||||
|
[r"deref_1_not_null", r"\blshr i64", r"store i64\* %\"v_tmp\", i64\*\* %\"v\""],
|
||||||
|
[r"\bashr i64"],
|
||||||
|
),
|
||||||
# `return p` on a map lookup dereferences through a null check and returns
|
# `return p` on a map lookup dereferences through a null check and returns
|
||||||
# the i64, never the pointer.
|
# the i64, never the pointer.
|
||||||
"return/map_value.py": (
|
"return/map_value.py": (
|
||||||
|
|||||||
Reference in New Issue
Block a user