Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Stas Sergeev <stsp@users.sourceforge.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * test sigaltstack(SS_ONSTACK | SS_AUTODISARM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * If that succeeds, then swapcontext() can be used inside sighandler safely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <ucontext.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <alloca.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "../kselftest.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #ifndef SS_AUTODISARM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define SS_AUTODISARM  (1U << 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static void *sstack, *ustack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static ucontext_t uc, sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static const char *msg = "[OK]\tStack preserved";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static const char *msg2 = "[FAIL]\tStack corrupted";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) struct stk_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	char msg[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	int flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) void my_usr1(int sig, siginfo_t *si, void *u)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	char *aa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	stack_t stk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct stk_data *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #if __s390x__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	register unsigned long sp asm("%15");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	register unsigned long sp asm("sp");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (sp < (unsigned long)sstack ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			sp >= (unsigned long)sstack + SIGSTKSZ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		ksft_exit_fail_msg("SP is not on sigaltstack\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	/* put some data on stack. other sighandler will try to overwrite it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	aa = alloca(1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	assert(aa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	p = (struct stk_data *)(aa + 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	strcpy(p->msg, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	p->flag = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	ksft_print_msg("[RUN]\tsignal USR1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	err = sigaltstack(NULL, &stk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (stk.ss_flags != SS_DISABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		ksft_test_result_fail("tss_flags=%x, should be SS_DISABLE\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 				stk.ss_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		ksft_test_result_pass(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 				"sigaltstack is disabled in sighandler\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	swapcontext(&sc, &uc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	ksft_print_msg("%s\n", p->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	if (!p->flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		ksft_exit_fail_msg("[RUN]\tAborting\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) void my_usr2(int sig, siginfo_t *si, void *u)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	char *aa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	struct stk_data *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	ksft_print_msg("[RUN]\tsignal USR2\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	aa = alloca(1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	/* dont run valgrind on this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	/* try to find the data stored by previous sighandler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	p = memmem(aa, 1024, msg, strlen(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		ksft_test_result_fail("sigaltstack re-used\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		/* corrupt the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		strcpy(p->msg, msg2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		/* tell other sighandler that his data is corrupted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		p->flag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) static void switch_fn(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	ksft_print_msg("[RUN]\tswitched to user ctx\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	raise(SIGUSR2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	setcontext(&sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int main(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct sigaction act;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	stack_t stk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	ksft_print_header();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	ksft_set_plan(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	sigemptyset(&act.sa_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	act.sa_flags = SA_ONSTACK | SA_SIGINFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	act.sa_sigaction = my_usr1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	sigaction(SIGUSR1, &act, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	act.sa_sigaction = my_usr2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	sigaction(SIGUSR2, &act, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	sstack = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		      MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (sstack == MAP_FAILED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		ksft_exit_fail_msg("mmap() - %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	err = sigaltstack(NULL, &stk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (stk.ss_flags == SS_DISABLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		ksft_test_result_pass(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 				"Initial sigaltstack state was SS_DISABLE\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		ksft_exit_fail_msg("Initial sigaltstack state was %x; "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		       "should have been SS_DISABLE\n", stk.ss_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	stk.ss_sp = sstack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	stk.ss_size = SIGSTKSZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	stk.ss_flags = SS_ONSTACK | SS_AUTODISARM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	err = sigaltstack(&stk, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		if (errno == EINVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			ksft_test_result_skip(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 				"[NOTE]\tThe running kernel doesn't support SS_AUTODISARM\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			 * If test cases for the !SS_AUTODISARM variant were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			 * added, we could still run them.  We don't have any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			 * test cases like that yet, so just exit and report
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			 * success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			ksft_exit_fail_msg(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				"sigaltstack(SS_ONSTACK | SS_AUTODISARM)  %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 					strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	ustack = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		      MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (ustack == MAP_FAILED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		ksft_exit_fail_msg("mmap() - %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	getcontext(&uc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	uc.uc_link = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	uc.uc_stack.ss_sp = ustack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	uc.uc_stack.ss_size = SIGSTKSZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	makecontext(&uc, switch_fn, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	raise(SIGUSR1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	err = sigaltstack(NULL, &stk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (stk.ss_flags != SS_AUTODISARM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		ksft_exit_fail_msg("ss_flags=%x, should be SS_AUTODISARM\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 				stk.ss_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	ksft_test_result_pass(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			"sigaltstack is still SS_AUTODISARM after signal\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	ksft_exit_pass();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }