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_mods_output {
17 int a, b, c, d, e, f, g, h;
18};
19
20typedef const int int_t;
21typedef const char *char_ptr_t;
22typedef const int arr_t[7];
23
24struct core_reloc_mods_substruct {
25 int x;
26 int y;
27};
28
29typedef struct {
30 int x;
31 int y;
32} core_reloc_mods_substruct_t;
33
34struct core_reloc_mods {
35 int a;
36 int_t b;
37 char *c;
38 char_ptr_t d;
39 int e[3];
40 arr_t f;
41 struct core_reloc_mods_substruct g;
42 core_reloc_mods_substruct_t h;
43};
44
45#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
46#define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
47#else
48#define CORE_READ(dst, src) ({ \
49 int __sz = sizeof(*(dst)) < sizeof(*(src)) ? sizeof(*(dst)) : \
50 sizeof(*(src)); \
51 bpf_core_read((char *)(dst) + sizeof(*(dst)) - __sz, __sz, \
52 (const char *)(src) + sizeof(*(src)) - __sz); \
53})
54#endif
55
56SEC("raw_tracepoint/sys_enter")
57int test_core_mods(void *ctx)
58{
59 struct core_reloc_mods *in = (void *)&data.in;
60 struct core_reloc_mods_output *out = (void *)&data.out;
61
62 if (CORE_READ(&out->a, &in->a) ||
63 CORE_READ(&out->b, &in->b) ||
64 CORE_READ(&out->c, &in->c) ||
65 CORE_READ(&out->d, &in->d) ||
66 CORE_READ(&out->e, &in->e[2]) ||
67 CORE_READ(&out->f, &in->f[1]) ||
68 CORE_READ(&out->g, &in->g.x) ||
69 CORE_READ(&out->h, &in->h.y))
70 return 1;
71
72 return 0;
73}
74
75

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