xref: /linux/tools/testing/selftests/bpf/progs/verifier_arena_globals2.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3 
4 #define BPF_NO_KFUNC_PROTOTYPES
5 #include <vmlinux.h>
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_tracing.h>
8 #include "bpf_misc.h"
9 #include "bpf_experimental.h"
10 #include "bpf_arena_common.h"
11 
12 #define ARENA_PAGES (32)
13 
14 struct {
15 	__uint(type, BPF_MAP_TYPE_ARENA);
16 	__uint(map_flags, BPF_F_MMAPABLE);
17 	__uint(max_entries, ARENA_PAGES);
18 #ifdef __TARGET_ARCH_arm64
19 	__ulong(map_extra, (1ull << 32) | (~0u - __PAGE_SIZE * ARENA_PAGES + 1));
20 #else
21 	__ulong(map_extra, (1ull << 44) | (~0u - __PAGE_SIZE * ARENA_PAGES + 1));
22 #endif
23 } arena SEC(".maps");
24 
25 /*
26  * Fill the entire arena with global data.
27  * The offset into the arena should be 0.
28  */
29 char __arena global_data[ARENA_PAGES][PAGE_SIZE];
30 
31 SEC("syscall")
32 __success __retval(0)
33 int check_reserve2(void *ctx)
34 {
35 #if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
36 	void __arena *guard;
37 	int ret;
38 
39 	guard = (void __arena *)arena_base(&arena);
40 
41 	/* Make sure the data at offset 0 case is properly handled. */
42 	ret = bpf_arena_reserve_pages(&arena, guard, 1);
43 	if (!ret)
44 		return 1;
45 #endif
46 	return 0;
47 }
48 
49 char _license[] SEC("license") = "GPL";
50