diff --git a/pythonbpf/expr/expr_pass.py b/pythonbpf/expr/expr_pass.py index d741459..9f3bfa4 100644 --- a/pythonbpf/expr/expr_pass.py +++ b/pythonbpf/expr/expr_pass.py @@ -241,7 +241,11 @@ def get_operand_value( var_type = var.type base_type, depth = get_base_type_and_depth(var_type) logger.info(f"var is {var}, base_type is {base_type}, depth is {depth}") - val = deref_to_depth(func, builder, var, depth) + if depth == 1: + val = builder.load(var) + return val + else: + val = deref_to_depth(func, builder, var, depth) return val else: # Check if it's a vmlinux enum/constant diff --git a/tests/c-form/xdp_test.bpf.c b/tests/c-form/xdp_test.bpf.c index 0e90ce1..e553c37 100644 --- a/tests/c-form/xdp_test.bpf.c +++ b/tests/c-form/xdp_test.bpf.c @@ -1,38 +1,36 @@ +#include "vmlinux.h" +#include #include #include #include -#include struct fake_iphdr { - unsigned short useless; - unsigned short tot_len; - unsigned short id; - unsigned short frag_off; - unsigned char ttl; - unsigned char protocol; - unsigned short check; - unsigned int saddr; - unsigned int daddr; + unsigned short useless; + unsigned short tot_len; + unsigned short id; + unsigned short frag_off; + unsigned char ttl; + unsigned char protocol; + unsigned short check; + unsigned int saddr; + unsigned int daddr; }; SEC("xdp") -int xdp_prog(struct xdp_md *ctx) -{ - void *data_end = (void *)(long)ctx->data_end; - void *data = (void *)(long)ctx->data; +int xdp_prog(struct xdp_md *ctx) { + unsigned long data = ctx->data; + unsigned long data_end = ctx->data_end; - struct ethhdr *eth = data; - if ((void *)(eth + 1) > data_end) - return XDP_ABORTED; - if (eth->h_proto != __constant_htons(ETH_P_IP)) - return XDP_PASS; + if (data + sizeof(struct ethhdr) + sizeof(struct fake_iphdr) <= data_end) { + struct fake_iphdr *iph = (void *)data + sizeof(struct ethhdr); - struct fake_iphdr *iph = (struct fake_iphdr *)(eth + 1); - if ((void *)(iph + 1) > data_end) - return XDP_ABORTED; bpf_printk("%d", iph->saddr); return XDP_PASS; + } else { + return XDP_ABORTED; + } + struct task_struct * a = btf_bpf_get_current_task_btf(); } char _license[] SEC("license") = "GPL"; diff --git a/tests/failing_tests/xdp/xdp_test_1.py b/tests/failing_tests/xdp/xdp_test_1.py index cdd3dca..302a617 100644 --- a/tests/failing_tests/xdp/xdp_test_1.py +++ b/tests/failing_tests/xdp/xdp_test_1.py @@ -1,10 +1,9 @@ -from vmlinux import XDP_PASS, XDP_DROP +from vmlinux import XDP_PASS, XDP_ABORTED from vmlinux import ( struct_xdp_md, - struct_ethhdr, ) from pythonbpf import bpf, section, bpfglobal, compile, compile_to_ir, struct -from ctypes import c_int64, c_ubyte, c_ushort, c_uint32 +from ctypes import c_int64, c_ubyte, c_ushort, c_uint32, c_void_p @bpf @@ -24,19 +23,15 @@ class iphdr: @bpf @section("xdp") def ip_detector(ctx: struct_xdp_md) -> c_int64: - data = ctx.data - data_end = ctx.data_end - if data + 14 > data_end: - return c_int64(XDP_DROP) - - eth = struct_ethhdr(data) - nh = data + 14 - if nh + 20 > data_end: - return c_int64(XDP_DROP) - - iph = iphdr(nh) - - print(f"ipaddress: {iph.saddr}") + data = c_void_p(ctx.data) + data_end = c_void_p(ctx.data_end) + if data + 34 < data_end: + hdr = data + 14 + iph = iphdr(hdr) + addr = iph.saddr + print(f"ipaddress: {addr}") + else: + return c_int64(XDP_ABORTED) return c_int64(XDP_PASS) diff --git a/tests/passing_tests/assign/ptr_to_char_array.py b/tests/passing_tests/assign/ptr_to_char_array.py index 6a090be..90daae7 100644 --- a/tests/passing_tests/assign/ptr_to_char_array.py +++ b/tests/passing_tests/assign/ptr_to_char_array.py @@ -1,4 +1,4 @@ -from pythonbpf import bpf, struct, section, bpfglobal +from pythonbpf import bpf, struct, section, bpfglobal, compile from pythonbpf.helper import comm from ctypes import c_void_p, c_int64 @@ -26,3 +26,6 @@ def hello(ctx: c_void_p) -> c_int64: @bpfglobal def LICENSE() -> str: return "GPL" + + +compile()