xref: /linux/tools/testing/selftests/bpf/progs/verifier_arena_large.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 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_SIZE (1ull << 32)
13 
14 struct {
15 	__uint(type, BPF_MAP_TYPE_ARENA);
16 	__uint(map_flags, BPF_F_MMAPABLE);
17 	__uint(max_entries, ARENA_SIZE / PAGE_SIZE);
18 } arena SEC(".maps");
19 
20 SEC("syscall")
21 __success __retval(0)
22 int big_alloc1(void *ctx)
23 {
24 #if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
25 	volatile char __arena *page1, *page2, *no_page, *page3;
26 	void __arena *base;
27 
28 	page1 = base = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
29 	if (!page1)
30 		return 1;
31 	*page1 = 1;
32 	page2 = bpf_arena_alloc_pages(&arena, base + ARENA_SIZE - PAGE_SIZE,
33 				      1, NUMA_NO_NODE, 0);
34 	if (!page2)
35 		return 2;
36 	*page2 = 2;
37 	no_page = bpf_arena_alloc_pages(&arena, base + ARENA_SIZE,
38 					1, NUMA_NO_NODE, 0);
39 	if (no_page)
40 		return 3;
41 	if (*page1 != 1)
42 		return 4;
43 	if (*page2 != 2)
44 		return 5;
45 	bpf_arena_free_pages(&arena, (void __arena *)page1, 1);
46 	if (*page2 != 2)
47 		return 6;
48 	if (*page1 != 0) /* use-after-free should return 0 */
49 		return 7;
50 	page3 = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
51 	if (!page3)
52 		return 8;
53 	*page3 = 3;
54 	if (page1 != page3)
55 		return 9;
56 	if (*page2 != 2)
57 		return 10;
58 	if (*(page1 + PAGE_SIZE) != 0)
59 		return 11;
60 	if (*(page1 - PAGE_SIZE) != 0)
61 		return 12;
62 	if (*(page2 + PAGE_SIZE) != 0)
63 		return 13;
64 	if (*(page2 - PAGE_SIZE) != 0)
65 		return 14;
66 #endif
67 	return 0;
68 }
69 char _license[] SEC("license") = "GPL";
70