1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <linux/ptrace.h>
7 #include <linux/bpf.h>
8 #include <bpf/bpf_helpers.h>
9
10 /* non-existing BPF helper, to test dead code elimination */
11 static int (*bpf_missing_helper)(const void *arg1, int arg2) = (void *) 999;
12
13 extern int LINUX_KERNEL_VERSION __kconfig;
14 extern bool CONFIG_BPF_SYSCALL __kconfig; /* strong */
15 extern enum libbpf_tristate CONFIG_TRISTATE __kconfig __weak;
16 extern bool CONFIG_BOOL __kconfig __weak;
17 extern char CONFIG_CHAR __kconfig __weak;
18 extern uint16_t CONFIG_USHORT __kconfig __weak;
19 extern int CONFIG_INT __kconfig __weak;
20 extern uint64_t CONFIG_ULONG __kconfig __weak;
21 extern const char CONFIG_STR[8] __kconfig __weak;
22 extern uint64_t CONFIG_MISSING __kconfig __weak;
23
24 uint64_t kern_ver = -1;
25 uint64_t bpf_syscall = -1;
26 uint64_t tristate_val = -1;
27 uint64_t bool_val = -1;
28 uint64_t char_val = -1;
29 uint64_t ushort_val = -1;
30 uint64_t int_val = -1;
31 uint64_t ulong_val = -1;
32 char str_val[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
33 uint64_t missing_val = -1;
34
35 SEC("raw_tp/sys_enter")
handle_sys_enter(struct pt_regs * ctx)36 int handle_sys_enter(struct pt_regs *ctx)
37 {
38 int i;
39
40 kern_ver = LINUX_KERNEL_VERSION;
41 bpf_syscall = CONFIG_BPF_SYSCALL;
42 tristate_val = CONFIG_TRISTATE;
43 bool_val = CONFIG_BOOL;
44 char_val = CONFIG_CHAR;
45 ushort_val = CONFIG_USHORT;
46 int_val = CONFIG_INT;
47 ulong_val = CONFIG_ULONG;
48
49 for (i = 0; i < sizeof(CONFIG_STR); i++) {
50 str_val[i] = CONFIG_STR[i];
51 }
52
53 if (CONFIG_MISSING)
54 /* invalid, but dead code - never executed */
55 missing_val = bpf_missing_helper(ctx, 123);
56 else
57 missing_val = 0xDEADC0DE;
58
59 return 0;
60 }
61
62 char _license[] SEC("license") = "GPL";
63