test_core_extern.c (1681B)
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 bool CONFIG_BPF_SYSCALL __kconfig; /* strong */ 15extern enum libbpf_tristate CONFIG_TRISTATE __kconfig __weak; 16extern bool CONFIG_BOOL __kconfig __weak; 17extern char CONFIG_CHAR __kconfig __weak; 18extern uint16_t CONFIG_USHORT __kconfig __weak; 19extern int CONFIG_INT __kconfig __weak; 20extern uint64_t CONFIG_ULONG __kconfig __weak; 21extern const char CONFIG_STR[8] __kconfig __weak; 22extern uint64_t CONFIG_MISSING __kconfig __weak; 23 24uint64_t kern_ver = -1; 25uint64_t bpf_syscall = -1; 26uint64_t tristate_val = -1; 27uint64_t bool_val = -1; 28uint64_t char_val = -1; 29uint64_t ushort_val = -1; 30uint64_t int_val = -1; 31uint64_t ulong_val = -1; 32char str_val[8] = {-1, -1, -1, -1, -1, -1, -1, -1}; 33uint64_t missing_val = -1; 34 35SEC("raw_tp/sys_enter") 36int 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 62char _license[] SEC("license") = "GPL";