"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-stack.c" (15 Mar 2019, 4708 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-stack.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.54_vs_0.09.55.

    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 
   29 /*
   30  *  stress_segvhandler()
   31  *  SEGV handler
   32  */
   33 static void MLOCKED_TEXT stress_segvhandler(int signum)
   34 {
   35     (void)signum;
   36 
   37     siglongjmp(jmp_env, 1);     /* Ugly, bounce back */
   38 }
   39 
   40 /*
   41  *  stress_stack_alloc()
   42  *  eat up stack. The default is to eat up lots of pages
   43  *  but only have 25% of the pages actually in memory
   44  *  so we a large stack with lots of pages not physically
   45  *  resident.
   46  */
   47 static void stress_stack_alloc(const args_t *args)
   48 {
   49     const size_t sz = 256 * KB;
   50     const size_t page_size4 = (args->page_size << 2);
   51     register size_t i;
   52     char data[sz];
   53 
   54     if (g_opt_flags & OPT_FLAGS_STACK_FILL) {
   55         (void)memset(data, 0, sz);
   56     } else {
   57         /* Touch 25% of the pages */
   58         for (i = 0; i < sz; i += page_size4)
   59             data[i] = 0;
   60     }
   61 
   62     inc_counter(args);
   63 
   64     if (keep_stressing())
   65         stress_stack_alloc(args);
   66 }
   67 
   68 
   69 /*
   70  *  stress_stack
   71  *  stress by forcing stack overflows
   72  */
   73 static int stress_stack(const args_t *args)
   74 {
   75     uint8_t stack[SIGSTKSZ + STACK_ALIGNMENT];
   76     pid_t pid;
   77 
   78     /*
   79      *  We need to create an alternative signal
   80      *  stack so when a segfault occurs we use
   81      *  this already allocated signal stack rather
   82      *  than try to push onto an already overflowed
   83      *  stack
   84      */
   85     (void)memset(stack, 0, sizeof(stack));
   86     if (stress_sigaltstack(stack, SIGSTKSZ) < 0)
   87         return EXIT_FAILURE;
   88 
   89 again:
   90     pid = fork();
   91     if (pid < 0) {
   92         if (g_keep_stressing_flag && (errno == EAGAIN))
   93             goto again;
   94         pr_err("%s: fork failed: errno=%d: (%s)\n",
   95             args->name, errno, strerror(errno));
   96     } else if (pid > 0) {
   97         int status, ret;
   98 
   99         /* Parent, wait for child */
  100         (void)setpgid(pid, g_pgrp);
  101         ret = waitpid(pid, &status, 0);
  102         if (ret < 0) {
  103             if (errno != EINTR)
  104                 pr_dbg("%s: waitpid(): errno=%d (%s)\n",
  105                     args->name, errno, strerror(errno));
  106             (void)kill(pid, SIGTERM);
  107             (void)kill(pid, SIGKILL);
  108             (void)waitpid(pid, &status, 0);
  109         } else if (WIFSIGNALED(status)) {
  110             pr_dbg("%s: child died: %s (instance %d)\n",
  111                 args->name, stress_strsignal(WTERMSIG(status)),
  112                 args->instance);
  113             /* If we got killed by OOM killer, re-start */
  114             if (WTERMSIG(status) == SIGKILL) {
  115                 log_system_mem_info();
  116                 pr_dbg("%s: assuming killed by OOM "
  117                     "killer, restarting again "
  118                     "(instance %d)\n", args->name, args->instance);
  119                 goto again;
  120             }
  121         }
  122     } else if (pid == 0) {
  123         char *start_ptr = shim_sbrk(0);
  124 
  125         (void)setpgid(0, g_pgrp);
  126         stress_parent_died_alarm();
  127 
  128         if (start_ptr == (void *) -1) {
  129             pr_err("%s: sbrk(0) failed: errno=%d (%s)\n",
  130                 args->name, errno, strerror(errno));
  131             _exit(EXIT_FAILURE);
  132         }
  133 
  134         /* Make sure this is killable by OOM killer */
  135         set_oom_adjustment(args->name, true);
  136 
  137         for (;;) {
  138             struct sigaction new_action;
  139             int ret;
  140 
  141             if (!keep_stressing())
  142                 break;
  143 
  144             (void)memset(&new_action, 0, sizeof new_action);
  145             new_action.sa_handler = stress_segvhandler;
  146             (void)sigemptyset(&new_action.sa_mask);
  147             new_action.sa_flags = SA_ONSTACK;
  148 
  149             if (sigaction(SIGSEGV, &new_action, NULL) < 0) {
  150                 pr_fail_err("sigaction");
  151                 return EXIT_FAILURE;
  152             }
  153             if (sigaction(SIGBUS, &new_action, NULL) < 0) {
  154                 pr_fail_err("sigaction");
  155                 return EXIT_FAILURE;
  156             }
  157             ret = sigsetjmp(jmp_env, 1);
  158             /*
  159              * We return here if we segfault, so
  160              * first check if we need to terminate
  161              */
  162             if (!keep_stressing())
  163                 break;
  164 
  165             if (ret) {
  166                 /* We end up here after handling the fault */
  167                 inc_counter(args);
  168             } else {
  169                 /* Expand the stack and cause a fault */
  170                 stress_stack_alloc(args);
  171             }
  172         }
  173         _exit(0);
  174     }
  175 
  176     return EXIT_SUCCESS;
  177 }
  178 
  179 stressor_info_t stress_stack_info = {
  180     .stressor = stress_stack,
  181     .class = CLASS_VM | CLASS_MEMORY
  182 };