File: test_clang.lua

package info (click to toggle)
bpfcc 0.18.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,368 kB
  • sloc: ansic: 132,727; python: 36,226; cpp: 26,973; sh: 710; yacc: 525; makefile: 141; lex: 94
file content (333 lines) | stat: -rw-r--r-- 8,200 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
local suite = require("test_helper")
local TestClang = {}

function TestClang:test_probe_read1()
  local text = [[
#include <linux/sched.h>
#include <uapi/linux/ptrace.h>
int count_sched(struct pt_regs *ctx, struct task_struct *prev) {
    pid_t p = prev->pid;
    return (p != -1);
}
]]
  local b = BPF:new{text=text, debug=0}
  local fn = b:load_func("count_sched", 'BPF_PROG_TYPE_KPROBE')
end

function TestClang:test_probe_read2()
  local text = [[
#include <linux/sched.h>
#include <uapi/linux/ptrace.h>
int count_foo(struct pt_regs *ctx, unsigned long a, unsigned long b) {
    return (a != b);
}
]]
  local b = BPF:new{text=text, debug=0}
  local fn = b:load_func("count_foo", 'BPF_PROG_TYPE_KPROBE')
end

function TestClang:test_probe_read_keys()
  local text = [[
#include <uapi/linux/ptrace.h>
#include <linux/blkdev.h>
BPF_HASH(start, struct request *);
int do_request(struct pt_regs *ctx, struct request *req) {
  u64 ts = bpf_ktime_get_ns();
  start.update(&req, &ts);
  return 0;
}

int do_completion(struct pt_regs *ctx, struct request *req) {
  u64 *tsp = start.lookup(&req);
  if (tsp != 0) {
    start.delete(&req);
  }
  return 0;
}
  ]]
  local b = BPF:new{text=text, debug=0}
  local fns = b:load_funcs('BPF_PROG_TYPE_KPROBE')
end

function TestClang:test_sscanf()
  local text = [[
BPF_HASH(stats, int, struct { u64 a; u64 b; u32 c:18; u32 d:14; struct { u32 a; u32 b; } s; }, 10);

int foo(void *ctx) {
    return 0;
}
]]
  local b = BPF:new{text=text, debug=0}
  local fn = b:load_func("foo", 'BPF_PROG_TYPE_KPROBE')
  local t = b:get_table("stats")
  local s1 = t:key_sprintf(2)

  assert_equals(s1, "0x2")

  local s2 = t:leaf_sprintf({{2, 3, 4, 1, {5, 6}}})
  local l = t:leaf_scanf(s2)

  assert_equals(tonumber(l.a), 2)
  assert_equals(tonumber(l.b), 3)
  assert_equals(tonumber(l.c), 4)
  assert_equals(tonumber(l.d), 1)
  assert_equals(tonumber(l.s.a), 5)
  assert_equals(tonumber(l.s.b), 6)
end

function TestClang:test_sscanf_array()
  local text = [[ BPF_HASH(stats, int, struct { u32 a[3]; u32 b; }, 10); ]]

  local b = BPF:new{text=text, debug=0}
  local t = b:get_table("stats")

  local s1 = t:key_sprintf(2)
  assert_equals(s1, "0x2")

  local s2 = t:leaf_sprintf({{{1, 2, 3}, 4}})
  assert_equals(s2, "{ [ 0x1 0x2 0x3 ] 0x4 }")

  local l = t:leaf_scanf(s2)
  assert_equals(l.a[0], 1)
  assert_equals(l.a[1], 2)
  assert_equals(l.a[2], 3)
  assert_equals(l.b, 4)
end

function TestClang:test_iosnoop()
  local text = [[
#include <linux/blkdev.h>
#include <uapi/linux/ptrace.h>

struct key_t {
    struct request *req;
};

BPF_HASH(start, struct key_t, u64, 1024);
int do_request(struct pt_regs *ctx, struct request *req) {
    struct key_t key = {};

    bpf_trace_printk("traced start %d\\n", req->__data_len);

    return 0;
}
]]

  local b = BPF:new{text=text, debug=0}
  local fn = b:load_func("do_request", 'BPF_PROG_TYPE_KPROBE')
end

function TestClang:test_blk_start_request()
  local text = [[
#include <linux/blkdev.h>
#include <uapi/linux/ptrace.h>
int do_request(struct pt_regs *ctx, int req) {
    bpf_trace_printk("req ptr: 0x%x\n", req);
    return 0;
}
]]
  local b = BPF:new{text=text, debug=0}
  local fn = b:load_func("do_request", 'BPF_PROG_TYPE_KPROBE')
end

function TestClang:test_bpf_hash()
  local text = [[
BPF_HASH(table1);
BPF_HASH(table2, u32);
BPF_HASH(table3, u32, int);
]]
  local b = BPF:new{text=text, debug=0}
end

function TestClang:test_consecutive_probe_read()
  local text = [[
#include <linux/fs.h>
#include <linux/mount.h>
BPF_HASH(table1, struct super_block *);
int trace_entry(struct pt_regs *ctx, struct file *file) {
    if (!file) return 0;
    struct vfsmount *mnt = file->f_path.mnt;
    if (mnt) {
        struct super_block *k = mnt->mnt_sb;
        u64 zero = 0;
        table1.update(&k, &zero);
        k = mnt->mnt_sb;
        table1.update(&k, &zero);
    }

    return 0;
}
]]
  local b = BPF:new{text=text, debug=0}
  local fn = b:load_func("trace_entry", 'BPF_PROG_TYPE_KPROBE')
