JDK-8295974 : jni_FatalError and Xcheck:jni warnings should print the native stack when there are no Java frames
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 8-pool,11-pool,17-pool,20
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2022-10-27
  • Updated: 2023-01-05
  • Resolved: 2023-01-03
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 21
21 b04Fixed
Description
JavaThread::print_stack_on is a no-op if there are no Java frames:

void JavaThread::print_stack_on(outputStream* st) {
  if (!has_last_Java_frame()) return;

But it is used to show where -Xcheck:jni warnings originate from and from jni_FatalError. That means that when the JNI code is executed at the top-level of a thread, such as in the launcher, with no Java code on the stack, then we get zero information about where the warning, or error, was generated. This makes tracking down such warnings very tedious.

If there are no Java frames the JNI code should call the debug utility print_native_stack; though I note a second problem if we do that is that os::current_frame() will trigger an assertion in the frame code due to a NULL pc. So a little more effort is needed to make that work (I commented out the assertion and got the stack trace I wanted).


Comments
Changeset: 37574333 Author: David Holmes <dholmes@openjdk.org> Date: 2023-01-03 04:22:51 +0000 URL: https://git.openjdk.org/jdk/commit/375743336dc15f9f945a03422eaa7ff773622cd8
03-01-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/11703 Date: 2022-12-16 07:07:06 +0000
16-12-2022

Here's a fabricated -Xcheck:jni warning occurring in an attached native thread. Before: WARNING in native method: JNI call made without checking exceptions when required to from CallStaticObjectMethod After: WARNING in native method: JNI call made without checking exceptions when required to from CallStaticObjectMethod Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) V [libjvm.so+0x1186e5c] functionEnter(JavaThread*)+0xbc (jniCheck.cpp:193) V [libjvm.so+0x119d77b] checked_jni_CallStaticObjectMethod+0xdb (jniCheck.cpp:1290) C [libterminatedThread.so+0xa6f] thread_start+0xbf (libterminatedThread.c:63)
16-12-2022

I injected a fault in the launcher when using -XshowSettings: if (showSettings != NULL) { + (*env)->FatalError(env, "Injected fatal error in launcher"); ShowSettings(env, showSettings); CHECK_EXCEPTION_LEAVE(1); this is the output before: java -XshowSettings FATAL ERROR in native method: Injected fatal error in launcher Aborted (core dumped) and after: FATAL ERROR in native method: Injected fatal error in launcher Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) V [libjvm.so+0x114ca6f] jni_FatalError+0xef (jni.cpp:630) C [libjli.so+0x3b47] JavaMain+0xc7 (java.c:419) C [libjli.so+0x7a49] ThreadJavaMain+0x9 (java_md.c:650) Aborted (core dumped)
16-12-2022