"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-sigsegv.c" (15 Mar 2019, 4263 Bytes) of package /linux/privat/stress-ng-0.09.56.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stress-sigsegv.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.52_vs_0.09.54.

    1 /*
    2  * Copyright (C) 2013-2019 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  *
   24  */
   25 #include "stress-ng.h"
   26 
   27 static sigjmp_buf jmp_env;
   28 #if defined(SA_SIGINFO)
   29 static volatile void *fault_addr;
   30 static volatile int signo;
   31 static volatile int code;
   32 #endif
   33 
   34 /*
   35  *  stress_segvhandler()
   36  *  SEGV handler
   37  */
   38 #if defined(SA_SIGINFO)
   39 static void MLOCKED_TEXT stress_segvhandler(
   40     int num,
   41     siginfo_t *info,
   42     void *ucontext)
   43 {
   44     (void)num;
   45     (void)ucontext;
   46 
   47     fault_addr = info->si_addr;
   48     signo = info->si_signo;
   49     code = info->si_code;
   50 
   51     siglongjmp(jmp_env, 1);     /* Ugly, bounce back */
   52 }
   53 #else
   54 static void MLOCKED_TEXT stress_segvhandler(int signum)
   55 {
   56     (void)signum;
   57 
   58     siglongjmp(jmp_env, 1);     /* Ugly, bounce back */
   59 }
   60 #endif
   61 
   62 /*
   63  *  stress_sigsegv
   64  *  stress by generating segmentation faults by
   65  *  writing to a read only page
   66  */
   67 static int stress_sigsegv(const args_t *args)
   68 {
   69     uint8_t *ptr;
   70     NOCLOBBER int rc = EXIT_FAILURE;
   71 #if defined(SA_SIGINFO)
   72     const bool verify = (g_opt_flags & OPT_FLAGS_VERIFY);
   73 #endif
   74 
   75     /* Allocate read only page */
   76     ptr = (uint8_t *)mmap(NULL, args->page_size, PROT_READ,
   77         MAP_SHARED | MAP_ANONYMOUS, -1, 0);
   78     if (ptr == MAP_FAILED) {
   79         pr_inf("%s: mmap of shared read only page failed: %d (%s)\n",
   80             args->name, errno, strerror(errno));
   81         return EXIT_NO_RESOURCE;
   82     }
   83 
   84     for (;;) {
   85         int ret;
   86         struct sigaction action;
   87 
   88         (void)memset(&action, 0, sizeof action);
   89 #if defined(SA_SIGINFO)
   90         action.sa_sigaction = stress_segvhandler;
   91 #else
   92         action.sa_handler = stress_segvhandler;
   93 #endif
   94         (void)sigemptyset(&action.sa_mask);
   95 #if defined(SA_SIGINFO)
   96         action.sa_flags = SA_SIGINFO;
   97 #endif
   98         ret = sigaction(SIGSEGV, &action, NULL);
   99         if (ret < 0) {
  100             pr_fail("%s: sigaction SIGSEGV: errno=%d (%s)\n",
  101                 args->name, errno, strerror(errno));
  102             goto tidy;
  103         }
  104         ret = sigaction(SIGILL, &action, NULL);
  105         if (ret < 0) {
  106             pr_fail("%s: sigaction SIGILL: errno=%d (%s)\n",
  107                 args->name, errno, strerror(errno));
  108             goto tidy;
  109         }
  110         ret = sigaction(SIGBUS, &action, NULL);
  111         if (ret < 0) {
  112             pr_fail("%s: sigaction SIGBUS: errno=%d (%s)\n",
  113                 args->name, errno, strerror(errno));
  114             goto tidy;
  115         }
  116 
  117         ret = sigsetjmp(jmp_env, 1);
  118         /*
  119          * We return here if we segfault, so
  120          * first check if we need to terminate
  121          */
  122         if (!keep_stressing())
  123             break;
  124 
  125         if (ret) {
  126             /* Signal was tripped */
  127 #if defined(SA_SIGINFO)
  128             if (verify && fault_addr && fault_addr != ptr) {
  129                 pr_fail("%s: expecting fault address %p, got %p instead\n",
  130                     args->name, fault_addr, ptr);
  131             }
  132             if (verify &&
  133                 (signo != -1) &&
  134                 (signo != SIGSEGV) &&
  135                 (signo != SIGILL) &&
  136                 (signo != SIGBUS)) {
  137                 pr_fail("%s: expecting SIGSEGV/SIGILL/SIGBUS, got %s instead\n",
  138                     args->name, strsignal(signo));
  139             }
  140             if (verify && (signo == SIGBUS) && (code != SEGV_ACCERR)) {
  141                 pr_fail("%s: expecting SIGBUS si_code SEGV_ACCERR (%d), got %d insread\n",
  142                     args->name, SEGV_ACCERR, code);
  143             }
  144 #endif
  145             inc_counter(args);
  146         } else {
  147 #if defined(SA_SIGINFO)
  148             signo = -1;
  149             code = -1;
  150             fault_addr = 0;
  151 #endif
  152             /* Trip a SIGSEGV/SIGILL/SIGBUS */
  153             *ptr = 0;
  154         }
  155     }
  156     rc = EXIT_SUCCESS;
  157 tidy:
  158     (void)munmap((void *)ptr, args->page_size);
  159 
  160     return rc;
  161 
  162 }
  163 
  164 stressor_info_t stress_sigsegv_info = {
  165     .stressor = stress_sigsegv,
  166     .class = CLASS_INTERRUPT | CLASS_OS
  167 };