A DESCRIPTION OF THE PROBLEM :
While developing a library which drives Java using the JNI I ran into issues calling methods which are marked with "jdk.internal.reflect.CallerSensitive". Methods with this annotation call Reflection.getCallerClass. The current implementation of Reflection.getCallerClass searches the stack frame until it finds the first stack frame which is not marked as internal. However, when calling directly from JNI no such stack frame exists thus methods either fails or return with NullPointerException.
There needs to be a documented method which allows JNI_CreateJavaVM to specify an object as the base caller for calls from within JNI, or some method to attach an object to a thread as the originator for this purpose. If there is an existing interaction with thread groups and CallerSensitive/getCallerClass it is not documented.
The only work around I can find is calling a another method in Java which then calls to desired method to act as a proxy, but if this is the desired solution it is also not documented within JNI.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a JVM and call java.lang.reflect.Method.invoke using JDK 12.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Method is invoked
ACTUAL -
java.lang.NullPointerException
at java.base/jdk.internal.reflect.Reflection.verifyMemberAccess(Reflection.java:130)
at java.base/java.lang.reflect.AccessibleObject.slowVerifyAccess(AccessibleObject.java:673)
at java.base/java.lang.reflect.AccessibleObject.verifyAccess(AccessibleObject.java:666)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:638)
at java.base/java.lang.reflect.Method.invoke(Method.java:559)
CUSTOMER SUBMITTED WORKAROUND :
No workaround exists in general for all CallerSensitive methods. Each must be individually wrapped with Java code.
FREQUENCY : always
While developing a library which drives Java using the JNI I ran into issues calling methods which are marked with "jdk.internal.reflect.CallerSensitive". Methods with this annotation call Reflection.getCallerClass. The current implementation of Reflection.getCallerClass searches the stack frame until it finds the first stack frame which is not marked as internal. However, when calling directly from JNI no such stack frame exists thus methods either fails or return with NullPointerException.
There needs to be a documented method which allows JNI_CreateJavaVM to specify an object as the base caller for calls from within JNI, or some method to attach an object to a thread as the originator for this purpose. If there is an existing interaction with thread groups and CallerSensitive/getCallerClass it is not documented.
The only work around I can find is calling a another method in Java which then calls to desired method to act as a proxy, but if this is the desired solution it is also not documented within JNI.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a JVM and call java.lang.reflect.Method.invoke using JDK 12.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Method is invoked
ACTUAL -
java.lang.NullPointerException
at java.base/jdk.internal.reflect.Reflection.verifyMemberAccess(Reflection.java:130)
at java.base/java.lang.reflect.AccessibleObject.slowVerifyAccess(AccessibleObject.java:673)
at java.base/java.lang.reflect.AccessibleObject.verifyAccess(AccessibleObject.java:666)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:638)
at java.base/java.lang.reflect.Method.invoke(Method.java:559)
CUSTOMER SUBMITTED WORKAROUND :
No workaround exists in general for all CallerSensitive methods. Each must be individually wrapped with Java code.
FREQUENCY : always
- relates to
-
JDK-8177155 Examine @CS methods when called from attached thread with no caller
-
- Resolved
-