1 /* 2 * Copyright (c) 2003, 2021, 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 "runtime/mutexLocker.hpp" 33 #include "runtime/thread.hpp" 34 #include "utilities/hashtable.inline.hpp" 35 36 // SeenThread objects represent list of threads that are 37 // currently performing a load action on a class. 38 // For class circularity, set before loading a superclass. 39 // For bootclasssearchpath, set before calling load_instance_class. 40 // Defining must be single threaded on a class/classloader basis 41 // For DEFINE_CLASS, the head of the queue owns the 42 // define token and the rest of the threads wait to return the 43 // result the first thread gets. 44 class SeenThread: public CHeapObj<mtInternal> { 45 private: 46 JavaThread* _thread; 47 SeenThread* _stnext; 48 SeenThread* _stprev; 49 public: 50 SeenThread(JavaThread* thread) { 51 _thread = thread; 52 _stnext = NULL; 53 _stprev = NULL; 54 } 55 JavaThread* thread() const { return _thread;} 56 void set_thread(JavaThread* thread) { _thread = thread; } 57 58 SeenThread* next() const { return _stnext;} 59 void set_next(SeenThread* seen) { _stnext = seen; } 60 void set_prev(SeenThread* seen) { _stprev = seen; } 61 62 void print_action_queue(outputStream* st) { 63 SeenThread* seen = this; 64 while (seen != NULL) { 65 seen->thread()->print_value_on(st); 66 st->print(", "); 67 seen = seen->next(); 68 } 69 } 70 }; 71 72 SeenThread* PlaceholderEntry::actionToQueue(PlaceholderTable::classloadAction action) { 73 SeenThread* queuehead = NULL; 74 switch (action) { 75 case PlaceholderTable::LOAD_INSTANCE: 76 queuehead = _loadInstanceThreadQ; 77 break; 78 case PlaceholderTable::LOAD_SUPER: 79 queuehead = _superThreadQ; 80 break; 81 case PlaceholderTable::DEFINE_CLASS: 82 queuehead = _defineThreadQ; 83 break; 84 default: Unimplemented(); 85 } 86 return queuehead; 87 } 88 89 void PlaceholderEntry::set_threadQ(SeenThread* seenthread, PlaceholderTable::classloadAction action) { 90 switch (action) { 91 case PlaceholderTable::LOAD_INSTANCE: 92 _loadInstanceThreadQ = seenthread; 93 break; 94 case PlaceholderTable::LOAD_SUPER: 95 _superThreadQ = seenthread; 96 break; 97 case PlaceholderTable::DEFINE_CLASS: 98 _defineThreadQ = seenthread; 99 break; 100 default: Unimplemented(); 101 } 102 return; 103 } 104 105 // Doubly-linked list of Threads per action for class/classloader pair 106 // Class circularity support: links in thread before loading superclass 107 // bootstrap loader support: links in a thread before load_instance_class 108 // definers: use as queue of define requestors, including owner of 109 // define token. Appends for debugging of requestor order 110 void PlaceholderEntry::add_seen_thread(JavaThread* thread, PlaceholderTable::classloadAction action) { 111 assert_lock_strong(SystemDictionary_lock); 112 SeenThread* threadEntry = new SeenThread(thread); 113 SeenThread* seen = actionToQueue(action); 114 115 assert(action != PlaceholderTable::LOAD_INSTANCE || seen == NULL, 116 "Only one LOAD_INSTANCE allowed at a time"); 117 118 if (seen == NULL) { 119 set_threadQ(threadEntry, action); 120 return; 121 } 122 SeenThread* next; 123 while ((next = seen->next()) != NULL) { 124 seen = next; 125 } 126 seen->set_next(threadEntry); 127 threadEntry->set_prev(seen); 128 return; 129 } 130 131 bool PlaceholderEntry::check_seen_thread(JavaThread* thread, PlaceholderTable::classloadAction action) { 132 assert_lock_strong(SystemDictionary_lock); 133 SeenThread* threadQ = actionToQueue(action); 134 SeenThread* seen = threadQ; 135 while (seen) { 136 if (thread == seen->thread()) { 137 return true; 138 } 139 seen = seen->next(); 140 } 141 return false; 142 } 143 144 // returns true if seenthreadQ is now empty 145 // Note, caller must ensure probe still exists while holding 146 // SystemDictionary_lock 147 // ignores if cleanup has already been done 148 // if found, deletes SeenThread 149 bool PlaceholderEntry::remove_seen_thread(JavaThread* thread, PlaceholderTable::classloadAction action) { 150 assert_lock_strong(SystemDictionary_lock); 151 SeenThread* threadQ = actionToQueue(action); 152 SeenThread* seen = threadQ; 153 SeenThread* prev = NULL; 154 while (seen) { 155 if (thread == seen->thread()) { 156 if (prev) { 157 prev->set_next(seen->next()); 158 } else { 159 set_threadQ(seen->next(), action); 160 } 161 if (seen->next()) { 162 seen->next()->set_prev(prev); 163 } 164 delete seen; 165 break; 166 } 167 prev = seen; 168 seen = seen->next(); 169 } 170 return (actionToQueue(action) == NULL); 171 } 172 173 174 // Placeholder methods 175 176 PlaceholderEntry* PlaceholderTable::new_entry(int hash, Symbol* name, 177 ClassLoaderData* loader_data, 178 Symbol* supername) { 179 PlaceholderEntry* entry = (PlaceholderEntry*)Hashtable<Symbol*, mtClass>::new_entry(hash, name); 180 // Hashtable with Symbol* literal must increment and decrement refcount. 181 name->increment_refcount(); 182 entry->set_loader_data(loader_data); 183 entry->set_supername(supername); 184 entry->set_superThreadQ(NULL); 185 entry->set_loadInstanceThreadQ(NULL); 186 entry->set_defineThreadQ(NULL); 187 entry->set_definer(NULL); 188 entry->set_instance_klass(NULL); 189 return entry; 190 } 191 192 void PlaceholderTable::free_entry(PlaceholderEntry* entry) { 193 // decrement Symbol refcount here because Hashtable doesn't. 194 entry->literal()->decrement_refcount(); 195 if (entry->supername() != NULL) entry->supername()->decrement_refcount(); 196 BasicHashtable<mtClass>::free_entry(entry); 197 } 198 199 200 // Placeholder objects represent classes currently being loaded. 201 // All threads examining the placeholder table must hold the 202 // SystemDictionary_lock, so we don't need special precautions 203 // on store ordering here. 204 PlaceholderEntry* PlaceholderTable::add_entry(unsigned int hash, 205 Symbol* class_name, ClassLoaderData* loader_data, 206 Symbol* supername){ 207 assert_locked_or_safepoint(SystemDictionary_lock); 208 assert(class_name != NULL, "adding NULL obj"); 209 210 // Both readers and writers are locked so it's safe to just 211 // create the placeholder and insert it in the list without a membar. 212 PlaceholderEntry* entry = new_entry(hash, class_name, loader_data, supername); 213 int index = hash_to_index(hash); 214 Hashtable<Symbol*, mtClass>::add_entry(index, entry); 215 return entry; 216 } 217 218 219 // Remove a placeholder object. 220 void PlaceholderTable::remove_entry(unsigned int hash, 221 Symbol* class_name, 222 ClassLoaderData* loader_data) { 223 assert_locked_or_safepoint(SystemDictionary_lock); 224 int index = hash_to_index(hash); 225 PlaceholderEntry** p = bucket_addr(index); 226 while (*p != NULL) { 227 PlaceholderEntry *probe = *p; 228 if (probe->hash() == hash && probe->equals(class_name, loader_data)) { 229 // Delete entry 230 *p = probe->next(); 231 free_entry(probe); 232 return; 233 } 234 p = probe->next_addr(); 235 } 236 } 237 238 PlaceholderEntry* PlaceholderTable::get_entry(unsigned int hash, 239 Symbol* class_name, 240 ClassLoaderData* loader_data) { 241 assert_locked_or_safepoint(SystemDictionary_lock); 242 243 int index = hash_to_index(hash); 244 for (PlaceholderEntry *place_probe = bucket(index); 245 place_probe != NULL; 246 place_probe = place_probe->next()) { 247 if (place_probe->hash() == hash && 248 place_probe->equals(class_name, loader_data)) { 249 return place_probe; 250 } 251 } 252 return NULL; 253 } 254 255 Symbol* PlaceholderTable::find_entry(unsigned int hash, 256 Symbol* class_name, 257 ClassLoaderData* loader_data) { 258 PlaceholderEntry* probe = get_entry(hash, class_name, loader_data); 259 return (probe != NULL ? probe->klassname() : NULL); 260 } 261 262 static const char* action_to_string(PlaceholderTable::classloadAction action) { 263 switch (action) { 264 case PlaceholderTable::LOAD_INSTANCE: return "LOAD_INSTANCE"; 265 case PlaceholderTable::LOAD_SUPER: return "LOAD_SUPER"; 266 case PlaceholderTable::DEFINE_CLASS: return "DEFINE_CLASS"; 267 } 268 return ""; 269 } 270 271 inline void log(PlaceholderEntry* entry, const char* function, PlaceholderTable::classloadAction action) { 272 if (log_is_enabled(Debug, class, load, placeholders)) { 273 LogTarget(Debug, class, load, placeholders) lt; 274 ResourceMark rm; 275 LogStream ls(lt); 276 ls.print("%s %s ", function, action_to_string(action)); 277 entry->print_entry(&ls); 278 } 279 } 280 281 // find_and_add returns probe pointer - old or new 282 // If no entry exists, add a placeholder entry 283 // If entry exists, reuse entry 284 // For both, push SeenThread for classloadAction 285 // If LOAD_SUPER, this is used for circularity detection for instanceklass loading. 286 PlaceholderEntry* PlaceholderTable::find_and_add(unsigned int hash, 287 Symbol* name, 288 ClassLoaderData* loader_data, 289 classloadAction action, 290 Symbol* supername, 291 JavaThread* thread) { 292 assert(action != LOAD_SUPER || supername != NULL, "must have a super class name"); 293 PlaceholderEntry* probe = get_entry(hash, name, loader_data); 294 if (probe == NULL) { 295 // Nothing found, add place holder 296 probe = add_entry(hash, name, loader_data, supername); 297 } else { 298 if (action == LOAD_SUPER) { 299 probe->set_supername(supername); 300 } 301 } 302 probe->add_seen_thread(thread, action); 303 log(probe, "find_and_add", action); 304 return probe; 305 } 306 307 308 // placeholder is used to track class loading internal states 309 // placeholder existence now for loading superclass/superinterface 310 // superthreadQ tracks class circularity, while loading superclass/superinterface 311 // loadInstanceThreadQ tracks load_instance_class calls 312 // definer() tracks the single thread that owns define token 313 // defineThreadQ tracks waiters on defining thread's results 314 // 1st claimant creates placeholder 315 // find_and_add adds SeenThread entry for appropriate queue 316 // All claimants remove SeenThread after completing action 317 // On removal: if definer and all queues empty, remove entry 318 // Note: you can be in both placeholders and systemDictionary 319 // Therefore - must always check SD first 320 // Ignores the case where entry is not found 321 void PlaceholderTable::find_and_remove(unsigned int hash, 322 Symbol* name, ClassLoaderData* loader_data, 323 classloadAction action, 324 JavaThread* thread) { 325 assert_locked_or_safepoint(SystemDictionary_lock); 326 PlaceholderEntry *probe = get_entry(hash, name, loader_data); 327 if (probe != NULL) { 328 log(probe, "find_and_remove", action); 329 probe->remove_seen_thread(thread, action); 330 // If no other threads using this entry, and this thread is not using this entry for other states 331 if ((probe->superThreadQ() == NULL) && (probe->loadInstanceThreadQ() == NULL) 332 && (probe->defineThreadQ() == NULL) && (probe->definer() == NULL)) { 333 remove_entry(hash, name, loader_data); 334 } 335 } 336 } 337 338 PlaceholderTable::PlaceholderTable(int table_size) 339 : Hashtable<Symbol*, mtClass>(table_size, sizeof(PlaceholderEntry)) { 340 } 341 342 void PlaceholderEntry::verify() const { 343 guarantee(loader_data() != NULL, "Must have been setup."); 344 guarantee(loader_data()->class_loader() == NULL || loader_data()->class_loader()->is_instance(), 345 "checking type of _loader"); 346 guarantee(instance_klass() == NULL 347 || instance_klass()->is_instance_klass(), 348 "checking type of instance_klass result"); 349 } 350 351 void PlaceholderTable::verify() { 352 verify_table<PlaceholderEntry>("Placeholder Table"); 353 } 354 355 356 // Note, doesn't append a cr 357 // Can't call this print_on because HashtableEntry doesn't initialize its vptr 358 // and print_on is a virtual function so the vptr call crashes. 359 void PlaceholderEntry::print_entry(outputStream* st) const { 360 klassname()->print_value_on(st); 361 if (loader_data() != NULL) { 362 st->print(", loader "); 363 loader_data()->print_value_on(st); 364 } 365 if (supername() != NULL) { 366 st->print(", supername "); 367 supername()->print_value_on(st); 368 } 369 if (definer() != NULL) { 370 st->print(", definer "); 371 definer()->print_value_on(st); 372 } 373 if (instance_klass() != NULL) { 374 st->print(", InstanceKlass "); 375 instance_klass()->print_value_on(st); 376 } 377 st->cr(); 378 st->print("loadInstanceThreadQ threads:"); 379 loadInstanceThreadQ()->print_action_queue(st); 380 st->cr(); 381 st->print("superThreadQ threads:"); 382 superThreadQ()->print_action_queue(st); 383 st->cr(); 384 st->print("defineThreadQ threads:"); 385 defineThreadQ()->print_action_queue(st); 386 st->cr(); 387 } 388 389 void PlaceholderTable::print_on(outputStream* st) const { 390 st->print_cr("Placeholder table (table_size=%d, placeholders=%d)", 391 table_size(), number_of_entries()); 392 for (int pindex = 0; pindex < table_size(); pindex++) { 393 for (PlaceholderEntry* probe = bucket(pindex); 394 probe != NULL; 395 probe = probe->next()) { 396 st->print("%4d: placeholder ", pindex); 397 probe->print_entry(st); 398 } 399 } 400 } 401 402 void PlaceholderTable::print() const { return print_on(tty); }