mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Compare commits
67 Commits
83e4094ca9
...
v0.1.4
| Author | SHA1 | Date | |
|---|---|---|---|
| 0678d70309 | |||
| 96fa5687f8 | |||
| 4d0dd68d56 | |||
| 89b0a07419 | |||
| 469ca43eaa | |||
| dc2b611cbc | |||
| 0c1acf1420 | |||
| 71b97e3e20 | |||
| 12ba3605e9 | |||
| d7427f306f | |||
| 0142381ce2 | |||
| 9223d7b5c5 | |||
| 3b74ade455 | |||
| dadcb69f1c | |||
| 2fd2a46838 | |||
| 1a66887f48 | |||
| 23f3cbcea7 | |||
| 429f51437f | |||
| c92272dd35 | |||
| 8792740eb0 | |||
| cf5faaad7f | |||
| 59b3d6514b | |||
| 3c956e671a | |||
| 8650297866 | |||
| 6831f11179 | |||
| d4e8e1bf73 | |||
| 08f2b283c9 | |||
| c38ecf6623 | |||
| 81807ace34 | |||
| 2f02f94b61 | |||
| 690ff7ffbc | |||
| bda88d3f8e | |||
| ba3e02052d | |||
| 9099b3eaec | |||
| 03da7c5cfc | |||
| cecf45061c | |||
| da9df2e6bf | |||
| 929eef31ef | |||
| 28cc0c5eec | |||
| 99d6c193f6 | |||
| 4f33db206c | |||
| 6ccbab402f | |||
| 7b01f1dde3 | |||
| 8ceb1d1ac3 | |||
| 668343532f | |||
| 84ad58b775 | |||
| 17f60d721b | |||
| d18c69fae1 | |||
| 9c58116c82 | |||
| 18f164bdec | |||
| 8d9ff2df3b | |||
| ffcd2de44d | |||
| 8dd2746411 | |||
| 7f6c318069 | |||
| d2e0f17ca8 | |||
| 4af6c4dcad | |||
| 5c8b132cb9 | |||
| 244ea143d4 | |||
| 58c372bcb3 | |||
| 168ab29be3 | |||
| 61f6743f0a | |||
| 6cd07498fe | |||
| c27da22bcb | |||
| b095828ae2 | |||
| 7e45864552 | |||
| fa2ff0a242 | |||
| c1466a5bca |
3
.github/workflows/format.yml
vendored
3
.github/workflows/format.yml
vendored
@ -5,10 +5,7 @@ name: Format
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
pull_request:
|
|
||||||
push:
|
push:
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
pre-commit:
|
pre-commit:
|
||||||
|
|||||||
@ -35,22 +35,21 @@ repos:
|
|||||||
- id: requirements-txt-fixer
|
- id: requirements-txt-fixer
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
|
|
||||||
#- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
# rev: "v0.4.2"
|
rev: "v0.4.2"
|
||||||
# hooks:
|
hooks:
|
||||||
# - id: ruff
|
- id: ruff
|
||||||
# args: ["--fix", "--show-fixes"]
|
args: ["--fix", "--show-fixes"]
|
||||||
# - id: ruff-format
|
- id: ruff-format
|
||||||
# exclude: ^(docs)
|
exclude: ^(docs)|^(tests)|^(examples)
|
||||||
|
|
||||||
## Checking static types
|
# Checking static types
|
||||||
#- repo: https://github.com/pre-commit/mirrors-mypy
|
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||||
# rev: "v1.10.0"
|
rev: "v1.10.0"
|
||||||
# hooks:
|
hooks:
|
||||||
# - id: mypy
|
- id: mypy
|
||||||
# files: "setup.py"
|
exclude: ^(tests)|^(examples)
|
||||||
# args: []
|
additional_dependencies: [types-setuptools]
|
||||||
# additional_dependencies: [types-setuptools]
|
|
||||||
|
|
||||||
# Changes tabs to spaces
|
# Changes tabs to spaces
|
||||||
- repo: https://github.com/Lucas-C/pre-commit-hooks
|
- repo: https://github.com/Lucas-C/pre-commit-hooks
|
||||||
|
|||||||
16
README.md
16
README.md
@ -1,4 +1,14 @@
|
|||||||
<img src="https://github.com/user-attachments/assets/b175bf39-23cb-475d-a6e1-7b5c99a1ed72" alt="pythonbpf" width="450">
|
<picture>
|
||||||
|
<source
|
||||||
|
media="(prefers-color-scheme: light)"
|
||||||
|
srcset="https://github.com/user-attachments/assets/f3738131-d7cb-4b5c-8699-c7010295a159"
|
||||||
|
width="450"
|
||||||
|
alt="Light‐mode image">
|
||||||
|
<img
|
||||||
|
src="https://github.com/user-attachments/assets/b175bf39-23cb-475d-a6e1-7b5c99a1ed72"
|
||||||
|
width="450"
|
||||||
|
alt="Dark‐mode image">
|
||||||
|
</picture>
|
||||||
<!-- Badges -->
|
<!-- Badges -->
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<!-- PyPI -->
|
<!-- PyPI -->
|
||||||
@ -50,12 +60,13 @@ pip install pythonbpf pylibbpf
|
|||||||
```python
|
```python
|
||||||
import time
|
import time
|
||||||
from pythonbpf import bpf, map, section, bpfglobal, BPF
|
from pythonbpf import bpf, map, section, bpfglobal, BPF
|
||||||
from pythonbpf.helpers import pid
|
from pythonbpf.helper import pid
|
||||||
from pythonbpf.maps import HashMap
|
from pythonbpf.maps import HashMap
|
||||||
from pylibbpf import *
|
from pylibbpf import *
|
||||||
from ctypes import c_void_p, c_int64, c_uint64, c_int32
|
from ctypes import c_void_p, c_int64, c_uint64, c_int32
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
|
||||||
# This program attaches an eBPF tracepoint to sys_enter_clone,
|
# This program attaches an eBPF tracepoint to sys_enter_clone,
|
||||||
# counts per-PID clone syscalls, stores them in a hash map,
|
# counts per-PID clone syscalls, stores them in a hash map,
|
||||||
# and then plots the distribution as a histogram using matplotlib.
|
# and then plots the distribution as a histogram using matplotlib.
|
||||||
@ -66,6 +77,7 @@ import matplotlib.pyplot as plt
|
|||||||
def hist() -> HashMap:
|
def hist() -> HashMap:
|
||||||
return HashMap(key=c_int32, value=c_uint64, max_entries=4096)
|
return HashMap(key=c_int32, value=c_uint64, max_entries=4096)
|
||||||
|
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
@section("tracepoint/syscalls/sys_enter_clone")
|
@section("tracepoint/syscalls/sys_enter_clone")
|
||||||
def hello(ctx: c_void_p) -> c_int64:
|
def hello(ctx: c_void_p) -> c_int64:
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
from pythonbpf import bpf, map, section, bpfglobal, compile
|
from pythonbpf import bpf, map, section, bpfglobal, compile
|
||||||
from pythonbpf.helpers import ktime
|
from pythonbpf.helper import ktime
|
||||||
from pythonbpf.maps import HashMap
|
from pythonbpf.maps import HashMap
|
||||||
|
|
||||||
from ctypes import c_void_p, c_int64, c_uint64
|
from ctypes import c_void_p, c_int64, c_uint64
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
from pythonbpf import bpf, map, section, bpfglobal, compile
|
from pythonbpf import bpf, map, section, bpfglobal, compile
|
||||||
from pythonbpf.helpers import ktime
|
from pythonbpf.helper import ktime
|
||||||
from pythonbpf.maps import HashMap
|
from pythonbpf.maps import HashMap
|
||||||
|
|
||||||
from ctypes import c_void_p, c_int32, c_uint64
|
from ctypes import c_void_p, c_int32, c_uint64
|
||||||
|
|||||||
@ -10,7 +10,7 @@
|
|||||||
"import time\n",
|
"import time\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from pythonbpf import bpf, map, section, bpfglobal, BPF\n",
|
"from pythonbpf import bpf, map, section, bpfglobal, BPF\n",
|
||||||
"from pythonbpf.helpers import pid\n",
|
"from pythonbpf.helper import pid\n",
|
||||||
"from pythonbpf.maps import HashMap\n",
|
"from pythonbpf.maps import HashMap\n",
|
||||||
"from pylibbpf import *\n",
|
"from pylibbpf import *\n",
|
||||||
"from ctypes import c_void_p, c_int64, c_uint64, c_int32\n",
|
"from ctypes import c_void_p, c_int64, c_uint64, c_int32\n",
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from pythonbpf import bpf, map, section, bpfglobal, BPF
|
from pythonbpf import bpf, map, section, bpfglobal, BPF
|
||||||
from pythonbpf.helpers import pid
|
from pythonbpf.helper import pid
|
||||||
from pythonbpf.maps import HashMap
|
from pythonbpf.maps import HashMap
|
||||||
from pylibbpf import *
|
from pylibbpf import BpfMap
|
||||||
from ctypes import c_void_p, c_int64, c_uint64, c_int32
|
from ctypes import c_void_p, c_int64, c_uint64, c_int32
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
from pythonbpf import bpf, map, struct, section, bpfglobal, compile
|
from pythonbpf import bpf, map, struct, section, bpfglobal, compile
|
||||||
from pythonbpf.helpers import ktime, pid
|
from pythonbpf.helper import ktime, pid
|
||||||
from pythonbpf.maps import PerfEventArray
|
from pythonbpf.maps import PerfEventArray
|
||||||
|
|
||||||
from ctypes import c_void_p, c_int32, c_uint64
|
from ctypes import c_void_p, c_int32, c_uint64
|
||||||
@ -23,13 +23,11 @@ def events() -> PerfEventArray:
|
|||||||
@section("tracepoint/syscalls/sys_enter_clone")
|
@section("tracepoint/syscalls/sys_enter_clone")
|
||||||
def hello(ctx: c_void_p) -> c_int32:
|
def hello(ctx: c_void_p) -> c_int32:
|
||||||
dataobj = data_t()
|
dataobj = data_t()
|
||||||
ts = ktime()
|
|
||||||
process_id = pid()
|
|
||||||
strobj = "hellohellohello"
|
strobj = "hellohellohello"
|
||||||
dataobj.pid = pid()
|
dataobj.pid = pid()
|
||||||
dataobj.ts = ktime()
|
dataobj.ts = ktime()
|
||||||
# dataobj.comm = strobj
|
# dataobj.comm = strobj
|
||||||
print(f"clone called at {dataobj.ts} by pid {dataobj.pid}, comm {strobj}")
|
print(f"clone called at {dataobj.ts} by pid" f"{dataobj.pid}, comm {strobj}")
|
||||||
events.output(dataobj)
|
events.output(dataobj)
|
||||||
return c_int32(0)
|
return c_int32(0)
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
from pythonbpf import bpf, map, section, bpfglobal, compile
|
from pythonbpf import bpf, map, section, bpfglobal, compile
|
||||||
from pythonbpf.helpers import ktime
|
from pythonbpf.helper import ktime
|
||||||
from pythonbpf.maps import HashMap
|
from pythonbpf.maps import HashMap
|
||||||
|
|
||||||
from ctypes import c_void_p, c_int64, c_uint64
|
from ctypes import c_void_p, c_int64, c_uint64
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
from pythonbpf import bpf, map, section, bpfglobal, compile
|
from pythonbpf import bpf, map, section, bpfglobal, compile
|
||||||
from pythonbpf.helpers import XDP_PASS
|
from pythonbpf.helper import XDP_PASS
|
||||||
from pythonbpf.maps import HashMap
|
from pythonbpf.maps import HashMap
|
||||||
|
|
||||||
from ctypes import c_void_p, c_int64
|
from ctypes import c_void_p, c_int64
|
||||||
|
|||||||
@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "pythonbpf"
|
name = "pythonbpf"
|
||||||
version = "0.1.3"
|
version = "0.1.4"
|
||||||
description = "Reduced Python frontend for eBPF"
|
description = "Reduced Python frontend for eBPF"
|
||||||
authors = [
|
authors = [
|
||||||
{ name = "r41k0u", email="pragyanshchaturvedi18@gmail.com" },
|
{ name = "r41k0u", email="pragyanshchaturvedi18@gmail.com" },
|
||||||
|
|||||||
@ -1,2 +1,13 @@
|
|||||||
from .decorators import bpf, map, section, bpfglobal, struct
|
from .decorators import bpf, map, section, bpfglobal, struct
|
||||||
from .codegen import compile_to_ir, compile, BPF
|
from .codegen import compile_to_ir, compile, BPF
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"bpf",
|
||||||
|
"map",
|
||||||
|
"section",
|
||||||
|
"bpfglobal",
|
||||||
|
"struct",
|
||||||
|
"compile_to_ir",
|
||||||
|
"compile",
|
||||||
|
"BPF",
|
||||||
|
]
|
||||||
|
|||||||
@ -1,5 +1,9 @@
|
|||||||
import ast
|
import ast
|
||||||
from llvmlite import ir
|
from llvmlite import ir
|
||||||
|
from logging import Logger
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def recursive_dereferencer(var, builder):
|
def recursive_dereferencer(var, builder):
|
||||||
@ -17,7 +21,7 @@ def recursive_dereferencer(var, builder):
|
|||||||
|
|
||||||
|
|
||||||
def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab, func):
|
def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab, func):
|
||||||
print(module)
|
logger.info(f"module {module}")
|
||||||
left = rval.left
|
left = rval.left
|
||||||
right = rval.right
|
right = rval.right
|
||||||
op = rval.op
|
op = rval.op
|
||||||
@ -25,7 +29,7 @@ def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab
|
|||||||
# Handle left operand
|
# Handle left operand
|
||||||
if isinstance(left, ast.Name):
|
if isinstance(left, ast.Name):
|
||||||
if left.id in local_sym_tab:
|
if left.id in local_sym_tab:
|
||||||
left = recursive_dereferencer(local_sym_tab[left.id][0], builder)
|
left = recursive_dereferencer(local_sym_tab[left.id].var, builder)
|
||||||
else:
|
else:
|
||||||
raise SyntaxError(f"Undefined variable: {left.id}")
|
raise SyntaxError(f"Undefined variable: {left.id}")
|
||||||
elif isinstance(left, ast.Constant):
|
elif isinstance(left, ast.Constant):
|
||||||
@ -35,7 +39,7 @@ def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab
|
|||||||
|
|
||||||
if isinstance(right, ast.Name):
|
if isinstance(right, ast.Name):
|
||||||
if right.id in local_sym_tab:
|
if right.id in local_sym_tab:
|
||||||
right = recursive_dereferencer(local_sym_tab[right.id][0], builder)
|
right = recursive_dereferencer(local_sym_tab[right.id].var, builder)
|
||||||
else:
|
else:
|
||||||
raise SyntaxError(f"Undefined variable: {right.id}")
|
raise SyntaxError(f"Undefined variable: {right.id}")
|
||||||
elif isinstance(right, ast.Constant):
|
elif isinstance(right, ast.Constant):
|
||||||
@ -43,29 +47,29 @@ def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab
|
|||||||
else:
|
else:
|
||||||
raise SyntaxError("Unsupported right operand type")
|
raise SyntaxError("Unsupported right operand type")
|
||||||
|
|
||||||
print(f"left is {left}, right is {right}, op is {op}")
|
logger.info(f"left is {left}, right is {right}, op is {op}")
|
||||||
|
|
||||||
if isinstance(op, ast.Add):
|
if isinstance(op, ast.Add):
|
||||||
builder.store(builder.add(left, right), local_sym_tab[var_name][0])
|
builder.store(builder.add(left, right), local_sym_tab[var_name].var)
|
||||||
elif isinstance(op, ast.Sub):
|
elif isinstance(op, ast.Sub):
|
||||||
builder.store(builder.sub(left, right), local_sym_tab[var_name][0])
|
builder.store(builder.sub(left, right), local_sym_tab[var_name].var)
|
||||||
elif isinstance(op, ast.Mult):
|
elif isinstance(op, ast.Mult):
|
||||||
builder.store(builder.mul(left, right), local_sym_tab[var_name][0])
|
builder.store(builder.mul(left, right), local_sym_tab[var_name].var)
|
||||||
elif isinstance(op, ast.Div):
|
elif isinstance(op, ast.Div):
|
||||||
builder.store(builder.sdiv(left, right), local_sym_tab[var_name][0])
|
builder.store(builder.sdiv(left, right), local_sym_tab[var_name].var)
|
||||||
elif isinstance(op, ast.Mod):
|
elif isinstance(op, ast.Mod):
|
||||||
builder.store(builder.srem(left, right), local_sym_tab[var_name][0])
|
builder.store(builder.srem(left, right), local_sym_tab[var_name].var)
|
||||||
elif isinstance(op, ast.LShift):
|
elif isinstance(op, ast.LShift):
|
||||||
builder.store(builder.shl(left, right), local_sym_tab[var_name][0])
|
builder.store(builder.shl(left, right), local_sym_tab[var_name].var)
|
||||||
elif isinstance(op, ast.RShift):
|
elif isinstance(op, ast.RShift):
|
||||||
builder.store(builder.lshr(left, right), local_sym_tab[var_name][0])
|
builder.store(builder.lshr(left, right), local_sym_tab[var_name].var)
|
||||||
elif isinstance(op, ast.BitOr):
|
elif isinstance(op, ast.BitOr):
|
||||||
builder.store(builder.or_(left, right), local_sym_tab[var_name][0])
|
builder.store(builder.or_(left, right), local_sym_tab[var_name].var)
|
||||||
elif isinstance(op, ast.BitXor):
|
elif isinstance(op, ast.BitXor):
|
||||||
builder.store(builder.xor(left, right), local_sym_tab[var_name][0])
|
builder.store(builder.xor(left, right), local_sym_tab[var_name].var)
|
||||||
elif isinstance(op, ast.BitAnd):
|
elif isinstance(op, ast.BitAnd):
|
||||||
builder.store(builder.and_(left, right), local_sym_tab[var_name][0])
|
builder.store(builder.and_(left, right), local_sym_tab[var_name].var)
|
||||||
elif isinstance(op, ast.FloorDiv):
|
elif isinstance(op, ast.FloorDiv):
|
||||||
builder.store(builder.udiv(left, right), local_sym_tab[var_name][0])
|
builder.store(builder.udiv(left, right), local_sym_tab[var_name].var)
|
||||||
else:
|
else:
|
||||||
raise SyntaxError("Unsupported binary operation")
|
raise SyntaxError("Unsupported binary operation")
|
||||||
|
|||||||
@ -1,654 +0,0 @@
|
|||||||
import ast
|
|
||||||
from llvmlite import ir
|
|
||||||
from .expr_pass import eval_expr
|
|
||||||
|
|
||||||
|
|
||||||
def bpf_ktime_get_ns_emitter(
|
|
||||||
call,
|
|
||||||
map_ptr,
|
|
||||||
module,
|
|
||||||
builder,
|
|
||||||
func,
|
|
||||||
local_sym_tab=None,
|
|
||||||
struct_sym_tab=None,
|
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
Emit LLVM IR for bpf_ktime_get_ns helper function call.
|
|
||||||
"""
|
|
||||||
# func is an arg to just have a uniform signature with other emitters
|
|
||||||
helper_id = ir.Constant(ir.IntType(64), 5)
|
|
||||||
fn_type = ir.FunctionType(ir.IntType(64), [], var_arg=False)
|
|
||||||
fn_ptr_type = ir.PointerType(fn_type)
|
|
||||||
fn_ptr = builder.inttoptr(helper_id, fn_ptr_type)
|
|
||||||
result = builder.call(fn_ptr, [], tail=False)
|
|
||||||
return result, ir.IntType(64)
|
|
||||||
|
|
||||||
|
|
||||||
def bpf_map_lookup_elem_emitter(
|
|
||||||
call,
|
|
||||||
map_ptr,
|
|
||||||
module,
|
|
||||||
builder,
|
|
||||||
func,
|
|
||||||
local_sym_tab=None,
|
|
||||||
struct_sym_tab=None,
|
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
Emit LLVM IR for bpf_map_lookup_elem helper function call.
|
|
||||||
"""
|
|
||||||
if call.args and len(call.args) != 1:
|
|
||||||
raise ValueError(
|
|
||||||
"Map lookup expects exactly one argument, got " f"{len(call.args)}"
|
|
||||||
)
|
|
||||||
key_arg = call.args[0]
|
|
||||||
if isinstance(key_arg, ast.Name):
|
|
||||||
key_name = key_arg.id
|
|
||||||
if local_sym_tab and key_name in local_sym_tab:
|
|
||||||
key_ptr = local_sym_tab[key_name][0]
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
f"Key variable {key_name} not found in local symbol table."
|
|
||||||
)
|
|
||||||
elif isinstance(key_arg, ast.Constant) and isinstance(key_arg.value, int):
|
|
||||||
# handle constant integer keys
|
|
||||||
key_val = key_arg.value
|
|
||||||
key_type = ir.IntType(64)
|
|
||||||
key_ptr = builder.alloca(key_type)
|
|
||||||
key_ptr.align = key_type // 8
|
|
||||||
builder.store(ir.Constant(key_type, key_val), key_ptr)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(
|
|
||||||
"Only simple variable names are supported as keys in map lookup."
|
|
||||||
)
|
|
||||||
|
|
||||||
if key_ptr is None:
|
|
||||||
raise ValueError("Key pointer is None.")
|
|
||||||
|
|
||||||
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
|
||||||
|
|
||||||
fn_type = ir.FunctionType(
|
|
||||||
ir.PointerType(), # Return type: void*
|
|
||||||
[ir.PointerType(), ir.PointerType()], # Args: (void*, void*)
|
|
||||||
var_arg=False,
|
|
||||||
)
|
|
||||||
fn_ptr_type = ir.PointerType(fn_type)
|
|
||||||
|
|
||||||
# Helper ID 1 is bpf_map_lookup_elem
|
|
||||||
fn_addr = ir.Constant(ir.IntType(64), 1)
|
|
||||||
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
|
||||||
|
|
||||||
result = builder.call(fn_ptr, [map_void_ptr, key_ptr], tail=False)
|
|
||||||
|
|
||||||
return result, ir.PointerType()
|
|
||||||
|
|
||||||
|
|
||||||
def bpf_printk_emitter(
|
|
||||||
call,
|
|
||||||
map_ptr,
|
|
||||||
module,
|
|
||||||
builder,
|
|
||||||
func,
|
|
||||||
local_sym_tab=None,
|
|
||||||
struct_sym_tab=None,
|
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
|
||||||
if not hasattr(func, "_fmt_counter"):
|
|
||||||
func._fmt_counter = 0
|
|
||||||
|
|
||||||
if not call.args:
|
|
||||||
raise ValueError("print expects at least one argument")
|
|
||||||
|
|
||||||
if isinstance(call.args[0], ast.JoinedStr):
|
|
||||||
fmt_parts = []
|
|
||||||
exprs = []
|
|
||||||
|
|
||||||
for value in call.args[0].values:
|
|
||||||
print("Value in f-string:", ast.dump(value))
|
|
||||||
if isinstance(value, ast.Constant):
|
|
||||||
if isinstance(value.value, str):
|
|
||||||
fmt_parts.append(value.value)
|
|
||||||
elif isinstance(value.value, int):
|
|
||||||
fmt_parts.append("%lld")
|
|
||||||
exprs.append(ir.Constant(ir.IntType(64), value.value))
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(
|
|
||||||
"Only string and integer constants are supported in f-string."
|
|
||||||
)
|
|
||||||
elif isinstance(value, ast.FormattedValue):
|
|
||||||
print("Formatted value:", ast.dump(value))
|
|
||||||
# TODO: Dirty handling here, only checks for int or str
|
|
||||||
if isinstance(value.value, ast.Name):
|
|
||||||
if local_sym_tab and value.value.id in local_sym_tab:
|
|
||||||
var_ptr, var_type = local_sym_tab[value.value.id]
|
|
||||||
if isinstance(var_type, ir.IntType):
|
|
||||||
fmt_parts.append("%lld")
|
|
||||||
exprs.append(value.value)
|
|
||||||
elif var_type == ir.PointerType(ir.IntType(8)):
|
|
||||||
# Case with string
|
|
||||||
fmt_parts.append("%s")
|
|
||||||
exprs.append(value.value)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(
|
|
||||||
"Only integer and pointer types are supported in formatted values."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
f"Variable {value.value.id} not found in local symbol table."
|
|
||||||
)
|
|
||||||
elif isinstance(value.value, ast.Attribute):
|
|
||||||
# object field access from struct
|
|
||||||
if (
|
|
||||||
isinstance(value.value.value, ast.Name)
|
|
||||||
and local_sym_tab
|
|
||||||
and value.value.value.id in local_sym_tab
|
|
||||||
):
|
|
||||||
var_name = value.value.value.id
|
|
||||||
field_name = value.value.attr
|
|
||||||
if local_var_metadata and var_name in local_var_metadata:
|
|
||||||
var_type = local_var_metadata[var_name]
|
|
||||||
if var_type in struct_sym_tab:
|
|
||||||
struct_info = struct_sym_tab[var_type]
|
|
||||||
if field_name in struct_info.fields:
|
|
||||||
field_type = struct_info.field_type(field_name)
|
|
||||||
if isinstance(field_type, ir.IntType):
|
|
||||||
fmt_parts.append("%lld")
|
|
||||||
exprs.append(value.value)
|
|
||||||
elif field_type == ir.PointerType(ir.IntType(8)):
|
|
||||||
fmt_parts.append("%s")
|
|
||||||
exprs.append(value.value)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(
|
|
||||||
"Only integer and pointer types are supported in formatted values."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
f"Field {field_name} not found in struct {var_type}."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
f"Struct type {var_type} for variable {var_name} not found in struct symbol table."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
f"Metadata for variable {var_name} not found in local variable metadata."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
f"Variable {value.value.value.id} not found in local symbol table."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(
|
|
||||||
"Only simple variable names are supported in formatted values."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError("Unsupported value type in f-string.")
|
|
||||||
|
|
||||||
fmt_str = "".join(fmt_parts) + "\n" + "\0"
|
|
||||||
fmt_name = f"{func.name}____fmt{func._fmt_counter}"
|
|
||||||
func._fmt_counter += 1
|
|
||||||
|
|
||||||
fmt_gvar = ir.GlobalVariable(
|
|
||||||
module, ir.ArrayType(ir.IntType(8), len(fmt_str)), name=fmt_name
|
|
||||||
)
|
|
||||||
fmt_gvar.global_constant = True
|
|
||||||
fmt_gvar.initializer = ir.Constant( # type: ignore
|
|
||||||
ir.ArrayType(ir.IntType(8), len(fmt_str)), bytearray(fmt_str.encode("utf8"))
|
|
||||||
)
|
|
||||||
fmt_gvar.linkage = "internal"
|
|
||||||
fmt_gvar.align = 1 # type: ignore
|
|
||||||
|
|
||||||
fmt_ptr = builder.bitcast(fmt_gvar, ir.PointerType())
|
|
||||||
|
|
||||||
args = [fmt_ptr, ir.Constant(ir.IntType(32), len(fmt_str))]
|
|
||||||
|
|
||||||
# Only 3 args supported in bpf_printk
|
|
||||||
if len(exprs) > 3:
|
|
||||||
print(
|
|
||||||
"Warning: bpf_printk supports up to 3 arguments, extra arguments will be ignored."
|
|
||||||
)
|
|
||||||
|
|
||||||
for expr in exprs[:3]:
|
|
||||||
print(f"{ast.dump(expr)}")
|
|
||||||
val, _ = eval_expr(
|
|
||||||
func,
|
|
||||||
module,
|
|
||||||
builder,
|
|
||||||
expr,
|
|
||||||
local_sym_tab,
|
|
||||||
None,
|
|
||||||
struct_sym_tab,
|
|
||||||
local_var_metadata,
|
|
||||||
)
|
|
||||||
if val:
|
|
||||||
if isinstance(val.type, ir.PointerType):
|
|
||||||
val = builder.ptrtoint(val, ir.IntType(64))
|
|
||||||
elif isinstance(val.type, ir.IntType):
|
|
||||||
if val.type.width < 64:
|
|
||||||
val = builder.sext(val, ir.IntType(64))
|
|
||||||
else:
|
|
||||||
print(
|
|
||||||
"Warning: Only integer and pointer types are supported in bpf_printk arguments. Others will be converted to 0."
|
|
||||||
)
|
|
||||||
val = ir.Constant(ir.IntType(64), 0)
|
|
||||||
args.append(val)
|
|
||||||
else:
|
|
||||||
print(
|
|
||||||
"Warning: Failed to evaluate expression for bpf_printk argument. It will be converted to 0."
|
|
||||||
)
|
|
||||||
args.append(ir.Constant(ir.IntType(64), 0))
|
|
||||||
fn_type = ir.FunctionType(
|
|
||||||
ir.IntType(64), [ir.PointerType(), ir.IntType(32)], var_arg=True
|
|
||||||
)
|
|
||||||
fn_ptr_type = ir.PointerType(fn_type)
|
|
||||||
fn_addr = ir.Constant(ir.IntType(64), 6)
|
|
||||||
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
|
||||||
return builder.call(fn_ptr, args, tail=True)
|
|
||||||
|
|
||||||
for arg in call.args:
|
|
||||||
if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
|
|
||||||
fmt_str = arg.value + "\n" + "\0"
|
|
||||||
fmt_name = f"{func.name}____fmt{func._fmt_counter}"
|
|
||||||
func._fmt_counter += 1
|
|
||||||
|
|
||||||
fmt_gvar = ir.GlobalVariable(
|
|
||||||
module, ir.ArrayType(ir.IntType(8), len(fmt_str)), name=fmt_name
|
|
||||||
)
|
|
||||||
fmt_gvar.global_constant = True
|
|
||||||
fmt_gvar.initializer = ir.Constant( # type: ignore
|
|
||||||
ir.ArrayType(ir.IntType(8), len(fmt_str)),
|
|
||||||
bytearray(fmt_str.encode("utf8")),
|
|
||||||
)
|
|
||||||
fmt_gvar.linkage = "internal"
|
|
||||||
fmt_gvar.align = 1 # type: ignore
|
|
||||||
|
|
||||||
fmt_ptr = builder.bitcast(fmt_gvar, ir.PointerType())
|
|
||||||
|
|
||||||
fn_type = ir.FunctionType(
|
|
||||||
ir.IntType(64), [ir.PointerType(), ir.IntType(32)], var_arg=True
|
|
||||||
)
|
|
||||||
fn_ptr_type = ir.PointerType(fn_type)
|
|
||||||
fn_addr = ir.Constant(ir.IntType(64), 6)
|
|
||||||
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
|
||||||
|
|
||||||
builder.call(
|
|
||||||
fn_ptr, [fmt_ptr, ir.Constant(ir.IntType(32), len(fmt_str))], tail=True
|
|
||||||
)
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def bpf_map_update_elem_emitter(
|
|
||||||
call,
|
|
||||||
map_ptr,
|
|
||||||
module,
|
|
||||||
builder,
|
|
||||||
func,
|
|
||||||
local_sym_tab=None,
|
|
||||||
struct_sym_tab=None,
|
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
Emit LLVM IR for bpf_map_update_elem helper function call.
|
|
||||||
Expected call signature: map.update(key, value, flags=0)
|
|
||||||
"""
|
|
||||||
if not call.args or len(call.args) < 2 or len(call.args) > 3:
|
|
||||||
raise ValueError(
|
|
||||||
"Map update expects 2 or 3 arguments (key, value, flags), got "
|
|
||||||
f"{len(call.args)}"
|
|
||||||
)
|
|
||||||
|
|
||||||
key_arg = call.args[0]
|
|
||||||
value_arg = call.args[1]
|
|
||||||
flags_arg = call.args[2] if len(call.args) > 2 else None
|
|
||||||
|
|
||||||
# Handle key
|
|
||||||
if isinstance(key_arg, ast.Name):
|
|
||||||
key_name = key_arg.id
|
|
||||||
if local_sym_tab and key_name in local_sym_tab:
|
|
||||||
key_ptr = local_sym_tab[key_name][0]
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
f"Key variable {key_name} not found in local symbol table."
|
|
||||||
)
|
|
||||||
elif isinstance(key_arg, ast.Constant) and isinstance(key_arg.value, int):
|
|
||||||
# Handle constant integer keys
|
|
||||||
key_val = key_arg.value
|
|
||||||
key_type = ir.IntType(64)
|
|
||||||
key_ptr = builder.alloca(key_type)
|
|
||||||
key_ptr.align = key_type.width // 8
|
|
||||||
builder.store(ir.Constant(key_type, key_val), key_ptr)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(
|
|
||||||
"Only simple variable names and integer constants are supported as keys in map update."
|
|
||||||
)
|
|
||||||
|
|
||||||
# Handle value
|
|
||||||
if isinstance(value_arg, ast.Name):
|
|
||||||
value_name = value_arg.id
|
|
||||||
if local_sym_tab and value_name in local_sym_tab:
|
|
||||||
value_ptr = local_sym_tab[value_name][0]
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
f"Value variable {value_name} not found in local symbol table."
|
|
||||||
)
|
|
||||||
elif isinstance(value_arg, ast.Constant) and isinstance(value_arg.value, int):
|
|
||||||
# Handle constant integers
|
|
||||||
value_val = value_arg.value
|
|
||||||
value_type = ir.IntType(64)
|
|
||||||
value_ptr = builder.alloca(value_type)
|
|
||||||
value_ptr.align = value_type.width // 8
|
|
||||||
builder.store(ir.Constant(value_type, value_val), value_ptr)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(
|
|
||||||
"Only simple variable names and integer constants are supported as values in map update."
|
|
||||||
)
|
|
||||||
|
|
||||||
# Handle flags argument (defaults to 0)
|
|
||||||
if flags_arg is not None:
|
|
||||||
if isinstance(flags_arg, ast.Constant) and isinstance(flags_arg.value, int):
|
|
||||||
flags_val = flags_arg.value
|
|
||||||
elif isinstance(flags_arg, ast.Name):
|
|
||||||
flags_name = flags_arg.id
|
|
||||||
if local_sym_tab and flags_name in local_sym_tab:
|
|
||||||
# Assume it's a stored integer value, load it
|
|
||||||
flags_ptr = local_sym_tab[flags_name][0]
|
|
||||||
flags_val = builder.load(flags_ptr)
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
f"Flags variable {flags_name} not found in local symbol table."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(
|
|
||||||
"Only integer constants and simple variable names are supported as flags in map update."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
flags_val = 0
|
|
||||||
|
|
||||||
if key_ptr is None or value_ptr is None:
|
|
||||||
raise ValueError("Key pointer or value pointer is None.")
|
|
||||||
|
|
||||||
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
|
||||||
fn_type = ir.FunctionType(
|
|
||||||
ir.IntType(64),
|
|
||||||
[ir.PointerType(), ir.PointerType(), ir.PointerType(), ir.IntType(64)],
|
|
||||||
var_arg=False,
|
|
||||||
)
|
|
||||||
fn_ptr_type = ir.PointerType(fn_type)
|
|
||||||
|
|
||||||
# helper id
|
|
||||||
fn_addr = ir.Constant(ir.IntType(64), 2)
|
|
||||||
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
|
||||||
|
|
||||||
if isinstance(flags_val, int):
|
|
||||||
flags_const = ir.Constant(ir.IntType(64), flags_val)
|
|
||||||
else:
|
|
||||||
flags_const = flags_val
|
|
||||||
|
|
||||||
result = builder.call(
|
|
||||||
fn_ptr, [map_void_ptr, key_ptr, value_ptr, flags_const], tail=False
|
|
||||||
)
|
|
||||||
|
|
||||||
return result, None
|
|
||||||
|
|
||||||
|
|
||||||
def bpf_map_delete_elem_emitter(
|
|
||||||
call,
|
|
||||||
map_ptr,
|
|
||||||
module,
|
|
||||||
builder,
|
|
||||||
func,
|
|
||||||
local_sym_tab=None,
|
|
||||||
struct_sym_tab=None,
|
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
Emit LLVM IR for bpf_map_delete_elem helper function call.
|
|
||||||
Expected call signature: map.delete(key)
|
|
||||||
"""
|
|
||||||
# Check for correct number of arguments
|
|
||||||
if not call.args or len(call.args) != 1:
|
|
||||||
raise ValueError(
|
|
||||||
"Map delete expects exactly 1 argument (key), got " f"{len(call.args)}"
|
|
||||||
)
|
|
||||||
|
|
||||||
key_arg = call.args[0]
|
|
||||||
|
|
||||||
# Handle key argument
|
|
||||||
if isinstance(key_arg, ast.Name):
|
|
||||||
key_name = key_arg.id
|
|
||||||
if local_sym_tab and key_name in local_sym_tab:
|
|
||||||
key_ptr = local_sym_tab[key_name][0]
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
f"Key variable {key_name} not found in local symbol table."
|
|
||||||
)
|
|
||||||
elif isinstance(key_arg, ast.Constant) and isinstance(key_arg.value, int):
|
|
||||||
# Handle constant integer keys
|
|
||||||
key_val = key_arg.value
|
|
||||||
key_type = ir.IntType(64)
|
|
||||||
key_ptr = builder.alloca(key_type)
|
|
||||||
key_ptr.align = key_type.width // 8
|
|
||||||
builder.store(ir.Constant(key_type, key_val), key_ptr)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(
|
|
||||||
"Only simple variable names and integer constants are supported as keys in map delete."
|
|
||||||
)
|
|
||||||
|
|
||||||
if key_ptr is None:
|
|
||||||
raise ValueError("Key pointer is None.")
|
|
||||||
|
|
||||||
# Cast map pointer to void*
|
|
||||||
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
|
||||||
|
|
||||||
# Define function type for bpf_map_delete_elem
|
|
||||||
fn_type = ir.FunctionType(
|
|
||||||
ir.IntType(64), # Return type: int64 (status code)
|
|
||||||
[ir.PointerType(), ir.PointerType()], # Args: (void*, void*)
|
|
||||||
var_arg=False,
|
|
||||||
)
|
|
||||||
fn_ptr_type = ir.PointerType(fn_type)
|
|
||||||
|
|
||||||
# Helper ID 3 is bpf_map_delete_elem
|
|
||||||
fn_addr = ir.Constant(ir.IntType(64), 3)
|
|
||||||
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
|
||||||
|
|
||||||
# Call the helper function
|
|
||||||
result = builder.call(fn_ptr, [map_void_ptr, key_ptr], tail=False)
|
|
||||||
|
|
||||||
return result, None
|
|
||||||
|
|
||||||
|
|
||||||
def bpf_get_current_pid_tgid_emitter(
|
|
||||||
call,
|
|
||||||
map_ptr,
|
|
||||||
module,
|
|
||||||
builder,
|
|
||||||
func,
|
|
||||||
local_sym_tab=None,
|
|
||||||
struct_sym_tab=None,
|
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
Emit LLVM IR for bpf_get_current_pid_tgid helper function call.
|
|
||||||
"""
|
|
||||||
# func is an arg to just have a uniform signature with other emitters
|
|
||||||
helper_id = ir.Constant(ir.IntType(64), 14)
|
|
||||||
fn_type = ir.FunctionType(ir.IntType(64), [], var_arg=False)
|
|
||||||
fn_ptr_type = ir.PointerType(fn_type)
|
|
||||||
fn_ptr = builder.inttoptr(helper_id, fn_ptr_type)
|
|
||||||
result = builder.call(fn_ptr, [], tail=False)
|
|
||||||
|
|
||||||
# Extract the lower 32 bits (PID) using bitwise AND with 0xFFFFFFFF
|
|
||||||
mask = ir.Constant(ir.IntType(64), 0xFFFFFFFF)
|
|
||||||
pid = builder.and_(result, mask)
|
|
||||||
return pid, ir.IntType(64)
|
|
||||||
|
|
||||||
|
|
||||||
def bpf_perf_event_output_handler(
|
|
||||||
call,
|
|
||||||
map_ptr,
|
|
||||||
module,
|
|
||||||
builder,
|
|
||||||
func,
|
|
||||||
local_sym_tab=None,
|
|
||||||
struct_sym_tab=None,
|
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
|
||||||
if len(call.args) != 1:
|
|
||||||
raise ValueError(
|
|
||||||
"Perf event output expects exactly one argument (data), got "
|
|
||||||
f"{len(call.args)}"
|
|
||||||
)
|
|
||||||
data_arg = call.args[0]
|
|
||||||
ctx_ptr = func.args[0] # First argument to the function is ctx
|
|
||||||
|
|
||||||
if isinstance(data_arg, ast.Name):
|
|
||||||
data_name = data_arg.id
|
|
||||||
if local_sym_tab and data_name in local_sym_tab:
|
|
||||||
data_ptr = local_sym_tab[data_name][0]
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
f"Data variable {data_name} not found in local symbol table."
|
|
||||||
)
|
|
||||||
# Check is data_name is a struct
|
|
||||||
if local_var_metadata and data_name in local_var_metadata:
|
|
||||||
data_type = local_var_metadata[data_name]
|
|
||||||
if data_type in struct_sym_tab:
|
|
||||||
struct_info = struct_sym_tab[data_type]
|
|
||||||
size_val = ir.Constant(ir.IntType(64), struct_info.size)
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
f"Struct type {data_type} for variable {data_name} not found in struct symbol table."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
f"Metadata for variable {data_name} not found in local variable metadata."
|
|
||||||
)
|
|
||||||
|
|
||||||
# BPF_F_CURRENT_CPU is -1 in 32 bit
|
|
||||||
flags_val = ir.Constant(ir.IntType(64), 0xFFFFFFFF)
|
|
||||||
|
|
||||||
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
|
||||||
data_void_ptr = builder.bitcast(data_ptr, ir.PointerType())
|
|
||||||
fn_type = ir.FunctionType(
|
|
||||||
ir.IntType(64),
|
|
||||||
[
|
|
||||||
ir.PointerType(ir.IntType(8)),
|
|
||||||
ir.PointerType(),
|
|
||||||
ir.IntType(64),
|
|
||||||
ir.PointerType(),
|
|
||||||
ir.IntType(64),
|
|
||||||
],
|
|
||||||
var_arg=False,
|
|
||||||
)
|
|
||||||
fn_ptr_type = ir.PointerType(fn_type)
|
|
||||||
|
|
||||||
# helper id
|
|
||||||
fn_addr = ir.Constant(ir.IntType(64), 25)
|
|
||||||
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
|
||||||
|
|
||||||
result = builder.call(
|
|
||||||
fn_ptr,
|
|
||||||
[ctx_ptr, map_void_ptr, flags_val, data_void_ptr, size_val],
|
|
||||||
tail=False,
|
|
||||||
)
|
|
||||||
return result, None
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(
|
|
||||||
"Only simple object names are supported as data in perf event output."
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
helper_func_list = {
|
|
||||||
"lookup": bpf_map_lookup_elem_emitter,
|
|
||||||
"print": bpf_printk_emitter,
|
|
||||||
"ktime": bpf_ktime_get_ns_emitter,
|
|
||||||
"update": bpf_map_update_elem_emitter,
|
|
||||||
"delete": bpf_map_delete_elem_emitter,
|
|
||||||
"pid": bpf_get_current_pid_tgid_emitter,
|
|
||||||
"output": bpf_perf_event_output_handler,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def handle_helper_call(
|
|
||||||
call,
|
|
||||||
module,
|
|
||||||
builder,
|
|
||||||
func,
|
|
||||||
local_sym_tab=None,
|
|
||||||
map_sym_tab=None,
|
|
||||||
struct_sym_tab=None,
|
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
|
||||||
print(local_var_metadata)
|
|
||||||
if isinstance(call.func, ast.Name):
|
|
||||||
func_name = call.func.id
|
|
||||||
if func_name in helper_func_list:
|
|
||||||
# it is not a map method call
|
|
||||||
return helper_func_list[func_name](
|
|
||||||
call,
|
|
||||||
None,
|
|
||||||
module,
|
|
||||||
builder,
|
|
||||||
func,
|
|
||||||
local_sym_tab,
|
|
||||||
struct_sym_tab,
|
|
||||||
local_var_metadata,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(
|
|
||||||
f"Function {func_name} is not implemented as a helper function."
|
|
||||||
)
|
|
||||||
elif isinstance(call.func, ast.Attribute):
|
|
||||||
# likely a map method call
|
|
||||||
if isinstance(call.func.value, ast.Call) and isinstance(
|
|
||||||
call.func.value.func, ast.Name
|
|
||||||
):
|
|
||||||
map_name = call.func.value.func.id
|
|
||||||
method_name = call.func.attr
|
|
||||||
if map_sym_tab and map_name in map_sym_tab:
|
|
||||||
map_ptr = map_sym_tab[map_name]
|
|
||||||
if method_name in helper_func_list:
|
|
||||||
print(local_var_metadata)
|
|
||||||
return helper_func_list[method_name](
|
|
||||||
call,
|
|
||||||
map_ptr,
|
|
||||||
module,
|
|
||||||
builder,
|
|
||||||
func,
|
|
||||||
local_sym_tab,
|
|
||||||
struct_sym_tab,
|
|
||||||
local_var_metadata,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(
|
|
||||||
f"Map method {method_name} is not implemented as a helper function."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise ValueError(f"Map variable {map_name} not found in symbol tables.")
|
|
||||||
elif isinstance(call.func.value, ast.Name):
|
|
||||||
obj_name = call.func.value.id
|
|
||||||
method_name = call.func.attr
|
|
||||||
if map_sym_tab and obj_name in map_sym_tab:
|
|
||||||
map_ptr = map_sym_tab[obj_name]
|
|
||||||
if method_name in helper_func_list:
|
|
||||||
return helper_func_list[method_name](
|
|
||||||
call,
|
|
||||||
map_ptr,
|
|
||||||
module,
|
|
||||||
builder,
|
|
||||||
func,
|
|
||||||
local_sym_tab,
|
|
||||||
struct_sym_tab,
|
|
||||||
local_var_metadata,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(
|
|
||||||
f"Map method {method_name} is not implemented as a helper function."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise ValueError(f"Map variable {obj_name} not found in symbol tables.")
|
|
||||||
else:
|
|
||||||
raise NotImplementedError("Attribute not supported for map method calls.")
|
|
||||||
return None
|
|
||||||
@ -12,8 +12,12 @@ import inspect
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from pylibbpf import BpfProgram
|
from pylibbpf import BpfProgram
|
||||||
import tempfile
|
import tempfile
|
||||||
|
from logging import Logger
|
||||||
|
import logging
|
||||||
|
|
||||||
VERSION = "v0.1.3"
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
VERSION = "v0.1.4"
|
||||||
|
|
||||||
|
|
||||||
def find_bpf_chunks(tree):
|
def find_bpf_chunks(tree):
|
||||||
@ -30,11 +34,11 @@ def find_bpf_chunks(tree):
|
|||||||
|
|
||||||
def processor(source_code, filename, module):
|
def processor(source_code, filename, module):
|
||||||
tree = ast.parse(source_code, filename)
|
tree = ast.parse(source_code, filename)
|
||||||
print(ast.dump(tree, indent=4))
|
logger.debug(ast.dump(tree, indent=4))
|
||||||
|
|
||||||
bpf_chunks = find_bpf_chunks(tree)
|
bpf_chunks = find_bpf_chunks(tree)
|
||||||
for func_node in bpf_chunks:
|
for func_node in bpf_chunks:
|
||||||
print(f"Found BPF function/struct: {func_node.name}")
|
logger.info(f"Found BPF function/struct: {func_node.name}")
|
||||||
|
|
||||||
structs_sym_tab = structs_proc(tree, module, bpf_chunks)
|
structs_sym_tab = structs_proc(tree, module, bpf_chunks)
|
||||||
map_sym_tab = maps_proc(tree, module, bpf_chunks)
|
map_sym_tab = maps_proc(tree, module, bpf_chunks)
|
||||||
@ -44,7 +48,10 @@ def processor(source_code, filename, module):
|
|||||||
globals_processing(tree, module)
|
globals_processing(tree, module)
|
||||||
|
|
||||||
|
|
||||||
def compile_to_ir(filename: str, output: str):
|
def compile_to_ir(filename: str, output: str, loglevel=logging.WARNING):
|
||||||
|
logging.basicConfig(
|
||||||
|
level=loglevel, format="%(asctime)s [%(levelname)s] %(name)s: %(message)s"
|
||||||
|
)
|
||||||
with open(filename) as f:
|
with open(filename) as f:
|
||||||
source = f.read()
|
source = f.read()
|
||||||
|
|
||||||
@ -121,7 +128,7 @@ def compile_to_ir(filename: str, output: str):
|
|||||||
|
|
||||||
module.add_named_metadata("llvm.ident", [f"PythonBPF {VERSION}"])
|
module.add_named_metadata("llvm.ident", [f"PythonBPF {VERSION}"])
|
||||||
|
|
||||||
print(f"IR written to {output}")
|
logger.info(f"IR written to {output}")
|
||||||
with open(output, "w") as f:
|
with open(output, "w") as f:
|
||||||
f.write(f'source_filename = "{filename}"\n')
|
f.write(f'source_filename = "{filename}"\n')
|
||||||
f.write(str(module))
|
f.write(str(module))
|
||||||
@ -130,7 +137,7 @@ def compile_to_ir(filename: str, output: str):
|
|||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
def compile() -> bool:
|
def compile(loglevel=logging.WARNING) -> bool:
|
||||||
# Look one level up the stack to the caller of this function
|
# Look one level up the stack to the caller of this function
|
||||||
caller_frame = inspect.stack()[1]
|
caller_frame = inspect.stack()[1]
|
||||||
caller_file = Path(caller_frame.filename).resolve()
|
caller_file = Path(caller_frame.filename).resolve()
|
||||||
@ -139,9 +146,11 @@ def compile() -> bool:
|
|||||||
o_file = caller_file.with_suffix(".o")
|
o_file = caller_file.with_suffix(".o")
|
||||||
|
|
||||||
success = True
|
success = True
|
||||||
success = compile_to_ir(str(caller_file), str(ll_file)) and success
|
|
||||||
|
|
||||||
success = (
|
success = (
|
||||||
|
compile_to_ir(str(caller_file), str(ll_file), loglevel=loglevel) and success
|
||||||
|
)
|
||||||
|
|
||||||
|
success = bool(
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
[
|
[
|
||||||
"llc",
|
"llc",
|
||||||
@ -157,11 +166,11 @@ def compile() -> bool:
|
|||||||
and success
|
and success
|
||||||
)
|
)
|
||||||
|
|
||||||
print(f"Object written to {o_file}")
|
logger.info(f"Object written to {o_file}")
|
||||||
return success
|
return success
|
||||||
|
|
||||||
|
|
||||||
def BPF() -> BpfProgram:
|
def BPF(loglevel=logging.WARNING) -> BpfProgram:
|
||||||
caller_frame = inspect.stack()[1]
|
caller_frame = inspect.stack()[1]
|
||||||
src = inspect.getsource(caller_frame.frame)
|
src = inspect.getsource(caller_frame.frame)
|
||||||
with tempfile.NamedTemporaryFile(
|
with tempfile.NamedTemporaryFile(
|
||||||
@ -174,7 +183,7 @@ def BPF() -> BpfProgram:
|
|||||||
f.write(src)
|
f.write(src)
|
||||||
f.flush()
|
f.flush()
|
||||||
source = f.name
|
source = f.name
|
||||||
compile_to_ir(source, str(inter.name))
|
compile_to_ir(source, str(inter.name), loglevel=loglevel)
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
[
|
[
|
||||||
"llc",
|
"llc",
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
from .dwarf_constants import *
|
from .dwarf_constants import * # noqa: F403
|
||||||
from .dtypes import *
|
from .dtypes import * # noqa: F403
|
||||||
from .debug_info_generator import DebugInfoGenerator
|
from .debug_info_generator import DebugInfoGenerator
|
||||||
|
|
||||||
|
__all__ = ["DebugInfoGenerator"]
|
||||||
|
|||||||
@ -21,6 +21,10 @@ class DebugInfoGenerator:
|
|||||||
)
|
)
|
||||||
return self._type_cache[key]
|
return self._type_cache[key]
|
||||||
|
|
||||||
|
def get_int32_type(self) -> Any:
|
||||||
|
"""Get debug info for signed 32-bit integer"""
|
||||||
|
return self.get_basic_type("int", 32, dc.DW_ATE_signed)
|
||||||
|
|
||||||
def get_uint32_type(self) -> Any:
|
def get_uint32_type(self) -> Any:
|
||||||
"""Get debug info for unsigned 32-bit integer"""
|
"""Get debug info for unsigned 32-bit integer"""
|
||||||
return self.get_basic_type("unsigned int", 32, dc.DW_ATE_unsigned)
|
return self.get_basic_type("unsigned int", 32, dc.DW_ATE_unsigned)
|
||||||
|
|||||||
@ -1,5 +1,9 @@
|
|||||||
import ast
|
import ast
|
||||||
from llvmlite import ir
|
from llvmlite import ir
|
||||||
|
from logging import Logger
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def eval_expr(
|
def eval_expr(
|
||||||
@ -10,17 +14,15 @@ def eval_expr(
|
|||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
map_sym_tab,
|
map_sym_tab,
|
||||||
structs_sym_tab=None,
|
structs_sym_tab=None,
|
||||||
local_var_metadata=None,
|
|
||||||
):
|
):
|
||||||
print(f"Evaluating expression: {ast.dump(expr)}")
|
logger.info(f"Evaluating expression: {ast.dump(expr)}")
|
||||||
print(local_var_metadata)
|
|
||||||
if isinstance(expr, ast.Name):
|
if isinstance(expr, ast.Name):
|
||||||
if expr.id in local_sym_tab:
|
if expr.id in local_sym_tab:
|
||||||
var = local_sym_tab[expr.id][0]
|
var = local_sym_tab[expr.id].var
|
||||||
val = builder.load(var)
|
val = builder.load(var)
|
||||||
return val, local_sym_tab[expr.id][1] # return value and type
|
return val, local_sym_tab[expr.id].ir_type # return value and type
|
||||||
else:
|
else:
|
||||||
print(f"Undefined variable {expr.id}")
|
logger.info(f"Undefined variable {expr.id}")
|
||||||
return None
|
return None
|
||||||
elif isinstance(expr, ast.Constant):
|
elif isinstance(expr, ast.Constant):
|
||||||
if isinstance(expr.value, int):
|
if isinstance(expr.value, int):
|
||||||
@ -28,18 +30,18 @@ def eval_expr(
|
|||||||
elif isinstance(expr.value, bool):
|
elif isinstance(expr.value, bool):
|
||||||
return ir.Constant(ir.IntType(1), int(expr.value)), ir.IntType(1)
|
return ir.Constant(ir.IntType(1), int(expr.value)), ir.IntType(1)
|
||||||
else:
|
else:
|
||||||
print("Unsupported constant type")
|
logger.info("Unsupported constant type")
|
||||||
return None
|
return None
|
||||||
elif isinstance(expr, ast.Call):
|
elif isinstance(expr, ast.Call):
|
||||||
# delayed import to avoid circular dependency
|
# delayed import to avoid circular dependency
|
||||||
from .bpf_helper_handler import helper_func_list, handle_helper_call
|
from pythonbpf.helper import HelperHandlerRegistry, handle_helper_call
|
||||||
|
|
||||||
if isinstance(expr.func, ast.Name):
|
if isinstance(expr.func, ast.Name):
|
||||||
# check deref
|
# check deref
|
||||||
if expr.func.id == "deref":
|
if expr.func.id == "deref":
|
||||||
print(f"Handling deref {ast.dump(expr)}")
|
logger.info(f"Handling deref {ast.dump(expr)}")
|
||||||
if len(expr.args) != 1:
|
if len(expr.args) != 1:
|
||||||
print("deref takes exactly one argument")
|
logger.info("deref takes exactly one argument")
|
||||||
return None
|
return None
|
||||||
arg = expr.args[0]
|
arg = expr.args[0]
|
||||||
if (
|
if (
|
||||||
@ -47,23 +49,23 @@ def eval_expr(
|
|||||||
and isinstance(arg.func, ast.Name)
|
and isinstance(arg.func, ast.Name)
|
||||||
and arg.func.id == "deref"
|
and arg.func.id == "deref"
|
||||||
):
|
):
|
||||||
print("Multiple deref not supported")
|
logger.info("Multiple deref not supported")
|
||||||
return None
|
return None
|
||||||
if isinstance(arg, ast.Name):
|
if isinstance(arg, ast.Name):
|
||||||
if arg.id in local_sym_tab:
|
if arg.id in local_sym_tab:
|
||||||
arg = local_sym_tab[arg.id][0]
|
arg = local_sym_tab[arg.id].var
|
||||||
else:
|
else:
|
||||||
print(f"Undefined variable {arg.id}")
|
logger.info(f"Undefined variable {arg.id}")
|
||||||
return None
|
return None
|
||||||
if arg is None:
|
if arg is None:
|
||||||
print("Failed to evaluate deref argument")
|
logger.info("Failed to evaluate deref argument")
|
||||||
return None
|
return None
|
||||||
# Since we are handling only name case, directly take type from sym tab
|
# Since we are handling only name case, directly take type from sym tab
|
||||||
val = builder.load(arg)
|
val = builder.load(arg)
|
||||||
return val, local_sym_tab[expr.args[0].id][1]
|
return val, local_sym_tab[expr.args[0].id].ir_type
|
||||||
|
|
||||||
# check for helpers
|
# check for helpers
|
||||||
if expr.func.id in helper_func_list:
|
if HelperHandlerRegistry.has_handler(expr.func.id):
|
||||||
return handle_helper_call(
|
return handle_helper_call(
|
||||||
expr,
|
expr,
|
||||||
module,
|
module,
|
||||||
@ -72,15 +74,14 @@ def eval_expr(
|
|||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
map_sym_tab,
|
map_sym_tab,
|
||||||
structs_sym_tab,
|
structs_sym_tab,
|
||||||
local_var_metadata,
|
|
||||||
)
|
)
|
||||||
elif isinstance(expr.func, ast.Attribute):
|
elif isinstance(expr.func, ast.Attribute):
|
||||||
print(f"Handling method call: {ast.dump(expr.func)}")
|
logger.info(f"Handling method call: {ast.dump(expr.func)}")
|
||||||
if isinstance(expr.func.value, ast.Call) and isinstance(
|
if isinstance(expr.func.value, ast.Call) and isinstance(
|
||||||
expr.func.value.func, ast.Name
|
expr.func.value.func, ast.Name
|
||||||
):
|
):
|
||||||
method_name = expr.func.attr
|
method_name = expr.func.attr
|
||||||
if method_name in helper_func_list:
|
if HelperHandlerRegistry.has_handler(method_name):
|
||||||
return handle_helper_call(
|
return handle_helper_call(
|
||||||
expr,
|
expr,
|
||||||
module,
|
module,
|
||||||
@ -89,13 +90,12 @@ def eval_expr(
|
|||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
map_sym_tab,
|
map_sym_tab,
|
||||||
structs_sym_tab,
|
structs_sym_tab,
|
||||||
local_var_metadata,
|
|
||||||
)
|
)
|
||||||
elif isinstance(expr.func.value, ast.Name):
|
elif isinstance(expr.func.value, ast.Name):
|
||||||
obj_name = expr.func.value.id
|
obj_name = expr.func.value.id
|
||||||
method_name = expr.func.attr
|
method_name = expr.func.attr
|
||||||
if obj_name in map_sym_tab:
|
if obj_name in map_sym_tab:
|
||||||
if method_name in helper_func_list:
|
if HelperHandlerRegistry.has_handler(method_name):
|
||||||
return handle_helper_call(
|
return handle_helper_call(
|
||||||
expr,
|
expr,
|
||||||
module,
|
module,
|
||||||
@ -104,25 +104,22 @@ def eval_expr(
|
|||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
map_sym_tab,
|
map_sym_tab,
|
||||||
structs_sym_tab,
|
structs_sym_tab,
|
||||||
local_var_metadata,
|
|
||||||
)
|
)
|
||||||
elif isinstance(expr, ast.Attribute):
|
elif isinstance(expr, ast.Attribute):
|
||||||
if isinstance(expr.value, ast.Name):
|
if isinstance(expr.value, ast.Name):
|
||||||
var_name = expr.value.id
|
var_name = expr.value.id
|
||||||
attr_name = expr.attr
|
attr_name = expr.attr
|
||||||
if var_name in local_sym_tab:
|
if var_name in local_sym_tab:
|
||||||
var_ptr, var_type = local_sym_tab[var_name]
|
var_ptr, var_type, var_metadata = local_sym_tab[var_name]
|
||||||
print(f"Loading attribute " f"{attr_name} from variable {var_name}")
|
logger.info(f"Loading attribute {attr_name} from variable {var_name}")
|
||||||
print(f"Variable type: {var_type}, Variable ptr: {var_ptr}")
|
logger.info(f"Variable type: {var_type}, Variable ptr: {var_ptr}")
|
||||||
print(local_var_metadata)
|
metadata = structs_sym_tab[var_metadata]
|
||||||
if local_var_metadata and var_name in local_var_metadata:
|
if attr_name in metadata.fields:
|
||||||
metadata = structs_sym_tab[local_var_metadata[var_name]]
|
gep = metadata.gep(builder, var_ptr, attr_name)
|
||||||
if attr_name in metadata.fields:
|
val = builder.load(gep)
|
||||||
gep = metadata.gep(builder, var_ptr, attr_name)
|
field_type = metadata.field_type(attr_name)
|
||||||
val = builder.load(gep)
|
return val, field_type
|
||||||
field_type = metadata.field_type(attr_name)
|
logger.info("Unsupported expression evaluation")
|
||||||
return val, field_type
|
|
||||||
print("Unsupported expression evaluation")
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@ -134,11 +131,9 @@ def handle_expr(
|
|||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
map_sym_tab,
|
map_sym_tab,
|
||||||
structs_sym_tab,
|
structs_sym_tab,
|
||||||
local_var_metadata,
|
|
||||||
):
|
):
|
||||||
"""Handle expression statements in the function body."""
|
"""Handle expression statements in the function body."""
|
||||||
print(f"Handling expression: {ast.dump(expr)}")
|
logger.info(f"Handling expression: {ast.dump(expr)}")
|
||||||
print(local_var_metadata)
|
|
||||||
call = expr.value
|
call = expr.value
|
||||||
if isinstance(call, ast.Call):
|
if isinstance(call, ast.Call):
|
||||||
eval_expr(
|
eval_expr(
|
||||||
@ -149,7 +144,6 @@ def handle_expr(
|
|||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
map_sym_tab,
|
map_sym_tab,
|
||||||
structs_sym_tab,
|
structs_sym_tab,
|
||||||
local_var_metadata,
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
print("Unsupported expression type")
|
logger.info("Unsupported expression type")
|
||||||
|
|||||||
@ -1,13 +1,27 @@
|
|||||||
from llvmlite import ir
|
from llvmlite import ir
|
||||||
import ast
|
import ast
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from .helper import HelperHandlerRegistry, handle_helper_call
|
||||||
from .bpf_helper_handler import helper_func_list, handle_helper_call
|
|
||||||
from .type_deducer import ctypes_to_ir
|
from .type_deducer import ctypes_to_ir
|
||||||
from .binary_ops import handle_binary_op
|
from .binary_ops import handle_binary_op
|
||||||
from .expr_pass import eval_expr, handle_expr
|
from .expr_pass import eval_expr, handle_expr
|
||||||
|
|
||||||
local_var_metadata = {}
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class LocalSymbol:
|
||||||
|
var: ir.AllocaInstr
|
||||||
|
ir_type: ir.Type
|
||||||
|
metadata: Any = None
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
yield self.var
|
||||||
|
yield self.ir_type
|
||||||
|
yield self.metadata
|
||||||
|
|
||||||
|
|
||||||
def get_probe_string(func_node):
|
def get_probe_string(func_node):
|
||||||
@ -32,28 +46,27 @@ def handle_assign(
|
|||||||
):
|
):
|
||||||
"""Handle assignment statements in the function body."""
|
"""Handle assignment statements in the function body."""
|
||||||
if len(stmt.targets) != 1:
|
if len(stmt.targets) != 1:
|
||||||
print("Unsupported multiassignment")
|
logger.info("Unsupported multiassignment")
|
||||||
return
|
return
|
||||||
|
|
||||||
num_types = ("c_int32", "c_int64", "c_uint32", "c_uint64")
|
num_types = ("c_int32", "c_int64", "c_uint32", "c_uint64")
|
||||||
|
|
||||||
target = stmt.targets[0]
|
target = stmt.targets[0]
|
||||||
print(f"Handling assignment to {ast.dump(target)}")
|
logger.info(f"Handling assignment to {ast.dump(target)}")
|
||||||
if not isinstance(target, ast.Name) and not isinstance(target, ast.Attribute):
|
if not isinstance(target, ast.Name) and not isinstance(target, ast.Attribute):
|
||||||
print("Unsupported assignment target")
|
logger.info("Unsupported assignment target")
|
||||||
return
|
return
|
||||||
var_name = target.id if isinstance(target, ast.Name) else target.value.id
|
var_name = target.id if isinstance(target, ast.Name) else target.value.id
|
||||||
rval = stmt.value
|
rval = stmt.value
|
||||||
if isinstance(target, ast.Attribute):
|
if isinstance(target, ast.Attribute):
|
||||||
# struct field assignment
|
# struct field assignment
|
||||||
field_name = target.attr
|
field_name = target.attr
|
||||||
if var_name in local_sym_tab and var_name in local_var_metadata:
|
if var_name in local_sym_tab:
|
||||||
struct_type = local_var_metadata[var_name]
|
struct_type = local_sym_tab[var_name].metadata
|
||||||
struct_info = structs_sym_tab[struct_type]
|
struct_info = structs_sym_tab[struct_type]
|
||||||
|
|
||||||
if field_name in struct_info.fields:
|
if field_name in struct_info.fields:
|
||||||
field_ptr = struct_info.gep(
|
field_ptr = struct_info.gep(
|
||||||
builder, local_sym_tab[var_name][0], field_name
|
builder, local_sym_tab[var_name].var, field_name
|
||||||
)
|
)
|
||||||
val = eval_expr(
|
val = eval_expr(
|
||||||
func,
|
func,
|
||||||
@ -74,28 +87,31 @@ def handle_assign(
|
|||||||
# print(f"Assigned to struct field {var_name}.{field_name}")
|
# print(f"Assigned to struct field {var_name}.{field_name}")
|
||||||
pass
|
pass
|
||||||
if val is None:
|
if val is None:
|
||||||
print("Failed to evaluate struct field assignment")
|
logger.info("Failed to evaluate struct field assignment")
|
||||||
return
|
return
|
||||||
print(field_ptr)
|
logger.info(field_ptr)
|
||||||
builder.store(val[0], field_ptr)
|
builder.store(val[0], field_ptr)
|
||||||
print(f"Assigned to struct field {var_name}.{field_name}")
|
logger.info(f"Assigned to struct field {var_name}.{field_name}")
|
||||||
return
|
return
|
||||||
elif isinstance(rval, ast.Constant):
|
elif isinstance(rval, ast.Constant):
|
||||||
if isinstance(rval.value, bool):
|
if isinstance(rval.value, bool):
|
||||||
if rval.value:
|
if rval.value:
|
||||||
builder.store(ir.Constant(ir.IntType(1), 1), local_sym_tab[var_name][0])
|
builder.store(
|
||||||
|
ir.Constant(ir.IntType(1), 1), local_sym_tab[var_name].var
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
builder.store(ir.Constant(ir.IntType(1), 0), local_sym_tab[var_name][0])
|
builder.store(
|
||||||
print(f"Assigned constant {rval.value} to {var_name}")
|
ir.Constant(ir.IntType(1), 0), local_sym_tab[var_name].var
|
||||||
|
)
|
||||||
|
logger.info(f"Assigned constant {rval.value} to {var_name}")
|
||||||
elif isinstance(rval.value, int):
|
elif isinstance(rval.value, int):
|
||||||
# Assume c_int64 for now
|
# Assume c_int64 for now
|
||||||
# var = builder.alloca(ir.IntType(64), name=var_name)
|
# var = builder.alloca(ir.IntType(64), name=var_name)
|
||||||
# var.align = 8
|
# var.align = 8
|
||||||
builder.store(
|
builder.store(
|
||||||
ir.Constant(ir.IntType(64), rval.value), local_sym_tab[var_name][0]
|
ir.Constant(ir.IntType(64), rval.value), local_sym_tab[var_name].var
|
||||||
)
|
)
|
||||||
# local_sym_tab[var_name] = var
|
logger.info(f"Assigned constant {rval.value} to {var_name}")
|
||||||
print(f"Assigned constant {rval.value} to {var_name}")
|
|
||||||
elif isinstance(rval.value, str):
|
elif isinstance(rval.value, str):
|
||||||
str_val = rval.value.encode("utf-8") + b"\x00"
|
str_val = rval.value.encode("utf-8") + b"\x00"
|
||||||
str_const = ir.Constant(
|
str_const = ir.Constant(
|
||||||
@ -108,14 +124,14 @@ def handle_assign(
|
|||||||
global_str.global_constant = True
|
global_str.global_constant = True
|
||||||
global_str.initializer = str_const
|
global_str.initializer = str_const
|
||||||
str_ptr = builder.bitcast(global_str, ir.PointerType(ir.IntType(8)))
|
str_ptr = builder.bitcast(global_str, ir.PointerType(ir.IntType(8)))
|
||||||
builder.store(str_ptr, local_sym_tab[var_name][0])
|
builder.store(str_ptr, local_sym_tab[var_name].var)
|
||||||
print(f"Assigned string constant '{rval.value}' to {var_name}")
|
logger.info(f"Assigned string constant '{rval.value}' to {var_name}")
|
||||||
else:
|
else:
|
||||||
print("Unsupported constant type")
|
logger.info("Unsupported constant type")
|
||||||
elif isinstance(rval, ast.Call):
|
elif isinstance(rval, ast.Call):
|
||||||
if isinstance(rval.func, ast.Name):
|
if isinstance(rval.func, ast.Name):
|
||||||
call_type = rval.func.id
|
call_type = rval.func.id
|
||||||
print(f"Assignment call type: {call_type}")
|
logger.info(f"Assignment call type: {call_type}")
|
||||||
if (
|
if (
|
||||||
call_type in num_types
|
call_type in num_types
|
||||||
and len(rval.args) == 1
|
and len(rval.args) == 1
|
||||||
@ -126,14 +142,14 @@ def handle_assign(
|
|||||||
# var = builder.alloca(ir_type, name=var_name)
|
# var = builder.alloca(ir_type, name=var_name)
|
||||||
# var.align = ir_type.width // 8
|
# var.align = ir_type.width // 8
|
||||||
builder.store(
|
builder.store(
|
||||||
ir.Constant(ir_type, rval.args[0].value), local_sym_tab[var_name][0]
|
ir.Constant(ir_type, rval.args[0].value),
|
||||||
|
local_sym_tab[var_name].var,
|
||||||
)
|
)
|
||||||
print(
|
logger.info(
|
||||||
f"Assigned {call_type} constant "
|
f"Assigned {call_type} constant "
|
||||||
f"{rval.args[0].value} to {var_name}"
|
f"{rval.args[0].value} to {var_name}"
|
||||||
)
|
)
|
||||||
# local_sym_tab[var_name] = var
|
elif HelperHandlerRegistry.has_handler(call_type):
|
||||||
elif call_type in helper_func_list:
|
|
||||||
# var = builder.alloca(ir.IntType(64), name=var_name)
|
# var = builder.alloca(ir.IntType(64), name=var_name)
|
||||||
# var.align = 8
|
# var.align = 8
|
||||||
val = handle_helper_call(
|
val = handle_helper_call(
|
||||||
@ -144,13 +160,11 @@ def handle_assign(
|
|||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
map_sym_tab,
|
map_sym_tab,
|
||||||
structs_sym_tab,
|
structs_sym_tab,
|
||||||
local_var_metadata,
|
|
||||||
)
|
)
|
||||||
builder.store(val[0], local_sym_tab[var_name][0])
|
builder.store(val[0], local_sym_tab[var_name].var)
|
||||||
# local_sym_tab[var_name] = var
|
logger.info(f"Assigned constant {rval.func.id} to {var_name}")
|
||||||
print(f"Assigned constant {rval.func.id} to {var_name}")
|
|
||||||
elif call_type == "deref" and len(rval.args) == 1:
|
elif call_type == "deref" and len(rval.args) == 1:
|
||||||
print(f"Handling deref assignment {ast.dump(rval)}")
|
logger.info(f"Handling deref assignment {ast.dump(rval)}")
|
||||||
val = eval_expr(
|
val = eval_expr(
|
||||||
func,
|
func,
|
||||||
module,
|
module,
|
||||||
@ -161,36 +175,32 @@ def handle_assign(
|
|||||||
structs_sym_tab,
|
structs_sym_tab,
|
||||||
)
|
)
|
||||||
if val is None:
|
if val is None:
|
||||||
print("Failed to evaluate deref argument")
|
logger.info("Failed to evaluate deref argument")
|
||||||
return
|
return
|
||||||
print(f"Dereferenced value: {val}, storing in {var_name}")
|
logger.info(f"Dereferenced value: {val}, storing in {var_name}")
|
||||||
builder.store(val[0], local_sym_tab[var_name][0])
|
builder.store(val[0], local_sym_tab[var_name].var)
|
||||||
# local_sym_tab[var_name] = var
|
logger.info(f"Dereferenced and assigned to {var_name}")
|
||||||
print(f"Dereferenced and assigned to {var_name}")
|
|
||||||
elif call_type in structs_sym_tab and len(rval.args) == 0:
|
elif call_type in structs_sym_tab and len(rval.args) == 0:
|
||||||
struct_info = structs_sym_tab[call_type]
|
struct_info = structs_sym_tab[call_type]
|
||||||
ir_type = struct_info.ir_type
|
ir_type = struct_info.ir_type
|
||||||
# var = builder.alloca(ir_type, name=var_name)
|
# var = builder.alloca(ir_type, name=var_name)
|
||||||
# Null init
|
# Null init
|
||||||
builder.store(ir.Constant(ir_type, None), local_sym_tab[var_name][0])
|
builder.store(ir.Constant(ir_type, None), local_sym_tab[var_name].var)
|
||||||
local_var_metadata[var_name] = call_type
|
logger.info(f"Assigned struct {call_type} to {var_name}")
|
||||||
print(f"Assigned struct {call_type} to {var_name}")
|
|
||||||
# local_sym_tab[var_name] = var
|
|
||||||
else:
|
else:
|
||||||
print(f"Unsupported assignment call type: {call_type}")
|
logger.info(f"Unsupported assignment call type: {call_type}")
|
||||||
elif isinstance(rval.func, ast.Attribute):
|
elif isinstance(rval.func, ast.Attribute):
|
||||||
print(f"Assignment call attribute: {ast.dump(rval.func)}")
|
logger.info(f"Assignment call attribute: {ast.dump(rval.func)}")
|
||||||
if isinstance(rval.func.value, ast.Name):
|
if isinstance(rval.func.value, ast.Name):
|
||||||
# TODO: probably a struct access
|
# TODO: probably a struct access
|
||||||
print(f"TODO STRUCT ACCESS {ast.dump(rval)}")
|
logger.info(f"TODO STRUCT ACCESS {ast.dump(rval)}")
|
||||||
elif isinstance(rval.func.value, ast.Call) and isinstance(
|
elif isinstance(rval.func.value, ast.Call) and isinstance(
|
||||||
rval.func.value.func, ast.Name
|
rval.func.value.func, ast.Name
|
||||||
):
|
):
|
||||||
map_name = rval.func.value.func.id
|
map_name = rval.func.value.func.id
|
||||||
method_name = rval.func.attr
|
method_name = rval.func.attr
|
||||||
if map_name in map_sym_tab:
|
if map_name in map_sym_tab:
|
||||||
map_ptr = map_sym_tab[map_name]
|
if HelperHandlerRegistry.has_handler(method_name):
|
||||||
if method_name in helper_func_list:
|
|
||||||
val = handle_helper_call(
|
val = handle_helper_call(
|
||||||
rval,
|
rval,
|
||||||
module,
|
module,
|
||||||
@ -199,22 +209,20 @@ def handle_assign(
|
|||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
map_sym_tab,
|
map_sym_tab,
|
||||||
structs_sym_tab,
|
structs_sym_tab,
|
||||||
local_var_metadata,
|
|
||||||
)
|
)
|
||||||
# var = builder.alloca(ir.IntType(64), name=var_name)
|
# var = builder.alloca(ir.IntType(64), name=var_name)
|
||||||
# var.align = 8
|
# var.align = 8
|
||||||
builder.store(val[0], local_sym_tab[var_name][0])
|
builder.store(val[0], local_sym_tab[var_name].var)
|
||||||
# local_sym_tab[var_name] = var
|
|
||||||
else:
|
else:
|
||||||
print("Unsupported assignment call structure")
|
logger.info("Unsupported assignment call structure")
|
||||||
else:
|
else:
|
||||||
print("Unsupported assignment call function type")
|
logger.info("Unsupported assignment call function type")
|
||||||
elif isinstance(rval, ast.BinOp):
|
elif isinstance(rval, ast.BinOp):
|
||||||
handle_binary_op(
|
handle_binary_op(
|
||||||
rval, module, builder, var_name, local_sym_tab, map_sym_tab, func
|
rval, module, builder, var_name, local_sym_tab, map_sym_tab, func
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
print("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(func, module, builder, cond, local_sym_tab, map_sym_tab):
|
||||||
@ -224,11 +232,11 @@ def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab):
|
|||||||
elif isinstance(cond.value, int):
|
elif isinstance(cond.value, int):
|
||||||
return ir.Constant(ir.IntType(1), int(bool(cond.value)))
|
return ir.Constant(ir.IntType(1), int(bool(cond.value)))
|
||||||
else:
|
else:
|
||||||
print("Unsupported constant type in condition")
|
logger.info("Unsupported constant type in condition")
|
||||||
return None
|
return None
|
||||||
elif isinstance(cond, ast.Name):
|
elif isinstance(cond, ast.Name):
|
||||||
if cond.id in local_sym_tab:
|
if cond.id in local_sym_tab:
|
||||||
var = local_sym_tab[cond.id][0]
|
var = local_sym_tab[cond.id].var
|
||||||
val = builder.load(var)
|
val = builder.load(var)
|
||||||
if val.type != ir.IntType(1):
|
if val.type != ir.IntType(1):
|
||||||
# Convert nonzero values to true, zero to false
|
# Convert nonzero values to true, zero to false
|
||||||
@ -241,12 +249,12 @@ def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab):
|
|||||||
val = builder.icmp_signed("!=", val, zero)
|
val = builder.icmp_signed("!=", val, zero)
|
||||||
return val
|
return val
|
||||||
else:
|
else:
|
||||||
print(f"Undefined variable {cond.id} in condition")
|
logger.info(f"Undefined variable {cond.id} in condition")
|
||||||
return None
|
return None
|
||||||
elif isinstance(cond, ast.Compare):
|
elif isinstance(cond, ast.Compare):
|
||||||
lhs = eval_expr(func, module, builder, cond.left, local_sym_tab, map_sym_tab)[0]
|
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:
|
if len(cond.ops) != 1 or len(cond.comparators) != 1:
|
||||||
print("Unsupported complex comparison")
|
logger.info("Unsupported complex comparison")
|
||||||
return None
|
return None
|
||||||
rhs = eval_expr(
|
rhs = eval_expr(
|
||||||
func, module, builder, cond.comparators[0], local_sym_tab, map_sym_tab
|
func, module, builder, cond.comparators[0], local_sym_tab, map_sym_tab
|
||||||
@ -261,7 +269,7 @@ def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab):
|
|||||||
elif lhs.type.width > rhs.type.width:
|
elif lhs.type.width > rhs.type.width:
|
||||||
rhs = builder.sext(rhs, lhs.type)
|
rhs = builder.sext(rhs, lhs.type)
|
||||||
else:
|
else:
|
||||||
print("Type mismatch in comparison")
|
logger.info("Type mismatch in comparison")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if isinstance(op, ast.Eq):
|
if isinstance(op, ast.Eq):
|
||||||
@ -277,10 +285,10 @@ def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab):
|
|||||||
elif isinstance(op, ast.GtE):
|
elif isinstance(op, ast.GtE):
|
||||||
return builder.icmp_signed(">=", lhs, rhs)
|
return builder.icmp_signed(">=", lhs, rhs)
|
||||||
else:
|
else:
|
||||||
print("Unsupported comparison operator")
|
logger.info("Unsupported comparison operator")
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
print("Unsupported condition expression")
|
logger.info("Unsupported condition expression")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@ -288,8 +296,8 @@ def handle_if(
|
|||||||
func, module, builder, stmt, map_sym_tab, local_sym_tab, structs_sym_tab=None
|
func, module, builder, stmt, map_sym_tab, local_sym_tab, structs_sym_tab=None
|
||||||
):
|
):
|
||||||
"""Handle if statements in the function body."""
|
"""Handle if statements in the function body."""
|
||||||
print("Handling if statement")
|
logger.info("Handling if statement")
|
||||||
start = builder.block.parent
|
# start = builder.block.parent
|
||||||
then_block = func.append_basic_block(name="if.then")
|
then_block = func.append_basic_block(name="if.then")
|
||||||
merge_block = func.append_basic_block(name="if.end")
|
merge_block = func.append_basic_block(name="if.end")
|
||||||
if stmt.orelse:
|
if stmt.orelse:
|
||||||
@ -341,9 +349,8 @@ def process_stmt(
|
|||||||
did_return,
|
did_return,
|
||||||
ret_type=ir.IntType(64),
|
ret_type=ir.IntType(64),
|
||||||
):
|
):
|
||||||
print(f"Processing statement: {ast.dump(stmt)}")
|
logger.info(f"Processing statement: {ast.dump(stmt)}")
|
||||||
if isinstance(stmt, ast.Expr):
|
if isinstance(stmt, ast.Expr):
|
||||||
print(local_var_metadata)
|
|
||||||
handle_expr(
|
handle_expr(
|
||||||
func,
|
func,
|
||||||
module,
|
module,
|
||||||
@ -352,7 +359,6 @@ def process_stmt(
|
|||||||
local_sym_tab,
|
local_sym_tab,
|
||||||
map_sym_tab,
|
map_sym_tab,
|
||||||
structs_sym_tab,
|
structs_sym_tab,
|
||||||
local_var_metadata,
|
|
||||||
)
|
)
|
||||||
elif isinstance(stmt, ast.Assign):
|
elif isinstance(stmt, ast.Assign):
|
||||||
handle_assign(
|
handle_assign(
|
||||||
@ -402,6 +408,7 @@ def allocate_mem(
|
|||||||
module, builder, body, func, ret_type, map_sym_tab, local_sym_tab, structs_sym_tab
|
module, builder, body, func, ret_type, map_sym_tab, local_sym_tab, structs_sym_tab
|
||||||
):
|
):
|
||||||
for stmt in body:
|
for stmt in body:
|
||||||
|
has_metadata = False
|
||||||
if isinstance(stmt, ast.If):
|
if isinstance(stmt, ast.If):
|
||||||
if stmt.body:
|
if stmt.body:
|
||||||
local_sym_tab = allocate_mem(
|
local_sym_tab = allocate_mem(
|
||||||
@ -427,11 +434,11 @@ def allocate_mem(
|
|||||||
)
|
)
|
||||||
elif isinstance(stmt, ast.Assign):
|
elif isinstance(stmt, ast.Assign):
|
||||||
if len(stmt.targets) != 1:
|
if len(stmt.targets) != 1:
|
||||||
print("Unsupported multiassignment")
|
logger.info("Unsupported multiassignment")
|
||||||
continue
|
continue
|
||||||
target = stmt.targets[0]
|
target = stmt.targets[0]
|
||||||
if not isinstance(target, ast.Name):
|
if not isinstance(target, ast.Name):
|
||||||
print("Unsupported assignment target")
|
logger.info("Unsupported assignment target")
|
||||||
continue
|
continue
|
||||||
var_name = target.id
|
var_name = target.id
|
||||||
rval = stmt.value
|
rval = stmt.value
|
||||||
@ -442,65 +449,72 @@ def allocate_mem(
|
|||||||
ir_type = ctypes_to_ir(call_type)
|
ir_type = ctypes_to_ir(call_type)
|
||||||
var = builder.alloca(ir_type, name=var_name)
|
var = builder.alloca(ir_type, name=var_name)
|
||||||
var.align = ir_type.width // 8
|
var.align = ir_type.width // 8
|
||||||
print(f"Pre-allocated variable {var_name} of type {call_type}")
|
logger.info(
|
||||||
elif call_type in helper_func_list:
|
f"Pre-allocated variable {var_name} of type {call_type}"
|
||||||
|
)
|
||||||
|
elif HelperHandlerRegistry.has_handler(call_type):
|
||||||
# Assume return type is int64 for now
|
# Assume return type is int64 for now
|
||||||
ir_type = ir.IntType(64)
|
ir_type = ir.IntType(64)
|
||||||
var = builder.alloca(ir_type, name=var_name)
|
var = builder.alloca(ir_type, name=var_name)
|
||||||
var.align = ir_type.width // 8
|
var.align = ir_type.width // 8
|
||||||
print(f"Pre-allocated variable {var_name} for helper")
|
logger.info(f"Pre-allocated variable {var_name} for helper")
|
||||||
elif call_type == "deref" and len(rval.args) == 1:
|
elif call_type == "deref" and len(rval.args) == 1:
|
||||||
# Assume return type is int64 for now
|
# Assume return type is int64 for now
|
||||||
ir_type = ir.IntType(64)
|
ir_type = ir.IntType(64)
|
||||||
var = builder.alloca(ir_type, name=var_name)
|
var = builder.alloca(ir_type, name=var_name)
|
||||||
var.align = ir_type.width // 8
|
var.align = ir_type.width // 8
|
||||||
print(f"Pre-allocated variable {var_name} for deref")
|
logger.info(f"Pre-allocated variable {var_name} for deref")
|
||||||
elif call_type in structs_sym_tab:
|
elif call_type in structs_sym_tab:
|
||||||
struct_info = structs_sym_tab[call_type]
|
struct_info = structs_sym_tab[call_type]
|
||||||
ir_type = struct_info.ir_type
|
ir_type = struct_info.ir_type
|
||||||
var = builder.alloca(ir_type, name=var_name)
|
var = builder.alloca(ir_type, name=var_name)
|
||||||
local_var_metadata[var_name] = call_type
|
has_metadata = True
|
||||||
print(
|
logger.info(
|
||||||
f"Pre-allocated variable {var_name} for struct {call_type}"
|
f"Pre-allocated variable {var_name} "
|
||||||
|
f"for struct {call_type}"
|
||||||
)
|
)
|
||||||
elif isinstance(rval.func, ast.Attribute):
|
elif isinstance(rval.func, ast.Attribute):
|
||||||
ir_type = ir.PointerType(ir.IntType(64))
|
ir_type = ir.PointerType(ir.IntType(64))
|
||||||
var = builder.alloca(ir_type, name=var_name)
|
var = builder.alloca(ir_type, name=var_name)
|
||||||
# var.align = ir_type.width // 8
|
# var.align = ir_type.width // 8
|
||||||
print(f"Pre-allocated variable {var_name} for map")
|
logger.info(f"Pre-allocated variable {var_name} for map")
|
||||||
else:
|
else:
|
||||||
print("Unsupported assignment call function type")
|
logger.info("Unsupported assignment call function type")
|
||||||
continue
|
continue
|
||||||
elif isinstance(rval, ast.Constant):
|
elif isinstance(rval, ast.Constant):
|
||||||
if isinstance(rval.value, bool):
|
if isinstance(rval.value, bool):
|
||||||
ir_type = ir.IntType(1)
|
ir_type = ir.IntType(1)
|
||||||
var = builder.alloca(ir_type, name=var_name)
|
var = builder.alloca(ir_type, name=var_name)
|
||||||
var.align = 1
|
var.align = 1
|
||||||
print(f"Pre-allocated variable {var_name} of type c_bool")
|
logger.info(f"Pre-allocated variable {var_name} of type c_bool")
|
||||||
elif isinstance(rval.value, int):
|
elif isinstance(rval.value, int):
|
||||||
# Assume c_int64 for now
|
# Assume c_int64 for now
|
||||||
ir_type = ir.IntType(64)
|
ir_type = ir.IntType(64)
|
||||||
var = builder.alloca(ir_type, name=var_name)
|
var = builder.alloca(ir_type, name=var_name)
|
||||||
var.align = ir_type.width // 8
|
var.align = ir_type.width // 8
|
||||||
print(f"Pre-allocated variable {var_name} of type c_int64")
|
logger.info(f"Pre-allocated variable {var_name} of type c_int64")
|
||||||
elif isinstance(rval.value, str):
|
elif isinstance(rval.value, str):
|
||||||
ir_type = ir.PointerType(ir.IntType(8))
|
ir_type = ir.PointerType(ir.IntType(8))
|
||||||
var = builder.alloca(ir_type, name=var_name)
|
var = builder.alloca(ir_type, name=var_name)
|
||||||
var.align = 8
|
var.align = 8
|
||||||
print(f"Pre-allocated variable {var_name} of type string")
|
logger.info(f"Pre-allocated variable {var_name} of type string")
|
||||||
else:
|
else:
|
||||||
print("Unsupported constant type")
|
logger.info("Unsupported constant type")
|
||||||
continue
|
continue
|
||||||
elif isinstance(rval, ast.BinOp):
|
elif isinstance(rval, ast.BinOp):
|
||||||
# Assume c_int64 for now
|
# Assume c_int64 for now
|
||||||
ir_type = ir.IntType(64)
|
ir_type = ir.IntType(64)
|
||||||
var = builder.alloca(ir_type, name=var_name)
|
var = builder.alloca(ir_type, name=var_name)
|
||||||
var.align = ir_type.width // 8
|
var.align = ir_type.width // 8
|
||||||
print(f"Pre-allocated variable {var_name} of type c_int64")
|
logger.info(f"Pre-allocated variable {var_name} of type c_int64")
|
||||||
else:
|
else:
|
||||||
print("Unsupported assignment value type")
|
logger.info("Unsupported assignment value type")
|
||||||
continue
|
continue
|
||||||
local_sym_tab[var_name] = (var, ir_type)
|
|
||||||
|
if has_metadata:
|
||||||
|
local_sym_tab[var_name] = LocalSymbol(var, ir_type, call_type)
|
||||||
|
else:
|
||||||
|
local_sym_tab[var_name] = LocalSymbol(var, ir_type)
|
||||||
return local_sym_tab
|
return local_sym_tab
|
||||||
|
|
||||||
|
|
||||||
@ -525,7 +539,7 @@ def process_func_body(
|
|||||||
structs_sym_tab,
|
structs_sym_tab,
|
||||||
)
|
)
|
||||||
|
|
||||||
print(f"Local symbol table: {local_sym_tab.keys()}")
|
logger.info(f"Local symbol table: {local_sym_tab.keys()}")
|
||||||
|
|
||||||
for stmt in func_node.body:
|
for stmt in func_node.body:
|
||||||
did_return = process_stmt(
|
did_return = process_stmt(
|
||||||
@ -597,7 +611,7 @@ def func_proc(tree, module, chunks, map_sym_tab, structs_sym_tab):
|
|||||||
if is_global:
|
if is_global:
|
||||||
continue
|
continue
|
||||||
func_type = get_probe_string(func_node)
|
func_type = get_probe_string(func_node)
|
||||||
print(f"Found probe_string of {func_node.name}: {func_type}")
|
logger.info(f"Found probe_string of {func_node.name}: {func_type}")
|
||||||
|
|
||||||
process_bpf_chunk(
|
process_bpf_chunk(
|
||||||
func_node,
|
func_node,
|
||||||
@ -656,13 +670,13 @@ def infer_return_type(func_node: ast.FunctionDef):
|
|||||||
except Exception:
|
except Exception:
|
||||||
return type(e).__name__
|
return type(e).__name__
|
||||||
|
|
||||||
for node in ast.walk(func_node):
|
for walked_node in ast.walk(func_node):
|
||||||
if isinstance(node, ast.Return):
|
if isinstance(walked_node, ast.Return):
|
||||||
t = _expr_type(node.value)
|
t = _expr_type(walked_node.value)
|
||||||
if found_type is None:
|
if found_type is None:
|
||||||
found_type = t
|
found_type = t
|
||||||
elif found_type != t:
|
elif found_type != t:
|
||||||
raise ValueError("Conflicting return types:" f"{found_type} vs {t}")
|
raise ValueError(f"Conflicting return types: {found_type} vs {t}")
|
||||||
return found_type or "None"
|
return found_type or "None"
|
||||||
|
|
||||||
|
|
||||||
@ -674,7 +688,7 @@ def assign_string_to_array(builder, target_array_ptr, source_string_ptr, array_l
|
|||||||
Copy a string (i8*) to a fixed-size array ([N x i8]*)
|
Copy a string (i8*) to a fixed-size array ([N x i8]*)
|
||||||
"""
|
"""
|
||||||
# Create a loop to copy characters one by one
|
# Create a loop to copy characters one by one
|
||||||
entry_block = builder.block
|
# entry_block = builder.block
|
||||||
copy_block = builder.append_basic_block("copy_char")
|
copy_block = builder.append_basic_block("copy_char")
|
||||||
end_block = builder.append_basic_block("copy_end")
|
end_block = builder.append_basic_block("copy_end")
|
||||||
|
|
||||||
|
|||||||
13
pythonbpf/helper/__init__.py
Normal file
13
pythonbpf/helper/__init__.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
from .helper_utils import HelperHandlerRegistry
|
||||||
|
from .bpf_helper_handler import handle_helper_call
|
||||||
|
from .helpers import ktime, pid, deref, XDP_DROP, XDP_PASS
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"HelperHandlerRegistry",
|
||||||
|
"handle_helper_call",
|
||||||
|
"ktime",
|
||||||
|
"pid",
|
||||||
|
"deref",
|
||||||
|
"XDP_DROP",
|
||||||
|
"XDP_PASS",
|
||||||
|
]
|
||||||
348
pythonbpf/helper/bpf_helper_handler.py
Normal file
348
pythonbpf/helper/bpf_helper_handler.py
Normal file
@ -0,0 +1,348 @@
|
|||||||
|
import ast
|
||||||
|
from llvmlite import ir
|
||||||
|
from enum import Enum
|
||||||
|
from .helper_utils import (
|
||||||
|
HelperHandlerRegistry,
|
||||||
|
get_or_create_ptr_from_arg,
|
||||||
|
get_flags_val,
|
||||||
|
handle_fstring_print,
|
||||||
|
simple_string_print,
|
||||||
|
get_data_ptr_and_size,
|
||||||
|
)
|
||||||
|
from logging import Logger
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class BPFHelperID(Enum):
|
||||||
|
BPF_MAP_LOOKUP_ELEM = 1
|
||||||
|
BPF_MAP_UPDATE_ELEM = 2
|
||||||
|
BPF_MAP_DELETE_ELEM = 3
|
||||||
|
BPF_KTIME_GET_NS = 5
|
||||||
|
BPF_PRINTK = 6
|
||||||
|
BPF_GET_CURRENT_PID_TGID = 14
|
||||||
|
BPF_PERF_EVENT_OUTPUT = 25
|
||||||
|
|
||||||
|
|
||||||
|
@HelperHandlerRegistry.register("ktime")
|
||||||
|
def bpf_ktime_get_ns_emitter(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Emit LLVM IR for bpf_ktime_get_ns helper function call.
|
||||||
|
"""
|
||||||
|
# func is an arg to just have a uniform signature with other emitters
|
||||||
|
helper_id = ir.Constant(ir.IntType(64), BPFHelperID.BPF_KTIME_GET_NS.value)
|
||||||
|
fn_type = ir.FunctionType(ir.IntType(64), [], var_arg=False)
|
||||||
|
fn_ptr_type = ir.PointerType(fn_type)
|
||||||
|
fn_ptr = builder.inttoptr(helper_id, fn_ptr_type)
|
||||||
|
result = builder.call(fn_ptr, [], tail=False)
|
||||||
|
return result, ir.IntType(64)
|
||||||
|
|
||||||
|
|
||||||
|
@HelperHandlerRegistry.register("lookup")
|
||||||
|
def bpf_map_lookup_elem_emitter(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Emit LLVM IR for bpf_map_lookup_elem helper function call.
|
||||||
|
"""
|
||||||
|
if not call.args or len(call.args) != 1:
|
||||||
|
raise ValueError(
|
||||||
|
"Map lookup expects exactly one argument (key), got " f"{len(call.args)}"
|
||||||
|
)
|
||||||
|
key_ptr = get_or_create_ptr_from_arg(call.args[0], builder, local_sym_tab)
|
||||||
|
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
||||||
|
|
||||||
|
fn_type = ir.FunctionType(
|
||||||
|
ir.PointerType(), # Return type: void*
|
||||||
|
[ir.PointerType(), ir.PointerType()], # Args: (void*, void*)
|
||||||
|
var_arg=False,
|
||||||
|
)
|
||||||
|
fn_ptr_type = ir.PointerType(fn_type)
|
||||||
|
|
||||||
|
fn_addr = ir.Constant(ir.IntType(64), BPFHelperID.BPF_MAP_LOOKUP_ELEM.value)
|
||||||
|
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
||||||
|
|
||||||
|
result = builder.call(fn_ptr, [map_void_ptr, key_ptr], tail=False)
|
||||||
|
|
||||||
|
return result, ir.PointerType()
|
||||||
|
|
||||||
|
|
||||||
|
@HelperHandlerRegistry.register("print")
|
||||||
|
def bpf_printk_emitter(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""Emit LLVM IR for bpf_printk helper function call."""
|
||||||
|
if not hasattr(func, "_fmt_counter"):
|
||||||
|
func._fmt_counter = 0
|
||||||
|
|
||||||
|
if not call.args:
|
||||||
|
raise ValueError("bpf_printk expects at least one argument (format string)")
|
||||||
|
|
||||||
|
args = []
|
||||||
|
if isinstance(call.args[0], ast.JoinedStr):
|
||||||
|
args = handle_fstring_print(
|
||||||
|
call.args[0],
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab,
|
||||||
|
struct_sym_tab,
|
||||||
|
)
|
||||||
|
elif isinstance(call.args[0], ast.Constant) and isinstance(call.args[0].value, str):
|
||||||
|
# TODO: We are only supporting single arguments for now.
|
||||||
|
# In case of multiple args, the first one will be taken.
|
||||||
|
args = simple_string_print(call.args[0].value, module, builder, func)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(
|
||||||
|
"Only simple strings or f-strings are supported in bpf_printk."
|
||||||
|
)
|
||||||
|
|
||||||
|
fn_type = ir.FunctionType(
|
||||||
|
ir.IntType(64), [ir.PointerType(), ir.IntType(32)], var_arg=True
|
||||||
|
)
|
||||||
|
fn_ptr_type = ir.PointerType(fn_type)
|
||||||
|
fn_addr = ir.Constant(ir.IntType(64), BPFHelperID.BPF_PRINTK.value)
|
||||||
|
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
||||||
|
|
||||||
|
builder.call(fn_ptr, args, tail=True)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@HelperHandlerRegistry.register("update")
|
||||||
|
def bpf_map_update_elem_emitter(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Emit LLVM IR for bpf_map_update_elem helper function call.
|
||||||
|
Expected call signature: map.update(key, value, flags=0)
|
||||||
|
"""
|
||||||
|
if not call.args or len(call.args) < 2 or len(call.args) > 3:
|
||||||
|
raise ValueError(
|
||||||
|
"Map update expects 2 or 3 args (key, value, flags), "
|
||||||
|
f"got {len(call.args)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
key_arg = call.args[0]
|
||||||
|
value_arg = call.args[1]
|
||||||
|
flags_arg = call.args[2] if len(call.args) > 2 else None
|
||||||
|
|
||||||
|
key_ptr = get_or_create_ptr_from_arg(key_arg, builder, local_sym_tab)
|
||||||
|
value_ptr = get_or_create_ptr_from_arg(value_arg, builder, local_sym_tab)
|
||||||
|
flags_val = get_flags_val(flags_arg, builder, local_sym_tab)
|
||||||
|
|
||||||
|
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
||||||
|
fn_type = ir.FunctionType(
|
||||||
|
ir.IntType(64),
|
||||||
|
[ir.PointerType(), ir.PointerType(), ir.PointerType(), ir.IntType(64)],
|
||||||
|
var_arg=False,
|
||||||
|
)
|
||||||
|
fn_ptr_type = ir.PointerType(fn_type)
|
||||||
|
|
||||||
|
fn_addr = ir.Constant(ir.IntType(64), BPFHelperID.BPF_MAP_UPDATE_ELEM.value)
|
||||||
|
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
||||||
|
|
||||||
|
if isinstance(flags_val, int):
|
||||||
|
flags_const = ir.Constant(ir.IntType(64), flags_val)
|
||||||
|
else:
|
||||||
|
flags_const = flags_val
|
||||||
|
|
||||||
|
result = builder.call(
|
||||||
|
fn_ptr, [map_void_ptr, key_ptr, value_ptr, flags_const], tail=False
|
||||||
|
)
|
||||||
|
|
||||||
|
return result, None
|
||||||
|
|
||||||
|
|
||||||
|
@HelperHandlerRegistry.register("delete")
|
||||||
|
def bpf_map_delete_elem_emitter(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Emit LLVM IR for bpf_map_delete_elem helper function call.
|
||||||
|
Expected call signature: map.delete(key)
|
||||||
|
"""
|
||||||
|
if not call.args or len(call.args) != 1:
|
||||||
|
raise ValueError(
|
||||||
|
"Map delete expects exactly one argument (key), got " f"{len(call.args)}"
|
||||||
|
)
|
||||||
|
key_ptr = get_or_create_ptr_from_arg(call.args[0], builder, local_sym_tab)
|
||||||
|
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
||||||
|
|
||||||
|
# Define function type for bpf_map_delete_elem
|
||||||
|
fn_type = ir.FunctionType(
|
||||||
|
ir.IntType(64), # Return type: int64 (status code)
|
||||||
|
[ir.PointerType(), ir.PointerType()], # Args: (void*, void*)
|
||||||
|
var_arg=False,
|
||||||
|
)
|
||||||
|
fn_ptr_type = ir.PointerType(fn_type)
|
||||||
|
|
||||||
|
fn_addr = ir.Constant(ir.IntType(64), BPFHelperID.BPF_MAP_DELETE_ELEM.value)
|
||||||
|
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
||||||
|
|
||||||
|
result = builder.call(fn_ptr, [map_void_ptr, key_ptr], tail=False)
|
||||||
|
|
||||||
|
return result, None
|
||||||
|
|
||||||
|
|
||||||
|
@HelperHandlerRegistry.register("pid")
|
||||||
|
def bpf_get_current_pid_tgid_emitter(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Emit LLVM IR for bpf_get_current_pid_tgid helper function call.
|
||||||
|
"""
|
||||||
|
# func is an arg to just have a uniform signature with other emitters
|
||||||
|
helper_id = ir.Constant(ir.IntType(64), BPFHelperID.BPF_GET_CURRENT_PID_TGID.value)
|
||||||
|
fn_type = ir.FunctionType(ir.IntType(64), [], var_arg=False)
|
||||||
|
fn_ptr_type = ir.PointerType(fn_type)
|
||||||
|
fn_ptr = builder.inttoptr(helper_id, fn_ptr_type)
|
||||||
|
result = builder.call(fn_ptr, [], tail=False)
|
||||||
|
|
||||||
|
# Extract the lower 32 bits (PID) using bitwise AND with 0xFFFFFFFF
|
||||||
|
mask = ir.Constant(ir.IntType(64), 0xFFFFFFFF)
|
||||||
|
pid = builder.and_(result, mask)
|
||||||
|
return pid, ir.IntType(64)
|
||||||
|
|
||||||
|
|
||||||
|
@HelperHandlerRegistry.register("output")
|
||||||
|
def bpf_perf_event_output_handler(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
):
|
||||||
|
if len(call.args) != 1:
|
||||||
|
raise ValueError(
|
||||||
|
"Perf event output expects exactly one argument, " f"got {len(call.args)}"
|
||||||
|
)
|
||||||
|
data_arg = call.args[0]
|
||||||
|
ctx_ptr = func.args[0] # First argument to the function is ctx
|
||||||
|
|
||||||
|
data_ptr, size_val = get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab)
|
||||||
|
|
||||||
|
# BPF_F_CURRENT_CPU is -1 in 32 bit
|
||||||
|
flags_val = ir.Constant(ir.IntType(64), 0xFFFFFFFF)
|
||||||
|
|
||||||
|
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
||||||
|
data_void_ptr = builder.bitcast(data_ptr, ir.PointerType())
|
||||||
|
fn_type = ir.FunctionType(
|
||||||
|
ir.IntType(64),
|
||||||
|
[
|
||||||
|
ir.PointerType(ir.IntType(8)),
|
||||||
|
ir.PointerType(),
|
||||||
|
ir.IntType(64),
|
||||||
|
ir.PointerType(),
|
||||||
|
ir.IntType(64),
|
||||||
|
],
|
||||||
|
var_arg=False,
|
||||||
|
)
|
||||||
|
fn_ptr_type = ir.PointerType(fn_type)
|
||||||
|
|
||||||
|
# helper id
|
||||||
|
fn_addr = ir.Constant(ir.IntType(64), BPFHelperID.BPF_PERF_EVENT_OUTPUT.value)
|
||||||
|
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
||||||
|
|
||||||
|
result = builder.call(
|
||||||
|
fn_ptr, [ctx_ptr, map_void_ptr, flags_val, data_void_ptr, size_val], tail=False
|
||||||
|
)
|
||||||
|
return result, None
|
||||||
|
|
||||||
|
|
||||||
|
def handle_helper_call(
|
||||||
|
call,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
map_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""Process a BPF helper function call and emit the appropriate LLVM IR."""
|
||||||
|
|
||||||
|
# Helper function to get map pointer and invoke handler
|
||||||
|
def invoke_helper(method_name, map_ptr=None):
|
||||||
|
handler = HelperHandlerRegistry.get_handler(method_name)
|
||||||
|
if not handler:
|
||||||
|
raise NotImplementedError(
|
||||||
|
f"Helper function '{method_name}' is not implemented."
|
||||||
|
)
|
||||||
|
return handler(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab,
|
||||||
|
struct_sym_tab,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Handle direct function calls (e.g., print(), ktime())
|
||||||
|
if isinstance(call.func, ast.Name):
|
||||||
|
return invoke_helper(call.func.id)
|
||||||
|
|
||||||
|
# Handle method calls (e.g., map.lookup(), map.update())
|
||||||
|
elif isinstance(call.func, ast.Attribute):
|
||||||
|
method_name = call.func.attr
|
||||||
|
value = call.func.value
|
||||||
|
logger.info(f"Handling method call: {ast.dump(call.func)}")
|
||||||
|
# Get map pointer from different styles of map access
|
||||||
|
if isinstance(value, ast.Call) and isinstance(value.func, ast.Name):
|
||||||
|
# Func style: my_map().lookup(key)
|
||||||
|
map_name = value.func.id
|
||||||
|
elif isinstance(value, ast.Name):
|
||||||
|
# Direct style: my_map.lookup(key)
|
||||||
|
map_name = value.id
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(
|
||||||
|
f"Unsupported map access pattern: {ast.dump(value)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Verify map exists and get pointer
|
||||||
|
if not map_sym_tab or map_name not in map_sym_tab:
|
||||||
|
raise ValueError(f"Map '{map_name}' not found in symbol table")
|
||||||
|
|
||||||
|
return invoke_helper(method_name, map_sym_tab[map_name])
|
||||||
|
|
||||||
|
return None
|
||||||
307
pythonbpf/helper/helper_utils.py
Normal file
307
pythonbpf/helper/helper_utils.py
Normal file
@ -0,0 +1,307 @@
|
|||||||
|
import ast
|
||||||
|
import logging
|
||||||
|
from collections.abc import Callable
|
||||||
|
|
||||||
|
from llvmlite import ir
|
||||||
|
from pythonbpf.expr_pass import eval_expr
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class HelperHandlerRegistry:
|
||||||
|
"""Registry for BPF helpers"""
|
||||||
|
|
||||||
|
_handlers: dict[str, Callable] = {}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def register(cls, helper_name):
|
||||||
|
"""Decorator to register a handler function for a helper"""
|
||||||
|
|
||||||
|
def decorator(func):
|
||||||
|
cls._handlers[helper_name] = func
|
||||||
|
return func
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_handler(cls, helper_name):
|
||||||
|
"""Get the handler function for a helper"""
|
||||||
|
return cls._handlers.get(helper_name)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def has_handler(cls, helper_name):
|
||||||
|
"""Check if a handler function is registered for a helper"""
|
||||||
|
return helper_name in cls._handlers
|
||||||
|
|
||||||
|
|
||||||
|
def get_var_ptr_from_name(var_name, local_sym_tab):
|
||||||
|
"""Get a pointer to a variable from the symbol table."""
|
||||||
|
if local_sym_tab and var_name in local_sym_tab:
|
||||||
|
return local_sym_tab[var_name].var
|
||||||
|
raise ValueError(f"Variable '{var_name}' not found in local symbol table")
|
||||||
|
|
||||||
|
|
||||||
|
def create_int_constant_ptr(value, builder, int_width=64):
|
||||||
|
"""Create a pointer to an integer constant."""
|
||||||
|
# Default to 64-bit integer
|
||||||
|
int_type = ir.IntType(int_width)
|
||||||
|
ptr = builder.alloca(int_type)
|
||||||
|
ptr.align = int_type.width // 8
|
||||||
|
builder.store(ir.Constant(int_type, value), ptr)
|
||||||
|
return ptr
|
||||||
|
|
||||||
|
|
||||||
|
def get_or_create_ptr_from_arg(arg, builder, local_sym_tab):
|
||||||
|
"""Extract or create pointer from the call arguments."""
|
||||||
|
|
||||||
|
if isinstance(arg, ast.Name):
|
||||||
|
ptr = get_var_ptr_from_name(arg.id, local_sym_tab)
|
||||||
|
elif isinstance(arg, ast.Constant) and isinstance(arg.value, int):
|
||||||
|
ptr = create_int_constant_ptr(arg.value, builder)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(
|
||||||
|
"Only simple variable names are supported as args in map helpers."
|
||||||
|
)
|
||||||
|
return ptr
|
||||||
|
|
||||||
|
|
||||||
|
def get_flags_val(arg, builder, local_sym_tab):
|
||||||
|
"""Extract or create flags value from the call arguments."""
|
||||||
|
if not arg:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if isinstance(arg, ast.Name):
|
||||||
|
if local_sym_tab and arg.id in local_sym_tab:
|
||||||
|
flags_ptr = local_sym_tab[arg.id].var
|
||||||
|
return builder.load(flags_ptr)
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Variable '{arg.id}' not found in local symbol table")
|
||||||
|
elif isinstance(arg, ast.Constant) and isinstance(arg.value, int):
|
||||||
|
return arg.value
|
||||||
|
|
||||||
|
raise NotImplementedError(
|
||||||
|
"Only var names or int consts are supported as map helpers flags."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def simple_string_print(string_value, module, builder, func):
|
||||||
|
"""Prepare arguments for bpf_printk from a simple string value"""
|
||||||
|
fmt_str = string_value + "\n\0"
|
||||||
|
fmt_ptr = _create_format_string_global(fmt_str, func, module, builder)
|
||||||
|
|
||||||
|
args = [fmt_ptr, ir.Constant(ir.IntType(32), len(fmt_str))]
|
||||||
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
def handle_fstring_print(
|
||||||
|
joined_str,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""Handle f-string formatting for bpf_printk emitter."""
|
||||||
|
fmt_parts = []
|
||||||
|
exprs = []
|
||||||
|
|
||||||
|
for value in joined_str.values:
|
||||||
|
logger.debug(f"Processing f-string value: {ast.dump(value)}")
|
||||||
|
|
||||||
|
if isinstance(value, ast.Constant):
|
||||||
|
_process_constant_in_fstring(value, fmt_parts, exprs)
|
||||||
|
elif isinstance(value, ast.FormattedValue):
|
||||||
|
_process_fval(
|
||||||
|
value,
|
||||||
|
fmt_parts,
|
||||||
|
exprs,
|
||||||
|
local_sym_tab,
|
||||||
|
struct_sym_tab,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(f"Unsupported f-string value type: {type(value)}")
|
||||||
|
|
||||||
|
fmt_str = "".join(fmt_parts)
|
||||||
|
args = simple_string_print(fmt_str, module, builder, func)
|
||||||
|
|
||||||
|
# NOTE: Process expressions (limited to 3 due to BPF constraints)
|
||||||
|
if len(exprs) > 3:
|
||||||
|
logger.warning("bpf_printk supports up to 3 args, extra args will be ignored.")
|
||||||
|
|
||||||
|
for expr in exprs[:3]:
|
||||||
|
arg_value = _prepare_expr_args(
|
||||||
|
expr,
|
||||||
|
func,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
local_sym_tab,
|
||||||
|
struct_sym_tab,
|
||||||
|
)
|
||||||
|
args.append(arg_value)
|
||||||
|
|
||||||
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
def _process_constant_in_fstring(cst, fmt_parts, exprs):
|
||||||
|
"""Process constant values in f-string."""
|
||||||
|
if isinstance(cst.value, str):
|
||||||
|
fmt_parts.append(cst.value)
|
||||||
|
elif isinstance(cst.value, int):
|
||||||
|
fmt_parts.append("%lld")
|
||||||
|
exprs.append(ir.Constant(ir.IntType(64), cst.value))
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(
|
||||||
|
f"Unsupported constant type in f-string: {type(cst.value)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _process_fval(fval, fmt_parts, exprs, local_sym_tab, struct_sym_tab):
|
||||||
|
"""Process formatted values in f-string."""
|
||||||
|
logger.debug(f"Processing formatted value: {ast.dump(fval)}")
|
||||||
|
|
||||||
|
if isinstance(fval.value, ast.Name):
|
||||||
|
_process_name_in_fval(fval.value, fmt_parts, exprs, local_sym_tab)
|
||||||
|
elif isinstance(fval.value, ast.Attribute):
|
||||||
|
_process_attr_in_fval(
|
||||||
|
fval.value,
|
||||||
|
fmt_parts,
|
||||||
|
exprs,
|
||||||
|
local_sym_tab,
|
||||||
|
struct_sym_tab,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(
|
||||||
|
f"Unsupported formatted value in f-string: {type(fval.value)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _process_name_in_fval(name_node, fmt_parts, exprs, local_sym_tab):
|
||||||
|
"""Process name nodes in formatted values."""
|
||||||
|
if local_sym_tab and name_node.id in local_sym_tab:
|
||||||
|
_, var_type, tmp = local_sym_tab[name_node.id]
|
||||||
|
_populate_fval(var_type, name_node, fmt_parts, exprs)
|
||||||
|
|
||||||
|
|
||||||
|
def _process_attr_in_fval(attr_node, fmt_parts, exprs, local_sym_tab, struct_sym_tab):
|
||||||
|
"""Process attribute nodes in formatted values."""
|
||||||
|
if (
|
||||||
|
isinstance(attr_node.value, ast.Name)
|
||||||
|
and local_sym_tab
|
||||||
|
and attr_node.value.id in local_sym_tab
|
||||||
|
):
|
||||||
|
var_name = attr_node.value.id
|
||||||
|
field_name = attr_node.attr
|
||||||
|
|
||||||
|
var_type = local_sym_tab[var_name].metadata
|
||||||
|
if var_type not in struct_sym_tab:
|
||||||
|
raise ValueError(
|
||||||
|
f"Struct '{var_type}' for '{var_name}' not in symbol table"
|
||||||
|
)
|
||||||
|
|
||||||
|
struct_info = struct_sym_tab[var_type]
|
||||||
|
if field_name not in struct_info.fields:
|
||||||
|
raise ValueError(f"Field '{field_name}' not found in struct '{var_type}'")
|
||||||
|
|
||||||
|
field_type = struct_info.field_type(field_name)
|
||||||
|
_populate_fval(field_type, attr_node, fmt_parts, exprs)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(
|
||||||
|
"Only simple attribute on local vars is supported in f-strings."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _populate_fval(ftype, node, fmt_parts, exprs):
|
||||||
|
"""Populate format parts and expressions based on field type."""
|
||||||
|
if isinstance(ftype, ir.IntType):
|
||||||
|
# TODO: We print as signed integers only for now
|
||||||
|
if ftype.width == 64:
|
||||||
|
fmt_parts.append("%lld")
|
||||||
|
exprs.append(node)
|
||||||
|
elif ftype.width == 32:
|
||||||
|
fmt_parts.append("%d")
|
||||||
|
exprs.append(node)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(
|
||||||
|
f"Unsupported integer width in f-string: {ftype.width}"
|
||||||
|
)
|
||||||
|
elif ftype == ir.PointerType(ir.IntType(8)):
|
||||||
|
# NOTE: We assume i8* is a string
|
||||||
|
fmt_parts.append("%s")
|
||||||
|
exprs.append(node)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(f"Unsupported field type in f-string: {ftype}")
|
||||||
|
|
||||||
|
|
||||||
|
def _create_format_string_global(fmt_str, func, module, builder):
|
||||||
|
"""Create a global variable for the format string."""
|
||||||
|
fmt_name = f"{func.name}____fmt{func._fmt_counter}"
|
||||||
|
func._fmt_counter += 1
|
||||||
|
|
||||||
|
fmt_gvar = ir.GlobalVariable(
|
||||||
|
module, ir.ArrayType(ir.IntType(8), len(fmt_str)), name=fmt_name
|
||||||
|
)
|
||||||
|
fmt_gvar.global_constant = True
|
||||||
|
fmt_gvar.initializer = ir.Constant(
|
||||||
|
ir.ArrayType(ir.IntType(8), len(fmt_str)), bytearray(fmt_str.encode("utf8"))
|
||||||
|
)
|
||||||
|
fmt_gvar.linkage = "internal"
|
||||||
|
fmt_gvar.align = 1
|
||||||
|
|
||||||
|
return builder.bitcast(fmt_gvar, ir.PointerType())
|
||||||
|
|
||||||
|
|
||||||
|
def _prepare_expr_args(expr, func, module, builder, local_sym_tab, struct_sym_tab):
|
||||||
|
"""Evaluate and prepare an expression to use as an arg for bpf_printk."""
|
||||||
|
val, _ = eval_expr(
|
||||||
|
func,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
expr,
|
||||||
|
local_sym_tab,
|
||||||
|
None,
|
||||||
|
struct_sym_tab,
|
||||||
|
)
|
||||||
|
|
||||||
|
if val:
|
||||||
|
if isinstance(val.type, ir.PointerType):
|
||||||
|
val = builder.ptrtoint(val, ir.IntType(64))
|
||||||
|
elif isinstance(val.type, ir.IntType):
|
||||||
|
if val.type.width < 64:
|
||||||
|
val = builder.sext(val, ir.IntType(64))
|
||||||
|
else:
|
||||||
|
logger.warning(
|
||||||
|
"Only int and ptr supported in bpf_printk args. " "Others default to 0."
|
||||||
|
)
|
||||||
|
val = ir.Constant(ir.IntType(64), 0)
|
||||||
|
return val
|
||||||
|
else:
|
||||||
|
logger.warning(
|
||||||
|
"Failed to evaluate expression for bpf_printk argument. "
|
||||||
|
"It will be converted to 0."
|
||||||
|
)
|
||||||
|
return ir.Constant(ir.IntType(64), 0)
|
||||||
|
|
||||||
|
|
||||||
|
def get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab):
|
||||||
|
"""Extract data pointer and size information for perf event output."""
|
||||||
|
if isinstance(data_arg, ast.Name):
|
||||||
|
data_name = data_arg.id
|
||||||
|
if local_sym_tab and data_name in local_sym_tab:
|
||||||
|
data_ptr = local_sym_tab[data_name].var
|
||||||
|
else:
|
||||||
|
raise ValueError(
|
||||||
|
f"Data variable {data_name} not found in local symbol table."
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check if data_name is a struct
|
||||||
|
data_type = local_sym_tab[data_name].metadata
|
||||||
|
if data_type in struct_sym_tab:
|
||||||
|
struct_info = struct_sym_tab[data_type]
|
||||||
|
size_val = ir.Constant(ir.IntType(64), struct_info.size)
|
||||||
|
return data_ptr, size_val
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Struct {data_type} for {data_name} not in symbol table.")
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(
|
||||||
|
"Only simple object names are supported as data in perf event output."
|
||||||
|
)
|
||||||
@ -1,5 +1,9 @@
|
|||||||
from llvmlite import ir
|
from llvmlite import ir
|
||||||
import ast
|
import ast
|
||||||
|
from logging import Logger
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def emit_license(module: ir.Module, license_str: str):
|
def emit_license(module: ir.Module, license_str: str):
|
||||||
@ -41,9 +45,9 @@ def license_processing(tree, module):
|
|||||||
emit_license(module, node.body[0].value.value)
|
emit_license(module, node.body[0].value.value)
|
||||||
return "LICENSE"
|
return "LICENSE"
|
||||||
else:
|
else:
|
||||||
print("ERROR: LICENSE() must return a string literal")
|
logger.info("ERROR: LICENSE() must return a string literal")
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
print("ERROR: LICENSE already defined")
|
logger.info("ERROR: LICENSE already defined")
|
||||||
return None
|
return None
|
||||||
return None
|
return None
|
||||||
|
|||||||
@ -1,2 +1,4 @@
|
|||||||
from .maps import HashMap, PerfEventArray
|
from .maps import HashMap, PerfEventArray, RingBuf
|
||||||
from .maps_pass import maps_proc
|
from .maps_pass import maps_proc
|
||||||
|
|
||||||
|
__all__ = ["HashMap", "PerfEventArray", "maps_proc", "RingBuf"]
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
# This file provides type and function hints only and does not actually give any functionality.
|
||||||
class HashMap:
|
class HashMap:
|
||||||
def __init__(self, key, value, max_entries):
|
def __init__(self, key, value, max_entries):
|
||||||
self.key = key
|
self.key = key
|
||||||
@ -33,3 +34,18 @@ class PerfEventArray:
|
|||||||
|
|
||||||
def output(self, data):
|
def output(self, data):
|
||||||
pass # Placeholder for output method
|
pass # Placeholder for output method
|
||||||
|
|
||||||
|
|
||||||
|
class RingBuf:
|
||||||
|
def __init__(self, max_entries):
|
||||||
|
self.max_entries = max_entries
|
||||||
|
|
||||||
|
def reserve(self, size: int, flags=0):
|
||||||
|
if size > self.max_entries:
|
||||||
|
raise ValueError("size cannot be greater than set maximum entries")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def submit(self, data, flags=0):
|
||||||
|
pass
|
||||||
|
|
||||||
|
# add discard, output and also give names to flags and stuff
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
import ast
|
import ast
|
||||||
|
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 ..debuginfo import DebugInfoGenerator
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def maps_proc(tree, module, chunks):
|
def maps_proc(tree, module, chunks):
|
||||||
@ -13,7 +14,7 @@ def maps_proc(tree, module, chunks):
|
|||||||
map_sym_tab = {}
|
map_sym_tab = {}
|
||||||
for func_node in chunks:
|
for func_node in chunks:
|
||||||
if is_map(func_node):
|
if is_map(func_node):
|
||||||
print(f"Found BPF map: {func_node.name}")
|
logger.info(f"Found BPF map: {func_node.name}")
|
||||||
map_sym_tab[func_node.name] = process_bpf_map(func_node, module)
|
map_sym_tab[func_node.name] = process_bpf_map(func_node, module)
|
||||||
return map_sym_tab
|
return map_sym_tab
|
||||||
|
|
||||||
@ -26,8 +27,41 @@ def is_map(func_node):
|
|||||||
|
|
||||||
|
|
||||||
class BPFMapType(Enum):
|
class BPFMapType(Enum):
|
||||||
|
UNSPEC = 0
|
||||||
HASH = 1
|
HASH = 1
|
||||||
|
ARRAY = 2
|
||||||
|
PROG_ARRAY = 3
|
||||||
PERF_EVENT_ARRAY = 4
|
PERF_EVENT_ARRAY = 4
|
||||||
|
PERCPU_HASH = 5
|
||||||
|
PERCPU_ARRAY = 6
|
||||||
|
STACK_TRACE = 7
|
||||||
|
CGROUP_ARRAY = 8
|
||||||
|
LRU_HASH = 9
|
||||||
|
LRU_PERCPU_HASH = 10
|
||||||
|
LPM_TRIE = 11
|
||||||
|
ARRAY_OF_MAPS = 12
|
||||||
|
HASH_OF_MAPS = 13
|
||||||
|
DEVMAP = 14
|
||||||
|
SOCKMAP = 15
|
||||||
|
CPUMAP = 16
|
||||||
|
XSKMAP = 17
|
||||||
|
SOCKHASH = 18
|
||||||
|
CGROUP_STORAGE_DEPRECATED = 19
|
||||||
|
CGROUP_STORAGE = 19
|
||||||
|
REUSEPORT_SOCKARRAY = 20
|
||||||
|
PERCPU_CGROUP_STORAGE_DEPRECATED = 21
|
||||||
|
PERCPU_CGROUP_STORAGE = 21
|
||||||
|
QUEUE = 22
|
||||||
|
STACK = 23
|
||||||
|
SK_STORAGE = 24
|
||||||
|
DEVMAP_HASH = 25
|
||||||
|
STRUCT_OPS = 26
|
||||||
|
RINGBUF = 27
|
||||||
|
INODE_STORAGE = 28
|
||||||
|
TASK_STORAGE = 29
|
||||||
|
BLOOM_FILTER = 30
|
||||||
|
USER_RINGBUF = 31
|
||||||
|
CGRP_STORAGE = 32
|
||||||
|
|
||||||
|
|
||||||
def create_bpf_map(module, map_name, map_params):
|
def create_bpf_map(module, map_name, map_params):
|
||||||
@ -51,13 +85,13 @@ def create_bpf_map(module, map_name, map_params):
|
|||||||
|
|
||||||
|
|
||||||
def create_map_debug_info(module, map_global, map_name, map_params):
|
def create_map_debug_info(module, map_global, map_name, map_params):
|
||||||
"""Generate debug information metadata for BPF map"""
|
"""Generate debug info metadata for BPF maps HASH and PERF_EVENT_ARRAY"""
|
||||||
generator = DebugInfoGenerator(module)
|
generator = DebugInfoGenerator(module)
|
||||||
|
|
||||||
uint_type = generator.get_uint32_type()
|
uint_type = generator.get_uint32_type()
|
||||||
ulong_type = generator.get_uint64_type()
|
ulong_type = generator.get_uint64_type()
|
||||||
array_type = generator.create_array_type(
|
array_type = generator.create_array_type(
|
||||||
uint_type, map_params.get("type", BPFMapType.HASH).value
|
uint_type, map_params.get("type", BPFMapType.UNSPEC).value
|
||||||
)
|
)
|
||||||
type_ptr = generator.create_pointer_type(array_type, 64)
|
type_ptr = generator.create_pointer_type(array_type, 64)
|
||||||
key_ptr = generator.create_pointer_type(
|
key_ptr = generator.create_pointer_type(
|
||||||
@ -112,6 +146,60 @@ def create_map_debug_info(module, map_global, map_name, map_params):
|
|||||||
return global_var
|
return global_var
|
||||||
|
|
||||||
|
|
||||||
|
def create_ringbuf_debug_info(module, map_global, map_name, map_params):
|
||||||
|
"""Generate debug information metadata for BPF RINGBUF map"""
|
||||||
|
generator = DebugInfoGenerator(module)
|
||||||
|
|
||||||
|
int_type = generator.get_int32_type()
|
||||||
|
|
||||||
|
type_array = generator.create_array_type(
|
||||||
|
int_type, map_params.get("type", BPFMapType.RINGBUF).value
|
||||||
|
)
|
||||||
|
type_ptr = generator.create_pointer_type(type_array, 64)
|
||||||
|
type_member = generator.create_struct_member("type", type_ptr, 0)
|
||||||
|
|
||||||
|
max_entries_array = generator.create_array_type(int_type, map_params["max_entries"])
|
||||||
|
max_entries_ptr = generator.create_pointer_type(max_entries_array, 64)
|
||||||
|
max_entries_member = generator.create_struct_member(
|
||||||
|
"max_entries", max_entries_ptr, 64
|
||||||
|
)
|
||||||
|
|
||||||
|
elements_arr = [type_member, max_entries_member]
|
||||||
|
|
||||||
|
struct_type = generator.create_struct_type(elements_arr, 128, is_distinct=True)
|
||||||
|
|
||||||
|
global_var = generator.create_global_var_debug_info(
|
||||||
|
map_name, struct_type, is_local=False
|
||||||
|
)
|
||||||
|
map_global.set_metadata("dbg", global_var)
|
||||||
|
return global_var
|
||||||
|
|
||||||
|
|
||||||
|
@MapProcessorRegistry.register("RingBuf")
|
||||||
|
def process_ringbuf_map(map_name, rval, module):
|
||||||
|
"""Process a BPF_RINGBUF map declaration"""
|
||||||
|
logger.info(f"Processing Ringbuf: {map_name}")
|
||||||
|
map_params = {"type": BPFMapType.RINGBUF}
|
||||||
|
|
||||||
|
# Parse max_entries if present
|
||||||
|
if len(rval.args) >= 1 and isinstance(rval.args[0], ast.Constant):
|
||||||
|
const_val = rval.args[0].value
|
||||||
|
if isinstance(const_val, int):
|
||||||
|
map_params["max_entries"] = const_val
|
||||||
|
|
||||||
|
for keyword in rval.keywords:
|
||||||
|
if keyword.arg == "max_entries" and isinstance(keyword.value, ast.Constant):
|
||||||
|
const_val = keyword.value.value
|
||||||
|
if isinstance(const_val, int):
|
||||||
|
map_params["max_entries"] = const_val
|
||||||
|
|
||||||
|
logger.info(f"Ringbuf map parameters: {map_params}")
|
||||||
|
|
||||||
|
map_global = create_bpf_map(module, map_name, map_params)
|
||||||
|
create_ringbuf_debug_info(module, map_global, map_name, map_params)
|
||||||
|
return map_global
|
||||||
|
|
||||||
|
|
||||||
@MapProcessorRegistry.register("HashMap")
|
@MapProcessorRegistry.register("HashMap")
|
||||||
def process_hash_map(map_name, rval, module):
|
def process_hash_map(map_name, rval, module):
|
||||||
"""Process a BPF_HASH map declaration"""
|
"""Process a BPF_HASH map declaration"""
|
||||||
|
|||||||
@ -1,7 +1,11 @@
|
|||||||
|
from collections.abc import Callable
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
class MapProcessorRegistry:
|
class MapProcessorRegistry:
|
||||||
"""Registry for map processor functions"""
|
"""Registry for map processor functions"""
|
||||||
|
|
||||||
_processors = {}
|
_processors: dict[str, Callable[..., Any]] = {}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def register(cls, map_type_name):
|
def register(cls, map_type_name):
|
||||||
|
|||||||
@ -1 +1,3 @@
|
|||||||
from .structs_pass import structs_proc
|
from .structs_pass import structs_proc
|
||||||
|
|
||||||
|
__all__ = ["structs_proc"]
|
||||||
|
|||||||
@ -19,7 +19,7 @@ def structs_proc(tree, module, chunks):
|
|||||||
structs_sym_tab = {}
|
structs_sym_tab = {}
|
||||||
for cls_node in chunks:
|
for cls_node in chunks:
|
||||||
if is_bpf_struct(cls_node):
|
if is_bpf_struct(cls_node):
|
||||||
print(f"Found BPF struct: {cls_node.name}")
|
logger.info(f"Found BPF struct: {cls_node.name}")
|
||||||
struct_info = process_bpf_struct(cls_node, module)
|
struct_info = process_bpf_struct(cls_node, module)
|
||||||
structs_sym_tab[cls_node.name] = struct_info
|
structs_sym_tab[cls_node.name] = struct_info
|
||||||
return structs_sym_tab
|
return structs_sym_tab
|
||||||
|
|||||||
@ -1,47 +0,0 @@
|
|||||||
// SPDX-License-Identifier: GPL-2.0
|
|
||||||
#include <linux/bpf.h>
|
|
||||||
#include <bpf/bpf_helpers.h>
|
|
||||||
#include <bpf/bpf_tracing.h>
|
|
||||||
#include <linux/blkdev.h>
|
|
||||||
#define __TARGET_ARCH_aarch64
|
|
||||||
#define u64 unsigned long long
|
|
||||||
|
|
||||||
struct {
|
|
||||||
__uint(type, BPF_MAP_TYPE_HASH);
|
|
||||||
__uint(max_entries, 10240);
|
|
||||||
__type(key, struct request *);
|
|
||||||
__type(value, u64);
|
|
||||||
} start SEC(".maps");
|
|
||||||
|
|
||||||
SEC("kprobe/blk_start_request")
|
|
||||||
int BPF_KPROBE(trace_start_req, struct request *req)
|
|
||||||
{
|
|
||||||
u64 ts = bpf_ktime_get_ns();
|
|
||||||
bpf_map_update_elem(&start, &req, &ts, BPF_ANY);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
SEC("kprobe/blk_mq_start_request")
|
|
||||||
int BPF_KPROBE(trace_start_mq, struct request *req)
|
|
||||||
{
|
|
||||||
u64 ts = bpf_ktime_get_ns();
|
|
||||||
bpf_map_update_elem(&start, &req, &ts, BPF_ANY);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
SEC("kprobe/blk_account_io_completion")
|
|
||||||
int BPF_KPROBE(trace_completion, struct request *req)
|
|
||||||
{
|
|
||||||
u64 *tsp, delta;
|
|
||||||
|
|
||||||
tsp = bpf_map_lookup_elem(&start, &req);
|
|
||||||
if (tsp) {
|
|
||||||
delta = bpf_ktime_get_ns() - *tsp;
|
|
||||||
bpf_printk("%d %x %d\n", req->__data_len,
|
|
||||||
req->cmd_flags, delta / 1000);
|
|
||||||
bpf_map_delete_elem(&start, &req);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
char LICENSE[] SEC("license") = "GPL";
|
|
||||||
51
tests/c-form/ringbuf.bpf.c
Normal file
51
tests/c-form/ringbuf.bpf.c
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
|
||||||
|
#include <linux/bpf.h>
|
||||||
|
#include <bpf/bpf_helpers.h>
|
||||||
|
#include <bpf/bpf_tracing.h>
|
||||||
|
#include <linux/types.h>
|
||||||
|
|
||||||
|
// Define the structure to be sent via ringbuf
|
||||||
|
struct event {
|
||||||
|
__u32 pid;
|
||||||
|
__u32 uid;
|
||||||
|
__u64 timestamp;
|
||||||
|
char comm[16]; // Process name
|
||||||
|
};
|
||||||
|
|
||||||
|
// Define the ringbuffer map
|
||||||
|
struct {
|
||||||
|
__uint(type, BPF_MAP_TYPE_RINGBUF);
|
||||||
|
__uint(max_entries, 256 * 1024); // 256 KB
|
||||||
|
} events SEC(".maps");
|
||||||
|
|
||||||
|
// Tracepoint for execve system calls
|
||||||
|
SEC("tracepoint/syscalls/sys_enter_execve")
|
||||||
|
int trace_execve(void *ctx)
|
||||||
|
{
|
||||||
|
struct event *e;
|
||||||
|
__u64 pid_tgid;
|
||||||
|
__u64 uid_gid;
|
||||||
|
|
||||||
|
// Reserve space in the ringbuffer
|
||||||
|
e = bpf_ringbuf_reserve(&events, sizeof(*e), 0);
|
||||||
|
if (!e)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// Fill the struct with data
|
||||||
|
pid_tgid = bpf_get_current_pid_tgid();
|
||||||
|
e->pid = pid_tgid >> 32;
|
||||||
|
|
||||||
|
uid_gid = bpf_get_current_uid_gid();
|
||||||
|
e->uid = uid_gid & 0xFFFFFFFF;
|
||||||
|
|
||||||
|
e->timestamp = bpf_ktime_get_ns();
|
||||||
|
|
||||||
|
bpf_get_current_comm(&e->comm, sizeof(e->comm));
|
||||||
|
|
||||||
|
// Submit the event to ringbuffer
|
||||||
|
bpf_ringbuf_submit(e, 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char LICENSE[] SEC("license") = "GPL";
|
||||||
@ -6,6 +6,7 @@ from ctypes import c_void_p, c_int64
|
|||||||
@section("sometag1")
|
@section("sometag1")
|
||||||
def sometag(ctx: c_void_p) -> c_int64:
|
def sometag(ctx: c_void_p) -> c_int64:
|
||||||
a = 1 + 2 + 1
|
a = 1 + 2 + 1
|
||||||
|
print(f"{a}")
|
||||||
return c_int64(0)
|
return c_int64(0)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
from pythonbpf import bpf, map, section, bpfglobal, compile
|
from pythonbpf import bpf, map, section, bpfglobal, compile
|
||||||
from pythonbpf.helpers import XDP_PASS
|
from pythonbpf.helper import XDP_PASS
|
||||||
from pythonbpf.maps import HashMap
|
from pythonbpf.maps import HashMap
|
||||||
|
|
||||||
from ctypes import c_void_p, c_int64
|
from ctypes import c_void_p, c_int64
|
||||||
|
|||||||
@ -7,6 +7,7 @@ from ctypes import c_void_p, c_int64
|
|||||||
@section("sometag1")
|
@section("sometag1")
|
||||||
def sometag(ctx: c_void_p) -> c_int64:
|
def sometag(ctx: c_void_p) -> c_int64:
|
||||||
a = 1 + 2
|
a = 1 + 2
|
||||||
|
print(f"{a}")
|
||||||
return c_int64(0)
|
return c_int64(0)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,51 @@
|
|||||||
|
from pythonbpf import bpf, map, struct, section, bpfglobal, compile, compile_to_ir, BPF
|
||||||
|
from pythonbpf.helper import ktime, pid
|
||||||
|
from pythonbpf.maps import PerfEventArray
|
||||||
|
import logging
|
||||||
|
from ctypes import c_void_p, c_int32, c_uint64
|
||||||
|
|
||||||
|
|
||||||
|
# PLACEHOLDER EXAMPLE. THIS SHOULD TECHNICALLY STILL FAIL TESTS
|
||||||
|
@bpf
|
||||||
|
@struct
|
||||||
|
class data_t:
|
||||||
|
pid: c_uint64
|
||||||
|
ts: c_uint64
|
||||||
|
comm: str(16)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@map
|
||||||
|
def events() -> PerfEventArray:
|
||||||
|
return PerfEventArray(key_size=c_int32, value_size=c_int32)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@section("tracepoint/syscalls/sys_enter_clone")
|
||||||
|
def hello(ctx: c_void_p) -> c_int32:
|
||||||
|
dataobj = data_t()
|
||||||
|
ts = ktime()
|
||||||
|
strobj = "hellohellohello"
|
||||||
|
dataobj.pid = pid()
|
||||||
|
dataobj.ts = ktime()
|
||||||
|
# dataobj.comm = strobj
|
||||||
|
print(
|
||||||
|
f"clone called at {dataobj.ts} by pid {dataobj.pid}, comm {strobj} at time {ts}"
|
||||||
|
)
|
||||||
|
events.output(dataobj)
|
||||||
|
return c_int32(0)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
compile_to_ir("perf_buffer_map.py", "perf_buffer_map.ll")
|
||||||
|
compile(loglevel=logging.INFO)
|
||||||
|
b = BPF()
|
||||||
|
b.load_and_attach()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
print("running")
|
||||||
|
|||||||
35
tests/passing_tests/ringbuf.py
Normal file
35
tests/passing_tests/ringbuf.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
from pythonbpf import bpf, BPF, map, bpfglobal, section, compile, compile_to_ir
|
||||||
|
from pythonbpf.maps import RingBuf, HashMap
|
||||||
|
from ctypes import c_int32, c_void_p
|
||||||
|
|
||||||
|
|
||||||
|
# Define a map
|
||||||
|
@bpf
|
||||||
|
@map
|
||||||
|
def mymap() -> RingBuf:
|
||||||
|
return RingBuf(max_entries=(1024))
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@map
|
||||||
|
def mymap2() -> HashMap:
|
||||||
|
return HashMap(key=c_int32, value=c_int32, max_entries=1024)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@section("tracepoint/syscalls/sys_enter_clone")
|
||||||
|
def random_section(ctx: c_void_p) -> c_int32:
|
||||||
|
print("Hello")
|
||||||
|
return c_int32(0)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
compile_to_ir("ringbuf.py", "ringbuf.ll")
|
||||||
|
compile()
|
||||||
|
b = BPF()
|
||||||
|
b.load_and_attach()
|
||||||
Reference in New Issue
Block a user