Skip to content

Commit d32ff13

Browse files
committed
8353117: Crash: assert(id >= ThreadIdentifier::initial() && id < ThreadIdentifier::current()) failed: must be reasonable)
Reviewed-by: dholmes, fbredberg
1 parent a0677d9 commit d32ff13

File tree

7 files changed

+24
-10
lines changed

7 files changed

+24
-10
lines changed

src/hotspot/share/prims/jni.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3804,6 +3804,10 @@ static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool dae
38043804

38053805
thread->cache_global_variables();
38063806

3807+
// Set the _monitor_owner_id to the next thread_id temporarily while initialization runs.
3808+
// Do it now before we make this thread visible in Threads::add().
3809+
thread->set_monitor_owner_id(ThreadIdentifier::next());
3810+
38073811
// This thread will not do a safepoint check, since it has
38083812
// not been added to the Thread list yet.
38093813
{ MutexLocker ml(Threads_lock);
@@ -3843,6 +3847,9 @@ static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool dae
38433847
return JNI_ERR;
38443848
}
38453849

3850+
// Update the _monitor_owner_id with the tid value.
3851+
thread->set_monitor_owner_id(java_lang_Thread::thread_id(thread->threadObj()));
3852+
38463853
// Want this inside 'attaching via jni'.
38473854
JFR_ONLY(Jfr::on_thread_start(thread);)
38483855

src/hotspot/share/runtime/javaThread.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,6 @@ void JavaThread::allocate_threadObj(Handle thread_group, const char* thread_name
236236
// constructor calls Thread.current(), which must be set here.
237237
java_lang_Thread::set_thread(thread_oop(), this);
238238
set_threadOopHandles(thread_oop());
239-
// Set the _monitor_owner_id to the next thread_id temporarily while initialization runs.
240-
set_monitor_owner_id(ThreadIdentifier::next());
241239

242240
JavaValue result(T_VOID);
243241
if (thread_name != nullptr) {
@@ -263,8 +261,6 @@ void JavaThread::allocate_threadObj(Handle thread_group, const char* thread_name
263261
Handle(),
264262
CHECK);
265263
}
266-
// Update the _monitor_owner_id with the tid value.
267-
set_monitor_owner_id(java_lang_Thread::thread_id(thread_oop()));
268264

269265
os::set_priority(this, NormPriority);
270266

src/hotspot/share/runtime/javaThread.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,14 @@ class JavaThread: public Thread {
170170

171171
public:
172172
void set_monitor_owner_id(int64_t id) {
173-
assert(id >= ThreadIdentifier::initial() && id < ThreadIdentifier::current(), "");
173+
ThreadIdentifier::verify_id(id);
174174
_monitor_owner_id = id;
175175
}
176-
int64_t monitor_owner_id() const { return _monitor_owner_id; }
176+
int64_t monitor_owner_id() const {
177+
int64_t id = _monitor_owner_id;
178+
ThreadIdentifier::verify_id(id);
179+
return id;
180+
}
177181

178182
// For tracking the heavyweight monitor the thread is pending on.
179183
ObjectMonitor* current_pending_monitor() {

src/hotspot/share/runtime/objectMonitor.inline.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,12 @@
4141
#include "utilities/globalDefinitions.hpp"
4242

4343
inline int64_t ObjectMonitor::owner_id_from(JavaThread* thread) {
44-
int64_t id = thread->monitor_owner_id();
45-
assert(id >= ThreadIdentifier::initial() && id < ThreadIdentifier::current(), "must be reasonable");
46-
return id;
44+
return thread->monitor_owner_id();
4745
}
4846

4947
inline int64_t ObjectMonitor::owner_id_from(oop vthread) {
5048
int64_t id = java_lang_Thread::thread_id(vthread);
51-
assert(id >= ThreadIdentifier::initial() && id < ThreadIdentifier::current(), "must be reasonable");
49+
ThreadIdentifier::verify_id(id);
5250
return id;
5351
}
5452

src/hotspot/share/runtime/threadIdentifier.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,10 @@ int64_t ThreadIdentifier::next() {
4848
} while (Atomic::cmpxchg(&next_thread_id, next_tid, next_tid + 1) != next_tid);
4949
return next_tid;
5050
}
51+
52+
#ifdef ASSERT
53+
void ThreadIdentifier::verify_id(int64_t id) {
54+
int64_t current_id = current();
55+
assert(id >= initial() && id < current_id, "invalid id, " INT64_FORMAT " and current is " INT64_FORMAT, id, current_id);
56+
}
57+
#endif

src/hotspot/share/runtime/threadIdentifier.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class ThreadIdentifier : AllStatic {
3939
static int64_t current();
4040
static int64_t unsafe_offset();
4141
static int64_t initial();
42+
static void verify_id(int64_t id) NOT_DEBUG_RETURN;
4243
};
4344

4445
#endif // SHARE_RUNTIME_THREADIDENTIFIER_HPP

src/hotspot/share/runtime/threads.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ jboolean Threads::is_supported_jni_version(jint version) {
10311031
void Threads::add(JavaThread* p, bool force_daemon) {
10321032
// The threads lock must be owned at this point
10331033
assert(Threads_lock->owned_by_self(), "must have threads lock");
1034+
assert(p->monitor_owner_id() != 0, "should be set");
10341035

10351036
BarrierSet::barrier_set()->on_thread_attach(p);
10361037

0 commit comments

Comments
 (0)