1*9e1a3ce0SKees Cook // SPDX-License-Identifier: GPL-2.0-only
2*9e1a3ce0SKees Cook #include <kunit/test.h>
3*9e1a3ce0SKees Cook
total_mapping_size_test(struct kunit * test)4*9e1a3ce0SKees Cook static void total_mapping_size_test(struct kunit *test)
5*9e1a3ce0SKees Cook {
6*9e1a3ce0SKees Cook struct elf_phdr empty[] = {
7*9e1a3ce0SKees Cook { .p_type = PT_LOAD, .p_vaddr = 0, .p_memsz = 0, },
8*9e1a3ce0SKees Cook { .p_type = PT_INTERP, .p_vaddr = 10, .p_memsz = 999999, },
9*9e1a3ce0SKees Cook };
10*9e1a3ce0SKees Cook /*
11*9e1a3ce0SKees Cook * readelf -lW /bin/mount | grep '^ .*0x0' | awk '{print "\t\t{ .p_type = PT_" \
12*9e1a3ce0SKees Cook * $1 ", .p_vaddr = " $3 ", .p_memsz = " $6 ", },"}'
13*9e1a3ce0SKees Cook */
14*9e1a3ce0SKees Cook struct elf_phdr mount[] = {
15*9e1a3ce0SKees Cook { .p_type = PT_PHDR, .p_vaddr = 0x00000040, .p_memsz = 0x0002d8, },
16*9e1a3ce0SKees Cook { .p_type = PT_INTERP, .p_vaddr = 0x00000318, .p_memsz = 0x00001c, },
17*9e1a3ce0SKees Cook { .p_type = PT_LOAD, .p_vaddr = 0x00000000, .p_memsz = 0x0033a8, },
18*9e1a3ce0SKees Cook { .p_type = PT_LOAD, .p_vaddr = 0x00004000, .p_memsz = 0x005c91, },
19*9e1a3ce0SKees Cook { .p_type = PT_LOAD, .p_vaddr = 0x0000a000, .p_memsz = 0x0022f8, },
20*9e1a3ce0SKees Cook { .p_type = PT_LOAD, .p_vaddr = 0x0000d330, .p_memsz = 0x000d40, },
21*9e1a3ce0SKees Cook { .p_type = PT_DYNAMIC, .p_vaddr = 0x0000d928, .p_memsz = 0x000200, },
22*9e1a3ce0SKees Cook { .p_type = PT_NOTE, .p_vaddr = 0x00000338, .p_memsz = 0x000030, },
23*9e1a3ce0SKees Cook { .p_type = PT_NOTE, .p_vaddr = 0x00000368, .p_memsz = 0x000044, },
24*9e1a3ce0SKees Cook { .p_type = PT_GNU_PROPERTY, .p_vaddr = 0x00000338, .p_memsz = 0x000030, },
25*9e1a3ce0SKees Cook { .p_type = PT_GNU_EH_FRAME, .p_vaddr = 0x0000b490, .p_memsz = 0x0001ec, },
26*9e1a3ce0SKees Cook { .p_type = PT_GNU_STACK, .p_vaddr = 0x00000000, .p_memsz = 0x000000, },
27*9e1a3ce0SKees Cook { .p_type = PT_GNU_RELRO, .p_vaddr = 0x0000d330, .p_memsz = 0x000cd0, },
28*9e1a3ce0SKees Cook };
29*9e1a3ce0SKees Cook size_t mount_size = 0xE070;
30*9e1a3ce0SKees Cook /* https://lore.kernel.org/linux-fsdevel/YfF18Dy85mCntXrx@fractal.localdomain */
31*9e1a3ce0SKees Cook struct elf_phdr unordered[] = {
32*9e1a3ce0SKees Cook { .p_type = PT_LOAD, .p_vaddr = 0x00000000, .p_memsz = 0x0033a8, },
33*9e1a3ce0SKees Cook { .p_type = PT_LOAD, .p_vaddr = 0x0000d330, .p_memsz = 0x000d40, },
34*9e1a3ce0SKees Cook { .p_type = PT_LOAD, .p_vaddr = 0x00004000, .p_memsz = 0x005c91, },
35*9e1a3ce0SKees Cook { .p_type = PT_LOAD, .p_vaddr = 0x0000a000, .p_memsz = 0x0022f8, },
36*9e1a3ce0SKees Cook };
37*9e1a3ce0SKees Cook
38*9e1a3ce0SKees Cook /* No headers, no size. */
39*9e1a3ce0SKees Cook KUNIT_EXPECT_EQ(test, total_mapping_size(NULL, 0), 0);
40*9e1a3ce0SKees Cook KUNIT_EXPECT_EQ(test, total_mapping_size(empty, 0), 0);
41*9e1a3ce0SKees Cook /* Empty headers, no size. */
42*9e1a3ce0SKees Cook KUNIT_EXPECT_EQ(test, total_mapping_size(empty, 1), 0);
43*9e1a3ce0SKees Cook /* No PT_LOAD headers, no size. */
44*9e1a3ce0SKees Cook KUNIT_EXPECT_EQ(test, total_mapping_size(&empty[1], 1), 0);
45*9e1a3ce0SKees Cook /* Empty PT_LOAD and non-PT_LOAD headers, no size. */
46*9e1a3ce0SKees Cook KUNIT_EXPECT_EQ(test, total_mapping_size(empty, 2), 0);
47*9e1a3ce0SKees Cook
48*9e1a3ce0SKees Cook /* Normal set of PT_LOADS, and expected size. */
49*9e1a3ce0SKees Cook KUNIT_EXPECT_EQ(test, total_mapping_size(mount, ARRAY_SIZE(mount)), mount_size);
50*9e1a3ce0SKees Cook /* Unordered PT_LOADs result in same size. */
51*9e1a3ce0SKees Cook KUNIT_EXPECT_EQ(test, total_mapping_size(unordered, ARRAY_SIZE(unordered)), mount_size);
52*9e1a3ce0SKees Cook }
53*9e1a3ce0SKees Cook
54*9e1a3ce0SKees Cook static struct kunit_case binfmt_elf_test_cases[] = {
55*9e1a3ce0SKees Cook KUNIT_CASE(total_mapping_size_test),
56*9e1a3ce0SKees Cook {},
57*9e1a3ce0SKees Cook };
58*9e1a3ce0SKees Cook
59*9e1a3ce0SKees Cook static struct kunit_suite binfmt_elf_test_suite = {
60*9e1a3ce0SKees Cook .name = KBUILD_MODNAME,
61*9e1a3ce0SKees Cook .test_cases = binfmt_elf_test_cases,
62*9e1a3ce0SKees Cook };
63*9e1a3ce0SKees Cook
64*9e1a3ce0SKees Cook kunit_test_suite(binfmt_elf_test_suite);
65