Skip to content

Commit 694731e

Browse files
anakryikoborkmann
authored andcommitted
selftests/bpf: Adjust CO-RE reloc tests for new bpf_core_read() macro
To allow adding a variadic BPF_CORE_READ macro with slightly different syntax and semantics, define CORE_READ in CO-RE reloc tests, which is a thin wrapper around low-level bpf_core_read() macro, which in turn is just a wrapper around bpf_probe_read(). Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Song Liu <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 36b5d47 commit 694731e

10 files changed

+58
-40
lines changed

tools/testing/selftests/bpf/bpf_helpers.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ struct pt_regs;
223223
#endif
224224

225225
/*
226-
* BPF_CORE_READ abstracts away bpf_probe_read() call and captures offset
226+
* bpf_core_read() abstracts away bpf_probe_read() call and captures offset
227227
* relocation for source address using __builtin_preserve_access_index()
228228
* built-in, provided by Clang.
229229
*
@@ -238,8 +238,8 @@ struct pt_regs;
238238
* actual field offset, based on target kernel BTF type that matches original
239239
* (local) BTF, used to record relocation.
240240
*/
241-
#define BPF_CORE_READ(dst, src) \
242-
bpf_probe_read((dst), sizeof(*(src)), \
243-
__builtin_preserve_access_index(src))
241+
#define bpf_core_read(dst, sz, src) \
242+
bpf_probe_read(dst, sz, \
243+
(const void *)__builtin_preserve_access_index(src))
244244

245245
#endif

tools/testing/selftests/bpf/progs/test_core_reloc_arrays.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,25 @@ struct core_reloc_arrays {
3131
struct core_reloc_arrays_substruct d[1][2];
3232
};
3333

34+
#define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
35+
3436
SEC("raw_tracepoint/sys_enter")
3537
int test_core_arrays(void *ctx)
3638
{
3739
struct core_reloc_arrays *in = (void *)&data.in;
3840
struct core_reloc_arrays_output *out = (void *)&data.out;
3941

4042
/* in->a[2] */
41-
if (BPF_CORE_READ(&out->a2, &in->a[2]))
43+
if (CORE_READ(&out->a2, &in->a[2]))
4244
return 1;
4345
/* in->b[1][2][3] */
44-
if (BPF_CORE_READ(&out->b123, &in->b[1][2][3]))
46+
if (CORE_READ(&out->b123, &in->b[1][2][3]))
4547
return 1;
4648
/* in->c[1].c */
47-
if (BPF_CORE_READ(&out->c1c, &in->c[1].c))
49+
if (CORE_READ(&out->c1c, &in->c[1].c))
4850
return 1;
4951
/* in->d[0][0].d */
50-
if (BPF_CORE_READ(&out->d00d, &in->d[0][0].d))
52+
if (CORE_READ(&out->d00d, &in->d[0][0].d))
5153
return 1;
5254

5355
return 0;

tools/testing/selftests/bpf/progs/test_core_reloc_flavors.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ struct core_reloc_flavors___weird {
3939
};
4040
};
4141

