mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-19 18:44:38 +02:00
8280422: thread_from_jni_environment can never return NULL
Reviewed-by: shade, kbarrett
This commit is contained in:
parent
e3076552ec
commit
f35df5bfb5
3 changed files with 21 additions and 15 deletions
|
@ -1191,10 +1191,6 @@ const intx ObjectAlignmentInBytes = 8;
|
||||||
develop(bool, VerifyJNIFields, trueInDebug, \
|
develop(bool, VerifyJNIFields, trueInDebug, \
|
||||||
"Verify jfieldIDs for instance fields") \
|
"Verify jfieldIDs for instance fields") \
|
||||||
\
|
\
|
||||||
notproduct(bool, VerifyJNIEnvThread, false, \
|
|
||||||
"Verify JNIEnv.thread == Thread::current() when entering VM " \
|
|
||||||
"from JNI") \
|
|
||||||
\
|
|
||||||
develop(bool, VerifyFPU, false, \
|
develop(bool, VerifyFPU, false, \
|
||||||
"Verify FPU state (check for NaN's, etc.)") \
|
"Verify FPU state (check for NaN's, etc.)") \
|
||||||
\
|
\
|
||||||
|
|
|
@ -340,6 +340,11 @@ class VMNativeEntryWrapper {
|
||||||
#define JRT_END }
|
#define JRT_END }
|
||||||
|
|
||||||
// Definitions for JNI
|
// 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) \
|
#define JNI_ENTRY(result_type, header) \
|
||||||
JNI_ENTRY_NO_PRESERVE(result_type, header) \
|
JNI_ENTRY_NO_PRESERVE(result_type, header) \
|
||||||
|
@ -349,7 +354,7 @@ class VMNativeEntryWrapper {
|
||||||
extern "C" { \
|
extern "C" { \
|
||||||
result_type JNICALL header { \
|
result_type JNICALL header { \
|
||||||
JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
|
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)); \
|
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); \
|
||||||
ThreadInVMfromNative __tiv(thread); \
|
ThreadInVMfromNative __tiv(thread); \
|
||||||
debug_only(VMNativeEntryWrapper __vew;) \
|
debug_only(VMNativeEntryWrapper __vew;) \
|
||||||
|
@ -360,7 +365,7 @@ extern "C" { \
|
||||||
extern "C" { \
|
extern "C" { \
|
||||||
result_type JNICALL header { \
|
result_type JNICALL header { \
|
||||||
JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
|
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)
|
VM_LEAF_BASE(result_type, header)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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.
|
* Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* 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
|
// Returns the jni environment for this thread
|
||||||
JNIEnv* jni_environment() { return &_jni_environment; }
|
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) {
|
static JavaThread* thread_from_jni_environment(JNIEnv* env) {
|
||||||
JavaThread *thread_from_jni_env = (JavaThread*)((intptr_t)env - in_bytes(jni_environment_offset()));
|
JavaThread* current = (JavaThread*)((intptr_t)env - in_bytes(jni_environment_offset()));
|
||||||
// Only return NULL if thread is off the thread list; starting to
|
// We can't get here in a thread that has completed its execution and so
|
||||||
// exit should not return NULL.
|
// "is_terminated", but a thread is also considered terminated if the VM
|
||||||
if (thread_from_jni_env->is_terminated()) {
|
// has exited, so we have to check this and block in case this is a daemon
|
||||||
thread_from_jni_env->block_if_vm_exited();
|
// thread returning to the VM (the JNI DirectBuffer entry points rely on
|
||||||
return NULL;
|
// this).
|
||||||
} else {
|
if (current->is_terminated()) {
|
||||||
return thread_from_jni_env;
|
current->block_if_vm_exited();
|
||||||
|
ShouldNotReachHere();
|
||||||
}
|
}
|
||||||
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
// JNI critical regions. These can nest.
|
// JNI critical regions. These can nest.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue