JDK-8253742 : POSIX signal code cleanup
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 16
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2020-09-28
  • Updated: 2020-11-26
  • Resolved: 2020-11-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 16
16 b26Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
A few cleanup opportunities came up during review for JDK-8252324:

#1 src/hotspot/os/posix/signals_posix.cpp
// signal handling (except suspend/resume)
// suspend/resume

 // glibc on Bsd platform uses non-documented flag
 
 @dholmes-ora dholmes-ora 15 hours ago  Member
This was added for Linux and then copied to the BSD port, so the comment is inaccurate. This seems to be referring to the SA_RESTORER flag which seems to be a linux kernel flag. But I'm unsure why we would need to even be aware of this. I think future cleanup could be done here.


#2 src/hotspot/os/posix/signals_posix.cpp

Are the following useful on other the other POSIX platforms? Currently they are only used on AIX:

set_thread_signal_mask()
unblock_program_error_signals()


#3 src/hotspot/os/posix/signals_posix.cpp

dholmes-ora 15 hours ago  Member
There should be a better way to dispatch here. If the handler has a platform-independent name, and is declared in the platform specific files, then it will link to that one definition.

#if defined(BSD)
  JVM_handle_bsd_signal(sig, info, uc, true);
#elif defined(AIX)
  JVM_handle_aix_signal(sig, info, uc, true);
#else
  JVM_handle_linux_signal(sig, info, uc, true);
#endif

Same with:

address PosixSignals::ucontext_get_pc(const ucontext_t* ctx) {
#if defined(AIX)
   return os::Aix::ucontext_get_pc(ctx);
#elif defined(BSD)
   return os::Bsd::ucontext_get_pc(ctx);
#elif defined(LINUX)
   return os::Linux::ucontext_get_pc(ctx);
#else
   VMError::report_and_die("unimplemented ucontext_get_pc");
#endif
}

void PosixSignals::ucontext_set_pc(ucontext_t* ctx, address pc) {
#if defined(AIX)
   os::Aix::ucontext_set_pc(ctx, pc);
#elif defined(BSD)
   os::Bsd::ucontext_set_pc(ctx, pc);
#elif defined(LINUX)
   os::Linux::ucontext_set_pc(ctx, pc);
#else
   VMError::report_and_die("unimplemented ucontext_set_pc");
#endif
}

#4 In src/hotspot/os/posix/signals_posix.hpp

#ifndef OS_POSIX_SIGNALS_POSIX_HPP
#define OS_POSIX_SIGNALS_POSIX_HPP

 #include "memory/allocation.hpp"
 
 @coleenp coleenp 20 hours ago  Member
There's a new memory/allStatic.hpp header file you can include instead.


#5 src/hotspot/os/posix/signals_posix.hpp

 // Signal number used to suspend/resume a thread
// do not use any signal number less than SIGSEGV, see 4355769
static int SR_signum = SIGUSR2;
 
 @coleenp coleenp 20 hours ago  Member
Can you hide this in the .cpp file so that you can avoid including <signal.h> in the header file?

And use forward declarations for outputStream, ucontext_t, and siginfo_t.
 
 @coleenp coleenp 15 hours ago  Member
Note that these changes are not important enough to rerun testing for this cleanup, so you can make it a new RFE. Thank you for doing consolidation!
 
 @dholmes-ora dholmes-ora 13 hours ago  ��� 
edited 
 Member
In reply to Coleen's query - SR_signum is referenced from the os_*.cpp files so needs to be in the header file now.
 
 @tstuefe tstuefe 9 hours ago  Member
I wondered why this is not const then I saw that it can be overwritten with an environment variable "_JAVA_SR_SIGNUM". Is this still needed? Seems to be an odd way of specifying a VM parameter. And it seems to be untested (I did not find any tests for that in the open codebase).

 @dholmes-ora dholmes-ora 14 hours ago  ��� 
In reply to Coleen's query - SR_signum is referenced from the os_*.cpp files so needs to be in the header file now.

tstuefe 9 hours ago  Member
I wondered why this is not const then I saw that it can be overwritten with an environment variable "_JAVA_SR_SIGNUM". Is this still needed? Seems to be an odd way of specifying a VM parameter. And it seems to be untested (I did not find any tests for that in the open codebase).


#6 src/hotspot/os/aix/os_aix.cpp

  // We also want to know if someone else adds a SIGDANGER handler because
  // that will interfere with OOM killling.
  print_signal_handler(st, SIGDANGER, buf, buflen);
  PosixSignals::print_signal_handler(st, SIGDANGER, buf, buflen);
}
 
 @coleenp coleenp 20 hours ago  Member
