Skip to content

Commit 225e400

Browse files
author
duke
committedJan 25, 2022
Automatic merge of jdk:master into master
2 parents a011568 + f35df5b commit 225e400

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed
 

‎src/hotspot/share/runtime/globals.hpp

-4
Original file line numberDiff line numberDiff line change
@@ -1191,10 +1191,6 @@ const intx ObjectAlignmentInBytes = 8;
11911191
develop(bool, VerifyJNIFields, trueInDebug, \
11921192
"Verify jfieldIDs for instance fields") \
11931193
\
1194-
notproduct(bool, VerifyJNIEnvThread, false, \
1195-
"Verify JNIEnv.thread == Thread::current() when entering VM " \
1196-
"from JNI") \
1197-
\
11981194
develop(bool, VerifyFPU, false, \
11991195
"Verify FPU state (check for NaN's, etc.)") \
12001196
\

‎src/hotspot/share/runtime/interfaceSupport.inline.hpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ class VMNativeEntryWrapper {
340340
#define JRT_END }
341341

342342
// Definitions for JNI
343+
//
344+
// As the JNIEnv can be passed from external native code we validate
345+
// it in debug builds, primarily for our own testing. In general JNI
346+
// does not attempt to detect programming errors and a bad JNIEnv may
347+
// not even be readable.
343348

344349
#define JNI_ENTRY(result_type, header) \
345350
JNI_ENTRY_NO_PRESERVE(result_type, header) \
@@ -349,7 +354,7 @@ class VMNativeEntryWrapper {
349354
extern "C" { \
350355
result_type JNICALL header { \
351356
JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
352-
assert( !VerifyJNIEnvThread || (thread == Thread::current()), "JNIEnv is only valid in same thread"); \
357+
assert(thread == Thread::current(), "JNIEnv is only valid in same thread"); \
353358
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); \
354359
ThreadInVMfromNative __tiv(thread); \
355360
debug_only(VMNativeEntryWrapper __vew;) \
@@ -360,7 +365,7 @@ extern "C" { \
360365
extern "C" { \
361366
result_type JNICALL header { \
362367
JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
363-
assert( !VerifyJNIEnvThread || (thread == Thread::current()), "JNIEnv is only valid in same thread"); \
368+
assert(thread == Thread::current(), "JNIEnv is only valid in same thread"); \
364369
VM_LEAF_BASE(result_type, header)
365370

366371

‎src/hotspot/share/runtime/thread.hpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -1312,16 +1312,21 @@ class JavaThread: public Thread {
13121312
// Returns the jni environment for this thread
13131313
JNIEnv* jni_environment() { return &_jni_environment; }
13141314

1315+
// Returns the current thread as indicated by the given JNIEnv.
1316+
// We don't assert it is Thread::current here as that is done at the
1317+
// external JNI entry points where the JNIEnv is passed into the VM.
13151318
static JavaThread* thread_from_jni_environment(JNIEnv* env) {
1316-
JavaThread *thread_from_jni_env = (JavaThread*)((intptr_t)env - in_bytes(jni_environment_offset()));
1317-
// Only return NULL if thread is off the thread list; starting to
1318-
// exit should not return NULL.
1319-
if (thread_from_jni_env->is_terminated()) {
1320-
thread_from_jni_env->block_if_vm_exited();
1321-
return NULL;
1322-
} else {
1323-
return thread_from_jni_env;
1319+
JavaThread* current = (JavaThread*)((intptr_t)env - in_bytes(jni_environment_offset()));
1320+
// We can't get here in a thread that has completed its execution and so
1321+
// "is_terminated", but a thread is also considered terminated if the VM
1322+
// has exited, so we have to check this and block in case this is a daemon
1323+
// thread returning to the VM (the JNI DirectBuffer entry points rely on
1324+
// this).
1325+
if (current->is_terminated()) {
1326+
current->block_if_vm_exited();
1327+
ShouldNotReachHere();
13241328
}
1329+
return current;
13251330
}
13261331

13271332
// JNI critical regions. These can nest.

0 commit comments

Comments
 (0)
Please sign in to comment.