8280422: thread_from_jni_environment can never return NULL

Reviewed-by: shade, kbarrett
This commit is contained in:
David Holmes 2022-01-25 01:22:48 +00:00
parent e3076552ec
commit f35df5bfb5
3 changed files with 21 additions and 15 deletions

View file

@ -1191,10 +1191,6 @@ const intx ObjectAlignmentInBytes = 8;
develop(bool, VerifyJNIFields, trueInDebug, \
"Verify jfieldIDs for instance fields") \
\
notproduct(bool, VerifyJNIEnvThread, false, \
"Verify JNIEnv.thread == Thread::current() when entering VM " \
"from JNI") \
\
develop(bool, VerifyFPU, false, \
"Verify FPU state (check for NaN's, etc.)") \
\

View file

@ -340,6 +340,11 @@ class VMNativeEntryWrapper {
#define JRT_END }
// Definitions for JNI
//
// As the JNIEnv can be passed from external native code we validate
// it in debug builds, primarily for our own testing. In general JNI
// does not attempt to detect programming errors and a bad JNIEnv may
// not even be readable.
#define JNI_ENTRY(result_type, header) \
JNI_ENTRY_NO_PRESERVE(result_type, header) \
@ -349,7 +354,7 @@ class VMNativeEntryWrapper {
extern "C" { \
result_type JNICALL header { \
JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
assert( !VerifyJNIEnvThread || (thread == Thread::current()), "JNIEnv is only valid in same thread"); \
assert(thread == Thread::current(), "JNIEnv is only valid in same thread"); \
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); \
ThreadInVMfromNative __tiv(thread); \
debug_only(VMNativeEntryWrapper __vew;) \
@ -360,7 +365,7 @@ extern "C" { \
extern "C" { \
result_type JNICALL header { \
JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
assert( !VerifyJNIEnvThread || (thread == Thread::current()), "JNIEnv is only valid in same thread"); \
assert(thread == Thread::current(), "JNIEnv is only valid in same thread"); \
VM_LEAF_BASE(result_type, header)

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
@ -1312,16 +1312,21 @@ class JavaThread: public Thread {
// Returns the jni environment for this thread
JNIEnv* jni_environment() { return &_jni_environment; }
// Returns the current thread as indicated by the given JNIEnv.
// We don't assert it is Thread::current here as that is done at the
// external JNI entry points where the JNIEnv is passed into the VM.
static JavaThread* thread_from_jni_environment(JNIEnv* env) {
JavaThread *thread_from_jni_env = (JavaThread*)((intptr_t)env - in_bytes(jni_environment_offset()));
// Only return NULL if thread is off the thread list; starting to
// exit should not return NULL.
if (thread_from_jni_env->is_terminated()) {
thread_from_jni_env->block_if_vm_exited();
return NULL;
} else {
return thread_from_jni_env;
JavaThread* current = (JavaThread*)((intptr_t)env - in_bytes(jni_environment_offset()));
// We can't get here in a thread that has completed its execution and so
// "is_terminated", but a thread is also considered terminated if the VM
// has exited, so we have to check this and block in case this is a daemon
// thread returning to the VM (the JNI DirectBuffer entry points rely on
// this).
if (current->is_terminated()) {
current->block_if_vm_exited();
ShouldNotReachHere();
}
return current;
}
// JNI critical regions. These can nest.