I wonder with some #ifdefs, this could also be moved to posix_signals.cpp. Not for this change but maybe a follow-up RFE.
 
 @tstuefe tstuefe 9 hours ago  Member
Yes, this os::print_signal_handlers() and os::print_signal_handler() could be unified across Posix platforms. At least the latter.

(Personally I also would like to print all signal handlers from 1.., regardless of whether its one of "us". E.g. if process overrides SIGCHILD that is interesting too.)


#7 src/hotspot/os/bsd/os_bsd.cpp
@@ -3207,10 +2282,10 @@ bool os::bind_to_processor(uint processor_id) {
}

 void os::SuspendedThreadTask::internal_do_task() {
  if (do_suspend(_thread->osthread())) {
  if (PosixSignals::do_suspend(_thread->osthread())) {
 
 @tstuefe tstuefe 9 hours ago  Member
Future RFE: os::SuspendedThreadTask::internal_do_task() can be unified across posix platforms.


#8 src/hotspot/os/bsd/os_bsd.cpp

  // initialize suspend/resume support - must do this before signal_sets_init()
  if (SR_initialize() != 0) {
  if (PosixSignals::SR_initialize() != 0) {
    perror("SR_initialize failed");
    return JNI_ERR;
  }

   Bsd::signal_sets_init();
  Bsd::install_signal_handlers();
  PosixSignals::signal_sets_init();
  PosixSignals::install_signal_handlers();
  // Initialize data for jdk.internal.misc.Signal
  if (!ReduceSignalUsage) {
    jdk_misc_signal_init();
    PosixSignals::jdk_misc_signal_init();
  }
Comment on lines 2141 to 2152
 
 @tstuefe tstuefe 9 hours ago  Member
This whole section could be unified too into an own PosixSignals::initial_signal_stuff() function. Potentially for a future RFE.


#9 src/hotspot/os/posix/signals_posix.cpp
extern "C" JNIEXPORT int JVM_handle_linux_signal(int signo, siginfo_t* siginfo,
                                               void* ucontext,
                                               int abort_if_unrecognized);
#endif
 
 @tstuefe tstuefe 9 hours ago  Member
Future cleanup: I think it will be difficult to unify the platform specific signal handlers (but worthwhile). But the naming could be at least the same, no reason to have the platform in the name.
 
 @YaSuenag YaSuenag 6 hours ago  Member
I agree with @tstuefe . Can we rename them to JVM_handle_posix_signal ?


#10 src/hotspot/os/posix/signals_posix.cpp
    tty->cr();
    tty->print("  found:");
    print_sa_flags(tty, act.sa_flags);
    tty->cr();
 
 @YaSuenag YaSuenag 6 hours ago  Member
Can we use unified logging at here? If do so, we should out them with single line.


#11 src/hotspot/os/posix/signals_posix.cpp

 void PosixSignals::jdk_misc_signal_init() {
  // Initialize signal structures
  ::memset((void*)pending_signals, 0, sizeof(pending_signals));
 
 @YaSuenag YaSuenag 6 hours ago  Member
Is this code needed? pending_signals is initialized with { 0 } and also jdk_misc_signal_init() would be called from os::init_2() .


Comments
Changeset: 50a2c22f Author: Gerard Ziemski <gziemski@openjdk.org> Date: 2020-11-18 15:29:13 +0000 URL: https://github.com/openjdk/jdk/commit/50a2c22f
18-11-2020

Re #11 void PosixSignals::jdk_misc_signal_init() { // Initialize signal structures ::memset((void*)pending_signals, 0, sizeof(pending_signals)); We do initialize it like so: static volatile jint pending_signals[NSIG+1] = { 0 }; But that will only set the first element (unless the compiler uses that as a hint to clear the entire array?)
30-10-2020

RE #10 I think that's out of scope for this follow up, perhaps we should file a new RFR
30-10-2020

Turned out to be my local Mac running out of disk space. Thank you David for pointing me to look for local issue.
14-10-2020

Must have been something with the state of my machine, rebooting it solved the issue. It also compiles fine on my other Mac and the Skara build and tier1 passes fine as well as shown in the PR.
13-10-2020

[~dholmes] Created a draft PR with items #1 and #2 (I did not push #3 in case I have to revert or change #2) here https://github.com/openjdk/jdk/pull/636
13-10-2020

Do you have a webrev? I can't see how you could introduce a build failure with this work. This looks more like a local build issue on your Mac using latest devkits.
12-10-2020

Yes, it fails while using the VM, which was just built: # make hotspot Building target 'hotspot' in configuration 'macosx-x86_64-server-fastdebug' Creating hotspot/variant-server/tools/adlc/adlc from 13 file(s) Compiling 2 files for BUILD_JVMTI_TOOLS Updating support/modules_libs/java.base/server/libjvm.dylib due to 1014 file(s) Finished building target 'hotspot' in configuration 'macosx-x86_64-server-fastdebug' # make images Building target 'images' in configuration 'macosx-x86_64-server-fastdebug' Updating images/sec-bin.zip ld: warning: dylib (/Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/support/modules_libs/java.base/server/libjvm.dylib) was built for newer macOS version (10.15) than being linked (10.9) ld: warning: dylib (/Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/support/modules_libs/java.base/server/libjvm.dylib) was built for newer macOS version (10.15) than being linked (10.9) Creating java.instrument.jmod mv: rename /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/support/images/jmods/java.logging.jmod to /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.logging.jmod: No such file or directory mv: rename /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/support/images/jmods/java.datatransfer.jmod to /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.datatransfer.jmod: No such file or directory mv: rename /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/support/images/jmods/java.compiler.jmod to /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.compiler.jmod: No such file or directory mv: rename /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/support/images/jmods/java.management.rmi.jmod to /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.management.rmi.jmod: No such file or directory mv: rename /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/support/images/jmods/java.naming.jmod to /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.naming.jmod: No such file or directory make[3]: *** [/Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.compiler.jmod] Error 1 make[3]: *** [/Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.datatransfer.jmod] Error 1 make[3]: *** [/Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.logging.jmod] Error 1 make[2]: *** [java.compiler-jmod] Error 2 make[2]: *** Waiting for unfinished jobs.... make[2]: *** [java.datatransfer-jmod] Error 2 make[2]: *** [java.logging-jmod] Error 2 make[3]: *** [/Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.management.rmi.jmod] Error 1 mv: rename /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/support/images/jmods/java.management.jmod to /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.management.jmod: No such file or directory make[2]: *** [java.management.rmi-jmod] Error 2 mv: rename /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/support/images/jmods/java.net.http.jmod to /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.net.http.jmod: No such file or directory make[3]: *** [/Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.naming.jmod] Error 1 make[2]: *** [java.naming-jmod] Error 2 make[3]: *** [/Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.management.jmod] Error 1 make[2]: *** [java.management-jmod] Error 2 make[3]: *** [/Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.net.http.jmod] Error 1 make[2]: *** [java.net.http-jmod] Error 2 mv: rename /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/support/images/jmods/java.prefs.jmod to /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.prefs.jmod: No such file or directory make[3]: *** [/Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.prefs.jmod] Error 1 make[2]: *** [java.prefs-jmod] Error 2 Creating interim java.base.jmod mv: rename /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/support/images/jmods/java.desktop.jmod to /Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.desktop.jmod: No such file or directory make[3]: *** [/Volumes/Work/bugs/8253742/jdk/build/macosx-x86_64-server-fastdebug/images/jmods/java.desktop.jmod] Error 1 make[2]: *** [java.desktop-jmod] Error 2 ERROR: Build failed for target 'images' in configuration 'macosx-x86_64-server-fastdebug' (exit code 2) Looks like it fails to build java.logging.jmod, java.datatransfer.jmod, etc. If anyone has suggestion on how to debug the build, please share. For now I'm going to punt out on this here and move it out to a separate issue (I think it's a worthwhile effort to include AIX code for all POSIX platforms, incorporating common POSIX code was the goal of the exercise here)
12-10-2020

Is that during a part of the build that uses the VM you have just built? Not sure how a build can timeout?
09-10-2020

Having some issues with building JDK locally - the build gets stuck and times out?
09-10-2020

Re #2: unblock_program_error_signals() looks useful on all platforms, so I decided to use it for all, not just AIX
08-10-2020

Re #1: a) According to https://man7.org/linux/man-pages/man2/sigaction.2.html "POSIX does not specify a sa_restorer field." and further in https://man7.org/linux/man-pages/man2/sigreturn.2.html it says that sigreturn "... call is not specified in POSIX, and details of its behavior vary across systems." b) As David points out it is internal Linux flag and according to https://man7.org/linux/man-pages/man2/sigaction.2.html "The sa_restorer field is not intended for application use." Summary: It appears that it is safe to remove "SIGNIFICANT_SIGNAL_MASK" related code from our POSIX signal implementation file. In worst case scenario on those Linux platforms that use it, it will simply show up in "sa_flags" when printing its value to the user.
08-10-2020