Skip to content

Commit

Permalink
refs qorelanguage/qore#4793 fixed signal handling, fixed random deadl…
Browse files Browse the repository at this point in the history
…ocks in JIT in the JVM on macOS aarch64
  • Loading branch information
davidnich committed Sep 16, 2023
1 parent a475307 commit 17fd387
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -17,7 +17,7 @@ if (POLICY CMP0042)
cmake_policy(SET CMP0042 NEW)
endif()

find_package(Qore 1.14 REQUIRED)
find_package(Qore 1.19 REQUIRED)
find_package(JNI REQUIRED)
find_package(Java REQUIRED)
if (${Java_VERSION_MAJOR} LESS 11)
Expand Down
23 changes: 23 additions & 0 deletions docs/mainpage.dox.tmpl
Expand Up @@ -215,6 +215,26 @@ class QoreIteratorTest extends AbstractIterator {
QORE_JNI_MIN_HEAP_SIZE=20m QORE_JNI_MAX_HEAP_SIZE=20m qore -l jni script.q
@endverbatim

@subsection jni_signals Signals Used By the JVM

The JVM requires signals to run properly, and, while %Qore initializes the JVM with reduced signals (using the
<tt>-Xrs</tt> argument to the JVM), certain signals are critical to the operation of the JVM and cannot be used by
%Qore programs.

The signals in the following table are reserved by the JVM.

<b>Signals Reserved by the JVM</b>
|!Signal|!Description
|\c SIGTRAP|Trap exception
|\c SIGSEGV|Segmentation violation
|\c SIGBUS|Bus error
|\c SIGCHLD|Child process terminated
|\c SIGILL|Illegal instruction
|\c SIGFPE|Floating-point exception

If any %Qore code has a signal handler assigned to any of the above signals (for the signals that can be
assigned), the \c jni module cannot be initialized and will fail to load.

@section jni_use_java_in_qore Using Java APIs in Qore

@subsection jniimport Importing Java APIs into Qore
Expand Down Expand Up @@ -830,6 +850,9 @@ set_save_object_callback(callback);
@section jnireleasenotes jni Module Release Notes

@subsection jni_2_3_2 jni Module Version 2.3.2
- fixed a bugs handling the signal mask that could cause hard-to-debug problems such as deadlocks in JIT in the
JVM on macOS aarch64
(<a href="https://github.com/qorelanguage/qore/issues/4793">issue 4793</a>)
- fixed signatures for Java methods with varargs params
(<a href="https://github.com/qorelanguage/qore/issues/4787">issue 4787</a>)

Expand Down
6 changes: 3 additions & 3 deletions qore-jni-module.spec
Expand Up @@ -17,9 +17,9 @@ BuildRequires: gcc-c++
BuildRequires: devtoolset-7-gcc-c++
%endif
BuildRequires: cmake >= 3.5
BuildRequires: qore-devel >= 1.14
BuildRequires: qore-stdlib >= 1.14
BuildRequires: qore >= 1.14
BuildRequires: qore-devel >= 1.19
BuildRequires: qore-stdlib >= 1.19
BuildRequires: qore >= 1.19
BuildRequires: java-17-openjdk-devel
BuildRequires: unzip
BuildRequires: doxygen
Expand Down
34 changes: 5 additions & 29 deletions src/jni-module.cpp
Expand Up @@ -58,7 +58,7 @@ using namespace jni;

sig_vec_t sig_vec = {
#ifndef Q_WINDOWS
SIGTRAP, SIGUSR1, SIGSEGV, SIGBUS, SIGCHLD
SIGTRAP, SIGSEGV, SIGBUS, SIGCHLD, SIGILL, SIGFPE
#endif
};

Expand Down Expand Up @@ -167,7 +167,10 @@ static QoreStringNode* jni_module_init() {

qore_set_module_option("jni", "jni-version", JNI_VERSION_10);

QoreStringNode* err = nullptr;
QoreStringNode* err = qore_reassign_signals(sig_vec, QORE_JNI_MODULE_NAME, true);
if (err) {
return err;
}

ValueHolder jvm_ptr(qore_get_module_option("jni", "jvm-ptr"), nullptr);
if (jvm_ptr->getType() == NT_INT) {
Expand Down Expand Up @@ -211,33 +214,6 @@ static QoreStringNode* jni_module_init() {

printd(5, "jni_module_init() initialized JVM\n");

#ifndef Q_WINDOWS
{
sig_vec_t new_sig_vec;
for (int sig : sig_vec) {
QoreStringNode* err = qore_reassign_signal(sig, QORE_JNI_MODULE_NAME);
if (err) {
printd(5, "%s\n", err->c_str());
// ignore errors; already assigned to another module
err->deref();
}
new_sig_vec.push_back(sig);
}
if (!new_sig_vec.empty()) {
sigset_t mask;
// setup signal mask
sigemptyset(&mask);
for (auto& sig : new_sig_vec) {
//printd(5, "jni_module_init() unblocking signal %d\n", sig);
sigaddset(&mask, sig);
}
// unblock threads
pthread_sigmask(SIG_UNBLOCK, &mask, 0);
}
}

#endif

if (!bootstrap) {
return jni_module_init_finalize();
}
Expand Down

0 comments on commit 17fd387

Please sign in to comment.