Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/testing/selftests: add self-test for verifying load alignment

This produces a PIE binary with a variety of p_align requirements,
suitable for verifying that the load address meets that alignment
requirement.

Signed-off-by: Chris Kennelly <ckennelly@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Fangrui Song <maskray@google.com>
Cc: Hugh Dickens <hughd@google.com>
Cc: Ian Rogers <irogers@google.com>
Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Sandeep Patil <sspatil@google.com>
Cc: Song Liu <songliubraving@fb.com>
Cc: Suren Baghdasaryan <surenb@google.com>
Link: https://lkml.kernel.org/r/20200820170541.1132271-3-ckennelly@google.com
Link: https://lkml.kernel.org/r/20200821233848.3904680-3-ckennelly@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Chris Kennelly and committed by
Linus Torvalds
206e22f0 ce81bb25

+76 -2
+1
tools/testing/selftests/exec/.gitignore
··· 7 7 execveat.path.ephemeral 8 8 execveat.ephemeral 9 9 execveat.denatured 10 + /load_address_* 10 11 /recursion-depth 11 12 xxxxxxxx* 12 13 pipe
+7 -2
tools/testing/selftests/exec/Makefile
··· 4 4 CFLAGS += -D_GNU_SOURCE 5 5 6 6 TEST_PROGS := binfmt_script non-regular 7 - TEST_GEN_PROGS := execveat 7 + TEST_GEN_PROGS := execveat load_address_4096 load_address_2097152 load_address_16777216 8 8 TEST_GEN_FILES := execveat.symlink execveat.denatured script subdir pipe 9 9 # Makefile is a run-time dependency, since it's accessed by the execveat test 10 10 TEST_FILES := Makefile ··· 27 27 $(OUTPUT)/execveat.denatured: $(OUTPUT)/execveat 28 28 cp $< $@ 29 29 chmod -x $@ 30 - 30 + $(OUTPUT)/load_address_4096: load_address.c 31 + $(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=0x1000 -pie $< -o $@ 32 + $(OUTPUT)/load_address_2097152: load_address.c 33 + $(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=0x200000 -pie $< -o $@ 34 + $(OUTPUT)/load_address_16777216: load_address.c 35 + $(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=0x1000000 -pie $< -o $@
+68
tools/testing/selftests/exec/load_address.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + #ifndef _GNU_SOURCE 3 + #define _GNU_SOURCE 4 + #endif 5 + #include <link.h> 6 + #include <stdio.h> 7 + #include <stdlib.h> 8 + 9 + struct Statistics { 10 + unsigned long long load_address; 11 + unsigned long long alignment; 12 + }; 13 + 14 + int ExtractStatistics(struct dl_phdr_info *info, size_t size, void *data) 15 + { 16 + struct Statistics *stats = (struct Statistics *) data; 17 + int i; 18 + 19 + if (info->dlpi_name != NULL && info->dlpi_name[0] != '\0') { 20 + // Ignore headers from other than the executable. 21 + return 2; 22 + } 23 + 24 + stats->load_address = (unsigned long long) info->dlpi_addr; 25 + stats->alignment = 0; 26 + 27 + for (i = 0; i < info->dlpi_phnum; i++) { 28 + if (info->dlpi_phdr[i].p_type != PT_LOAD) 29 + continue; 30 + 31 + if (info->dlpi_phdr[i].p_align > stats->alignment) 32 + stats->alignment = info->dlpi_phdr[i].p_align; 33 + } 34 + 35 + return 1; // Terminate dl_iterate_phdr. 36 + } 37 + 38 + int main(int argc, char **argv) 39 + { 40 + struct Statistics extracted; 41 + unsigned long long misalign; 42 + int ret; 43 + 44 + ret = dl_iterate_phdr(ExtractStatistics, &extracted); 45 + if (ret != 1) { 46 + fprintf(stderr, "FAILED\n"); 47 + return 1; 48 + } 49 + 50 + if (extracted.alignment == 0) { 51 + fprintf(stderr, "No alignment found\n"); 52 + return 1; 53 + } else if (extracted.alignment & (extracted.alignment - 1)) { 54 + fprintf(stderr, "Alignment is not a power of 2\n"); 55 + return 1; 56 + } 57 + 58 + misalign = extracted.load_address & (extracted.alignment - 1); 59 + if (misalign) { 60 + printf("alignment = %llu, load_address = %llu\n", 61 + extracted.alignment, extracted.load_address); 62 + fprintf(stderr, "FAILED\n"); 63 + return 1; 64 + } 65 + 66 + fprintf(stderr, "PASS\n"); 67 + return 0; 68 + }