xref: /linux/tools/testing/selftests/bpf/progs/test_log_fixup.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3 
4 #include <linux/bpf.h>
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_core_read.h>
7 
8 struct task_struct___bad {
9 	int pid;
10 	int fake_field;
11 	void *fake_field_subprog;
12 } __attribute__((preserve_access_index));
13 
14 SEC("?raw_tp/sys_enter")
bad_relo(const void * ctx)15 int bad_relo(const void *ctx)
16 {
17 	static struct task_struct___bad *t;
18 
19 	return bpf_core_field_size(t->fake_field);
20 }
21 
bad_subprog(void)22 static __noinline int bad_subprog(void)
23 {
24 	static struct task_struct___bad *t;
25 
26 	/* ugliness below is a field offset relocation */
27 	return (void *)&t->fake_field_subprog - (void *)t;
28 }
29 
30 SEC("?raw_tp/sys_enter")
bad_relo_subprog(const void * ctx)31 int bad_relo_subprog(const void *ctx)
32 {
33 	static struct task_struct___bad *t;
34 
35 	return bad_subprog() + bpf_core_field_size(t->pid);
36 }
37 
38 struct {
39 	__uint(type, BPF_MAP_TYPE_ARRAY);
40 	__uint(max_entries, 1);
41 	__type(key, int);
42 	__type(value, int);
43 } existing_map SEC(".maps");
44 
45 struct {
46 	__uint(type, BPF_MAP_TYPE_ARRAY);
47 	__uint(max_entries, 1);
48 	__type(key, int);
49 	__type(value, int);
50 } missing_map SEC(".maps");
51 
52 SEC("?raw_tp/sys_enter")
use_missing_map(const void * ctx)53 int use_missing_map(const void *ctx)
54 {
55 	int zero = 0, *value;
56 
57 	value = bpf_map_lookup_elem(&existing_map, &zero);
58 
59 	value = bpf_map_lookup_elem(&missing_map, &zero);
60 
61 	return value != NULL;
62 }
63 
64 extern int bpf_nonexistent_kfunc(void) __ksym __weak;
65 
66 SEC("?raw_tp/sys_enter")
use_missing_kfunc(const void * ctx)67 int use_missing_kfunc(const void *ctx)
68 {
69 	bpf_nonexistent_kfunc();
70 
71 	return 0;
72 }
73 
74 char _license[] SEC("license") = "GPL";
75