Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright 2018, Breno Leitao, Gustavo Romero, IBM Corp.
0004  *
0005  * This test raises a SIGUSR1 signal, and toggle the MSR[TS]
0006  * fields at the signal handler. With MSR[TS] being set, the kernel will
0007  * force a recheckpoint, which may cause a segfault when returning to
0008  * user space. Since the test needs to re-run, the segfault needs to be
0009  * caught and handled.
0010  *
0011  * In order to continue the test even after a segfault, the context is
0012  * saved prior to the signal being raised, and it is restored when there is
0013  * a segmentation fault. This happens for COUNT_MAX times.
0014  *
0015  * This test never fails (as returning EXIT_FAILURE). It either succeeds,
0016  * or crash the kernel (on a buggy kernel).
0017  */
0018 
0019 #define _GNU_SOURCE
0020 #include <stdio.h>
0021 #include <stdlib.h>
0022 #include <signal.h>
0023 #include <string.h>
0024 #include <ucontext.h>
0025 #include <unistd.h>
0026 #include <sys/mman.h>
0027 
0028 #include "tm.h"
0029 #include "utils.h"
0030 #include "reg.h"
0031 
0032 #define COUNT_MAX       5000        /* Number of interactions */
0033 
0034 /*
0035  * This test only runs on 64 bits system. Unsetting MSR_TS_S to avoid
0036  * compilation issue on 32 bits system. There is no side effect, since the
0037  * whole test will be skipped if it is not running on 64 bits system.
0038  */
0039 #ifndef __powerpc64__
0040 #undef  MSR_TS_S
0041 #define MSR_TS_S    0
0042 #endif
0043 
0044 /* Setting contexts because the test will crash and we want to recover */
0045 ucontext_t init_context;
0046 
0047 /* count is changed in the signal handler, so it must be volatile */
0048 static volatile int count;
0049 
0050 void usr_signal_handler(int signo, siginfo_t *si, void *uc)
0051 {
0052     ucontext_t *ucp = uc;
0053     int ret;
0054 
0055     /*
0056      * Allocating memory in a signal handler, and never freeing it on
0057      * purpose, forcing the heap increase, so, the memory leak is what
0058      * we want here.
0059      */
0060     ucp->uc_link = mmap(NULL, sizeof(ucontext_t),
0061                 PROT_READ | PROT_WRITE,
0062                 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
0063     if (ucp->uc_link == (void *)-1) {
0064         perror("Mmap failed");
0065         exit(-1);
0066     }
0067 
0068     /* Forcing the page to be allocated in a page fault */
0069     ret = madvise(ucp->uc_link, sizeof(ucontext_t), MADV_DONTNEED);
0070     if (ret) {
0071         perror("madvise failed");
0072         exit(-1);
0073     }
0074 
0075     memcpy(&ucp->uc_link->uc_mcontext, &ucp->uc_mcontext,
0076         sizeof(ucp->uc_mcontext));
0077 
0078     /* Forcing to enable MSR[TM] */
0079     UCONTEXT_MSR(ucp) |= MSR_TS_S;
0080 
0081     /*
0082      * A fork inside a signal handler seems to be more efficient than a
0083      * fork() prior to the signal being raised.
0084      */
0085     if (fork() == 0) {
0086         /*
0087          * Both child and parent will return, but, child returns
0088          * with count set so it will exit in the next segfault.
0089          * Parent will continue to loop.
0090          */
0091         count = COUNT_MAX;
0092     }
0093 
0094     /*
0095      * If the change above does not hit the bug, it will cause a
0096      * segmentation fault, since the ck structures are NULL.
0097      */
0098 }
0099 
0100 void seg_signal_handler(int signo, siginfo_t *si, void *uc)
0101 {
0102     count++;
0103 
0104     /* Reexecute the test */
0105     setcontext(&init_context);
0106 }
0107 
0108 void tm_trap_test(void)
0109 {
0110     struct sigaction usr_sa, seg_sa;
0111     stack_t ss;
0112 
0113     usr_sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
0114     usr_sa.sa_sigaction = usr_signal_handler;
0115 
0116     seg_sa.sa_flags = SA_SIGINFO;
0117     seg_sa.sa_sigaction = seg_signal_handler;
0118 
0119     /*
0120      * Set initial context. Will get back here from
0121      * seg_signal_handler()
0122      */
0123     getcontext(&init_context);
0124 
0125     while (count < COUNT_MAX) {
0126         /* Allocated an alternative signal stack area */
0127         ss.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE,
0128                 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
0129         ss.ss_size = SIGSTKSZ;
0130         ss.ss_flags = 0;
0131 
0132         if (ss.ss_sp == (void *)-1) {
0133             perror("mmap error\n");
0134             exit(-1);
0135         }
0136 
0137         /* Force the allocation through a page fault */
0138         if (madvise(ss.ss_sp, SIGSTKSZ, MADV_DONTNEED)) {
0139             perror("madvise\n");
0140             exit(-1);
0141         }
0142 
0143         /*
0144          * Setting an alternative stack to generate a page fault when
0145          * the signal is raised.
0146          */
0147         if (sigaltstack(&ss, NULL)) {
0148             perror("sigaltstack\n");
0149             exit(-1);
0150         }
0151 
0152         /* The signal handler will enable MSR_TS */
0153         sigaction(SIGUSR1, &usr_sa, NULL);
0154         /* If it does not crash, it might segfault, avoid it to retest */
0155         sigaction(SIGSEGV, &seg_sa, NULL);
0156 
0157         raise(SIGUSR1);
0158         count++;
0159     }
0160 }
0161 
0162 int tm_signal_context_force_tm(void)
0163 {
0164     SKIP_IF(!have_htm());
0165     /*
0166      * Skipping if not running on 64 bits system, since I think it is
0167      * not possible to set mcontext's [MSR] with TS, due to it being 32
0168      * bits.
0169      */
0170     SKIP_IF(!is_ppc64le());
0171 
0172     tm_trap_test();
0173 
0174     return EXIT_SUCCESS;
0175 }
0176 
0177 int main(int argc, char **argv)
0178 {
0179     test_harness(tm_signal_context_force_tm, "tm_signal_context_force_tm");
0180 }