--- old/src/java.base/aix/native/libjsig/jsig.c 2018-03-27 11:31:02.262711585 +0200 +++ new/src/java.base/aix/native/libjsig/jsig.c 2018-03-27 11:31:02.026711587 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2015 SAP SE. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * @@ -23,89 +23,145 @@ * */ -/* CopyrightVersion 1.2 */ - /* This is a special library that should be loaded before libc & * libthread to interpose the signal handler installation functions: * sigaction(), signal(), sigset(). * Used for signal-chaining. See RFE 4381843. */ -#include #include -#include +#include #include #include -#include -#define bool int -#define true 1 -#define false 0 +#ifdef SOLARIS + #include + #include +#else + #include +#endif + +#if (__STDC_VERSION__ >= 199901L) + #include +#else + #define bool int + #define true 1 + #define false 0 +#endif + +#ifndef NSIG + #define NSIG SIGRTMAX +#endif static struct sigaction sact[NSIG]; /* saved signal handlers */ static sigset_t jvmsigs; /* Signals used by jvm. */ +static __thread bool reentry = false; /* prevent reentry deadlock (per-thread) */ /* Used to synchronize the installation of signal handlers. */ +#ifdef SOLARIS +static mutex_t mutex = DEFAULTMUTEX; +static cond_t cond = DEFAULTCV; +static thread_t tid = 0; +#else static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static pthread_t tid = 0; +#endif typedef void (*sa_handler_t)(int); typedef void (*sa_sigaction_t)(int, siginfo_t *, void *); -// signal_t is already defined on AIX. -typedef sa_handler_t (*signal_like_function_t)(int, sa_handler_t); +typedef sa_handler_t (*signal_function_t)(int, sa_handler_t); typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *); -static signal_like_function_t os_signal = 0; /* os's version of signal()/sigset() */ +static signal_function_t os_signal = 0; /* os's version of signal()/sigset() */ static sigaction_t os_sigaction = 0; /* os's version of sigaction() */ static bool jvm_signal_installing = false; static bool jvm_signal_installed = false; static void signal_lock() { +#ifdef SOLARIS + mutex_lock(&mutex); +#else pthread_mutex_lock(&mutex); +#endif /* When the jvm is installing its set of signal handlers, threads * other than the jvm thread should wait. */ if (jvm_signal_installing) { +#ifdef SOLARIS + if (tid != thr_self()) { + cond_wait(&cond, &mutex); + } +#else if (tid != pthread_self()) { pthread_cond_wait(&cond, &mutex); } +#endif } } static void signal_unlock() { +#ifdef SOLARIS + mutex_unlock(&mutex); +#else pthread_mutex_unlock(&mutex); +#endif } static sa_handler_t call_os_signal(int sig, sa_handler_t disp, bool is_sigset) { + sa_handler_t res; + if (os_signal == NULL) { if (!is_sigset) { - // Aix: call functions directly instead of dlsym'ing them. +#ifdef AIX os_signal = signal; +#else + os_signal = (signal_function_t)dlsym(RTLD_NEXT, "signal"); +#endif } else { - // Aix: call functions directly instead of dlsym'ing them. +#ifdef AIX os_signal = sigset; +#else + os_signal = (signal_function_t)dlsym(RTLD_NEXT, "sigset"); +#endif } if (os_signal == NULL) { printf("%s\n", dlerror()); exit(0); } } - return (*os_signal)(sig, disp); + /* On some OSes like macosx, the OS implementation of signal calls sigaction. + * Make sure we do not deadlock with ourself. (See JDK-8072147). */ + reentry = true; + res = (*os_signal)(sig, disp); + reentry = false; + return res; } -static void save_signal_handler(int sig, sa_handler_t disp) { +static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) { sigset_t set; sact[sig].sa_handler = disp; sigemptyset(&set); sact[sig].sa_mask = set; - sact[sig].sa_flags = 0; + if (!is_sigset) { +#ifdef SOLARIS + sact[sig].sa_flags = SA_NODEFER; + if (sig != SIGILL && sig != SIGTRAP && sig != SIGPWR) { + sact[sig].sa_flags |= SA_RESETHAND; + } +#else + sact[sig].sa_flags = 0; +#endif + } else { + sact[sig].sa_flags = 0; + } } static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) { sa_handler_t oldhandler; bool sigused; + bool sigblocked; signal_lock(); @@ -113,8 +169,18 @@ if (jvm_signal_installed && sigused) { /* jvm has installed its signal handler for this signal. */ /* Save the handler. Don't really install it. */ + if (is_sigset) { + sigblocked = sigismember(&(sact[sig].sa_mask), sig); + } oldhandler = sact[sig].sa_handler; - save_signal_handler(sig, disp); + save_signal_handler(sig, disp, is_sigset); + +#ifdef SOLARIS + if (is_sigset && sigblocked) { + /* We won't honor the SIG_HOLD request to change the signal mask */ + oldhandler = SIG_HOLD; + } +#endif signal_unlock(); return oldhandler; @@ -123,7 +189,7 @@ * handlers and save the old ones. jvm uses sigaction(). * Leave the piece here just in case. */ oldhandler = call_os_signal(sig, disp, is_sigset); - save_signal_handler(sig, oldhandler); + save_signal_handler(sig, oldhandler, is_sigset); /* Record the signals used by jvm */ sigaddset(&jvmsigs, sig); @@ -145,14 +211,22 @@ } sa_handler_t sigset(int sig, sa_handler_t disp) { +#ifdef _ALLBSD_SOURCE + printf("sigset() is not supported by BSD"); + exit(0); +#else return set_signal(sig, disp, true); +#endif } static int call_os_sigaction(int sig, const struct sigaction *act, struct sigaction *oact) { if (os_sigaction == NULL) { - // Aix: call functions directly instead of dlsym'ing them. +#ifdef AIX os_sigaction = sigaction; +#else + os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction"); +#endif if (os_sigaction == NULL) { printf("%s\n", dlerror()); exit(0); @@ -166,6 +240,10 @@ bool sigused; struct sigaction oldAct; + if (reentry) { + return call_os_sigaction(sig, act, oact); + } + signal_lock(); sigused = sigismember(&jvmsigs, sig); @@ -210,7 +288,11 @@ signal_lock(); sigemptyset(&jvmsigs); jvm_signal_installing = true; +#ifdef SOLARIS + tid = thr_self(); +#else tid = pthread_self(); +#endif signal_unlock(); } @@ -218,7 +300,11 @@ signal_lock(); jvm_signal_installed = true; jvm_signal_installing = false; +#ifdef SOLARIS + cond_broadcast(&cond); +#else pthread_cond_broadcast(&cond); +#endif signal_unlock(); } --- old/src/java.base/linux/native/libjsig/jsig.c 2018-03-27 11:31:02.866711581 +0200 +++ new/src/java.base/linux/native/libjsig/jsig.c 2018-03-27 11:31:02.642711583 +0200 @@ -1,5 +1,6 @@ /* - * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2015 SAP SE. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -22,112 +23,176 @@ * */ -/* CopyrightVersion 1.2 */ - /* This is a special library that should be loaded before libc & * libthread to interpose the signal handler installation functions: * sigaction(), signal(), sigset(). * Used for signal-chaining. See RFE 4381843. */ -#include #include -#include +#include #include #include -#include -#define bool int -#define true 1 -#define false 0 - -#define MASK(sig) ((uint64_t)1 << (sig-1)) // 0 is not a signal. -// Check whether all signals fit into jvmsigs. -1 as MASK shifts by -1. -#if (64 < NSIG-1) -#error "Not all signals can be encoded in jvmsigs. Adapt its type!" +#ifdef SOLARIS + #include + #include +#else + #include +#endif + +#if (__STDC_VERSION__ >= 199901L) + #include +#else + #define bool int + #define true 1 + #define false 0 +#endif + +#ifndef NSIG + #define NSIG SIGRTMAX #endif + static struct sigaction sact[NSIG]; /* saved signal handlers */ -static uint64_t jvmsigs = 0; /* signals used by jvm */ +static sigset_t jvmsigs; /* Signals used by jvm. */ +static __thread bool reentry = false; /* prevent reentry deadlock (per-thread) */ -/* used to synchronize the installation of signal handlers */ +/* Used to synchronize the installation of signal handlers. */ +#ifdef SOLARIS +static mutex_t mutex = DEFAULTMUTEX; +static cond_t cond = DEFAULTCV; +static thread_t tid = 0; +#else static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static pthread_t tid = 0; +#endif typedef void (*sa_handler_t)(int); typedef void (*sa_sigaction_t)(int, siginfo_t *, void *); -typedef sa_handler_t (*signal_t)(int, sa_handler_t); +typedef sa_handler_t (*signal_function_t)(int, sa_handler_t); typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *); -static signal_t os_signal = 0; /* os's version of signal()/sigset() */ +static signal_function_t os_signal = 0; /* os's version of signal()/sigset() */ static sigaction_t os_sigaction = 0; /* os's version of sigaction() */ static bool jvm_signal_installing = false; static bool jvm_signal_installed = false; static void signal_lock() { +#ifdef SOLARIS + mutex_lock(&mutex); +#else pthread_mutex_lock(&mutex); +#endif /* When the jvm is installing its set of signal handlers, threads - * other than the jvm thread should wait */ + * other than the jvm thread should wait. */ if (jvm_signal_installing) { +#ifdef SOLARIS + if (tid != thr_self()) { + cond_wait(&cond, &mutex); + } +#else if (tid != pthread_self()) { pthread_cond_wait(&cond, &mutex); } +#endif } } static void signal_unlock() { +#ifdef SOLARIS + mutex_unlock(&mutex); +#else pthread_mutex_unlock(&mutex); +#endif } static sa_handler_t call_os_signal(int sig, sa_handler_t disp, bool is_sigset) { + sa_handler_t res; + if (os_signal == NULL) { if (!is_sigset) { - os_signal = (signal_t)dlsym(RTLD_NEXT, "signal"); +#ifdef AIX + os_signal = signal; +#else + os_signal = (signal_function_t)dlsym(RTLD_NEXT, "signal"); +#endif } else { - os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset"); +#ifdef AIX + os_signal = sigset; +#else + os_signal = (signal_function_t)dlsym(RTLD_NEXT, "sigset"); +#endif } if (os_signal == NULL) { printf("%s\n", dlerror()); exit(0); } } - return (*os_signal)(sig, disp); + /* On some OSes like macosx, the OS implementation of signal calls sigaction. + * Make sure we do not deadlock with ourself. (See JDK-8072147). */ + reentry = true; + res = (*os_signal)(sig, disp); + reentry = false; + return res; } -static void save_signal_handler(int sig, sa_handler_t disp) { +static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) { sigset_t set; sact[sig].sa_handler = disp; sigemptyset(&set); sact[sig].sa_mask = set; - sact[sig].sa_flags = 0; + if (!is_sigset) { +#ifdef SOLARIS + sact[sig].sa_flags = SA_NODEFER; + if (sig != SIGILL && sig != SIGTRAP && sig != SIGPWR) { + sact[sig].sa_flags |= SA_RESETHAND; + } +#else + sact[sig].sa_flags = 0; +#endif + } else { + sact[sig].sa_flags = 0; + } } static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) { sa_handler_t oldhandler; bool sigused; + bool sigblocked; signal_lock(); - sigused = (sig < NSIG) && ((MASK(sig) & jvmsigs) != 0); + sigused = sigismember(&jvmsigs, sig); if (jvm_signal_installed && sigused) { /* jvm has installed its signal handler for this signal. */ /* Save the handler. Don't really install it. */ + if (is_sigset) { + sigblocked = sigismember(&(sact[sig].sa_mask), sig); + } oldhandler = sact[sig].sa_handler; - save_signal_handler(sig, disp); + save_signal_handler(sig, disp, is_sigset); + +#ifdef SOLARIS + if (is_sigset && sigblocked) { + /* We won't honor the SIG_HOLD request to change the signal mask */ + oldhandler = SIG_HOLD; + } +#endif signal_unlock(); return oldhandler; - } else if (sig < NSIG && jvm_signal_installing) { + } else if (jvm_signal_installing) { /* jvm is installing its signal handlers. Install the new * handlers and save the old ones. jvm uses sigaction(). * Leave the piece here just in case. */ oldhandler = call_os_signal(sig, disp, is_sigset); - save_signal_handler(sig, oldhandler); + save_signal_handler(sig, oldhandler, is_sigset); /* Record the signals used by jvm */ - jvmsigs |= MASK(sig); + sigaddset(&jvmsigs, sig); signal_unlock(); return oldhandler; @@ -146,13 +211,22 @@ } sa_handler_t sigset(int sig, sa_handler_t disp) { +#ifdef _ALLBSD_SOURCE + printf("sigset() is not supported by BSD"); + exit(0); +#else return set_signal(sig, disp, true); - } +#endif +} static int call_os_sigaction(int sig, const struct sigaction *act, struct sigaction *oact) { if (os_sigaction == NULL) { +#ifdef AIX + os_sigaction = sigaction; +#else os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction"); +#endif if (os_sigaction == NULL) { printf("%s\n", dlerror()); exit(0); @@ -166,9 +240,13 @@ bool sigused; struct sigaction oldAct; + if (reentry) { + return call_os_sigaction(sig, act, oact); + } + signal_lock(); - sigused = (sig < NSIG) && ((MASK(sig) & jvmsigs) != 0); + sigused = sigismember(&jvmsigs, sig); if (jvm_signal_installed && sigused) { /* jvm has installed its signal handler for this signal. */ /* Save the handler. Don't really install it. */ @@ -181,7 +259,7 @@ signal_unlock(); return 0; - } else if (sig < NSIG && jvm_signal_installing) { + } else if (jvm_signal_installing) { /* jvm is installing its signal handlers. Install the new * handlers and save the old ones. */ res = call_os_sigaction(sig, act, &oldAct); @@ -190,8 +268,8 @@ *oact = oldAct; } - /* Record the signals used by jvm */ - jvmsigs |= MASK(sig); + /* Record the signals used by jvm. */ + sigaddset(&jvmsigs, sig); signal_unlock(); return res; @@ -205,11 +283,16 @@ } } -/* The three functions for the jvm to call into */ +/* The three functions for the jvm to call into. */ void JVM_begin_signal_setting() { signal_lock(); + sigemptyset(&jvmsigs); jvm_signal_installing = true; +#ifdef SOLARIS + tid = thr_self(); +#else tid = pthread_self(); +#endif signal_unlock(); } @@ -217,13 +300,17 @@ signal_lock(); jvm_signal_installed = true; jvm_signal_installing = false; +#ifdef SOLARIS + cond_broadcast(&cond); +#else pthread_cond_broadcast(&cond); +#endif signal_unlock(); } struct sigaction *JVM_get_signal_action(int sig) { /* Does race condition make sense here? */ - if ((MASK(sig) & jvmsigs) != 0) { + if (sigismember(&jvmsigs, sig)) { return &sact[sig]; } return NULL; --- old/src/java.base/macosx/native/libjsig/jsig.c 2018-03-27 11:31:03.466711577 +0200 +++ new/src/java.base/macosx/native/libjsig/jsig.c 2018-03-27 11:31:03.234711578 +0200 @@ -1,5 +1,6 @@ /* - * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2015 SAP SE. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -22,59 +23,89 @@ * */ -/* CopyrightVersion 1.2 */ - /* This is a special library that should be loaded before libc & * libthread to interpose the signal handler installation functions: * sigaction(), signal(), sigset(). * Used for signal-chaining. See RFE 4381843. */ -#include #include -#include +#include #include #include -#include -#include -#define MASK(sig) ((uint32_t)1 << (sig-1)) // 0 is not a signal. -#if (32 < NSIG-1) -#error "Not all signals can be encoded in jvmsigs. Adapt its type!" +#ifdef SOLARIS + #include + #include +#else + #include +#endif + +#if (__STDC_VERSION__ >= 199901L) + #include +#else + #define bool int + #define true 1 + #define false 0 +#endif + +#ifndef NSIG + #define NSIG SIGRTMAX #endif + static struct sigaction sact[NSIG]; /* saved signal handlers */ -static uint32_t jvmsigs = 0; /* signals used by jvm */ +static sigset_t jvmsigs; /* Signals used by jvm. */ static __thread bool reentry = false; /* prevent reentry deadlock (per-thread) */ -/* used to synchronize the installation of signal handlers */ +/* Used to synchronize the installation of signal handlers. */ +#ifdef SOLARIS +static mutex_t mutex = DEFAULTMUTEX; +static cond_t cond = DEFAULTCV; +static thread_t tid = 0; +#else static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static pthread_t tid = 0; +#endif typedef void (*sa_handler_t)(int); typedef void (*sa_sigaction_t)(int, siginfo_t *, void *); -typedef sa_handler_t (*signal_t)(int, sa_handler_t); +typedef sa_handler_t (*signal_function_t)(int, sa_handler_t); typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *); -static signal_t os_signal = 0; /* os's version of signal()/sigset() */ +static signal_function_t os_signal = 0; /* os's version of signal()/sigset() */ static sigaction_t os_sigaction = 0; /* os's version of sigaction() */ static bool jvm_signal_installing = false; static bool jvm_signal_installed = false; static void signal_lock() { +#ifdef SOLARIS + mutex_lock(&mutex); +#else pthread_mutex_lock(&mutex); +#endif /* When the jvm is installing its set of signal handlers, threads - * other than the jvm thread should wait */ + * other than the jvm thread should wait. */ if (jvm_signal_installing) { +#ifdef SOLARIS + if (tid != thr_self()) { + cond_wait(&cond, &mutex); + } +#else if (tid != pthread_self()) { pthread_cond_wait(&cond, &mutex); } +#endif } } static void signal_unlock() { +#ifdef SOLARIS + mutex_unlock(&mutex); +#else pthread_mutex_unlock(&mutex); +#endif } static sa_handler_t call_os_signal(int sig, sa_handler_t disp, @@ -83,41 +114,73 @@ if (os_signal == NULL) { if (!is_sigset) { - os_signal = (signal_t)dlsym(RTLD_NEXT, "signal"); +#ifdef AIX + os_signal = signal; +#else + os_signal = (signal_function_t)dlsym(RTLD_NEXT, "signal"); +#endif } else { - os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset"); +#ifdef AIX + os_signal = sigset; +#else + os_signal = (signal_function_t)dlsym(RTLD_NEXT, "sigset"); +#endif } if (os_signal == NULL) { printf("%s\n", dlerror()); exit(0); } } + /* On some OSes like macosx, the OS implementation of signal calls sigaction. + * Make sure we do not deadlock with ourself. (See JDK-8072147). */ reentry = true; res = (*os_signal)(sig, disp); reentry = false; return res; } -static void save_signal_handler(int sig, sa_handler_t disp) { +static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) { sigset_t set; sact[sig].sa_handler = disp; sigemptyset(&set); sact[sig].sa_mask = set; - sact[sig].sa_flags = 0; + if (!is_sigset) { +#ifdef SOLARIS + sact[sig].sa_flags = SA_NODEFER; + if (sig != SIGILL && sig != SIGTRAP && sig != SIGPWR) { + sact[sig].sa_flags |= SA_RESETHAND; + } +#else + sact[sig].sa_flags = 0; +#endif + } else { + sact[sig].sa_flags = 0; + } } static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) { sa_handler_t oldhandler; bool sigused; + bool sigblocked; signal_lock(); - sigused = (MASK(sig) & jvmsigs) != 0; + sigused = sigismember(&jvmsigs, sig); if (jvm_signal_installed && sigused) { /* jvm has installed its signal handler for this signal. */ /* Save the handler. Don't really install it. */ + if (is_sigset) { + sigblocked = sigismember(&(sact[sig].sa_mask), sig); + } oldhandler = sact[sig].sa_handler; - save_signal_handler(sig, disp); + save_signal_handler(sig, disp, is_sigset); + +#ifdef SOLARIS + if (is_sigset && sigblocked) { + /* We won't honor the SIG_HOLD request to change the signal mask */ + oldhandler = SIG_HOLD; + } +#endif signal_unlock(); return oldhandler; @@ -126,10 +189,10 @@ * handlers and save the old ones. jvm uses sigaction(). * Leave the piece here just in case. */ oldhandler = call_os_signal(sig, disp, is_sigset); - save_signal_handler(sig, oldhandler); + save_signal_handler(sig, oldhandler, is_sigset); /* Record the signals used by jvm */ - jvmsigs |= MASK(sig); + sigaddset(&jvmsigs, sig); signal_unlock(); return oldhandler; @@ -148,14 +211,22 @@ } sa_handler_t sigset(int sig, sa_handler_t disp) { +#ifdef _ALLBSD_SOURCE printf("sigset() is not supported by BSD"); exit(0); - } +#else + return set_signal(sig, disp, true); +#endif +} static int call_os_sigaction(int sig, const struct sigaction *act, struct sigaction *oact) { if (os_sigaction == NULL) { +#ifdef AIX + os_sigaction = sigaction; +#else os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction"); +#endif if (os_sigaction == NULL) { printf("%s\n", dlerror()); exit(0); @@ -175,7 +246,7 @@ signal_lock(); - sigused = (MASK(sig) & jvmsigs) != 0; + sigused = sigismember(&jvmsigs, sig); if (jvm_signal_installed && sigused) { /* jvm has installed its signal handler for this signal. */ /* Save the handler. Don't really install it. */ @@ -197,8 +268,8 @@ *oact = oldAct; } - /* Record the signals used by jvm */ - jvmsigs |= MASK(sig); + /* Record the signals used by jvm. */ + sigaddset(&jvmsigs, sig); signal_unlock(); return res; @@ -212,11 +283,16 @@ } } -/* The three functions for the jvm to call into */ +/* The three functions for the jvm to call into. */ void JVM_begin_signal_setting() { signal_lock(); + sigemptyset(&jvmsigs); jvm_signal_installing = true; +#ifdef SOLARIS + tid = thr_self(); +#else tid = pthread_self(); +#endif signal_unlock(); } @@ -224,13 +300,17 @@ signal_lock(); jvm_signal_installed = true; jvm_signal_installing = false; +#ifdef SOLARIS + cond_broadcast(&cond); +#else pthread_cond_broadcast(&cond); +#endif signal_unlock(); } struct sigaction *JVM_get_signal_action(int sig) { /* Does race condition make sense here? */ - if ((MASK(sig) & jvmsigs) != 0) { + if (sigismember(&jvmsigs, sig)) { return &sact[sig]; } return NULL; --- old/src/java.base/solaris/native/libjsig/jsig.c 2018-03-27 11:31:04.082711572 +0200 +++ new/src/java.base/solaris/native/libjsig/jsig.c 2018-03-27 11:31:03.846711574 +0200 @@ -1,5 +1,6 @@ /* - * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2015 SAP SE. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -22,108 +23,136 @@ * */ -/* CopyrightVersion 1.2 */ - /* This is a special library that should be loaded before libc & * libthread to interpose the signal handler installation functions: * sigaction(), signal(), sigset(). * Used for signal-chaining. See RFE 4381843. */ -#include -#include -#include -#include #include -#include -#include -#include "jvm_md.h" - -#define bool int -#define true 1 -#define false 0 +#include +#include +#include -static struct sigaction *sact = (struct sigaction *)NULL; /* saved signal handlers */ -static sigset_t jvmsigs; +#ifdef SOLARIS + #include + #include +#else + #include +#endif + +#if (__STDC_VERSION__ >= 199901L) + #include +#else + #define bool int + #define true 1 + #define false 0 +#endif + +#ifndef NSIG + #define NSIG SIGRTMAX +#endif + +static struct sigaction sact[NSIG]; /* saved signal handlers */ +static sigset_t jvmsigs; /* Signals used by jvm. */ +static __thread bool reentry = false; /* prevent reentry deadlock (per-thread) */ -/* used to synchronize the installation of signal handlers */ +/* Used to synchronize the installation of signal handlers. */ +#ifdef SOLARIS static mutex_t mutex = DEFAULTMUTEX; static cond_t cond = DEFAULTCV; static thread_t tid = 0; +#else +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; +static pthread_t tid = 0; +#endif typedef void (*sa_handler_t)(int); typedef void (*sa_sigaction_t)(int, siginfo_t *, void *); -typedef sa_handler_t (*signal_t)(int, sa_handler_t); +typedef sa_handler_t (*signal_function_t)(int, sa_handler_t); typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *); -static signal_t os_signal = 0; /* os's version of signal()/sigset() */ +static signal_function_t os_signal = 0; /* os's version of signal()/sigset() */ static sigaction_t os_sigaction = 0; /* os's version of sigaction() */ static bool jvm_signal_installing = false; static bool jvm_signal_installed = false; - -/* assume called within signal_lock */ -static void allocate_sact() { - size_t maxsignum; - maxsignum = SIGRTMAX; - if (sact == NULL) { - sact = (struct sigaction *)malloc((maxsignum+1) * (size_t)sizeof(struct sigaction)); - memset(sact, 0, (maxsignum+1) * (size_t)sizeof(struct sigaction)); - } - - if (sact == NULL) { - printf("%s\n", "libjsig.so unable to allocate memory"); - exit(0); - } - - sigemptyset(&jvmsigs); -} - static void signal_lock() { +#ifdef SOLARIS mutex_lock(&mutex); +#else + pthread_mutex_lock(&mutex); +#endif /* When the jvm is installing its set of signal handlers, threads - * other than the jvm thread should wait */ + * other than the jvm thread should wait. */ if (jvm_signal_installing) { +#ifdef SOLARIS if (tid != thr_self()) { cond_wait(&cond, &mutex); } +#else + if (tid != pthread_self()) { + pthread_cond_wait(&cond, &mutex); + } +#endif } } static void signal_unlock() { +#ifdef SOLARIS mutex_unlock(&mutex); +#else + pthread_mutex_unlock(&mutex); +#endif } static sa_handler_t call_os_signal(int sig, sa_handler_t disp, bool is_sigset) { + sa_handler_t res; + if (os_signal == NULL) { if (!is_sigset) { - os_signal = (signal_t)dlsym(RTLD_NEXT, "signal"); +#ifdef AIX + os_signal = signal; +#else + os_signal = (signal_function_t)dlsym(RTLD_NEXT, "signal"); +#endif } else { - os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset"); +#ifdef AIX + os_signal = sigset; +#else + os_signal = (signal_function_t)dlsym(RTLD_NEXT, "sigset"); +#endif } if (os_signal == NULL) { printf("%s\n", dlerror()); exit(0); } } - return (*os_signal)(sig, disp); + /* On some OSes like macosx, the OS implementation of signal calls sigaction. + * Make sure we do not deadlock with ourself. (See JDK-8072147). */ + reentry = true; + res = (*os_signal)(sig, disp); + reentry = false; + return res; } static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) { sigset_t set; - if (sact == NULL) { - allocate_sact(); - } sact[sig].sa_handler = disp; sigemptyset(&set); sact[sig].sa_mask = set; if (!is_sigset) { +#ifdef SOLARIS sact[sig].sa_flags = SA_NODEFER; if (sig != SIGILL && sig != SIGTRAP && sig != SIGPWR) { sact[sig].sa_flags |= SA_RESETHAND; } +#else + sact[sig].sa_flags = 0; +#endif } else { sact[sig].sa_flags = 0; } @@ -131,26 +160,27 @@ static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) { sa_handler_t oldhandler; + bool sigused; bool sigblocked; signal_lock(); - if (sact == NULL) { - allocate_sact(); - } - if (jvm_signal_installed && sigismember(&jvmsigs, sig)) { + sigused = sigismember(&jvmsigs, sig); + if (jvm_signal_installed && sigused) { /* jvm has installed its signal handler for this signal. */ /* Save the handler. Don't really install it. */ if (is_sigset) { - /* We won't honor the SIG_HOLD request to change the signal mask */ sigblocked = sigismember(&(sact[sig].sa_mask), sig); } oldhandler = sact[sig].sa_handler; save_signal_handler(sig, disp, is_sigset); +#ifdef SOLARIS if (is_sigset && sigblocked) { + /* We won't honor the SIG_HOLD request to change the signal mask */ oldhandler = SIG_HOLD; } +#endif signal_unlock(); return oldhandler; @@ -181,13 +211,22 @@ } sa_handler_t sigset(int sig, sa_handler_t disp) { +#ifdef _ALLBSD_SOURCE + printf("sigset() is not supported by BSD"); + exit(0); +#else return set_signal(sig, disp, true); +#endif } static int call_os_sigaction(int sig, const struct sigaction *act, struct sigaction *oact) { if (os_sigaction == NULL) { +#ifdef AIX + os_sigaction = sigaction; +#else os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction"); +#endif if (os_sigaction == NULL) { printf("%s\n", dlerror()); exit(0); @@ -198,14 +237,17 @@ int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) { int res; + bool sigused; struct sigaction oldAct; + if (reentry) { + return call_os_sigaction(sig, act, oact); + } + signal_lock(); - if (sact == NULL ) { - allocate_sact(); - } - if (jvm_signal_installed && sigismember(&jvmsigs, sig)) { + sigused = sigismember(&jvmsigs, sig); + if (jvm_signal_installed && sigused) { /* jvm has installed its signal handler for this signal. */ /* Save the handler. Don't really install it. */ if (oact != NULL) { @@ -226,7 +268,7 @@ *oact = oldAct; } - /* Record the signals used by jvm */ + /* Record the signals used by jvm. */ sigaddset(&jvmsigs, sig); signal_unlock(); @@ -241,11 +283,16 @@ } } -/* The four functions for the jvm to call into */ +/* The three functions for the jvm to call into. */ void JVM_begin_signal_setting() { signal_lock(); + sigemptyset(&jvmsigs); jvm_signal_installing = true; +#ifdef SOLARIS tid = thr_self(); +#else + tid = pthread_self(); +#endif signal_unlock(); } @@ -253,21 +300,18 @@ signal_lock(); jvm_signal_installed = true; jvm_signal_installing = false; +#ifdef SOLARIS cond_broadcast(&cond); +#else + pthread_cond_broadcast(&cond); +#endif signal_unlock(); } struct sigaction *JVM_get_signal_action(int sig) { - if (sact == NULL) { - allocate_sact(); - } /* Does race condition make sense here? */ if (sigismember(&jvmsigs, sig)) { return &sact[sig]; } return NULL; } - -int JVM_get_libjsig_version() { - return JSIG_VERSION_1_4_1; -}