aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/test_core_reloc_flavors.c
blob: 9fda73e879727a6aafc3b6d81f41f8debb6e8c69 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2019 Facebook

#include <linux/bpf.h>
#include <stdint.h>
#include "bpf_helpers.h"

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

static volatile struct data {
	char in[256];
	char out[256];
} data;

struct core_reloc_flavors {
	int a;
	int b;
	int c;
};

/* local flavor with reversed layout */
struct core_reloc_flavors___reversed {
	int c;
	int b;
	int a;
};

/* local flavor with nested/overlapping layout */
struct core_reloc_flavors___weird {
	struct {
		int b;
	};
	/* a and c overlap in local flavor, but this should still work
	 * correctly with target original flavor
	 */
	union {
		int a;
		int c;
	};
};

SEC("raw_tracepoint/sys_enter")
int test_core_flavors(void *ctx)
{
	struct core_reloc_flavors *in_orig = (void *)&data.in;
	struct core_reloc_flavors___reversed *in_rev = (void *)&data.in;
	struct core_reloc_flavors___weird *in_weird = (void *)&data.in;
	struct core_reloc_flavors *out = (void *)&data.out;

	/* read a using weird layout */
	if (BPF_CORE_READ(&out->a, &in_weird->a))
		return 1;
	/* read b using reversed layout */
	if (BPF_CORE_READ(&out->b, &in_rev->b))
		return 1;
	/* read c using original layout */
	if (BPF_CORE_READ(&out->c, &in_orig->c))
		return 1;

	return 0;
}