1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
8 #include "bpf_experimental.h"
9 #include "bpf_arena_common.h"
12 __uint(type, BPF_MAP_TYPE_ARENA);
13 __uint(map_flags, BPF_F_MMAPABLE);
14 __uint(max_entries, 2); /* arena of two pages close to 32-bit boundary*/
15 __ulong(map_extra, (1ull << 44) | (~0u - __PAGE_SIZE * 2 + 1)); /* start of mmap() region */
20 int basic_alloc1(void *ctx)
22 #if defined(__BPF_FEATURE_ARENA_CAST)
23 volatile int __arena *page1, *page2, *no_page, *page3;
25 page1 = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
29 page2 = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
33 no_page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
40 bpf_arena_free_pages(&arena, (void __arena *)page2, 1);
43 if (*page2 != 0) /* use-after-free should return 0 */
45 page3 = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
59 int basic_alloc2(void *ctx)
61 #if defined(__BPF_FEATURE_ARENA_CAST)
62 volatile char __arena *page1, *page2, *page3, *page4;
64 page1 = bpf_arena_alloc_pages(&arena, NULL, 2, NUMA_NO_NODE, 0);
67 page2 = page1 + __PAGE_SIZE;
68 page3 = page1 + __PAGE_SIZE * 2;
69 page4 = page1 - __PAGE_SIZE;
82 bpf_arena_free_pages(&arena, (void __arena *)page1, 2);
95 struct bpf_arena___l {
97 } __attribute__((preserve_access_index));
100 __success __retval(0) __log_level(2)
101 int basic_alloc3(void *ctx)
103 struct bpf_arena___l *ar = (struct bpf_arena___l *)&arena;
104 volatile char __arena *pages;
106 pages = bpf_arena_alloc_pages(&ar->map, NULL, ar->map.max_entries, NUMA_NO_NODE, 0);
112 SEC("iter.s/bpf_map")
113 __success __log_level(2)
114 int iter_maps1(struct bpf_iter__bpf_map *ctx)
116 struct bpf_map *map = ctx->map;
120 bpf_arena_alloc_pages(map, NULL, map->max_entries, 0, 0);
124 SEC("iter.s/bpf_map")
125 __failure __msg("expected pointer to STRUCT bpf_map")
126 int iter_maps2(struct bpf_iter__bpf_map *ctx)
128 struct seq_file *seq = ctx->meta->seq;
130 bpf_arena_alloc_pages((void *)seq, NULL, 1, 0, 0);
134 SEC("iter.s/bpf_map")
135 __failure __msg("untrusted_ptr_bpf_map")
136 int iter_maps3(struct bpf_iter__bpf_map *ctx)
138 struct bpf_map *map = ctx->map;
142 bpf_arena_alloc_pages(map->inner_map_meta, NULL, map->max_entries, 0, 0);
146 char _license[] SEC("license") = "GPL";