selftests/bpf: Add arena test case for 4Gbyte corner case
authorAlexei Starovoitov <ast@kernel.org>
Fri, 15 Mar 2024 02:18:34 +0000 (19:18 -0700)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 15 Mar 2024 21:24:06 +0000 (14:24 -0700)
Check that 4Gbyte arena can be allocated and overflow/underflow access in
the first and the last page behaves as expected.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Stanislav Fomichev <sdf@google.com>
Link: https://lore.kernel.org/bpf/20240315021834.62988-5-alexei.starovoitov@gmail.com
tools/testing/selftests/bpf/prog_tests/verifier.c
tools/testing/selftests/bpf/progs/verifier_arena_large.c [new file with mode: 0644]

index 985273832f891c291a308d03d35d1469204f9708..c4f9f306646ed3e7918ae484c78bde4aa56d342e 100644 (file)
@@ -5,6 +5,7 @@
 #include "cap_helpers.h"
 #include "verifier_and.skel.h"
 #include "verifier_arena.skel.h"
+#include "verifier_arena_large.skel.h"
 #include "verifier_array_access.skel.h"
 #include "verifier_basic_stack.skel.h"
 #include "verifier_bitfield_write.skel.h"
@@ -120,6 +121,7 @@ static void run_tests_aux(const char *skel_name,
 
 void test_verifier_and(void)                  { RUN(verifier_and); }
 void test_verifier_arena(void)                { RUN(verifier_arena); }
+void test_verifier_arena_large(void)          { RUN(verifier_arena_large); }
 void test_verifier_basic_stack(void)          { RUN(verifier_basic_stack); }
 void test_verifier_bitfield_write(void)       { RUN(verifier_bitfield_write); }
 void test_verifier_bounds(void)               { RUN(verifier_bounds); }
diff --git a/tools/testing/selftests/bpf/progs/verifier_arena_large.c b/tools/testing/selftests/bpf/progs/verifier_arena_large.c
new file mode 100644 (file)
index 0000000..ef66ea4
--- /dev/null
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+#include "bpf_arena_common.h"
+
+#define ARENA_SIZE (1ull << 32)
+
+struct {
+       __uint(type, BPF_MAP_TYPE_ARENA);
+       __uint(map_flags, BPF_F_MMAPABLE);
+       __uint(max_entries, ARENA_SIZE / PAGE_SIZE);
+} arena SEC(".maps");
+
+SEC("syscall")
+__success __retval(0)
+int big_alloc1(void *ctx)
+{
+#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
+       volatile char __arena *page1, *page2, *no_page, *page3;
+       void __arena *base;
+
+       page1 = base = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+       if (!page1)
+               return 1;
+       *page1 = 1;
+       page2 = bpf_arena_alloc_pages(&arena, base + ARENA_SIZE - PAGE_SIZE,
+                                     1, NUMA_NO_NODE, 0);
+       if (!page2)
+               return 2;
+       *page2 = 2;
+       no_page = bpf_arena_alloc_pages(&arena, base + ARENA_SIZE,
+                                       1, NUMA_NO_NODE, 0);
+       if (no_page)
+               return 3;
+       if (*page1 != 1)
+               return 4;
+       if (*page2 != 2)
+               return 5;
+       bpf_arena_free_pages(&arena, (void __arena *)page1, 1);
+       if (*page2 != 2)
+               return 6;
+       if (*page1 != 0) /* use-after-free should return 0 */
+               return 7;
+       page3 = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+       if (!page3)
+               return 8;
+       *page3 = 3;
+       if (page1 != page3)
+               return 9;
+       if (*page2 != 2)
+               return 10;
+       if (*(page1 + PAGE_SIZE) != 0)
+               return 11;
+       if (*(page1 - PAGE_SIZE) != 0)
+               return 12;
+       if (*(page2 + PAGE_SIZE) != 0)
+               return 13;
+       if (*(page2 - PAGE_SIZE) != 0)
+               return 14;
+#endif
+       return 0;
+}
+char _license[] SEC("license") = "GPL";