1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "memory/metaspaceShared.hpp"
  27 #include "runtime/arguments.hpp"
  28 #include "runtime/os.hpp"
  29 #include "runtime/thread.hpp"
  30 #include "utilities/debug.hpp"
  31 #include "utilities/vmError.hpp"
  32 
  33 #include <sys/types.h>
  34 #include <sys/wait.h>
  35 #include <signal.h>
  36 
  37 #ifdef LINUX
  38 #include <sys/syscall.h>
  39 #include <unistd.h>
  40 #endif
  41 #ifdef AIX
  42 #include <unistd.h>
  43 #endif
  44 #ifdef BSD
  45 #include <sys/syscall.h>
  46 #include <unistd.h>
  47 #endif
  48 
  49 
  50 // handle all synchronous program error signals which may happen during error
  51 // reporting. They must be unblocked, caught, handled.
  52 
  53 static const int SIGNALS[] = { SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP }; // add more if needed
  54 static const int NUM_SIGNALS = sizeof(SIGNALS) / sizeof(int);
  55 
  56 // Space for our "saved" signal flags and handlers
  57 static int resettedSigflags[NUM_SIGNALS];
  58 static address resettedSighandler[NUM_SIGNALS];
  59 
  60 // Needed for cancelable steps.
  61 static volatile pthread_t reporter_thread_id;
  62 
  63 void VMError::reporting_started() {
  64   // record pthread id of reporter thread.
  65   reporter_thread_id = ::pthread_self();
  66 }
  67 
  68 void VMError::interrupt_reporting_thread() {
  69   // We misuse SIGILL here, but it does not really matter. We need
  70   //  a signal which is handled by crash_handler and not likely to
  71   //  occurr during error reporting itself.
  72   ::pthread_kill(reporter_thread_id, SIGILL);
  73 }
  74 
  75 static void save_signal(int idx, int sig)
  76 {
  77   struct sigaction sa;
  78   sigaction(sig, NULL, &sa);
  79   resettedSigflags[idx]   = sa.sa_flags;
  80   resettedSighandler[idx] = (sa.sa_flags & SA_SIGINFO)
  81                               ? CAST_FROM_FN_PTR(address, sa.sa_sigaction)
  82                               : CAST_FROM_FN_PTR(address, sa.sa_handler);
  83 }
  84 
  85 int VMError::get_resetted_sigflags(int sig) {
  86   for (int i = 0; i < NUM_SIGNALS; i++) {
  87     if (SIGNALS[i] == sig) {
  88       return resettedSigflags[i];
  89     }
  90   }
  91   return -1;
  92 }
  93 
  94 address VMError::get_resetted_sighandler(int sig) {
  95   for (int i = 0; i < NUM_SIGNALS; i++) {
  96     if (SIGNALS[i] == sig) {
  97       return resettedSighandler[i];
  98     }
  99   }
 100   return NULL;
 101 }
 102 
 103 static void crash_handler(int sig, siginfo_t* info, void* ucVoid) {
 104   // unmask current signal
 105   sigset_t newset;
 106   sigemptyset(&newset);
 107   sigaddset(&newset, sig);
 108   // also unmask other synchronous signals
 109   for (int i = 0; i < NUM_SIGNALS; i++) {
 110     sigaddset(&newset, SIGNALS[i]);
 111   }
 112   os::Posix::unblock_thread_signal_mask(&newset);
 113 
 114   // support safefetch faults in error handling
 115   ucontext_t* const uc = (ucontext_t*) ucVoid;
 116   address pc = (uc != NULL) ? os::Posix::ucontext_get_pc(uc) : NULL;
 117 
 118   // Correct pc for SIGILL, SIGFPE (see JDK-8176872)
 119   if (sig == SIGILL || sig == SIGFPE) {
 120     pc = (address) info->si_addr;
 121   }
 122 
 123   // Needed to make it possible to call SafeFetch.. APIs in error handling.
 124   if (uc && pc && StubRoutines::is_safefetch_fault(pc)) {
 125     os::Posix::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc));
 126     return;
 127   }
 128 
 129   // Needed because asserts may happen in error handling too.
 130 #ifdef CAN_SHOW_REGISTERS_ON_ASSERT
 131   if ((sig == SIGSEGV || sig == SIGBUS) && info != NULL && info->si_addr == g_assert_poison) {
 132     if (handle_assert_poison_fault(ucVoid, info->si_addr)) {
 133       return;
 134     }
 135   }
 136 #endif // CAN_SHOW_REGISTERS_ON_ASSERT
 137 
 138   VMError::report_and_die(NULL, sig, pc, info, ucVoid);
 139 }
 140 
 141 void VMError::reset_signal_handlers() {
 142   // install signal handlers for all synchronous program error signals
 143   sigset_t newset;
 144   sigemptyset(&newset);
 145 
 146   for (int i = 0; i < NUM_SIGNALS; i++) {
 147     save_signal(i, SIGNALS[i]);
 148     os::signal(SIGNALS[i], CAST_FROM_FN_PTR(void *, crash_handler));
 149     sigaddset(&newset, SIGNALS[i]);
 150   }
 151   os::Posix::unblock_thread_signal_mask(&newset);
 152 
 153 }
 154 
 155 // Write a hint to the stream in case siginfo relates to a segv/bus error
 156 // and the offending address points into CDS archive.
 157 void VMError::check_failing_cds_access(outputStream* st, const void* siginfo) {
 158 #if INCLUDE_CDS
 159   if (siginfo && UseSharedSpaces) {
 160     const siginfo_t* const si = (siginfo_t*)siginfo;
 161     if (si->si_signo == SIGBUS || si->si_signo == SIGSEGV) {
 162       const void* const fault_addr = si->si_addr;
 163       if (fault_addr != NULL) {
 164         if (MetaspaceShared::is_in_shared_metaspace(fault_addr)) {
 165           st->print("Error accessing class data sharing archive. "
 166             "Mapped file inaccessible during execution, possible disk/network problem.");
 167         }
 168       }
 169     }
 170   }
 171 #endif
 172 }
 173