"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-vm-segv.c" (15 Mar 2019, 3931 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-vm-segv.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 NOINLINE void vm_unmap_child(const size_t page_size)
   28 {
   29     size_t len = ~(size_t)0;
   30     void *addr = stress_align_address((void *)vm_unmap_child, page_size);
   31 
   32     len = len ^ (len >> 1);
   33     while (len > page_size) {
   34         munmap((void *)0, len - page_size);
   35         len >>= 1;
   36         clflush(addr);
   37         shim_clear_cache(addr, addr + 64);
   38     }
   39 }
   40 
   41 static NOINLINE void vm_unmap_self(const size_t page_size)
   42 {
   43     void *addr = stress_align_address((void *)vm_unmap_self, page_size);
   44 
   45     munmap(addr, page_size);
   46     clflush(addr);
   47     shim_clear_cache(addr, addr + 64);
   48 }
   49 
   50 /*
   51  *  stress_vm_segv()
   52  *  stress vm segv by unmapping child's address space
   53  *  and generating a segv on return because child has
   54  *  no address space on return.
   55  */
   56 static int stress_vm_segv(const args_t *args)
   57 {
   58     set_oom_adjustment(args->name, true);
   59 
   60     do {
   61         pid_t pid;
   62 
   63 again:
   64         if (!g_keep_stressing_flag)
   65             return EXIT_SUCCESS;
   66         pid = fork();
   67         if (pid < 0) {
   68             if ((errno == EAGAIN) || (errno == EINTR) || (errno == ENOMEM))
   69                 goto again;
   70             pr_err("%s: fork failed: errno=%d: (%s)\n",
   71                 args->name, errno, strerror(errno));
   72             return EXIT_NO_RESOURCE;
   73         } else if (pid > 0) {
   74             int status, ret;
   75 
   76             (void)setpgid(pid, g_pgrp);
   77             /* Parent, wait for child */
   78 
   79             ret = waitpid(pid, &status, 0);
   80             if (ret < 0)
   81                 goto kill_child;
   82 #if !defined(HAVE_PTRACE)
   83             if (WTERMSIG(status) == SIGSEGV) {
   84                 inc_counter(args);
   85                 continue;
   86             }
   87 #else
   88 
   89             (void)ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACESYSGOOD);
   90 
   91             while (keep_stressing()) {
   92                 (void)ptrace(PTRACE_SYSCALL, pid, 0, 0);
   93 
   94                 ret = waitpid(pid, &status, 0);
   95                 if (ret < 0)
   96                     goto kill_child;
   97                 if (WIFSTOPPED(status)) {
   98                     int signum = WSTOPSIG(status);
   99 
  100                     if ((signum & 0x7f) == SIGSEGV) {
  101                         inc_counter(args);
  102                         break;
  103                     }
  104                     if (signum & 0x80) 
  105                         continue;
  106                 }
  107                 if (WIFEXITED(status)) {
  108                     inc_counter(args);
  109                     break;
  110                 }
  111             }
  112 #endif
  113 kill_child:
  114             (void)kill(pid, SIGTERM);
  115             (void)kill(pid, SIGKILL);
  116             (void)waitpid(pid, &status, 0);
  117         } else if (pid == 0) {
  118             /* Child */
  119             sigset_t set;
  120             const size_t page_size = args->page_size;
  121 
  122             set_oom_adjustment(args->name, true);
  123             stress_process_dumpable(false);
  124 
  125 #if defined(HAVE_PTRACE)
  126             (void)ptrace(PTRACE_TRACEME);
  127             kill(getpid(), SIGSTOP);
  128 #endif
  129             (void)sigemptyset(&set);
  130             (void)sigaddset(&set, SIGSEGV);
  131             (void)sigprocmask(SIG_BLOCK, &set, NULL);
  132 
  133             /*
  134              *  Try to ummap the child's address space, should cause
  135              *  a SIGSEGV at some point..
  136              */
  137             vm_unmap_child(page_size);
  138 
  139             /*
  140              *  That failed, so try unmapping this function
  141              */
  142             vm_unmap_self(page_size);
  143 
  144             /* No luck, well that's unexpected.. */
  145             _exit(EXIT_FAILURE);
  146         }
  147     } while (keep_stressing());
  148 
  149     return EXIT_SUCCESS;
  150 }
  151 
  152 stressor_info_t stress_vm_segv_info = {
  153     .stressor = stress_vm_segv,
  154     .class = CLASS_VM | CLASS_MEMORY | CLASS_OS
  155 };