eclipse-openj9 / openj9

Eclipse OpenJ9: A Java Virtual Machine for OpenJDK that's optimized for small footprint, fast start-up, and high throughput. Builds on Eclipse OMR (https://github.com/eclipse/omr) and combines with the Extensions for OpenJDK for OpenJ9 repo.
Other
3.28k stars 721 forks source link

Incomplete null checks for JNI-refs #16081

Open babsingh opened 2 years ago

babsingh commented 2 years ago

In JVMTI (and few instances outside JVMTI), there are occurrences of incomplete null checks for JNI-refs.

Example:

jobject ref = ...;
...
if (NULL == ref) { /* Incomplete */
   ...

It's legal to have a JNI ref to NULL (think weak global ref).

A complete null check will be as follows:

jobject ref = ...;
...
j9object_t obj = (NULL == ref) ? NULL : J9_JNI_UNWRAP_REFERENCE(ref);
if (NULL == obj) {
   ...
babsingh commented 2 years ago

fyi @gacholio

gacholio commented 2 years ago

There may be other instances outside of JVMTI.

The most readable way of fixing this is to use a local for the unwrapped object:

j9object_t obj = (NULL == ref) ? NULL : J9_JNI_UNWRAP_REFERENCE(ref);
if (NULL == obj) {
}
gacholio commented 2 years ago

Another option would be to handle NULL refs in J9_JNI_UNWRAP_REFERENCE. This would mean removing some existing NULL checks to avoid duplication. The downside is that the NULL checks are sometimes unnecessary (no native will ever be called with a wrapped NULL).