end

function TestClang:test_nested_probe_read()
  local text = [[
#include <linux/fs.h>
int trace_entry(struct pt_regs *ctx, struct file *file) {
    if (!file) return 0;
    const char *name = file->f_path.dentry->d_name.name;
    bpf_trace_printk("%s\\n", name);
    return 0;
}
]]
  local b = BPF:new{text=text, debug=0}
  local fn = b:load_func("trace_entry", 'BPF_PROG_TYPE_KPROBE')
end

function TestClang:test_char_array_probe()
  local b = BPF:new{text=[[#include <linux/blkdev.h>
int kprobe__blk_update_request(struct pt_regs *ctx, struct request *req) {
    bpf_trace_printk("%s\\n", req->rq_disk->disk_name);
    return 0;
}]]}
end

function TestClang:test_probe_read_helper()
  local b = BPF:new{text=[[
#include <linux/fs.h>
static void print_file_name(struct file *file) {
    if (!file) return;
    const char *name = file->f_path.dentry->d_name.name;
    bpf_trace_printk("%s\\n", name);
}
static void print_file_name2(int unused, struct file *file) {
    print_file_name(file);
}
int trace_entry1(struct pt_regs *ctx, struct file *file) {
    print_file_name(file);
    return 0;
}
int trace_entry2(struct pt_regs *ctx, int unused, struct file *file) {
    print_file_name2(unused, file);
    return 0;
}
]]}
  local fn1 = b:load_func("trace_entry1", 'BPF_PROG_TYPE_KPROBE')
  local fn2 = b:load_func("trace_entry2", 'BPF_PROG_TYPE_KPROBE')
end

function TestClang:test_probe_struct_assign()
  local b = BPF:new{text = [[
#include <uapi/linux/ptrace.h>
struct args_t {
    const char *filename;
    int flags;
    int mode;
};
int kprobe__sys_open(struct pt_regs *ctx, const char *filename,
        int flags, int mode) {
    struct args_t args = {};
    args.filename = filename;
    args.flags = flags;
    args.mode = mode;
    bpf_trace_printk("%s\\n", args.filename);
    return 0;
};
]]}
end

function TestClang:test_task_switch()
  local b = BPF:new{text=[[
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>
struct key_t {
  u32 prev_pid;
  u32 curr_pid;
};
BPF_HASH(stats, struct key_t, u64, 1024);
int kprobe__finish_task_switch(struct pt_regs *ctx, struct task_struct *prev) {
  struct key_t key = {};
  u64 zero = 0, *val;
  key.curr_pid = bpf_get_current_pid_tgid();
  key.prev_pid = prev->pid;

  val = stats.lookup_or_try_init(&key, &zero);
  if (val) {
    (*val)++;
  }
  return 0;
}
]]}
end

function TestClang:test_probe_simple_assign()
  local b = BPF:new{text=[[
#include <uapi/linux/ptrace.h>
#include <linux/gfp.h>
struct leaf { size_t size; };
BPF_HASH(simple_map, u32, struct leaf);
int kprobe____kmalloc(struct pt_regs *ctx, size_t size) {
    u32 pid = bpf_get_current_pid_tgid();
    struct leaf* leaf = simple_map.lookup(&pid);
    if (leaf)
        leaf->size += size;
    return 0;
}]]}
end

function TestClang:test_unop_probe_read()
  local text = [[
#include <linux/blkdev.h>
int trace_entry(struct pt_regs *ctx, struct request *req) {
    if (!(req->bio->bi_flags & 1))
        return 1;
    if (((req->bio->bi_flags)))
        return 1;
    return 0;
}
]]
  local b = BPF:new{text=text}
  local fn = b:load_func("trace_entry", 'BPF_PROG_TYPE_KPROBE')
end

function TestClang:test_complex_leaf_types()
  local text = [[
struct list;
struct list {
  struct list *selfp;
  struct list *another_selfp;
  struct list *selfp_array[2];
};
struct empty {
};
union emptyu {
  struct empty *em1;
  struct empty em2;
  struct empty em3;
  struct empty em4;
};
BPF_ARRAY(t1, struct list, 1);
BPF_ARRAY(t2, struct list *, 1);
BPF_ARRAY(t3, union emptyu, 1);
]]
  local b = BPF:new{text=text}
  local ffi = require("ffi")

  -- TODO: ptrs?
  assert_equals(ffi.sizeof(b:get_table("t3").c_leaf), 8)
end

function TestClang:test_cflags()
  local text = [[
#ifndef MYFLAG
#error "MYFLAG not set as expected"
#endif
]]
  local b = BPF:new{text=text, cflags={"-DMYFLAG"}}
end

function TestClang:test_exported_maps()
  local b1 = BPF{text=[[BPF_TABLE_PUBLIC("hash", int, int, table1, 10);]]}
  local b2 = BPF{text=[[BPF_TABLE("extern", int, int, table1, 10);]]}
end

function TestClang:test_syntax_error()
  assert_error_msg_contains(
    "failed to compile BPF module",
    BPF.new,
    BPF, {text=[[int failure(void *ctx) { if (); return 0; }]]})
end

suite("TestClang", TestClang)