Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.

Commit 6f948a7

Browse files
committed
8267842: SIGSEGV in get_current_contended_monitor
Reviewed-by: dcubed Backport-of: 1e29005a22c7951242cf3b0d8cf2e6adc0b7b315
1 parent 53dd617 commit 6f948a7

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/hotspot/share/runtime/thread.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,15 +407,15 @@ class Thread: public ThreadShadow {
407407

408408
JFR_ONLY(DEFINE_THREAD_LOCAL_FIELD_JFR;) // Thread-local data for jfr
409409

410-
ObjectMonitor* _current_pending_monitor; // ObjectMonitor this thread
410+
ObjectMonitor* volatile _current_pending_monitor; // ObjectMonitor this thread
411411
// is waiting to lock
412412
bool _current_pending_monitor_is_from_java; // locking is from Java code
413413
JvmtiRawMonitor* _current_pending_raw_monitor; // JvmtiRawMonitor this thread
414414
// is waiting to lock
415415

416416

417417
// ObjectMonitor on which this thread called Object.wait()
418-
ObjectMonitor* _current_waiting_monitor;
418+
ObjectMonitor* volatile _current_waiting_monitor;
419419

420420
#ifdef ASSERT
421421
private:
@@ -620,10 +620,13 @@ class Thread: public ThreadShadow {
620620

621621
// For tracking the heavyweight monitor the thread is pending on.
622622
ObjectMonitor* current_pending_monitor() {
623-
return _current_pending_monitor;
623+
// Use Atomic::load() to prevent data race between concurrent modification and
624+
// concurrent readers, e.g. ThreadService::get_current_contended_monitor().
625+
// Especially, reloading pointer from thread after NULL check must be prevented.
626+
return Atomic::load(&_current_pending_monitor);
624627
}
625628
void set_current_pending_monitor(ObjectMonitor* monitor) {
626-
_current_pending_monitor = monitor;
629+
Atomic::store(&_current_pending_monitor, monitor);
627630
}
628631
void set_current_pending_monitor_is_from_java(bool from_java) {
629632
_current_pending_monitor_is_from_java = from_java;
@@ -634,10 +637,11 @@ class Thread: public ThreadShadow {
634637

635638
// For tracking the ObjectMonitor on which this thread called Object.wait()
636639
ObjectMonitor* current_waiting_monitor() {
637-
return _current_waiting_monitor;
640+
// See the comment in current_pending_monitor() above.
641+
return Atomic::load(&_current_waiting_monitor);
638642
}
639643
void set_current_waiting_monitor(ObjectMonitor* monitor) {
640-
_current_waiting_monitor = monitor;
644+
Atomic::store(&_current_waiting_monitor, monitor);
641645
}
642646

643647
// For tracking the Jvmti raw monitor the thread is pending on.

src/hotspot/share/runtime/vmStructs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,9 +738,9 @@ typedef HashtableEntry<InstanceKlass*, mtClass> KlassHashtableEntry;
738738
nonstatic_field(Thread, _active_handles, JNIHandleBlock*) \
739739
nonstatic_field(Thread, _tlab, ThreadLocalAllocBuffer) \
740740
nonstatic_field(Thread, _allocated_bytes, jlong) \
741-
nonstatic_field(Thread, _current_pending_monitor, ObjectMonitor*) \
741+
volatile_nonstatic_field(Thread, _current_pending_monitor, ObjectMonitor*) \
742742
nonstatic_field(Thread, _current_pending_monitor_is_from_java, bool) \
743-
nonstatic_field(Thread, _current_waiting_monitor, ObjectMonitor*) \
743+
volatile_nonstatic_field(Thread, _current_waiting_monitor, ObjectMonitor*) \
744744
nonstatic_field(NamedThread, _name, char*) \
745745
nonstatic_field(NamedThread, _processed_thread, Thread*) \
746746
nonstatic_field(JavaThread, _threadObj, OopHandle) \

0 commit comments

Comments
 (0)