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
8struct task_struct___bad {
9	int pid;
10	int fake_field;
11	void *fake_field_subprog;
12} __attribute__((preserve_access_index));
13
14SEC("?raw_tp/sys_enter")
15int 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
22static __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
30SEC("?raw_tp/sys_enter")
31int 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
38struct {
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
45struct {
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
52SEC("?raw_tp/sys_enter")
53int 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
64extern int bpf_nonexistent_kfunc(void) __ksym __weak;
65
66SEC("?raw_tp/sys_enter")
67int use_missing_kfunc(const void *ctx)
68{
69	bpf_nonexistent_kfunc();
70
71	return 0;
72}
73
74char _license[] SEC("license") = "GPL";
75