8215451: JNI IsSameObject should not keep objects alive

Reviewed-by: eosterlund, kbarrett
This commit is contained in:
Per Lidén 2018-12-17 16:55:17 +01:00
parent 2e989366ea
commit 6a9c6a0392
5 changed files with 28 additions and 18 deletions

View File

@ -823,9 +823,7 @@ JNI_QUICK_ENTRY(jboolean, jni_IsSameObject(JNIEnv *env, jobject r1, jobject r2))
HOTSPOT_JNI_ISSAMEOBJECT_ENTRY(env, r1, r2);
oop a = JNIHandles::resolve(r1);
oop b = JNIHandles::resolve(r2);
jboolean ret = oopDesc::equals(a, b) ? JNI_TRUE : JNI_FALSE;
jboolean ret = JNIHandles::is_same_object(r1, r2) ? JNI_TRUE : JNI_FALSE;
HOTSPOT_JNI_ISSAMEOBJECT_RETURN(ret);
return ret;

View File

@ -152,17 +152,11 @@ jobject JNIHandles::make_weak_global(Handle obj, AllocFailType alloc_failmode) {
oop JNIHandles::resolve_external_guard(jobject handle) {
oop result = NULL;
if (handle != NULL) {
result = resolve_impl<true /* external_guard */ >(handle);
result = resolve_impl<0 /* decorators */, true /* external_guard */>(handle);
}
return result;
}
oop JNIHandles::resolve_jweak(jweak handle) {
assert(handle != NULL, "precondition");
assert(is_jweak(handle), "precondition");
return NativeAccess<ON_PHANTOM_OOP_REF>::oop_load(jweak_ptr(handle));
}
bool JNIHandles::is_global_weak_cleared(jweak handle) {
assert(handle != NULL, "precondition");
assert(is_jweak(handle), "not a weak handle");

View File

@ -42,8 +42,10 @@ class JNIHandles : AllStatic {
inline static oop* jobject_ptr(jobject handle); // NOT jweak!
inline static oop* jweak_ptr(jobject handle);
template<bool external_guard> inline static oop resolve_impl(jobject handle);
static oop resolve_jweak(jweak handle);
template <DecoratorSet decorators, bool external_guard> inline static oop resolve_impl(jobject handle);
// Resolve handle into oop, without keeping the object alive
inline static oop resolve_no_keepalive(jobject handle);
// This method is not inlined in order to avoid circular includes between
// this header file and thread.hpp.
@ -70,6 +72,9 @@ class JNIHandles : AllStatic {
// Resolve externally provided handle into oop with some guards
static oop resolve_external_guard(jobject handle);
// Check for equality without keeping objects alive
static bool is_same_object(jobject handle1, jobject handle2);
// Local handles
static jobject make_local(oop obj);
static jobject make_local(JNIEnv* env, oop obj); // Fast version when env is known

View File

@ -49,15 +49,15 @@ inline oop* JNIHandles::jweak_ptr(jobject handle) {
}
// external_guard is true if called from resolve_external_guard.
template<bool external_guard>
template <DecoratorSet decorators, bool external_guard>
inline oop JNIHandles::resolve_impl(jobject handle) {
assert(handle != NULL, "precondition");
assert(!current_thread_in_native(), "must not be in native");
oop result;
if (is_jweak(handle)) { // Unlikely
result = resolve_jweak(handle);
result = NativeAccess<ON_PHANTOM_OOP_REF|decorators>::oop_load(jweak_ptr(handle));
} else {
result = NativeAccess<>::oop_load(jobject_ptr(handle));
result = NativeAccess<decorators>::oop_load(jobject_ptr(handle));
// Construction of jobjects canonicalize a null value into a null
// jobject, so for non-jweak the pointee should never be null.
assert(external_guard || result != NULL, "Invalid JNI handle");
@ -68,14 +68,28 @@ inline oop JNIHandles::resolve_impl(jobject handle) {
inline oop JNIHandles::resolve(jobject handle) {
oop result = NULL;
if (handle != NULL) {
result = resolve_impl<false /* external_guard */ >(handle);
result = resolve_impl<0 /* decorators */, false /* external_guard */>(handle);
}
return result;
}
inline oop JNIHandles::resolve_no_keepalive(jobject handle) {
oop result = NULL;
if (handle != NULL) {
result = resolve_impl<AS_NO_KEEPALIVE, false /* external_guard */>(handle);
}
return result;
}
inline bool JNIHandles::is_same_object(jobject handle1, jobject handle2) {
oop obj1 = resolve_no_keepalive(handle1);
oop obj2 = resolve_no_keepalive(handle2);
return oopDesc::equals(obj1, obj2);
}
inline oop JNIHandles::resolve_non_null(jobject handle) {
assert(handle != NULL, "JNI handle should not be null");
oop result = resolve_impl<false /* external_guard */ >(handle);
oop result = resolve_impl<0 /* decorators */, false /* external_guard */>(handle);
assert(result != NULL, "NULL read from jni handle");
return result;
}

View File

@ -30,7 +30,6 @@ package MyPackage;
* @summary Verifies the JVMTI Heap Monitor Thread information sanity.
* @compile HeapMonitorThreadTest.java
* @run main/othervm/native -Xmx512m -agentlib:HeapMonitorTest MyPackage.HeapMonitorThreadTest
* @requires !vm.gc.Z
*/
import java.util.List;