xref: /linux/tools/testing/selftests/bpf/progs/test_core_extern.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
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 int LINUX_UNKNOWN_VIRTUAL_EXTERN __kconfig __weak;
15 extern bool CONFIG_BPF_SYSCALL __kconfig; /* strong */
16 extern enum libbpf_tristate CONFIG_TRISTATE __kconfig __weak;
17 extern bool CONFIG_BOOL __kconfig __weak;
18 extern char CONFIG_CHAR __kconfig __weak;
19 extern uint16_t CONFIG_USHORT __kconfig __weak;
20 extern int CONFIG_INT __kconfig __weak;
21 extern uint64_t CONFIG_ULONG __kconfig __weak;
22 extern const char CONFIG_STR[8] __kconfig __weak;
23 extern uint64_t CONFIG_MISSING __kconfig __weak;
24 
25 uint64_t kern_ver = -1;
26 uint64_t unkn_virt_val = -1;
27 uint64_t bpf_syscall = -1;
28 uint64_t tristate_val = -1;
29 uint64_t bool_val = -1;
30 uint64_t char_val = -1;
31 uint64_t ushort_val = -1;
32 uint64_t int_val = -1;
33 uint64_t ulong_val = -1;
34 char str_val[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
35 uint64_t missing_val = -1;
36 
37 SEC("raw_tp/sys_enter")
handle_sys_enter(struct pt_regs * ctx)38 int handle_sys_enter(struct pt_regs *ctx)
39 {
40 	int i;
41 
42 	kern_ver = LINUX_KERNEL_VERSION;
43 	unkn_virt_val = LINUX_UNKNOWN_VIRTUAL_EXTERN;
44 	bpf_syscall = CONFIG_BPF_SYSCALL;
45 	tristate_val = CONFIG_TRISTATE;
46 	bool_val = CONFIG_BOOL;
47 	char_val = CONFIG_CHAR;
48 	ushort_val = CONFIG_USHORT;
49 	int_val = CONFIG_INT;
50 	ulong_val = CONFIG_ULONG;
51 
52 	for (i = 0; i < sizeof(CONFIG_STR); i++) {
53 		str_val[i] = CONFIG_STR[i];
54 	}
55 
56 	if (CONFIG_MISSING)
57 		/* invalid, but dead code - never executed */
58 		missing_val = bpf_missing_helper(ctx, 123);
59 	else
60 		missing_val = 0xDEADC0DE;
61 
62 	return 0;
63 }
64 
65 char _license[] SEC("license") = "GPL";
66