Skip to content

Commit 70183f4

Browse files
author
Gerard Ziemski
committed
8251438: Issues with our POSIX set_signal_handler()
Reviewed-by: dholmes, stuefe
1 parent 6eca296 commit 70183f4

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/hotspot/os/posix/signals_posix.cpp

+16-15
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,17 @@ static void set_our_sigflags(int sig, int flags) {
768768
}
769769
}
770770

771+
// Implementation may use the same storage for both the sa_sigaction field and the sa_handler field,
772+
// so check for "sigAct.sa_flags == SA_SIGINFO"
773+
static address get_signal_handler(const struct sigaction* action) {
774+
bool siginfo_flag_set = (action->sa_flags & SA_SIGINFO) != 0;
775+
if (siginfo_flag_set) {
776+
return CAST_FROM_FN_PTR(address, action->sa_sigaction);
777+
} else {
778+
return CAST_FROM_FN_PTR(address, action->sa_handler);
779+
}
780+
}
781+
771782
typedef int (*os_sigaction_t)(int, const struct sigaction *, struct sigaction *);
772783

773784
static void SR_handler(int sig, siginfo_t* siginfo, ucontext_t* context);
@@ -789,10 +800,7 @@ static void check_signal_handler(int sig) {
789800
// See comment for SA_RESTORER_FLAG_MASK
790801
LINUX_ONLY(act.sa_flags &= SA_RESTORER_FLAG_MASK;)
791802

792-
address thisHandler = (act.sa_flags & SA_SIGINFO)
793-
? CAST_FROM_FN_PTR(address, act.sa_sigaction)
794-
: CAST_FROM_FN_PTR(address, act.sa_handler);
795-
803+
address thisHandler = get_signal_handler(&act);
796804

797805
switch (sig) {
798806
case SIGSEGV:
@@ -1213,9 +1221,7 @@ void set_signal_handler(int sig) {
12131221
struct sigaction oldAct;
12141222
sigaction(sig, (struct sigaction*)NULL, &oldAct);
12151223

1216-
void* oldhand = oldAct.sa_sigaction
1217-
? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)
1218-
: CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
1224+
void* oldhand = get_signal_handler(&oldAct);
12191225
if (oldhand != CAST_FROM_FN_PTR(void*, SIG_DFL) &&
12201226
oldhand != CAST_FROM_FN_PTR(void*, SIG_IGN) &&
12211227
oldhand != CAST_FROM_FN_PTR(void*, (sa_sigaction_t)javaSignalHandler)) {
@@ -1258,9 +1264,7 @@ void set_signal_handler(int sig) {
12581264
int ret = sigaction(sig, &sigAct, &oldAct);
12591265
assert(ret == 0, "check");
12601266

1261-
void* oldhand2 = oldAct.sa_sigaction
1262-
? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)
1263-
: CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
1267+
void* oldhand2 = get_signal_handler(&oldAct);
12641268
assert(oldhand2 == oldhand, "no concurrent signal handler installation");
12651269
}
12661270

@@ -1370,9 +1374,7 @@ void PosixSignals::print_signal_handler(outputStream* st, int sig,
13701374

13711375
st->print("%s: ", os::exception_name(sig, buf, buflen));
13721376

1373-
address handler = (sa.sa_flags & SA_SIGINFO)
1374-
? CAST_FROM_FN_PTR(address, sa.sa_sigaction)
1375-
: CAST_FROM_FN_PTR(address, sa.sa_handler);
1377+
address handler = get_signal_handler(&sa);
13761378

13771379
if (handler == CAST_FROM_FN_PTR(address, SIG_DFL)) {
13781380
st->print("SIG_DFL");
@@ -1437,8 +1439,7 @@ void os::print_signal_handlers(outputStream* st, char* buf, size_t buflen) {
14371439
bool PosixSignals::is_sig_ignored(int sig) {
14381440
struct sigaction oact;
14391441
sigaction(sig, (struct sigaction*)NULL, &oact);
1440-
void* ohlr = oact.sa_sigaction ? CAST_FROM_FN_PTR(void*, oact.sa_sigaction)
1441-
: CAST_FROM_FN_PTR(void*, oact.sa_handler);
1442+
void* ohlr = get_signal_handler(&oact);
14421443
if (ohlr == CAST_FROM_FN_PTR(void*, SIG_IGN)) {
14431444
return true;
14441445
} else {

0 commit comments

Comments
 (0)