1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2019 Facebook
3
4#include <linux/bpf.h>
5#include <stdint.h>
6#include <bpf/bpf_helpers.h>
7#include <bpf/bpf_core_read.h>
8
9char _license[] SEC("license") = "GPL";
10
11struct {
12 char in[256];
13 char out[256];
14} data = {};
15
16struct core_reloc_flavors {
17 int a;
18 int b;
19 int c;
20};
21
22/* local flavor with reversed layout */
23struct core_reloc_flavors___reversed {
24 int c;
25 int b;
26 int a;
27};
28
29/* local flavor with nested/overlapping layout */
30struct core_reloc_flavors___weird {
31 struct {
32 int b;
33 };
34 /* a and c overlap in local flavor, but this should still work
35 * correctly with target original flavor
36 */
37 union {
38 int a;
39 int c;
40 };
41};
42
43#define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
44
45SEC("raw_tracepoint/sys_enter")
46int test_core_flavors(void *ctx)
47{
48 struct core_reloc_flavors *in_orig = (void *)&data.in;
49 struct core_reloc_flavors___reversed *in_rev = (void *)&data.in;
50 struct core_reloc_flavors___weird *in_weird = (void *)&data.in;
51 struct core_reloc_flavors *out = (void *)&data.out;
52
53 /* read a using weird layout */
54 if (CORE_READ(&out->a, &in_weird->a))
55 return 1;
56 /* read b using reversed layout */
57 if (CORE_READ(&out->b, &in_rev->b))
58 return 1;
59 /* read c using original layout */
60 if (CORE_READ(&out->c, &in_orig->c))
61 return 1;
62
63 return 0;
64}
65
66

source code of linux/tools/testing/selftests/bpf/progs/test_core_reloc_flavors.c