Skip to content

Commit 4d0da18

Browse files
author
David Holmes
committed
8369250: Assess and remedy any unsafe usage of the Semaphore used by NonJavaThread::List
Reviewed-by: kbarrett, stefank
1 parent 92f2ab2 commit 4d0da18

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/hotspot/share/runtime/nonJavaThread.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,19 @@ class NonJavaThread::List {
5050
List() : _head(nullptr), _protect() {}
5151
};
5252

53-
NonJavaThread::List NonJavaThread::_the_list;
53+
DeferredStatic<NonJavaThread::List> NonJavaThread::_the_list;
54+
55+
void NonJavaThread::init() {
56+
_the_list.initialize();
57+
}
5458

5559
NonJavaThread::Iterator::Iterator() :
56-
_protect_enter(_the_list._protect.enter()),
57-
_current(AtomicAccess::load_acquire(&_the_list._head))
60+
_protect_enter(_the_list->_protect.enter()),
61+
_current(AtomicAccess::load_acquire(&_the_list->_head))
5862
{}
5963

6064
NonJavaThread::Iterator::~Iterator() {
61-
_the_list._protect.exit(_protect_enter);
65+
_the_list->_protect.exit(_protect_enter);
6266
}
6367

6468
void NonJavaThread::Iterator::step() {
@@ -76,16 +80,16 @@ void NonJavaThread::add_to_the_list() {
7680
MutexLocker ml(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag);
7781
// Initialize BarrierSet-related data before adding to list.
7882
BarrierSet::barrier_set()->on_thread_attach(this);
79-
AtomicAccess::release_store(&_next, _the_list._head);
80-
AtomicAccess::release_store(&_the_list._head, this);
83+
AtomicAccess::release_store(&_next, _the_list->_head);
84+
AtomicAccess::release_store(&_the_list->_head, this);
8185
}
8286

8387
void NonJavaThread::remove_from_the_list() {
8488
{
8589
MutexLocker ml(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag);
8690
// Cleanup BarrierSet-related data before removing from list.
8791
BarrierSet::barrier_set()->on_thread_detach(this);
88-
NonJavaThread* volatile* p = &_the_list._head;
92+
NonJavaThread* volatile* p = &_the_list->_head;
8993
for (NonJavaThread* t = *p; t != nullptr; p = &t->_next, t = *p) {
9094
if (t == this) {
9195
*p = _next;
@@ -97,7 +101,7 @@ void NonJavaThread::remove_from_the_list() {
97101
// allowed, so do it while holding a dedicated lock. Outside and distinct
98102
// from NJTList_lock in case an iteration attempts to lock it.
99103
MutexLocker ml(NonJavaThreadsListSync_lock, Mutex::_no_safepoint_check_flag);
100-
_the_list._protect.synchronize();
104+
_the_list->_protect.synchronize();
101105
_next = nullptr; // Safe to drop the link now.
102106
}
103107

@@ -344,4 +348,3 @@ void WatcherThread::print_on(outputStream* st) const {
344348
Thread::print_on(st);
345349
st->cr();
346350
}
347-

src/hotspot/share/runtime/nonJavaThread.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,12 +26,18 @@
2626
#define SHARE_RUNTIME_NONJAVATHREAD_HPP
2727

2828
#include "runtime/thread.hpp"
29+
#include "utilities/deferredStatic.hpp"
2930

3031
class NonJavaThread: public Thread {
31-
NonJavaThread* volatile _next;
32+
friend class Threads;
3233

3334
class List;
34-
static List _the_list;
35+
static DeferredStatic<List> _the_list;
36+
37+
// Deferred static initialization
38+
static void init();
39+
40+
NonJavaThread* volatile _next;
3541

3642
void add_to_the_list();
3743
void remove_from_the_list();

src/hotspot/share/runtime/threads.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,9 @@ jint Threads::create_vm(JavaVMInitArgs* args, bool* canTryAgain) {
446446
// Check version
447447
if (!is_supported_jni_version(args->version)) return JNI_EVERSION;
448448

449+
// Deferred "static" initialization
450+
NonJavaThread::init();
451+
449452
// Initialize library-based TLS
450453
ThreadLocalStorage::init();
451454

0 commit comments

Comments
 (0)