mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-04-24 06:31:27 +00:00
Compare commits
6 Commits
7529820c0b
...
c9bbe1ffd8
| Author | SHA1 | Date | |
|---|---|---|---|
| c9bbe1ffd8 | |||
| 91a3fe140d | |||
| c2c17741e5 | |||
| cac88d1560 | |||
| 317575644f | |||
| a756f5e4b7 |
@ -3,13 +3,16 @@ from llvmlite import ir
|
|||||||
from logging import Logger
|
from logging import Logger
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from pythonbpf.expr import get_base_type_and_depth, deref_to_depth
|
from pythonbpf.expr import get_base_type_and_depth, deref_to_depth, eval_expr
|
||||||
|
|
||||||
logger: Logger = logging.getLogger(__name__)
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def get_operand_value(func, operand, builder, local_sym_tab):
|
def get_operand_value(
|
||||||
|
func, module, operand, builder, local_sym_tab, map_sym_tab, structs_sym_tab=None
|
||||||
|
):
|
||||||
"""Extract the value from an operand, handling variables and constants."""
|
"""Extract the value from an operand, handling variables and constants."""
|
||||||
|
logger.info(f"Getting operand value for: {ast.dump(operand)}")
|
||||||
if isinstance(operand, ast.Name):
|
if isinstance(operand, ast.Name):
|
||||||
if operand.id in local_sym_tab:
|
if operand.id in local_sym_tab:
|
||||||
var = local_sym_tab[operand.id].var
|
var = local_sym_tab[operand.id].var
|
||||||
@ -17,36 +20,48 @@ def get_operand_value(func, operand, builder, local_sym_tab):
|
|||||||
base_type, depth = get_base_type_and_depth(var_type)
|
base_type, depth = get_base_type_and_depth(var_type)
|
||||||
logger.info(f"var is {var}, base_type is {base_type}, depth is {depth}")
|
logger.info(f"var is {var}, base_type is {base_type}, depth is {depth}")
|
||||||
val = deref_to_depth(func, builder, var, depth)
|
val = deref_to_depth(func, builder, var, depth)
|
||||||
return val, [val], var
|
return val
|
||||||
raise ValueError(f"Undefined variable: {operand.id}")
|
raise ValueError(f"Undefined variable: {operand.id}")
|
||||||
elif isinstance(operand, ast.Constant):
|
elif isinstance(operand, ast.Constant):
|
||||||
if isinstance(operand.value, int):
|
if isinstance(operand.value, int):
|
||||||
cst = ir.Constant(ir.IntType(64), operand.value)
|
cst = ir.Constant(ir.IntType(64), int(operand.value))
|
||||||
return cst, [cst], None
|
return cst
|
||||||
raise TypeError(f"Unsupported constant type: {type(operand.value)}")
|
raise TypeError(f"Unsupported constant type: {type(operand.value)}")
|
||||||
elif isinstance(operand, ast.BinOp):
|
elif isinstance(operand, ast.BinOp):
|
||||||
res = handle_binary_op_impl(func, operand, builder, local_sym_tab)
|
res = handle_binary_op_impl(
|
||||||
return res, [res], None
|
func, module, operand, builder, local_sym_tab, map_sym_tab, structs_sym_tab
|
||||||
|
)
|
||||||
|
return res
|
||||||
|
else:
|
||||||
|
res = eval_expr(
|
||||||
|
func, module, builder, operand, local_sym_tab, map_sym_tab, structs_sym_tab
|
||||||
|
)
|
||||||
|
if res is None:
|
||||||
|
raise ValueError(f"Failed to evaluate call expression: {operand}")
|
||||||
|
val, _ = res
|
||||||
|
return val
|
||||||
raise TypeError(f"Unsupported operand type: {type(operand)}")
|
raise TypeError(f"Unsupported operand type: {type(operand)}")
|
||||||
|
|
||||||
|
|
||||||
def store_through_chain(value, chain, builder):
|
def handle_binary_op_impl(
|
||||||
"""Store a value through a pointer chain."""
|
func, module, rval, builder, local_sym_tab, map_sym_tab, structs_sym_tab=None
|
||||||
if not chain or len(chain) < 2:
|
):
|
||||||
raise ValueError("Pointer chain must have at least two elements")
|
|
||||||
|
|
||||||
for ptr in reversed(chain[1:]):
|
|
||||||
builder.store(value, ptr)
|
|
||||||
value = ptr
|
|
||||||
|
|
||||||
|
|
||||||
def handle_binary_op_impl(func, rval, builder, local_sym_tab):
|
|
||||||
op = rval.op
|
op = rval.op
|
||||||
left, lchain, _ = get_operand_value(func, rval.left, builder, local_sym_tab)
|
left = get_operand_value(
|
||||||
right, rchain, _ = get_operand_value(func, rval.right, builder, local_sym_tab)
|
func, module, rval.left, builder, local_sym_tab, map_sym_tab, structs_sym_tab
|
||||||
|
)
|
||||||
|
right = get_operand_value(
|
||||||
|
func, module, rval.right, builder, local_sym_tab, map_sym_tab, structs_sym_tab
|
||||||
|
)
|
||||||
logger.info(f"left is {left}, right is {right}, op is {op}")
|
logger.info(f"left is {left}, right is {right}, op is {op}")
|
||||||
|
|
||||||
logger.info(f"left chain: {lchain}, right chain: {rchain}")
|
# NOTE: Before doing the operation, if the operands are integers
|
||||||
|
# we always extend them to i64. The assignment to LHS will take
|
||||||
|
# care of truncation if needed.
|
||||||
|
if isinstance(left.type, ir.IntType) and left.type.width < 64:
|
||||||
|
left = builder.sext(left, ir.IntType(64))
|
||||||
|
if isinstance(right.type, ir.IntType) and right.type.width < 64:
|
||||||
|
right = builder.sext(right, ir.IntType(64))
|
||||||
|
|
||||||
# Map AST operation nodes to LLVM IR builder methods
|
# Map AST operation nodes to LLVM IR builder methods
|
||||||
op_map = {
|
op_map = {
|
||||||
@ -70,8 +85,19 @@ def handle_binary_op_impl(func, rval, builder, local_sym_tab):
|
|||||||
raise SyntaxError("Unsupported binary operation")
|
raise SyntaxError("Unsupported binary operation")
|
||||||
|
|
||||||
|
|
||||||
def handle_binary_op(func, rval, builder, var_name, local_sym_tab):
|
def handle_binary_op(
|
||||||
result = handle_binary_op_impl(func, rval, builder, local_sym_tab)
|
func,
|
||||||
|
module,
|
||||||
|
rval,
|
||||||
|
builder,
|
||||||
|
var_name,
|
||||||
|
local_sym_tab,
|
||||||
|
map_sym_tab,
|
||||||
|
structs_sym_tab=None,
|
||||||
|
):
|
||||||
|
result = handle_binary_op_impl(
|
||||||
|
func, module, rval, builder, local_sym_tab, map_sym_tab, structs_sym_tab
|
||||||
|
)
|
||||||
if var_name and var_name in local_sym_tab:
|
if var_name and var_name in local_sym_tab:
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Storing result {result} into variable {local_sym_tab[var_name].var}"
|
f"Storing result {result} into variable {local_sym_tab[var_name].var}"
|
||||||
|
|||||||
@ -402,7 +402,16 @@ def eval_expr(
|
|||||||
elif isinstance(expr, ast.BinOp):
|
elif isinstance(expr, ast.BinOp):
|
||||||
from pythonbpf.binary_ops import handle_binary_op
|
from pythonbpf.binary_ops import handle_binary_op
|
||||||
|
|
||||||
return handle_binary_op(func, expr, builder, None, local_sym_tab)
|
return handle_binary_op(
|
||||||
|
func,
|
||||||
|
module,
|
||||||
|
expr,
|
||||||
|
builder,
|
||||||
|
None,
|
||||||
|
local_sym_tab,
|
||||||
|
map_sym_tab,
|
||||||
|
structs_sym_tab,
|
||||||
|
)
|
||||||
elif isinstance(expr, ast.Compare):
|
elif isinstance(expr, ast.Compare):
|
||||||
return _handle_compare(
|
return _handle_compare(
|
||||||
func, module, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab
|
func, module, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab
|
||||||
|
|||||||
34
tests/passing_tests/assign/helper.py
Normal file
34
tests/passing_tests/assign/helper.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
from pythonbpf import bpf, map, section, bpfglobal, compile
|
||||||
|
from ctypes import c_void_p, c_int64, c_uint64
|
||||||
|
from pythonbpf.maps import HashMap
|
||||||
|
|
||||||
|
# NOTE: An example of i64** assignment with binops on the RHS
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@map
|
||||||
|
def last() -> HashMap:
|
||||||
|
return HashMap(key=c_uint64, value=c_uint64, max_entries=3)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@section("tracepoint/syscalls/sys_enter_execve")
|
||||||
|
def hello_world(ctx: c_void_p) -> c_int64:
|
||||||
|
last.update(0, 1)
|
||||||
|
x = last.lookup(0)
|
||||||
|
print(f"{x}")
|
||||||
|
x = x + 1
|
||||||
|
if x == 2:
|
||||||
|
print("Hello, World!")
|
||||||
|
else:
|
||||||
|
print("Goodbye, World!")
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
compile()
|
||||||
Reference in New Issue
Block a user