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 */
11static int (*bpf_missing_helper)(const void *arg1, int arg2) = (void *) 999;
12
13extern int LINUX_KERNEL_VERSION __kconfig;
14extern int LINUX_UNKNOWN_VIRTUAL_EXTERN __kconfig __weak;
15extern bool CONFIG_BPF_SYSCALL __kconfig; /* strong */
16extern enum libbpf_tristate CONFIG_TRISTATE __kconfig __weak;
17extern bool CONFIG_BOOL __kconfig __weak;
18extern char CONFIG_CHAR __kconfig __weak;
19extern uint16_t CONFIG_USHORT __kconfig __weak;
20extern int CONFIG_INT __kconfig __weak;
21extern uint64_t CONFIG_ULONG __kconfig __weak;
22extern const char CONFIG_STR[8] __kconfig __weak;
23extern uint64_t CONFIG_MISSING __kconfig __weak;
24
25uint64_t kern_ver = -1;
26uint64_t unkn_virt_val = -1;
27uint64_t bpf_syscall = -1;
28uint64_t tristate_val = -1;
29uint64_t bool_val = -1;
30uint64_t char_val = -1;
31uint64_t ushort_val = -1;
32uint64_t int_val = -1;
33uint64_t ulong_val = -1;
34char str_val[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
35uint64_t missing_val = -1;
36
37SEC("raw_tp/sys_enter")
38int 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
65char _license[] SEC("license") = "GPL";
66