1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "vmlinux.h"
4 #include <bpf/bpf_helpers.h>
5 #include "bpf_misc.h"
6 
7 #ifndef __clang__
8 #pragma GCC diagnostic ignored "-Warray-bounds"
9 #endif
10 
11 char _license[] SEC("license") = "GPL";
12 
13 struct {
14 	__uint(type, BPF_MAP_TYPE_HASH);
15 	__uint(max_entries, 1);
16 	__type(key, u64);
17 	__type(value, u64);
18 } m_hash SEC(".maps");
19 
20 SEC("?raw_tp")
21 __failure __msg("R8 invalid mem access 'map_value_or_null")
jeq_infer_not_null_ptr_to_btfid(void * ctx)22 int jeq_infer_not_null_ptr_to_btfid(void *ctx)
23 {
24 	struct bpf_map *map = (struct bpf_map *)&m_hash;
25 	struct bpf_map *inner_map = map->inner_map_meta;
26 	u64 key = 0, ret = 0, *val;
27 
28 	val = bpf_map_lookup_elem(map, &key);
29 	/* Do not mark ptr as non-null if one of them is
30 	 * PTR_TO_BTF_ID (R9), reject because of invalid
31 	 * access to map value (R8).
32 	 *
33 	 * Here, we need to inline those insns to access
34 	 * R8 directly, since compiler may use other reg
35 	 * once it figures out val==inner_map.
36 	 */
37 	asm volatile("r8 = %[val];\n"
38 		     "r9 = %[inner_map];\n"
39 		     "if r8 != r9 goto +1;\n"
40 		     "%[ret] = *(u64 *)(r8 +0);\n"
41 		     : [ret] "+r"(ret)
42 		     : [inner_map] "r"(inner_map), [val] "r"(val)
43 		     : "r8", "r9");
44 
45 	return ret;
46 }
47