Skip to content

Commit de8c486

Browse files
fix: remove deref_to_depth on single depth pointers
1 parent f135cdb commit de8c486

File tree

4 files changed

+40
-40
lines changed

4 files changed

+40
-40
lines changed

pythonbpf/expr/expr_pass.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,11 @@ def get_operand_value(
241241
var_type = var.type
242242
base_type, depth = get_base_type_and_depth(var_type)
243243
logger.info(f"var is {var}, base_type is {base_type}, depth is {depth}")
244-
val = deref_to_depth(func, builder, var, depth)
244+
if depth == 1:
245+
val = builder.load(var)
246+
return val
247+
else:
248+
val = deref_to_depth(func, builder, var, depth)
245249
return val
246250
else:
247251
# Check if it's a vmlinux enum/constant

tests/c-form/xdp_test.bpf.c

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
1+
#include "vmlinux.h"
2+
#include <bpf/bpf_helpers.h>
13
#include <linux/bpf.h>
24
#include <linux/if_ether.h>
35
#include <linux/ip.h>
4-
#include <bpf/bpf_helpers.h>
56

67
struct fake_iphdr {
7-
unsigned short useless;
8-
unsigned short tot_len;
9-
unsigned short id;
10-
unsigned short frag_off;
11-
unsigned char ttl;
12-
unsigned char protocol;
13-
unsigned short check;
14-
unsigned int saddr;
15-
unsigned int daddr;
8+
unsigned short useless;
9+
unsigned short tot_len;
10+
unsigned short id;
11+
unsigned short frag_off;
12+
unsigned char ttl;
13+
unsigned char protocol;
14+
unsigned short check;
15+
unsigned int saddr;
16+
unsigned int daddr;
1617
};
1718

1819
SEC("xdp")
19-
int xdp_prog(struct xdp_md *ctx)
20-
{
21-
void *data_end = (void *)(long)ctx->data_end;
22-
void *data = (void *)(long)ctx->data;
20+
int xdp_prog(struct xdp_md *ctx) {
21+
unsigned long data = ctx->data;
22+
unsigned long data_end = ctx->data_end;
2323

24-
struct ethhdr *eth = data;
25-
if ((void *)(eth + 1) > data_end)
26-
return XDP_ABORTED;
27-
if (eth->h_proto != __constant_htons(ETH_P_IP))
28-
return XDP_PASS;
24+
if (data + sizeof(struct ethhdr) + sizeof(struct fake_iphdr) <= data_end) {
25+
struct fake_iphdr *iph = (void *)data + sizeof(struct ethhdr);
2926

30-
struct fake_iphdr *iph = (struct fake_iphdr *)(eth + 1);
31-
if ((void *)(iph + 1) > data_end)
32-
return XDP_ABORTED;
3327
bpf_printk("%d", iph->saddr);
3428

3529
return XDP_PASS;
30+
} else {
31+
return XDP_ABORTED;
32+
}
33+
struct task_struct * a = btf_bpf_get_current_task_btf();
3634
}
3735

3836
char _license[] SEC("license") = "GPL";

tests/failing_tests/xdp/xdp_test_1.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from vmlinux import XDP_PASS, XDP_DROP
1+
from vmlinux import XDP_PASS, XDP_ABORTED
22
from vmlinux import (
33
struct_xdp_md,
4-
struct_ethhdr,
54
)
65
from pythonbpf import bpf, section, bpfglobal, compile, compile_to_ir, struct
7-
from ctypes import c_int64, c_ubyte, c_ushort, c_uint32
6+
from ctypes import c_int64, c_ubyte, c_ushort, c_uint32, c_void_p
87

98

109
@bpf
@@ -24,19 +23,15 @@ class iphdr:
2423
@bpf
2524
@section("xdp")
2625
def ip_detector(ctx: struct_xdp_md) -> c_int64:
27-
data = ctx.data
28-
data_end = ctx.data_end
29-
if data + 14 > data_end:
30-
return c_int64(XDP_DROP)
31-
32-
eth = struct_ethhdr(data)
33-
nh = data + 14
34-
if nh + 20 > data_end:
35-
return c_int64(XDP_DROP)
36-
37-
iph = iphdr(nh)
38-
39-
print(f"ipaddress: {iph.saddr}")
26+
data = c_void_p(ctx.data)
27+
data_end = c_void_p(ctx.data_end)
28+
if data + 34 < data_end:
29+
hdr = data + 14
30+
iph = iphdr(hdr)
31+
addr = iph.saddr
32+
print(f"ipaddress: {addr}")
33+
else:
34+
return c_int64(XDP_ABORTED)
4035

4136
return c_int64(XDP_PASS)
4237

tests/passing_tests/assign/ptr_to_char_array.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pythonbpf import bpf, struct, section, bpfglobal
1+
from pythonbpf import bpf, struct, section, bpfglobal, compile
22
from pythonbpf.helper import comm
33

44
from ctypes import c_void_p, c_int64
@@ -26,3 +26,6 @@ def hello(ctx: c_void_p) -> c_int64:
2626
@bpfglobal
2727
def LICENSE() -> str:
2828
return "GPL"
29+
30+
31+
compile()

0 commit comments

Comments
 (0)