From: Alexei Starovoitov libbpf maps all max_entries pages of an arena and never maps it at address 0. No test maps an arena differently. Thus the checks from "bpf: Refuse an arena mmap() longer than max_entries pages" and "bpf: Refuse to map an arena at address 0" have no test. Test that a longer mmap() fails with E2BIG, and that an mmap() at address 0 fails with EINVAL. Signed-off-by: Alexei Starovoitov --- .../selftests/bpf/prog_tests/arena_mmap.c | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_mmap.c diff --git a/tools/testing/selftests/bpf/prog_tests/arena_mmap.c b/tools/testing/selftests/bpf/prog_tests/arena_mmap.c new file mode 100644 index 000000000000..2babc9ecf182 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/arena_mmap.c @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +#define ARENA_PAGES 8 + +static void test_refused(int pages, int flags, int err) +{ + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE); + void *area; + int fd; + + fd = bpf_map_create(BPF_MAP_TYPE_ARENA, "arena", 0, 0, ARENA_PAGES, &opts); + if (fd == -EOPNOTSUPP) { + test__skip(); + return; + } + if (!ASSERT_GE(fd, 0, "bpf_map_create")) + return; + + area = mmap(NULL, pages * getpagesize(), PROT_READ | PROT_WRITE, + MAP_SHARED | flags, fd, 0); + if (ASSERT_EQ(area, MAP_FAILED, "mmap")) + ASSERT_EQ(errno, err, "errno"); + else + munmap(area, pages * getpagesize()); + close(fd); +} + +void test_arena_mmap(void) +{ + if (test__start_subtest("long")) + test_refused(ARENA_PAGES + 1, 0, E2BIG); + if (test__start_subtest("address_0")) + test_refused(ARENA_PAGES, MAP_FIXED, EINVAL); +} -- 2.55.0