Any native library calling JNI_FindClass from JNI_OnUnload?

Mandy Chung mandy.chung at oracle.com
Sun Apr 13 02:48:42 UTC 2014


Does anyone know of a native library whose JNI_OnUnload calls
JNI_FindClass?

I'm wondering how much existing code out there depends on the
existing behavior that is unsupported.

During the investigation of JDK-4240589, I found that the implementation
JNI_FindClass has a special handling that calls
ClassLoader.NativeLibrary.getFromClass method to determine
if JNI_FindClass is being called by JNI_OnLoad and JNI_OnUnload
directly or indirectly.  This special handling makes sense for
JNI_OnLoad, i.e. when the native library is loaded, that
may call JNI_FindClass.

I don't think this should apply to JNI_OnUnload.  When a native library
is being unloaded, it means that the ClassLoader has become
unreachable and GC'ed.  When JNI_OnUnloader, there is no guarantee
that the class loader still exists for doing any class loading.

The JNI spec of JNI_OnUnload [1] also makes it clear:

The VM calls JNI_OnUnload when the class loader containing the native
library is garbage collected. This function can be used to perform
cleanup operations. Because this function is called in an unknown
context (such as from a finalizer), the programmer should be
conservative on using Java VM services, and refrain from arbitrary Java
call-backs.

A little more background about this:  The current ClassLoader
uses finalizer to clean up and unload the native library, if any.
I'm considering to replace the finalizer with phantom reference
which is generally a good change and discover this VM/JNI/libs
dependency.

Any data would be appreciated.
Mandy
[1] http://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/invocation.html#JNI_OnUnload




More information about the core-libs-dev mailing list