1 /*
2 * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "classfile/classLoaderData.inline.hpp"
26 #include "classfile/placeholders.hpp"
27 #include "logging/log.hpp"
28 #include "logging/logStream.hpp"
29 #include "logging/logTag.hpp"
30 #include "memory/resourceArea.hpp"
31 #include "oops/symbolHandle.hpp"
32 #include "runtime/javaThread.hpp"
33 #include "runtime/mutexLocker.hpp"
34 #include "utilities/hashTable.hpp"
35
36 class PlaceholderKey {
37 SymbolHandle _name;
38 ClassLoaderData* _loader_data;
39 public:
40 PlaceholderKey(Symbol* name, ClassLoaderData* l) : _name(name), _loader_data(l) {}
41
42 static bool equals(PlaceholderKey const& k1, PlaceholderKey const& k2) {
43 return (k1._name == k2._name && k1._loader_data == k2._loader_data);
44 }
45 static unsigned hash(PlaceholderKey const& k) {
46 return (unsigned) k._name->identity_hash() ^ (int)((intptr_t)k._loader_data >> 3);
47 }
48 void print_on(outputStream* st) const;
49 };
50
51 const int _placeholder_table_size = 503; // Does this really have to be prime?
52 using InternalPlaceholderTable = HashTable<PlaceholderKey, PlaceholderEntry, _placeholder_table_size, AnyObj::C_HEAP, mtClass,
53 PlaceholderKey::hash, PlaceholderKey::equals>;
54 static InternalPlaceholderTable* _placeholders;
55
56 // SeenThread objects represent list of threads that are
57 // currently performing a load action on a class.
58 // For class circularity, set before loading a superclass.
59 // For bootclasssearchpath, set before calling load_instance_class.
60 // Defining must be single threaded on a class/classloader basis
61 // For DEFINE_CLASS, the head of the queue owns the
62 // define token and the rest of the threads wait to return the
63 // result the first thread gets.
64 // For DETECT_CIRCULARITY, set when loading super class, interfaces, or inline type
65 // fields for class circularity checking.
66 class SeenThread: public CHeapObj<mtInternal> {
67 private:
68 JavaThread* _thread;
69 SeenThread* _stnext;
70 SeenThread* _stprev;
71 public:
72 SeenThread(JavaThread* thread) {
73 _thread = thread;
74 _stnext = nullptr;
75 _stprev = nullptr;
76 }
77 JavaThread* thread() const { return _thread;}
78 void set_thread(JavaThread* thread) { _thread = thread; }
79
80 SeenThread* next() const { return _stnext;}
81 void set_next(SeenThread* seen) { _stnext = seen; }
82 void set_prev(SeenThread* seen) { _stprev = seen; }
83
84 static void print_action_queue(SeenThread* seen, outputStream* st) {
85 while (seen != nullptr) {
86 seen->thread()->print_value_on(st);
87 st->print(", ");
88 seen = seen->next();
89 }
90 }
91 };
92
93 SeenThread* PlaceholderEntry::actionToQueue(PlaceholderTable::classloadAction action) {
94 SeenThread* queuehead = nullptr;
95 switch (action) {
96 case PlaceholderTable::LOAD_INSTANCE:
97 queuehead = _loadInstanceThreadQ;
98 break;
99 case PlaceholderTable::DETECT_CIRCULARITY:
100 queuehead = _circularityThreadQ;
101 break;
102 case PlaceholderTable::DEFINE_CLASS:
103 queuehead = _defineThreadQ;
104 break;
105 default: Unimplemented();
106 }
107 return queuehead;
108 }
109
110 void PlaceholderEntry::set_threadQ(SeenThread* seenthread, PlaceholderTable::classloadAction action) {
111 switch (action) {
112 case PlaceholderTable::LOAD_INSTANCE:
113 _loadInstanceThreadQ = seenthread;
114 break;
115 case PlaceholderTable::DETECT_CIRCULARITY:
116 _circularityThreadQ = seenthread;
117 break;
118 case PlaceholderTable::DEFINE_CLASS:
119 _defineThreadQ = seenthread;
120 break;
121 default: Unimplemented();
122 }
123 return;
124 }
125
126 // Doubly-linked list of Threads per action for class/classloader pair
127 // Class circularity support: links in thread before loading superclass
128 // bootstrap loader support: links in a thread before load_instance_class
129 // definers: use as queue of define requestors, including owner of
130 // define token. Appends for debugging of requestor order
131 void PlaceholderEntry::add_seen_thread(JavaThread* thread, PlaceholderTable::classloadAction action) {
132 assert_lock_strong(SystemDictionary_lock);
133 SeenThread* threadEntry = new SeenThread(thread);
134 SeenThread* seen = actionToQueue(action);
135
136 if (seen == nullptr) {
137 set_threadQ(threadEntry, action);
138 return;
139 }
140 SeenThread* next;
141 while ((next = seen->next()) != nullptr) {
142 seen = next;
143 }
144 seen->set_next(threadEntry);
145 threadEntry->set_prev(seen);
146 return;
147 }
148
149 bool PlaceholderEntry::check_seen_thread(JavaThread* thread, PlaceholderTable::classloadAction action) {
150 assert_lock_strong(SystemDictionary_lock);
151 SeenThread* threadQ = actionToQueue(action);
152 SeenThread* seen = threadQ;
153 while (seen) {
154 if (thread == seen->thread()) {
155 return true;
156 }
157 seen = seen->next();
158 }
159 return false;
160 }
161
162 // returns true if seenthreadQ is now empty
163 // Note, caller must ensure probe still exists while holding
164 // SystemDictionary_lock
165 // ignores if cleanup has already been done
166 // if found, deletes SeenThread
167 bool PlaceholderEntry::remove_seen_thread(JavaThread* thread, PlaceholderTable::classloadAction action) {
168 assert_lock_strong(SystemDictionary_lock);
169 SeenThread* threadQ = actionToQueue(action);
170 SeenThread* seen = threadQ;
171 SeenThread* prev = nullptr;
172 while (seen) {
173 if (thread == seen->thread()) {
174 if (prev) {
175 prev->set_next(seen->next());
176 } else {
177 set_threadQ(seen->next(), action);
178 }
179 if (seen->next()) {
180 seen->next()->set_prev(prev);
181 }
182 delete seen;
183 break;
184 }
185 prev = seen;
186 seen = seen->next();
187 }
188 return (actionToQueue(action) == nullptr);
189 }
190
191
192 void PlaceholderEntry::set_next_klass_name(Symbol* next_klass_name) {
193 assert_locked_or_safepoint(SystemDictionary_lock);
194 assert(_next_klass_name == nullptr || _next_klass_name->refcount() > 1, "must be referenced also by the loader");
195 _next_klass_name = next_klass_name;
196 }
197
198 // Placeholder objects represent classes currently being loaded.
199 // All threads examining the placeholder table must hold the
200 // SystemDictionary_lock, so we don't need special precautions
201 // on store ordering here.
202 static PlaceholderEntry* add_entry(Symbol* class_name, ClassLoaderData* loader_data,
203 Symbol* next_klass_name){
204 assert_locked_or_safepoint(SystemDictionary_lock);
205 assert(class_name != nullptr, "adding nullptr obj");
206
207 PlaceholderEntry entry;
208 entry.set_next_klass_name(next_klass_name);
209 PlaceholderKey key(class_name, loader_data);
210 bool created;
211 PlaceholderEntry* table_copy = _placeholders->put_if_absent(key, entry, &created);
212 assert(created, "better be absent");
213 return table_copy;
214 }
215
216 // Remove a placeholder object.
217 static void remove_entry(Symbol* class_name, ClassLoaderData* loader_data) {
218 assert_locked_or_safepoint(SystemDictionary_lock);
219
220 PlaceholderKey key(class_name, loader_data);
221 _placeholders->remove(key);
222 }
223
224
225 PlaceholderEntry* PlaceholderTable::get_entry(Symbol* class_name, ClassLoaderData* loader_data) {
226 assert_locked_or_safepoint(SystemDictionary_lock);
227 PlaceholderKey key(class_name, loader_data);
228 return _placeholders->get(key);
229 }
230
231 static const char* action_to_string(PlaceholderTable::classloadAction action) {
232 switch (action) {
233 case PlaceholderTable::LOAD_INSTANCE: return "LOAD_INSTANCE";
234 case PlaceholderTable::DETECT_CIRCULARITY: return "DETECT_CIRCULARITY";
235 case PlaceholderTable::DEFINE_CLASS: return "DEFINE_CLASS";
236 }
237 return "";
238 }
239
240 inline void log(Symbol* name, PlaceholderEntry* entry, const char* function, PlaceholderTable::classloadAction action) {
241 if (log_is_enabled(Debug, class, load, placeholders)) {
242 LogTarget(Debug, class, load, placeholders) lt;
243 ResourceMark rm;
244 LogStream ls(lt);
245 ls.print("entry %s : %s %s ", name->as_C_string(), function, action_to_string(action));
246 entry->print_on(&ls);
247 }
248 }
249
250 // find_and_add returns probe pointer - old or new
251 // If no entry exists, add a placeholder entry
252 // If entry exists, reuse entry
253 // For both, push SeenThread for classloadAction
254 // If DETECT_CIRCULARITY, this is used for circularity detection for instanceklass loading.
255 PlaceholderEntry* PlaceholderTable::find_and_add(Symbol* name,
256 ClassLoaderData* loader_data,
257 classloadAction action,
258 Symbol* next_klass_name,
259 JavaThread* thread) {
260 assert(action != DETECT_CIRCULARITY || next_klass_name != nullptr,
261 "must have a class name for the next step in the class resolution recursion");
262 PlaceholderEntry* probe = get_entry(name, loader_data);
263 if (probe == nullptr) {
264 // Nothing found, add place holder
265 probe = add_entry(name, loader_data, next_klass_name);
266 } else {
267 if (action == DETECT_CIRCULARITY) {
268 probe->set_next_klass_name(next_klass_name);
269 }
270 }
271 probe->add_seen_thread(thread, action);
272 log(name, probe, "find_and_add", action);
273 return probe;
274 }
275
276 void PlaceholderTable::initialize(){
277 _placeholders = new (mtClass) InternalPlaceholderTable();
278 }
279
280
281 // placeholder is used to track class loading internal states
282 // superthreadQ tracks class circularity, while loading superclass/superinterface
283 // loadInstanceThreadQ tracks load_instance_class calls
284 // definer() tracks the single thread that owns define token
285 // defineThreadQ tracks waiters on defining thread's results
286 // 1st claimant creates placeholder
287 // find_and_add adds SeenThread entry for appropriate queue
288 // All claimants remove SeenThread after completing action
289 // On removal: if definer and all queues empty, remove entry
290 // Note: you can be in both placeholders and systemDictionary
291 // Therefore - must always check SD first
292 void PlaceholderTable::find_and_remove(Symbol* name, ClassLoaderData* loader_data,
293 classloadAction action,
294 JavaThread* thread) {
295 assert_locked_or_safepoint(SystemDictionary_lock);
296 PlaceholderEntry* probe = get_entry(name, loader_data);
297 assert(probe != nullptr, "must find an entry");
298 log(name, probe, "find_and_remove", action);
299 probe->remove_seen_thread(thread, action);
300 if (probe->circularityThreadQ() == nullptr) {
301 probe->set_next_klass_name(nullptr);
302 }
303 // If no other threads using this entry, and this thread is not using this entry for other states
304 if ((probe->circularityThreadQ() == nullptr) && (probe->loadInstanceThreadQ() == nullptr)
305 && (probe->defineThreadQ() == nullptr) && (probe->definer() == nullptr)) {
306 remove_entry(name, loader_data);
307 }
308 }
309
310 void PlaceholderKey::print_on(outputStream* st) const {
311 _name->print_value_on(st);
312 st->print(", loader ");
313 _loader_data->print_value_on(st);
314 }
315
316 void PlaceholderEntry::print_on(outputStream* st) const {
317 if (next_klass_name() != nullptr) {
318 st->print(", next_klass_name ");
319 next_klass_name()->print_value_on(st);
320 }
321 if (definer() != nullptr) {
322 st->print(", definer ");
323 definer()->print_value_on(st);
324 }
325 if (instance_klass() != nullptr) {
326 st->print(", InstanceKlass ");
327 instance_klass()->print_value_on(st);
328 }
329 st->cr();
330 st->print("loadInstanceThreadQ threads:");
331 SeenThread::print_action_queue(loadInstanceThreadQ(), st);
332 st->cr();
333 st->print("circularityThreadQ threads:");
334 SeenThread::print_action_queue(circularityThreadQ(), st);
335 st->cr();
336 st->print("defineThreadQ threads:");
337 SeenThread::print_action_queue(defineThreadQ(), st);
338 st->cr();
339 }
340
341 void PlaceholderTable::print_on(outputStream* st) {
342 auto printer = [&] (PlaceholderKey& key, PlaceholderEntry& entry) {
343 st->print("placeholder ");
344 key.print_on(st);
345 entry.print_on(st);
346 return true;
347 };
348 st->print_cr("Placeholder table (table_size=%d, placeholders=%d)",
349 _placeholders->table_size(), _placeholders->number_of_entries());
350 _placeholders->iterate(printer);
351 }
352
353 void PlaceholderTable::print() { return print_on(tty); }