42+
#define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
43+
4244
SEC("raw_tracepoint/sys_enter")
4345
int test_core_flavors(void *ctx)
4446
{
@@ -48,13 +50,13 @@ int test_core_flavors(void *ctx)
4850
struct core_reloc_flavors *out = (void *)&data.out;
4951

5052
/* read a using weird layout */
51-
if (BPF_CORE_READ(&out->a, &in_weird->a))
53+
if (CORE_READ(&out->a, &in_weird->a))
5254
return 1;
5355
/* read b using reversed layout */
54-
if (BPF_CORE_READ(&out->b, &in_rev->b))
56+
if (CORE_READ(&out->b, &in_rev->b))
5557
return 1;
5658
/* read c using original layout */
57-
if (BPF_CORE_READ(&out->c, &in_orig->c))
59+
if (CORE_READ(&out->c, &in_orig->c))
5860
return 1;
5961

6062
return 0;

tools/testing/selftests/bpf/progs/test_core_reloc_ints.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@ struct core_reloc_ints {
2323
int64_t s64_field;
2424
};
2525

26+
#define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
27+
2628
SEC("raw_tracepoint/sys_enter")
2729
int test_core_ints(void *ctx)
2830
{
2931
struct core_reloc_ints *in = (void *)&data.in;
3032
struct core_reloc_ints *out = (void *)&data.out;
3133

32-
if (BPF_CORE_READ(&out->u8_field, &in->u8_field) ||
33-
BPF_CORE_READ(&out->s8_field, &in->s8_field) ||
34-
BPF_CORE_READ(&out->u16_field, &in->u16_field) ||
35-
BPF_CORE_READ(&out->s16_field, &in->s16_field) ||
36-
BPF_CORE_READ(&out->u32_field, &in->u32_field) ||
37-
BPF_CORE_READ(&out->s32_field, &in->s32_field) ||
38-
BPF_CORE_READ(&out->u64_field, &in->u64_field) ||
39-
BPF_CORE_READ(&out->s64_field, &in->s64_field))
34+
if (CORE_READ(&out->u8_field, &in->u8_field) ||
35+
CORE_READ(&out->s8_field, &in->s8_field) ||
36+
CORE_READ(&out->u16_field, &in->u16_field) ||
37+
CORE_READ(&out->s16_field, &in->s16_field) ||
38+
CORE_READ(&out->u32_field, &in->u32_field) ||
39+
CORE_READ(&out->s32_field, &in->s32_field) ||
40+
CORE_READ(&out->u64_field, &in->u64_field) ||
41+
CORE_READ(&out->s64_field, &in->s64_field))
4042
return 1;
4143

4244
return 0;

tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ struct task_struct {
1717
int tgid;
1818
};
1919

20+
#define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
21+
2022
SEC("raw_tracepoint/sys_enter")
2123
int test_core_kernel(void *ctx)
2224
{
2325
struct task_struct *task = (void *)bpf_get_current_task();
2426
uint64_t pid_tgid = bpf_get_current_pid_tgid();
2527
int pid, tgid;
2628

27-
if (BPF_CORE_READ(&pid, &task->pid) ||
28-
BPF_CORE_READ(&tgid, &task->tgid))
29+
if (CORE_READ(&pid, &task->pid) ||
30+
CORE_READ(&tgid, &task->tgid))
2931
return 1;
3032

3133
/* validate pid + tgid matches */

tools/testing/selftests/bpf/progs/test_core_reloc_misc.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ struct core_reloc_misc_extensible {
3232
int b;
3333
};
3434

35+
#define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
36+
3537
SEC("raw_tracepoint/sys_enter")
3638
int test_core_misc(void *ctx)
3739
{
@@ -41,15 +43,15 @@ int test_core_misc(void *ctx)
4143
struct core_reloc_misc_output *out = (void *)&data.out;
4244

4345
/* record two different relocations with the same accessor string */
44-
if (BPF_CORE_READ(&out->a, &in_a->a1) || /* accessor: 0:0 */
45-
BPF_CORE_READ(&out->b, &in_b->b1)) /* accessor: 0:0 */
46+
if (CORE_READ(&out->a, &in_a->a1) || /* accessor: 0:0 */
47+
CORE_READ(&out->b, &in_b->b1)) /* accessor: 0:0 */
4648
return 1;
4749

4850
/* Validate relocations capture array-only accesses for structs with
4951
* fixed header, but with potentially extendable tail. This will read
5052
* first 4 bytes of 2nd element of in_ext array of potentially
5153
* variably sized struct core_reloc_misc_extensible. */
52-
if (BPF_CORE_READ(&out->c, &in_ext[2])) /* accessor: 2 */
54+
if (CORE_READ(&out->c, &in_ext[2])) /* accessor: 2 */
5355
return 1;
5456

5557
return 0;

tools/testing/selftests/bpf/progs/test_core_reloc_mods.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,22 @@ struct core_reloc_mods {
4141
core_reloc_mods_substruct_t h;
4242
};
4343

44+
#define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
45+
4446
SEC("raw_tracepoint/sys_enter")
4547
int test_core_mods(void *ctx)
4648
{
4749
struct core_reloc_mods *in = (void *)&data.in;
4850
struct core_reloc_mods_output *out = (void *)&data.out;
4951

50-
if (BPF_CORE_READ(&out->a, &in->a) ||
51-
BPF_CORE_READ(&out->b, &in->b) ||
52-
BPF_CORE_READ(&out->c, &in->c) ||
53-
BPF_CORE_READ(&out->d, &in->d) ||
54-
BPF_CORE_READ(&out->e, &in->e[2]) ||
55-
BPF_CORE_READ(&out->f, &in->f[1]) ||
56-
BPF_CORE_READ(&out->g, &in->g.x) ||
57-
BPF_CORE_READ(&out->h, &in->h.y))
52+
if (CORE_READ(&out->a, &in->a) ||
53+
CORE_READ(&out->b, &in->b) ||
54+
CORE_READ(&out->c, &in->c) ||
55+
CORE_READ(&out->d, &in->d) ||
56+
CORE_READ(&out->e, &in->e[2]) ||
57+
CORE_READ(&out->f, &in->f[1]) ||
58+
CORE_READ(&out->g, &in->g.x) ||
59+
CORE_READ(&out->h, &in->h.y))
5860
return 1;
5961

6062
return 0;

tools/testing/selftests/bpf/progs/test_core_reloc_nesting.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ struct core_reloc_nesting {
3030
} b;
3131
};
3232

33+
#define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
34+
3335
SEC("raw_tracepoint/sys_enter")
3436
int test_core_nesting(void *ctx)
3537
{
3638
struct core_reloc_nesting *in = (void *)&data.in;
3739
struct core_reloc_nesting *out = (void *)&data.out;
3840

39-
if (BPF_CORE_READ(&out->a.a.a, &in->a.a.a))
41+
if (CORE_READ(&out->a.a.a, &in->a.a.a))
4042
return 1;
41-
if (BPF_CORE_READ(&out->b.b.b, &in->b.b.b))
43+
if (CORE_READ(&out->b.b.b, &in->b.b.b))
4244
return 1;
4345

4446
return 0;

tools/testing/selftests/bpf/progs/test_core_reloc_primitives.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@ struct core_reloc_primitives {
2525
int (*f)(const char *);
2626
};
2727

28+
#define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
29+
2830
SEC("raw_tracepoint/sys_enter")
2931
int test_core_primitives(void *ctx)
3032
{
3133
struct core_reloc_primitives *in = (void *)&data.in;
3234
struct core_reloc_primitives *out = (void *)&data.out;
3335

34-
if (BPF_CORE_READ(&out->a, &in->a) ||
35-
BPF_CORE_READ(&out->b, &in->b) ||
36-
BPF_CORE_READ(&out->c, &in->c) ||
37-
BPF_CORE_READ(&out->d, &in->d) ||
38-
BPF_CORE_READ(&out->f, &in->f))
36+
if (CORE_READ(&out->a, &in->a) ||
37+
CORE_READ(&out->b, &in->b) ||
38+
CORE_READ(&out->c, &in->c) ||
39+
CORE_READ(&out->d, &in->d) ||
40+
CORE_READ(&out->f, &in->f))
3941
return 1;
4042

4143
return 0;

tools/testing/selftests/bpf/progs/test_core_reloc_ptr_as_arr.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ struct core_reloc_ptr_as_arr {
1616
int a;
1717
};
1818

19+
#define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
20+
1921
SEC("raw_tracepoint/sys_enter")
2022
int test_core_ptr_as_arr(void *ctx)
2123
{
2224
struct core_reloc_ptr_as_arr *in = (void *)&data.in;
2325
struct core_reloc_ptr_as_arr *out = (void *)&data.out;
2426

25-
if (BPF_CORE_READ(&out->a, &in[2].a))
27+
if (CORE_READ(&out->a, &in[2].a))
2628
return 1;
2729

2830
return 0;

0 commit comments

Comments
 (0)