JDK-4973054 : Need to clarify how to handle pending exceptions in JNI calls
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 1.4.2,6
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2003-12-29
  • Updated: 2017-05-16
  • Resolved: 2006-02-02
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 6
6 b71Fixed
Related Reports
Duplicate :  
Relates :  
Description
There seems vague statements related to exception handking in JNI.

SUN should clarify which functions can be called even if an exception
is pending in JNI calls.

---
The java JNI web page says,
(http://java.sun.com/j2se/1.4.2/docs/guide/jni/spec/design.html#wp17626)


 "...After an exception has been raised, the native code must first
 clear the exception before making other JNI calls. When there is a
 pending exception, the only JNI functions that are safe to call are
 ExceptionOccurred(), ExceptionDescribe(), and ExceptionClear(). The
 ExceptionDescribe() function prints a debugging message about the
 pending exception. ..."

 According to the statement, we can only call
  ExceptionOccurred()
  ExceptionDescribe()
  ExceptionClear()
 when there is a pending exception.

 however, we found the folllowing part in
 ./j2se/src/share/native/java/io/ObjectStreamClass.c

 -->
 ...
 JNIEXPORT jboolean JNICALL
 Java_java_io_ObjectStreamClass_hasStaticInitializer(JNIEnv *env,
 jclass this,
                                                    jclass clazz)
 {
    jclass superCl = NULL;
    jmethodID superClinitId = NULL;
    jmethodID clinitId =
        (*env)->GetStaticMethodID(env, clazz, "", "()V");
    if (clinitId == NULL) {     /* error thrown */
        jthrowable th = (*env)->ExceptionOccurred(env);
        if ((*env)->IsInstanceOf(env, th, noSuchMethodErrCl)) { <=== (*)
            (*env)->ExceptionClear(env);        /* normal return */
        }
        return JNI_FALSE;
    }
 ...
 <--

 Although the web statement says any JNI call should not be called
 before ExceptionClear(), IsInstanceOf() is called.(above (*))
 
 ===========================================================

Comments
EVALUATION There are two parts to this, the JNI spec definitely needs to be synced up with the JNI book by Sheng Liang. The second part is in the ./j2se/src/share/native/java/io/ObjectStreamClass.c which has been fixed since and no longer an issue. This bug will address the first part which is making the jni spec to be consistent. The following will be the proposed new text in the documentation: After an exception has been raised, the native code must first clear the exception before making other JNI calls. When there is a pending exception, the JNI functions that are safe to call are // Original set of methods allowed to carry exceptions. ExceptionOccurred() ExceptionDescribe() ExceptionClear() ExceptionDescribe() // Exception safe by code inspection && JNI Book - Sheng Liang, but not documented in this spec. ReleaseStringChars() ReleaseStringUTFchars() ReleaseStringCritical() Release<Type>ArrayElements() ReleasePrimitiveArrayCritical() DeleteLocalRef() DeleteGlobalRef() DeleteWeakGlobalRef() MonitorExit() // Added two new ones to the existing set. PushLocalFrame() PopLocalFrame() *** (#1 of 1): [ UNSAVED ] ###@###.###
19-01-2006