mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Compare commits
28 Commits
f41693bc6d
...
refactor_c
| Author | SHA1 | Date | |
|---|---|---|---|
| 9fdc6fa3ed | |||
| 17004d58df | |||
| 6362a5e665 | |||
| d38d73d5c6 | |||
| 0a6571726a | |||
| e62557bd1d | |||
| ee90ee9392 | |||
| 5f9eaff59c | |||
| b86341ce7a | |||
| 4857739eec | |||
| 3bb4b099c1 | |||
| e7912a088f | |||
| 95d63d969e | |||
| 1f96bab944 | |||
| f98491f3bd | |||
| 98f262ae22 | |||
| d2ff53052c | |||
| ecac24c1d2 | |||
| a764b095f8 | |||
| 95a196a91f | |||
| 6b59980874 | |||
| 3f9604a370 | |||
| 480afd1341 | |||
| ab71275566 | |||
| 2d850f457f | |||
| 9e1142bf05 | |||
| 1843ca6c53 | |||
| caa5d92c32 |
1
TODO.md
1
TODO.md
@ -5,7 +5,6 @@
|
|||||||
- XDP support in pylibbpf
|
- XDP support in pylibbpf
|
||||||
- ringbuf support
|
- ringbuf support
|
||||||
- recursive expression resolution
|
- recursive expression resolution
|
||||||
- Add supoprt for BoolOp and short circuiting in conditions
|
|
||||||
|
|
||||||
## Long term
|
## Long term
|
||||||
|
|
||||||
|
|||||||
4
pythonbpf/expr/__init__.py
Normal file
4
pythonbpf/expr/__init__.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
from .expr_pass import eval_expr, handle_expr
|
||||||
|
from .type_normalization import convert_to_bool
|
||||||
|
|
||||||
|
__all__ = ["eval_expr", "handle_expr", "convert_to_bool"]
|
||||||
@ -4,7 +4,8 @@ from logging import Logger
|
|||||||
import logging
|
import logging
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
|
||||||
from .type_deducer import ctypes_to_ir, is_ctypes
|
from pythonbpf.type_deducer import ctypes_to_ir, is_ctypes
|
||||||
|
from .type_normalization import convert_to_bool, handle_comparator
|
||||||
|
|
||||||
logger: Logger = logging.getLogger(__name__)
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -43,7 +44,6 @@ def _handle_attribute_expr(
|
|||||||
var_ptr, var_type, var_metadata = local_sym_tab[var_name]
|
var_ptr, var_type, var_metadata = local_sym_tab[var_name]
|
||||||
logger.info(f"Loading attribute {attr_name} from variable {var_name}")
|
logger.info(f"Loading attribute {attr_name} from variable {var_name}")
|
||||||
logger.info(f"Variable type: {var_type}, Variable ptr: {var_ptr}")
|
logger.info(f"Variable type: {var_type}, Variable ptr: {var_ptr}")
|
||||||
|
|
||||||
metadata = structs_sym_tab[var_metadata]
|
metadata = structs_sym_tab[var_metadata]
|
||||||
if attr_name in metadata.fields:
|
if attr_name in metadata.fields:
|
||||||
gep = metadata.gep(builder, var_ptr, attr_name)
|
gep = metadata.gep(builder, var_ptr, attr_name)
|
||||||
@ -130,30 +130,6 @@ def _handle_ctypes_call(
|
|||||||
return val
|
return val
|
||||||
|
|
||||||
|
|
||||||
def _handle_comparator(builder, op, lhs, rhs):
|
|
||||||
"""Handle comparison operations."""
|
|
||||||
|
|
||||||
# NOTE: For now assume same types
|
|
||||||
|
|
||||||
comparison_ops = {
|
|
||||||
ast.Eq: "==",
|
|
||||||
ast.NotEq: "!=",
|
|
||||||
ast.Lt: "<",
|
|
||||||
ast.LtE: "<=",
|
|
||||||
ast.Gt: ">",
|
|
||||||
ast.GtE: ">=",
|
|
||||||
}
|
|
||||||
|
|
||||||
if type(op) not in comparison_ops:
|
|
||||||
logger.error(f"Unsupported comparison operator: {type(op)}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
predicate = comparison_ops[type(op)]
|
|
||||||
result = builder.icmp_signed(predicate, lhs, rhs)
|
|
||||||
logger.debug(f"Comparison result: {result}")
|
|
||||||
return result, ir.IntType(1)
|
|
||||||
|
|
||||||
|
|
||||||
def _handle_compare(
|
def _handle_compare(
|
||||||
func, module, builder, cond, local_sym_tab, map_sym_tab, structs_sym_tab=None
|
func, module, builder, cond, local_sym_tab, map_sym_tab, structs_sym_tab=None
|
||||||
):
|
):
|
||||||
@ -187,17 +163,7 @@ def _handle_compare(
|
|||||||
|
|
||||||
lhs, _ = lhs
|
lhs, _ = lhs
|
||||||
rhs, _ = rhs
|
rhs, _ = rhs
|
||||||
return _handle_comparator(builder, cond.ops[0], lhs, rhs)
|
return handle_comparator(func, builder, cond.ops[0], lhs, rhs)
|
||||||
|
|
||||||
|
|
||||||
def convert_to_bool(builder, val):
|
|
||||||
if val.type == ir.IntType(1):
|
|
||||||
return val
|
|
||||||
if isinstance(val.type, ir.PointerType):
|
|
||||||
zero = ir.Constant(val.type, None)
|
|
||||||
else:
|
|
||||||
zero = ir.Constant(val.type, 0)
|
|
||||||
return builder.icmp_signed("!=", val, zero)
|
|
||||||
|
|
||||||
|
|
||||||
def _handle_unary_op(
|
def _handle_unary_op(
|
||||||
@ -227,6 +193,136 @@ def _handle_unary_op(
|
|||||||
return result, ir.IntType(1)
|
return result, ir.IntType(1)
|
||||||
|
|
||||||
|
|
||||||
|
def _handle_and_op(func, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab):
|
||||||
|
"""Handle `and` boolean operations."""
|
||||||
|
|
||||||
|
logger.debug(f"Handling 'and' operator with {len(expr.values)} operands")
|
||||||
|
|
||||||
|
merge_block = func.append_basic_block(name="and.merge")
|
||||||
|
false_block = func.append_basic_block(name="and.false")
|
||||||
|
|
||||||
|
incoming_values = []
|
||||||
|
|
||||||
|
for i, value in enumerate(expr.values):
|
||||||
|
is_last = i == len(expr.values) - 1
|
||||||
|
|
||||||
|
# Evaluate current operand
|
||||||
|
operand_result = eval_expr(
|
||||||
|
func, None, builder, value, local_sym_tab, map_sym_tab, structs_sym_tab
|
||||||
|
)
|
||||||
|
if operand_result is None:
|
||||||
|
logger.error(f"Failed to evaluate operand {i} in 'and' expression")
|
||||||
|
return None
|
||||||
|
|
||||||
|
operand_val, operand_type = operand_result
|
||||||
|
|
||||||
|
# Convert to boolean if needed
|
||||||
|
operand_bool = convert_to_bool(builder, operand_val)
|
||||||
|
current_block = builder.block
|
||||||
|
|
||||||
|
if is_last:
|
||||||
|
# Last operand: result is this value
|
||||||
|
builder.branch(merge_block)
|
||||||
|
incoming_values.append((operand_bool, current_block))
|
||||||
|
else:
|
||||||
|
# Not last: check if true, continue or short-circuit
|
||||||
|
next_check = func.append_basic_block(name=f"and.check_{i + 1}")
|
||||||
|
builder.cbranch(operand_bool, next_check, false_block)
|
||||||
|
builder.position_at_end(next_check)
|
||||||
|
|
||||||
|
# False block: short-circuit with false
|
||||||
|
builder.position_at_end(false_block)
|
||||||
|
builder.branch(merge_block)
|
||||||
|
false_value = ir.Constant(ir.IntType(1), 0)
|
||||||
|
incoming_values.append((false_value, false_block))
|
||||||
|
|
||||||
|
# Merge block: phi node
|
||||||
|
builder.position_at_end(merge_block)
|
||||||
|
phi = builder.phi(ir.IntType(1), name="and.result")
|
||||||
|
for val, block in incoming_values:
|
||||||
|
phi.add_incoming(val, block)
|
||||||
|
|
||||||
|
logger.debug(f"Generated 'and' with {len(incoming_values)} incoming values")
|
||||||
|
return phi, ir.IntType(1)
|
||||||
|
|
||||||
|
|
||||||
|
def _handle_or_op(func, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab):
|
||||||
|
"""Handle `or` boolean operations."""
|
||||||
|
|
||||||
|
logger.debug(f"Handling 'or' operator with {len(expr.values)} operands")
|
||||||
|
|
||||||
|
merge_block = func.append_basic_block(name="or.merge")
|
||||||
|
true_block = func.append_basic_block(name="or.true")
|
||||||
|
|
||||||
|
incoming_values = []
|
||||||
|
|
||||||
|
for i, value in enumerate(expr.values):
|
||||||
|
is_last = i == len(expr.values) - 1
|
||||||
|
|
||||||
|
# Evaluate current operand
|
||||||
|
operand_result = eval_expr(
|
||||||
|
func, None, builder, value, local_sym_tab, map_sym_tab, structs_sym_tab
|
||||||
|
)
|
||||||
|
if operand_result is None:
|
||||||
|
logger.error(f"Failed to evaluate operand {i} in 'or' expression")
|
||||||
|
return None
|
||||||
|
|
||||||
|
operand_val, operand_type = operand_result
|
||||||
|
|
||||||
|
# Convert to boolean if needed
|
||||||
|
operand_bool = convert_to_bool(builder, operand_val)
|
||||||
|
current_block = builder.block
|
||||||
|
|
||||||
|
if is_last:
|
||||||
|
# Last operand: result is this value
|
||||||
|
builder.branch(merge_block)
|
||||||
|
incoming_values.append((operand_bool, current_block))
|
||||||
|
else:
|
||||||
|
# Not last: check if false, continue or short-circuit
|
||||||
|
next_check = func.append_basic_block(name=f"or.check_{i + 1}")
|
||||||
|
builder.cbranch(operand_bool, true_block, next_check)
|
||||||
|
builder.position_at_end(next_check)
|
||||||
|
|
||||||
|
# True block: short-circuit with true
|
||||||
|
builder.position_at_end(true_block)
|
||||||
|
builder.branch(merge_block)
|
||||||
|
true_value = ir.Constant(ir.IntType(1), 1)
|
||||||
|
incoming_values.append((true_value, true_block))
|
||||||
|
|
||||||
|
# Merge block: phi node
|
||||||
|
builder.position_at_end(merge_block)
|
||||||
|
phi = builder.phi(ir.IntType(1), name="or.result")
|
||||||
|
for val, block in incoming_values:
|
||||||
|
phi.add_incoming(val, block)
|
||||||
|
|
||||||
|
logger.debug(f"Generated 'or' with {len(incoming_values)} incoming values")
|
||||||
|
return phi, ir.IntType(1)
|
||||||
|
|
||||||
|
|
||||||
|
def _handle_boolean_op(
|
||||||
|
func,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
expr: ast.BoolOp,
|
||||||
|
local_sym_tab,
|
||||||
|
map_sym_tab,
|
||||||
|
structs_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""Handle `and` and `or` boolean operations."""
|
||||||
|
|
||||||
|
if isinstance(expr.op, ast.And):
|
||||||
|
return _handle_and_op(
|
||||||
|
func, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab
|
||||||
|
)
|
||||||
|
elif isinstance(expr.op, ast.Or):
|
||||||
|
return _handle_or_op(
|
||||||
|
func, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
logger.error(f"Unsupported boolean operator: {type(expr.op).__name__}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def eval_expr(
|
def eval_expr(
|
||||||
func,
|
func,
|
||||||
module,
|
module,
|
||||||
@ -315,6 +411,10 @@ def eval_expr(
|
|||||||
return _handle_unary_op(
|
return _handle_unary_op(
|
||||||
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
|
||||||
)
|
)
|
||||||
|
elif isinstance(expr, ast.BoolOp):
|
||||||
|
return _handle_boolean_op(
|
||||||
|
func, module, builder, expr, local_sym_tab, map_sym_tab, structs_sym_tab
|
||||||
|
)
|
||||||
logger.info("Unsupported expression evaluation")
|
logger.info("Unsupported expression evaluation")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
128
pythonbpf/expr/type_normalization.py
Normal file
128
pythonbpf/expr/type_normalization.py
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
from llvmlite import ir
|
||||||
|
import logging
|
||||||
|
import ast
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
COMPARISON_OPS = {
|
||||||
|
ast.Eq: "==",
|
||||||
|
ast.NotEq: "!=",
|
||||||
|
ast.Lt: "<",
|
||||||
|
ast.LtE: "<=",
|
||||||
|
ast.Gt: ">",
|
||||||
|
ast.GtE: ">=",
|
||||||
|
ast.Is: "==",
|
||||||
|
ast.IsNot: "!=",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _get_base_type_and_depth(ir_type):
|
||||||
|
"""Get the base type for pointer types."""
|
||||||
|
cur_type = ir_type
|
||||||
|
depth = 0
|
||||||
|
while isinstance(cur_type, ir.PointerType):
|
||||||
|
depth += 1
|
||||||
|
cur_type = cur_type.pointee
|
||||||
|
return cur_type, depth
|
||||||
|
|
||||||
|
|
||||||
|
def _deref_to_depth(func, builder, val, target_depth):
|
||||||
|
"""Dereference a pointer to a certain depth."""
|
||||||
|
|
||||||
|
cur_val = val
|
||||||
|
cur_type = val.type
|
||||||
|
|
||||||
|
for depth in range(target_depth):
|
||||||
|
if not isinstance(val.type, ir.PointerType):
|
||||||
|
logger.error("Cannot dereference further, non-pointer type")
|
||||||
|
return None
|
||||||
|
|
||||||
|
# dereference with null check
|
||||||
|
pointee_type = cur_type.pointee
|
||||||
|
null_check_block = builder.block
|
||||||
|
not_null_block = func.append_basic_block(name=f"deref_not_null_{depth}")
|
||||||
|
merge_block = func.append_basic_block(name=f"deref_merge_{depth}")
|
||||||
|
|
||||||
|
null_ptr = ir.Constant(cur_type, None)
|
||||||
|
is_not_null = builder.icmp_signed("!=", cur_val, null_ptr)
|
||||||
|
logger.debug(f"Inserted null check for pointer at depth {depth}")
|
||||||
|
|
||||||
|
builder.cbranch(is_not_null, not_null_block, merge_block)
|
||||||
|
|
||||||
|
builder.position_at_end(not_null_block)
|
||||||
|
dereferenced_val = builder.load(cur_val)
|
||||||
|
logger.debug(f"Dereferenced to depth {depth - 1}, type: {pointee_type}")
|
||||||
|
builder.branch(merge_block)
|
||||||
|
|
||||||
|
builder.position_at_end(merge_block)
|
||||||
|
phi = builder.phi(pointee_type, name=f"deref_result_{depth}")
|
||||||
|
|
||||||
|
zero_value = (
|
||||||
|
ir.Constant(pointee_type, 0)
|
||||||
|
if isinstance(pointee_type, ir.IntType)
|
||||||
|
else ir.Constant(pointee_type, None)
|
||||||
|
)
|
||||||
|
phi.add_incoming(zero_value, null_check_block)
|
||||||
|
|
||||||
|
phi.add_incoming(dereferenced_val, not_null_block)
|
||||||
|
|
||||||
|
# Continue with phi result
|
||||||
|
cur_val = phi
|
||||||
|
cur_type = pointee_type
|
||||||
|
return cur_val
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_types(func, builder, lhs, rhs):
|
||||||
|
"""Normalize types for comparison."""
|
||||||
|
|
||||||
|
logger.info(f"Normalizing types: {lhs.type} vs {rhs.type}")
|
||||||
|
if isinstance(lhs.type, ir.IntType) and isinstance(rhs.type, ir.IntType):
|
||||||
|
if lhs.type.width < rhs.type.width:
|
||||||
|
lhs = builder.sext(lhs, rhs.type)
|
||||||
|
else:
|
||||||
|
rhs = builder.sext(rhs, lhs.type)
|
||||||
|
return lhs, rhs
|
||||||
|
elif not isinstance(lhs.type, ir.PointerType) and not isinstance(
|
||||||
|
rhs.type, ir.PointerType
|
||||||
|
):
|
||||||
|
logger.error(f"Type mismatch: {lhs.type} vs {rhs.type}")
|
||||||
|
return None, None
|
||||||
|
else:
|
||||||
|
lhs_base, lhs_depth = _get_base_type_and_depth(lhs.type)
|
||||||
|
rhs_base, rhs_depth = _get_base_type_and_depth(rhs.type)
|
||||||
|
if lhs_base == rhs_base:
|
||||||
|
if lhs_depth < rhs_depth:
|
||||||
|
rhs = _deref_to_depth(func, builder, rhs, rhs_depth - lhs_depth)
|
||||||
|
elif rhs_depth < lhs_depth:
|
||||||
|
lhs = _deref_to_depth(func, builder, lhs, lhs_depth - rhs_depth)
|
||||||
|
return _normalize_types(func, builder, lhs, rhs)
|
||||||
|
|
||||||
|
|
||||||
|
def convert_to_bool(builder, val):
|
||||||
|
"""Convert a value to boolean."""
|
||||||
|
if val.type == ir.IntType(1):
|
||||||
|
return val
|
||||||
|
if isinstance(val.type, ir.PointerType):
|
||||||
|
zero = ir.Constant(val.type, None)
|
||||||
|
else:
|
||||||
|
zero = ir.Constant(val.type, 0)
|
||||||
|
return builder.icmp_signed("!=", val, zero)
|
||||||
|
|
||||||
|
|
||||||
|
def handle_comparator(func, builder, op, lhs, rhs):
|
||||||
|
"""Handle comparison operations."""
|
||||||
|
|
||||||
|
if lhs.type != rhs.type:
|
||||||
|
lhs, rhs = _normalize_types(func, builder, lhs, rhs)
|
||||||
|
|
||||||
|
if lhs is None or rhs is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if type(op) not in COMPARISON_OPS:
|
||||||
|
logger.error(f"Unsupported comparison operator: {type(op)}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
predicate = COMPARISON_OPS[type(op)]
|
||||||
|
result = builder.icmp_signed(predicate, lhs, rhs)
|
||||||
|
logger.debug(f"Comparison result: {result}")
|
||||||
|
return result, ir.IntType(1)
|
||||||
@ -7,7 +7,7 @@ from dataclasses import dataclass
|
|||||||
from pythonbpf.helper import HelperHandlerRegistry, handle_helper_call
|
from pythonbpf.helper import HelperHandlerRegistry, handle_helper_call
|
||||||
from pythonbpf.type_deducer import ctypes_to_ir
|
from pythonbpf.type_deducer import ctypes_to_ir
|
||||||
from pythonbpf.binary_ops import handle_binary_op
|
from pythonbpf.binary_ops import handle_binary_op
|
||||||
from pythonbpf.expr_pass import eval_expr, handle_expr, convert_to_bool
|
from pythonbpf.expr import eval_expr, handle_expr, convert_to_bool
|
||||||
|
|
||||||
from .return_utils import _handle_none_return, _handle_xdp_return, _is_xdp_name
|
from .return_utils import _handle_none_return, _handle_xdp_return, _is_xdp_name
|
||||||
|
|
||||||
@ -240,72 +240,13 @@ def handle_assign(
|
|||||||
logger.info("Unsupported assignment value type")
|
logger.info("Unsupported assignment value type")
|
||||||
|
|
||||||
|
|
||||||
def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab):
|
def handle_cond(
|
||||||
if True:
|
func, module, builder, cond, local_sym_tab, map_sym_tab, structs_sym_tab=None
|
||||||
val = eval_expr(func, module, builder, cond, local_sym_tab, map_sym_tab)[0]
|
):
|
||||||
return convert_to_bool(builder, val)
|
val = eval_expr(
|
||||||
if isinstance(cond, ast.Constant):
|
func, module, builder, cond, local_sym_tab, map_sym_tab, structs_sym_tab
|
||||||
if isinstance(cond.value, bool) or isinstance(cond.value, int):
|
)[0]
|
||||||
return ir.Constant(ir.IntType(1), int(cond.value))
|
return convert_to_bool(builder, val)
|
||||||
else:
|
|
||||||
raise ValueError("Unsupported constant type in condition")
|
|
||||||
return None
|
|
||||||
elif isinstance(cond, ast.Name):
|
|
||||||
if cond.id in local_sym_tab:
|
|
||||||
var = local_sym_tab[cond.id].var
|
|
||||||
val = builder.load(var)
|
|
||||||
if val.type != ir.IntType(1):
|
|
||||||
# Convert nonzero values to true, zero to false
|
|
||||||
if isinstance(val.type, ir.PointerType):
|
|
||||||
# For pointer types, compare with null pointer
|
|
||||||
zero = ir.Constant(val.type, None)
|
|
||||||
else:
|
|
||||||
# For integer types, compare with zero
|
|
||||||
zero = ir.Constant(val.type, 0)
|
|
||||||
val = builder.icmp_signed("!=", val, zero)
|
|
||||||
return val
|
|
||||||
else:
|
|
||||||
logger.info(f"Undefined variable {cond.id} in condition")
|
|
||||||
return None
|
|
||||||
elif isinstance(cond, ast.Compare):
|
|
||||||
lhs = eval_expr(func, module, builder, cond.left, local_sym_tab, map_sym_tab)[0]
|
|
||||||
if len(cond.ops) != 1 or len(cond.comparators) != 1:
|
|
||||||
logger.info("Unsupported complex comparison")
|
|
||||||
return None
|
|
||||||
rhs = eval_expr(
|
|
||||||
func, module, builder, cond.comparators[0], local_sym_tab, map_sym_tab
|
|
||||||
)[0]
|
|
||||||
op = cond.ops[0]
|
|
||||||
|
|
||||||
if lhs.type != rhs.type:
|
|
||||||
if isinstance(lhs.type, ir.IntType) and isinstance(rhs.type, ir.IntType):
|
|
||||||
# Extend the smaller type to the larger type
|
|
||||||
if lhs.type.width < rhs.type.width:
|
|
||||||
lhs = builder.sext(lhs, rhs.type)
|
|
||||||
elif lhs.type.width > rhs.type.width:
|
|
||||||
rhs = builder.sext(rhs, lhs.type)
|
|
||||||
else:
|
|
||||||
logger.info("Type mismatch in comparison")
|
|
||||||
return None
|
|
||||||
|
|
||||||
if isinstance(op, ast.Eq):
|
|
||||||
return builder.icmp_signed("==", lhs, rhs)
|
|
||||||
elif isinstance(op, ast.NotEq):
|
|
||||||
return builder.icmp_signed("!=", lhs, rhs)
|
|
||||||
elif isinstance(op, ast.Lt):
|
|
||||||
return builder.icmp_signed("<", lhs, rhs)
|
|
||||||
elif isinstance(op, ast.LtE):
|
|
||||||
return builder.icmp_signed("<=", lhs, rhs)
|
|
||||||
elif isinstance(op, ast.Gt):
|
|
||||||
return builder.icmp_signed(">", lhs, rhs)
|
|
||||||
elif isinstance(op, ast.GtE):
|
|
||||||
return builder.icmp_signed(">=", lhs, rhs)
|
|
||||||
else:
|
|
||||||
logger.info("Unsupported comparison operator")
|
|
||||||
return None
|
|
||||||
else:
|
|
||||||
logger.info("Unsupported condition expression")
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def handle_if(
|
def handle_if(
|
||||||
@ -321,7 +262,9 @@ def handle_if(
|
|||||||
else:
|
else:
|
||||||
else_block = None
|
else_block = None
|
||||||
|
|
||||||
cond = handle_cond(func, module, builder, stmt.test, local_sym_tab, map_sym_tab)
|
cond = handle_cond(
|
||||||
|
func, module, builder, stmt.test, local_sym_tab, map_sym_tab, structs_sym_tab
|
||||||
|
)
|
||||||
if else_block:
|
if else_block:
|
||||||
builder.cbranch(cond, then_block, else_block)
|
builder.cbranch(cond, then_block, else_block)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import logging
|
|||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
|
||||||
from llvmlite import ir
|
from llvmlite import ir
|
||||||
from pythonbpf.expr_pass import eval_expr
|
from pythonbpf.expr import eval_expr
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@ from logging import Logger
|
|||||||
from llvmlite import ir
|
from llvmlite import ir
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from .maps_utils import MapProcessorRegistry
|
from .maps_utils import MapProcessorRegistry
|
||||||
from ..debuginfo import DebugInfoGenerator
|
from pythonbpf.debuginfo import DebugInfoGenerator
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logger: Logger = logging.getLogger(__name__)
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|||||||
34
tests/failing_tests/conditionals/helper_cond.py
Normal file
34
tests/failing_tests/conditionals/helper_cond.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: Decided against fixing this
|
||||||
|
# as a workaround is assigning the result of lookup to a variable
|
||||||
|
# and then using that variable in the if statement.
|
||||||
|
# Might fix in future.
|
||||||
|
|
||||||
|
|
||||||
|
@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)
|
||||||
|
if last.lookup(0) > 0:
|
||||||
|
print("Hello, World!")
|
||||||
|
else:
|
||||||
|
print("Goodbye, World!")
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
compile()
|
||||||
34
tests/failing_tests/conditionals/struct_ptr.py
Normal file
34
tests/failing_tests/conditionals/struct_ptr.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
from pythonbpf import bpf, struct, section, bpfglobal, compile
|
||||||
|
from ctypes import c_void_p, c_int64, c_uint64
|
||||||
|
|
||||||
|
# NOTE: Decided against fixing this
|
||||||
|
# as one workaround is to just check any field of the struct
|
||||||
|
# in the if statement. Ugly but works.
|
||||||
|
# Might fix in future.
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@struct
|
||||||
|
class data_t:
|
||||||
|
pid: c_uint64
|
||||||
|
ts: c_uint64
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@section("tracepoint/syscalls/sys_enter_execve")
|
||||||
|
def hello_world(ctx: c_void_p) -> c_int64:
|
||||||
|
dat = data_t()
|
||||||
|
if dat:
|
||||||
|
print("Hello, World!")
|
||||||
|
else:
|
||||||
|
print("Goodbye, World!")
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
compile()
|
||||||
32
tests/passing_tests/conditionals/and.py
Normal file
32
tests/passing_tests/conditionals/and.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
from pythonbpf import bpf, map, section, bpfglobal, compile
|
||||||
|
from ctypes import c_void_p, c_int64, c_uint64
|
||||||
|
from pythonbpf.maps import HashMap
|
||||||
|
|
||||||
|
|
||||||
|
@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)
|
||||||
|
last.update(1, 2)
|
||||||
|
x = last.lookup(0)
|
||||||
|
y = last.lookup(1)
|
||||||
|
if x and y:
|
||||||
|
print("Hello, World!")
|
||||||
|
else:
|
||||||
|
print("Goodbye, World!")
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
compile()
|
||||||
@ -14,7 +14,7 @@ def last() -> HashMap:
|
|||||||
def hello_world(ctx: c_void_p) -> c_int64:
|
def hello_world(ctx: c_void_p) -> c_int64:
|
||||||
last.update(0, 1)
|
last.update(0, 1)
|
||||||
tsp = last.lookup(0)
|
tsp = last.lookup(0)
|
||||||
if not (tsp > 0):
|
if tsp > 0:
|
||||||
print("Hello, World!")
|
print("Hello, World!")
|
||||||
else:
|
else:
|
||||||
print("Goodbye, World!")
|
print("Goodbye, World!")
|
||||||
32
tests/passing_tests/conditionals/or.py
Normal file
32
tests/passing_tests/conditionals/or.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
from pythonbpf import bpf, map, section, bpfglobal, compile
|
||||||
|
from ctypes import c_void_p, c_int64, c_uint64
|
||||||
|
from pythonbpf.maps import HashMap
|
||||||
|
|
||||||
|
|
||||||
|
@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)
|
||||||
|
# last.update(1, 2)
|
||||||
|
x = last.lookup(0)
|
||||||
|
y = last.lookup(1)
|
||||||
|
if x or y:
|
||||||
|
print("Hello, World!")
|
||||||
|
else:
|
||||||
|
print("Goodbye, World!")
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
compile()
|
||||||
@ -13,7 +13,7 @@ class data_t:
|
|||||||
@section("tracepoint/syscalls/sys_enter_execve")
|
@section("tracepoint/syscalls/sys_enter_execve")
|
||||||
def hello_world(ctx: c_void_p) -> c_int64:
|
def hello_world(ctx: c_void_p) -> c_int64:
|
||||||
dat = data_t()
|
dat = data_t()
|
||||||
if dat.pid:
|
if dat.ts:
|
||||||
print("Hello, World!")
|
print("Hello, World!")
|
||||||
else:
|
else:
|
||||||
print("Goodbye, World!")
|
print("Goodbye, World!")
|
||||||
23
tests/passing_tests/conditionals/type_mismatch.py
Normal file
23
tests/passing_tests/conditionals/type_mismatch.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
from pythonbpf import bpf, section, bpfglobal, compile
|
||||||
|
from ctypes import c_void_p, c_int64, c_int32
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@section("tracepoint/syscalls/sys_enter_execve")
|
||||||
|
def hello_world(ctx: c_void_p) -> c_int64:
|
||||||
|
x = 0
|
||||||
|
y = c_int32(0)
|
||||||
|
if x == y:
|
||||||
|
print("Hello, World!")
|
||||||
|
else:
|
||||||
|
print("Goodbye, World!")
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
compile()
|
||||||
Reference in New Issue
Block a user