A new selftests/ directory for arm compatibility testing
v2 cover A new selftests/ directory for arm compatibility testing
This series introduces the selftests/arm directory, which tests 32 and 64-bit kernel compatibility with 32-bit ELFs running on the Aarch platform. The need for this bucket of tests is that 32 bit applications built on legacy ARM architecture must not break on the new Aarch64 platforms and the 64-bit kernel. The kernel must emulate the data structures, system calls and the registers according to Aarch32, when running a 32-bit process; this directory fills that testing requirement.
One may find similarity between this directory and selftests/arm64; it is advisable to refer to that since a lot has been copied from there itself.
The mm directory includes a test for checking 4GB limit of the virtual address space of a process.
The signal directory contains two tests, following a common theme: mangle with arm_cpsr, dumped by the kernel to user space while invoking the signal handler; kernel must spot this illegal attempt and terminate the program by SEGV.
The elf directory includes a test for checking the 32-bit status of the ELF.
The series has been tested on 6.9-rc5, on Aarch64 platform. Testing remains to be done on Aaarch32.
Changes in v2:
- Formatting changes
- Add .gitignore files and config file
Dev Jain (4):
selftests/arm: Add mm test
selftests/arm: Add signal tests selftests/arm: Add elf test
selftests: Add build infrastructure along with README
tools/testing/selftests/Makefile | 1 + tools/testing/selftests/arm/Makefile | 57 ++++ tools/testing/selftests/arm/README | 31 +++ tools/testing/selftests/arm/config | 1 + tools/testing/selftests/arm/elf/.gitignore | 2 + tools/testing/selftests/arm/elf/Makefile | 6 + tools/testing/selftests/arm/elf/parse_elf.c | 74 +++++ tools/testing/selftests/arm/mm/.gitignore | 2 + tools/testing/selftests/arm/mm/Makefile | 6 + tools/testing/selftests/arm/mm/compat_va.c | 92 +++++++ tools/testing/selftests/arm/signal/.gitignore | 3 + tools/testing/selftests/arm/signal/Makefile | 30 ++ .../selftests/arm/signal/test_signals.c | 27 ++ .../selftests/arm/signal/test_signals.h | 74 +++++ .../selftests/arm/signal/test_signals_utils.c | 257 ++++++++++++++++++ .../selftests/arm/signal/test_signals_utils.h | 128 +++++++++ .../signal/testcases/mangle_cpsr_aif_bits.c | 33 +++ .../mangle_cpsr_invalid_compat_toggle.c | 29 ++ 18 files changed, 853 insertions(+) create mode 100644 tools/testing/selftests/arm/Makefile create mode 100644 tools/testing/selftests/arm/README create mode 100644 tools/testing/selftests/arm/config create mode 100644 tools/testing/selftests/arm/elf/.gitignore create mode 100644 tools/testing/selftests/arm/elf/Makefile create mode 100644 tools/testing/selftests/arm/elf/parse_elf.c create mode 100644 tools/testing/selftests/arm/mm/.gitignore create mode 100644 tools/testing/selftests/arm/mm/Makefile create mode 100644 tools/testing/selftests/arm/mm/compat_va.c create mode 100644 tools/testing/selftests/arm/signal/.gitignore create mode 100644 tools/testing/selftests/arm/signal/Makefile create mode 100644 tools/testing/selftests/arm/signal/test_signals.c create mode 100644 tools/testing/selftests/arm/signal/test_signals.h create mode 100644 tools/testing/selftests/arm/signal/test_signals_utils.c create mode 100644 tools/testing/selftests/arm/signal/test_signals_utils.h create mode 100644 tools/testing/selftests/arm/signal/testcases/mangle_cpsr_aif_bits.c create mode 100644 tools/testing/selftests/arm/signal/testcases/mangle_cpsr_invalid_compat_toggle.c
Signature · 1 line
2.39.2
PATCH v2 1/4 selftests/arm: Add mm test
This patch tests the 4GB VA restriction for 32-bit processes; it is required to test the compat layer, whether the kernel knows that it is running a 32-bit process or not. Chunks are allocated until the VA gets exhausted; mmap must fail beyond 4GB. This is asserted against the VA mappings found in /proc/self/maps.
--- tools/testing/selftests/arm/mm/compat_va.c | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 tools/testing/selftests/arm/mm/compat_va.c
tools/testing/selftests/arm/mm/compat_va.c new file +92 -0
@@ -0,0 +1,92 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2024 ARM Limited +* +* Author : Dev Jain <[email protected]> +* +* Tests 4GB VA restriction for 32 bit process +*/ + +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/mman.h> + +#include <linux/sizes.h> +#include <kselftest.h> + +#define MAP_CHUNK_SIZE SZ_1M +#define NR_CHUNKS_4G (SZ_1G / MAP_CHUNK_SIZE) * 4/* prevent overflow */ + +static int validate_address_hint (void ) +{ +char *ptr; + + ptr =mmap ((void *) (1UL <<29 ),MAP_CHUNK_SIZE ,PROT_READ | +PROT_WRITE ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); + +if (ptr ==MAP_FAILED ) +return 0 ; + +return -1 ; +} + +int main (int argc,char *argv[]) +{ +char *ptr[NR_CHUNKS_4G +3 ]; +char line[1000 ]; +int chunks; +FILE *file; +int i; + +ksft_print_header (); +ksft_set_plan (1 ); + +/* try allocation beyond 4 GB */ +for (i =0 ; i <NR_CHUNKS_4G +3 ; ++i) { + ptr[i] =mmap (NULL ,MAP_CHUNK_SIZE ,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); + +if (ptr[i] ==MAP_FAILED ) { +if (validate_address_hint ()) +ksft_exit_fail_msg ("VA exhaustion failed\n" ); +break ; + } + } + + chunks = i; +if (chunks >=NR_CHUNKS_4G ) { +ksft_test_result_fail ("mmapped chunks beyond 4GB\n" ); +ksft_finished (); + } + +/* parse /proc/self/maps, confirm 32 bit VA mappings */ + file =fopen ("/proc/self/maps" ,"r" ); +if (file ==NULL ) +ksft_exit_fail_msg ("/proc/self/maps cannot be opened\n" ); + +while (fgets (line,sizeof (line), file)) { +const char *whitespace_loc, *hyphen_loc; + + hyphen_loc =strchr (line,'-' ); + whitespace_loc =strchr (line,' ' ); + +if (!(hyphen_loc && whitespace_loc)) { +ksft_test_result_skip ("Unexpected format" ); +ksft_finished (); + } + +if ((hyphen_loc - line >8 ) || + (whitespace_loc - hyphen_loc) >9 ) { +ksft_test_result_fail ("Memory map more than 32 bits\n" ); +ksft_finished (); + } + } + +for (int i =0 ; i < chunks; ++i) +munmap (ptr[i],MAP_CHUNK_SIZE ); + +ksft_test_result_pass ("Test\n" ); +ksft_finished (); +}
Signature · 1 line
2.39.2
PATCH v2 2/4 selftests/arm: Add signal tests
In reply to Dev JainThis patch introduces two signal tests, and generic test wrappers similar to selftests/arm64/signal directory, along with the mangling testcases found therein. arm_cpsr, dumped by the kernel to user space in the ucontext structure to the signal handler, is mangled with. The kernel must spot this illegal attempt and the testcases are expected to terminate via SEGV.
--- .../selftests/arm/signal/test_signals.c | 27 ++ .../selftests/arm/signal/test_signals.h | 74 +++++ .../selftests/arm/signal/test_signals_utils.c | 257 ++++++++++++++++++ .../selftests/arm/signal/test_signals_utils.h | 128 +++++++++ .../signal/testcases/mangle_cpsr_aif_bits.c | 33 +++ .../mangle_cpsr_invalid_compat_toggle.c | 29 ++ 6 files changed, 548 insertions(+) create mode 100644 tools/testing/selftests/arm/signal/test_signals.c create mode 100644 tools/testing/selftests/arm/signal/test_signals.h create mode 100644 tools/testing/selftests/arm/signal/test_signals_utils.c create mode 100644 tools/testing/selftests/arm/signal/test_signals_utils.h create mode 100644 tools/testing/selftests/arm/signal/testcases/mangle_cpsr_aif_bits.c create mode 100644 tools/testing/selftests/arm/signal/testcases/mangle_cpsr_invalid_compat_toggle.c
tools/testing/selftests/arm/signal/test_signals.c new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2024 ARM Limited +* +* Generic test wrapper for arm signal tests. +* +* Each test provides its own tde struct tdescr descriptor to link with +* this wrapper. Framework provides common helpers. +*/ +#include <kselftest.h> + +#include "test_signals.h" +#include "test_signals_utils.h" + +struct tdescr *current = &tde; + +int main (int argc,char *argv[]) +{ +ksft_print_msg ("%s :: %s\n" , current->name, current->descr); +if (test_setup (current) &&test_init (current)) { +test_run (current); +test_cleanup (current); + } +test_result (current); + +return current->result; +}
tools/testing/selftests/arm/signal/test_signals.h new file +74 -0
74 lines not on this page · raw message
tools/testing/selftests/arm/signal/test_signals_utils.c new file +257 -0
257 lines not on this page · raw message
tools/testing/selftests/arm/signal/test_signals_utils.h new file +128 -0
128 lines not on this page · raw message
tools/testing/selftests/arm/signal/testcases/mangle_cpsr_aif_bits.c new file +33 -0
@@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2024 ARM Limited +* +* Try to mangle the ucontext from inside a signal handler, mangling the +* AIF bits in an illegal manner: this attempt must be spotted by Kernel +* and the test case is expected to be terminated via SEGV. +* +*/ + +#include "test_signals_utils.h" + +static int mangle_invalid_cpsr_run (struct tdescr *td,siginfo_t *si, +ucontext_t *uc) +{ + +/* +* This config should trigger a SIGSEGV by Kernel when it checks +* the sigframe consistency in valid_user_regs() routine. +*/ + uc->uc_mcontext.arm_cpsr |=PSR_A_BIT |PSR_I_BIT |PSR_F_BIT ; + +return 1 ; +} + +struct tdescr tde = { + .sanity_disabled = true, + .name ="MANGLE_CPSR_INVALID_AIF_BITS" , + .descr ="Mangling uc_mcontext with INVALID AIF_BITS" , + .sig_trig =SIGUSR1 , + .sig_ok =SIGSEGV , + .run = mangle_invalid_cpsr_run, +};
tools/testing/selftests/arm/signal/testcases/mangle_cpsr_invalid_compat_toggle.c new file +29 -0
@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2024 ARM Limited +* +* Try to mangle the ucontext from inside a signal handler, toggling +* the execution state bit: this attempt must be spotted by Kernel and +* the test case is expected to be terminated via SEGV. +*/ + +#include "test_signals_utils.h" + +static int mangle_invalid_cpsr_run (struct tdescr *td,siginfo_t *si, +ucontext_t *uc) +{ + +/* This config should trigger a SIGSEGV by Kernel */ + uc->uc_mcontext.arm_cpsr ^=MODE32_BIT ; + +return 1 ; +} + +struct tdescr tde = { + .sanity_disabled = true, + .name ="MANGLE_CPSR_INVALID_STATE_TOGGLE" , + .descr ="Mangling uc_mcontext with INVALID STATE_TOGGLE" , + .sig_trig =SIGUSR1 , + .sig_ok =SIGSEGV , + .run = mangle_invalid_cpsr_run, +};
Signature · 1 line
2.39.2
PATCH v2 3/4 selftests/arm: Add elf test
In reply to Dev JainThis patch introduces an ELF parsing test; the 5th byte of the ELF header must be 0x01 for a 32-bit process. A basic sanity check is required to ensure that we are actually testing a 32-bit build.
--- tools/testing/selftests/arm/elf/parse_elf.c | 74 +++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 tools/testing/selftests/arm/elf/parse_elf.c
tools/testing/selftests/arm/elf/parse_elf.c new file +74 -0
@@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2024 ARM Limited +* +* Author : Dev Jain <[email protected]> +* +* Parse elf header to confirm 32-bit process +*/ + +#define _GNU_SOURCE +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <elf.h> +#include <stdint.h> + +#include <kselftest.h> + +/* The ELF file header. This appears at the start of every ELF file. */ + +struct elf_header { +unsigned char e_ident[16 ];/* Magic number and other info */ +uint16_t e_type;/* Object file type */ +uint16_t e_machine;/* Architecture */ +uint32_t e_version;/* Object file version */ +uint64_t e_entry;/* Entry point virtual address */ +uint64_t e_phoff;/* Program header table file offset */ +uint64_t e_shoff;/* Section header table file offset */ +uint32_t e_flags;/* Processor-specific flags */ +uint16_t e_ehsize;/* ELF header size in bytes */ +uint16_t e_phentsize;/* Program header table entry size */ +uint16_t e_phnum;/* Program header table entry count */ +uint16_t e_shentsize;/* Section header table entry size */ +uint16_t e_shnum;/* Section header table entry count */ +uint16_t e_shstrndx;/* Section header string table index */ +}; + +static int read_elf_header (const char *elfFile) +{ +struct elf_header header; +FILE *file; +int ret =-1 ; + + file =fopen (elfFile,"r" ); +if (file) { + +/* store header in struct */ +fread (&header,1 ,sizeof (header), file); +fclose (file); + +/* sanity check: does it really follow ELF format */ +if (header.e_ident[0 ] ==0x7f && + header.e_ident[1 ] =='E' && + header.e_ident[2 ] =='L' && + header.e_ident[3 ] =='F' ) { +if (header.e_ident[4 ] ==0x01 ) + ret =0 ; + }else { +ksft_print_msg ("Cannot parse /proc/self/exe\n" ); + } + }else { +ksft_print_msg ("Cannot open /proc/self/exe\n" ); + } +return ret; +} + +int main (int argc,char *argv[]) +{ +ksft_print_header (); +ksft_set_plan (1 ); + +ksft_test_result (read_elf_header ("/proc/self/exe" ) ==0 ,"ELF is 32 bit\n" ); +ksft_finished (); +}
Signature · 1 line
2.39.2
PATCH v2 4/4 selftests: Add build infrastructure along with README
In reply to Dev JainAdd arm target, individual Makefile targets, and instructions to build the tests, along with .gitignore files and a config file.
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/arm/Makefile | 57 +++++++++++++++++++ tools/testing/selftests/arm/README | 31 ++++++++++ tools/testing/selftests/arm/config | 1 + tools/testing/selftests/arm/elf/.gitignore | 2 + tools/testing/selftests/arm/elf/Makefile | 6 ++ tools/testing/selftests/arm/mm/.gitignore | 2 + tools/testing/selftests/arm/mm/Makefile | 6 ++ tools/testing/selftests/arm/signal/.gitignore | 3 + tools/testing/selftests/arm/signal/Makefile | 30 ++++++++++ 10 files changed, 139 insertions(+) create mode 100644 tools/testing/selftests/arm/Makefile create mode 100644 tools/testing/selftests/arm/README create mode 100644 tools/testing/selftests/arm/config create mode 100644 tools/testing/selftests/arm/elf/.gitignore create mode 100644 tools/testing/selftests/arm/elf/Makefile create mode 100644 tools/testing/selftests/arm/mm/.gitignore create mode 100644 tools/testing/selftests/arm/mm/Makefile create mode 100644 tools/testing/selftests/arm/signal/.gitignore create mode 100644 tools/testing/selftests/arm/signal/Makefile
tools/testing/selftests/Makefile +1 -0
@@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 TARGETS += alsa TARGETS += amd-pstate +TARGETS += arm TARGETS += arm64 TARGETS += bpf TARGETS += breakpoints
tools/testing/selftests/arm/Makefile new file +57 -0
57 lines not on this page · raw message
tools/testing/selftests/arm/README new file +31 -0
@@ -0,0 +1,31 @@ +KSelfTest ARM +=============== + +- This is a series of compatibility tests, wherein the source files are + built statically into a 32 bit ELF; they should pass on both 32 and 64 + bit kernels. They are not built or run but just skipped completely when + env-variable ARCH is found to be different than 'arm64' or 'arm' and + `uname -m` reports other than 'aarch64', 'armv7l' or 'armv8l'. + +- Please ensure that the test kernel is built with CONFIG_COMPAT enabled. + +- Holding true the above, ARM KSFT tests can be run within the KSelfTest + framework using standard Linux top-level-makefile targets. Please set + $(CROSS_COMPILE) to 'arm-linux-gnueabi-' or 'arm-linux-gnueabihf-'. + + $ make TARGETS=arm kselftest-clean + $ make $(CROSS_COMPILE) TARGETS=arm kselftest + + or + + $ make $(CROSS_COMPILE) -C tools/testing/selftests TARGETS=arm \ + INSTALL_PATH=<your-installation-path> install + + or, alternatively, only specific arm/ subtargets can be picked: + + $ make $(CROSS_COMPILE) -C tools/testing/selftests TARGETS=arm \ + ARM_SUBTARGETS="signal" INSTALL_PATH=<your-installation-path> \ + install + + Further details on building and running KFST can be found in: + Documentation/dev-tools/kselftest.rst
tools/testing/selftests/arm/config new file +1 -0
@@ -0,0 +1 @@ +CONFIG_COMPAT=y
tools/testing/selftests/arm/elf/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +parse_elf
tools/testing/selftests/arm/elf/Makefile new file +6 -0
@@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2024 ARM Limited + +TEST_GEN_PROGS := parse_elf + +include ../../lib.mk
tools/testing/selftests/arm/mm/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +compat_va
tools/testing/selftests/arm/mm/Makefile new file +6 -0
@@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2024 ARM Limited + +TEST_GEN_PROGS := compat_va + +include ../../lib.mk
tools/testing/selftests/arm/signal/.gitignore new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +mangle_cpsr_aif_bits +mangle_cpsr_invalid_compat_toggle
tools/testing/selftests/arm/signal/Makefile new file +30 -0
@@ -0,0 +1,30 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2024 ARM Limited + +# Additional include paths needed by kselftest.h and local headers +CFLAGS += -D_GNU_SOURCE -std=gnu99 -I. + +SRCS := $(filter-out testcases/testcases.c,$(wildcard testcases/*.c)) +PROGS := $(patsubst %.c,%,$(SRCS)) + +# Generated binaries to be installed by top KSFT script +TEST_GEN_PROGS := $(notdir $(PROGS)) + +# Get Kernel headers installed and use them. + +# Including KSFT lib.mk here will also mangle the TEST_GEN_PROGS list +# to account for any OUTPUT target-dirs optionally provided by +# the toplevel makefile +include ../../lib.mk + +$(TEST_GEN_PROGS): $(PROGS) + cp $(PROGS) $(OUTPUT)/ + +# Common test-unit targets to build common-layout test-cases executables +# Needs secondary expansion to properly include the testcase c-file in pre-reqs +COMMON_SOURCES := test_signals.c test_signals_utils.c +COMMON_HEADERS := test_signals.h test_signals_utils.h + +.SECONDEXPANSION: +$(PROGS): [email protected] ${COMMON_SOURCES} ${COMMON_HEADERS} + $(CC) $(CFLAGS) ${@}.c ${COMMON_SOURCES} -o $@
Signature · 1 line
2.39.2
Quoted 10 lines · Dev Jain
This series introduces the selftests/arm directory, which tests 32 and 64-bit kernel compatibility with 32-bit ELFs running on the Aarch platform. The need for this bucket of tests is that 32 bit applications built on legacy ARM architecture must not break on the new Aarch64 platforms and the 64-bit kernel. The kernel must emulate the data structures, system calls and the registers according to Aarch32, when running a 32-bit process; this directory fills that testing requirement.
One may find similarity between this directory and selftests/arm64; it is advisable to refer to that since a lot has been copied from there itself.
Isn't this going to be difficult to maintain if we have two divergent copies of the same stuff? From a very quick skim, a bunch of the signals stuff is idential to what we have on arm64...
Will
Quoted 9 lines · Will Deacon
Quoted 8 lines · Dev Jain
This series introduces the selftests/arm directory, which tests 32 and 64-bit kernel compatibility with 32-bit ELFs running on the Aarch platform. The need for this bucket of tests is that 32 bit applications built on legacy ARM architecture must not break on the new Aarch64 platforms and the 64-bit kernel. The kernel must emulate the data structures, system calls and the registers according to Aarch32, when running a 32-bit process; this directory fills that testing requirement.
Quoted 6 lines
Quoted 2 lines
One may find similarity between this directory and selftests/arm64; it is advisable to refer to that since a lot has been copied from there itself.
Isn't this going to be difficult to maintain if we have two divergent copies of the same stuff? From a very quick skim, a bunch of the signals stuff is idential to what we have on arm64...
Agreed.
Why don't we follow what x86 suite has been doing? Compile tests for both arm and arm64, and add conditionals in the tests based on architecture.
If someone has objection that the suite name is arm64, just rename it to arm which would be a generic name.
Quoted 3 lines
Will
Signature · 2 lines
BR, Muhammad Usama Anjum
Quoted 6 lines · Muhammad Usama Anjum
Quoted 3 lines · Will Deacon
Isn't this going to be difficult to maintain if we have two divergent copies of the same stuff? From a very quick skim, a bunch of the signals stuff is idential to what we have on arm64...
Quoted 7 lines
Agreed.
Why don't we follow what x86 suite has been doing? Compile tests for both arm and arm64, and add conditionals in the tests based on architecture.
If someone has objection that the suite name is arm64, just rename it to arm which would be a generic name.
Given that we haven't done that for any other arm/arm64 stuff I suspect that specific approach would lead to some combination of confusion and people just plain missing the fact that the 32 bit tests are there. It's just not the idiom that's been used for handling things elsewhere so it feels like asking for people to make mistakes. With the signal tests (which seem like the only bit that's really shared here) it's mostly just some of the library code that we want to duplicate, the actual test set for 32 bit will be different.
What some of the other testsuites do to pull in other tests as libraries is to just reference the source files (KVM does this with rseq IIRC). We could do that here and try to refactor the framework code for the signal tests so that it can be built for both 64 and 32 bit, then have the 32 bit Makefile pull in the shared code from the 64 bit directory. I'm not sure if it's worth the effort or not - there is some obvious duplication but a good chunk of the arm64 code is feature detection and handling of the extensible signal context neither of which apply for 32 bit.
If we are pulling stuff out it might even make sense to have something that other non-arm architectures can use since I'm not sure how much of the framework code that can be shared is even arm specific.