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