1 /*
2 * Copyright (c) 1999, 2015, 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 *
812
813 const int me = (int) ::getpid();
814 const int pid = (int) si->si_pid;
815
816 if (si->si_code == SI_USER || si->si_code == SI_QUEUE) {
817 if (IS_VALID_PID(pid) && pid != me) {
818 os->print(", sent from pid: %d (uid: %d)", pid, (int) si->si_uid);
819 }
820 } else if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
821 sig == SIGTRAP || sig == SIGFPE) {
822 os->print(", si_addr: " PTR_FORMAT, si->si_addr);
823 #ifdef SIGPOLL
824 } else if (sig == SIGPOLL) {
825 os->print(", si_band: " PTR64_FORMAT, (uint64_t)si->si_band);
826 #endif
827 } else if (sig == SIGCHLD) {
828 os->print_cr(", si_pid: %d, si_uid: %d, si_status: %d", (int) si->si_pid, si->si_uid, si->si_status);
829 }
830 }
831
832 os::WatcherThreadCrashProtection::WatcherThreadCrashProtection() {
833 assert(Thread::current()->is_Watcher_thread(), "Must be WatcherThread");
834 }
835
836 /*
837 * See the caveats for this class in os_posix.hpp
838 * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this
839 * method and returns false. If none of the signals are raised, returns true.
840 * The callback is supposed to provide the method that should be protected.
841 */
842 bool os::WatcherThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
843 sigset_t saved_sig_mask;
844
845 assert(Thread::current()->is_Watcher_thread(), "Only for WatcherThread");
846 assert(!WatcherThread::watcher_thread()->has_crash_protection(),
847 "crash_protection already set?");
848
849 // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask
850 // since on at least some systems (OS X) siglongjmp will restore the mask
851 // for the process, not the thread
|
1 /*
2 * Copyright (c) 1999, 2019, 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 *
812
813 const int me = (int) ::getpid();
814 const int pid = (int) si->si_pid;
815
816 if (si->si_code == SI_USER || si->si_code == SI_QUEUE) {
817 if (IS_VALID_PID(pid) && pid != me) {
818 os->print(", sent from pid: %d (uid: %d)", pid, (int) si->si_uid);
819 }
820 } else if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
821 sig == SIGTRAP || sig == SIGFPE) {
822 os->print(", si_addr: " PTR_FORMAT, si->si_addr);
823 #ifdef SIGPOLL
824 } else if (sig == SIGPOLL) {
825 os->print(", si_band: " PTR64_FORMAT, (uint64_t)si->si_band);
826 #endif
827 } else if (sig == SIGCHLD) {
828 os->print_cr(", si_pid: %d, si_uid: %d, si_status: %d", (int) si->si_pid, si->si_uid, si->si_status);
829 }
830 }
831
832 Thread* os::ThreadCrashProtection::_protected_thread = NULL;
833 os::ThreadCrashProtection* os::ThreadCrashProtection::_crash_protection = NULL;
834 volatile intptr_t os::ThreadCrashProtection::_crash_mux = 0;
835
836 os::ThreadCrashProtection::ThreadCrashProtection() {
837 }
838
839 /*
840 * See the caveats for this class in os_posix.hpp
841 * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this
842 * method and returns false. If none of the signals are raised, returns true.
843 * The callback is supposed to provide the method that should be protected.
844 */
845 bool os::ThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
846 sigset_t saved_sig_mask;
847
848 Thread::muxAcquire(&_crash_mux, "CrashProtection");
849
850 _protected_thread = ThreadLocalStorage::thread();
851 assert(_protected_thread != NULL, "Cannot crash protect a NULL thread");
852
853 // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask
854 // since on at least some systems (OS X) siglongjmp will restore the mask
855 // for the process, not the thread
856 pthread_sigmask(0, NULL, &saved_sig_mask);
857 if (sigsetjmp(_jmpbuf, 0) == 0) {
858 // make sure we can see in the signal handler that we have crash protection
859 // installed
860 _crash_protection = this;
861 cb.call();
862 // and clear the crash protection
863 _crash_protection = NULL;
864 _protected_thread = NULL;
865 Thread::muxRelease(&_crash_mux);
866 return true;
867 }
868 // this happens when we siglongjmp() back
869 pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL);
870 _crash_protection = NULL;
871 _protected_thread = NULL;
872 Thread::muxRelease(&_crash_mux);
873 return false;
874 }
875
876 void os::ThreadCrashProtection::restore() {
877 assert(_crash_protection != NULL, "must have crash protection");
878 siglongjmp(_jmpbuf, 1);
879 }
880
881 void os::ThreadCrashProtection::check_crash_protection(int sig,
882 Thread* thread) {
883
884 if (thread != NULL &&
885 thread == _protected_thread &&
886 _crash_protection != NULL) {
887
888 if (sig == SIGSEGV || sig == SIGBUS) {
889 _crash_protection->restore();
890 }
891 }
892 }
893
894 os::WatcherThreadCrashProtection::WatcherThreadCrashProtection() {
895 assert(Thread::current()->is_Watcher_thread(), "Must be WatcherThread");
896 }
897
898 /*
899 * See the caveats for this class in os_posix.hpp
900 * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this
901 * method and returns false. If none of the signals are raised, returns true.
902 * The callback is supposed to provide the method that should be protected.
903 */
904 bool os::WatcherThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
905 sigset_t saved_sig_mask;
906
907 assert(Thread::current()->is_Watcher_thread(), "Only for WatcherThread");
908 assert(!WatcherThread::watcher_thread()->has_crash_protection(),
909 "crash_protection already set?");
910
911 // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask
912 // since on at least some systems (OS X) siglongjmp will restore the mask
913 // for the process, not the thread
|