--- old/src/hotspot/share/prims/jni.cpp 2020-07-23 03:18:56.668471959 -0400 +++ new/src/hotspot/share/prims/jni.cpp 2020-07-23 03:18:55.563458747 -0400 @@ -753,7 +753,7 @@ HOTSPOT_JNI_NEWGLOBALREF_ENTRY(env, ref); Handle ref_handle(thread, JNIHandles::resolve(ref)); - jobject ret = JNIHandles::make_global(ref_handle); + jobject ret = JNIHandles::make_global(ref_handle, AllocFailStrategy::RETURN_NULL); HOTSPOT_JNI_NEWGLOBALREF_RETURN(ret); return ret; @@ -797,7 +797,8 @@ HOTSPOT_JNI_NEWLOCALREF_ENTRY(env, ref); - jobject ret = JNIHandles::make_local(THREAD, JNIHandles::resolve(ref)); + jobject ret = JNIHandles::make_local(THREAD, JNIHandles::resolve(ref), + AllocFailStrategy::RETURN_NULL); HOTSPOT_JNI_NEWLOCALREF_RETURN(ret); return ret; @@ -3048,10 +3049,13 @@ JNI_ENTRY(jweak, jni_NewWeakGlobalRef(JNIEnv *env, jobject ref)) JNIWrapper("jni_NewWeakGlobalRef"); - HOTSPOT_JNI_NEWWEAKGLOBALREF_ENTRY(env, ref); + HOTSPOT_JNI_NEWWEAKGLOBALREF_ENTRY(env, ref); Handle ref_handle(thread, JNIHandles::resolve(ref)); - jweak ret = JNIHandles::make_weak_global(ref_handle); - HOTSPOT_JNI_NEWWEAKGLOBALREF_RETURN(ret); + jweak ret = JNIHandles::make_weak_global(ref_handle, AllocFailStrategy::RETURN_NULL); + if (ret == NULL) { + THROW_OOP_(Universe::out_of_memory_error_c_heap(), NULL); + } + HOTSPOT_JNI_NEWWEAKGLOBALREF_RETURN(ret); return ret; JNI_END @@ -3127,6 +3131,12 @@ directBufferClass = (jclass) env->NewGlobalRef(directBufferClass); directByteBufferClass = (jclass) env->NewGlobalRef(directByteBufferClass); + // Global refs will be NULL if out-of-memory (no exception is pending) + if (bufferClass == NULL || directBufferClass == NULL || directByteBufferClass == NULL) { + directBufferSupportInitializeFailed = 1; + return false; + } + // Get needed field and method IDs directByteBufferConstructor = env->GetMethodID(directByteBufferClass, "", "(JI)V"); if (env->ExceptionCheck()) {