1 /* 2 * Copyright (c) 2003, 2022, 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 ResourceHashtable<PlaceholderKey, PlaceholderEntry, _placeholder_table_size, AnyObj::C_HEAP, mtClass, 54 PlaceholderKey::hash, PlaceholderKey::equals> _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 INLINE_FIELD, set when loading inline type fields for 65 // 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 = NULL; 75 _stprev = NULL; 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 void print_action_queue(outputStream* st) { 85 SeenThread* seen = this; 86 while (seen != NULL) { 87 seen->thread()->print_value_on(st); 88 st->print(", "); 89 seen = seen->next(); 90 } 91 } 92 }; 93 94 SeenThread* PlaceholderEntry::actionToQueue(PlaceholderTable::classloadAction action) { 95 SeenThread* queuehead = NULL; 96 switch (action) { 97 case PlaceholderTable::LOAD_INSTANCE: 98 queuehead = _loadInstanceThreadQ; 99 break; 100 case PlaceholderTable::LOAD_SUPER: 101 queuehead = _superThreadQ; 102 break; 103 case PlaceholderTable::DEFINE_CLASS: 104 queuehead = _defineThreadQ; 105 break; 106 case PlaceholderTable::PRIMITIVE_OBJECT_FIELD: 107 queuehead = _inlineTypeFieldQ; 108 break; 109 default: Unimplemented(); 110 } 111 return queuehead; 112 } 113 114 void PlaceholderEntry::set_threadQ(SeenThread* seenthread, PlaceholderTable::classloadAction action) { 115 switch (action) { 116 case PlaceholderTable::LOAD_INSTANCE: 117 _loadInstanceThreadQ = seenthread; 118 break; 119 case PlaceholderTable::LOAD_SUPER: 120 _superThreadQ = seenthread; 121 break; 122 case PlaceholderTable::DEFINE_CLASS: 123 _defineThreadQ = seenthread; 124 break; 125 case PlaceholderTable::PRIMITIVE_OBJECT_FIELD: 126 _inlineTypeFieldQ = seenthread; 127 break; 128 default: Unimplemented(); 129 } 130 return; 131 } 132 133 // Doubly-linked list of Threads per action for class/classloader pair 134 // Class circularity support: links in thread before loading superclass 135 // bootstrap loader support: links in a thread before load_instance_class 136 // definers: use as queue of define requestors, including owner of 137 // define token. Appends for debugging of requestor order 138 void PlaceholderEntry::add_seen_thread(JavaThread* thread, PlaceholderTable::classloadAction action) { 139 assert_lock_strong(SystemDictionary_lock); 140 SeenThread* threadEntry = new SeenThread(thread); 141 SeenThread* seen = actionToQueue(action); 142 143 assert(action != PlaceholderTable::LOAD_INSTANCE || !EnableWaitForParallelLoad || seen == NULL, 144 "Only one LOAD_INSTANCE allowed at a time"); 145 146 if (seen == NULL) { 147 set_threadQ(threadEntry, action); 148 return; 149 } 150 SeenThread* next; 151 while ((next = seen->next()) != NULL) { 152 seen = next; 153 } 154 seen->set_next(threadEntry); 155 threadEntry->set_prev(seen); 156 return; 157 } 158 159 bool PlaceholderEntry::check_seen_thread(JavaThread* thread, PlaceholderTable::classloadAction action) { 160 assert_lock_strong(SystemDictionary_lock); 161 SeenThread* threadQ = actionToQueue(action); 162 SeenThread* seen = threadQ; 163 while (seen) { 164 if (thread == seen->thread()) { 165 return true; 166 } 167 seen = seen->next(); 168 } 169 return false; 170 } 171 172 // returns true if seenthreadQ is now empty 173 // Note, caller must ensure probe still exists while holding 174 // SystemDictionary_lock 175 // ignores if cleanup has already been done 176 // if found, deletes SeenThread 177 bool PlaceholderEntry::remove_seen_thread(JavaThread* thread, PlaceholderTable::classloadAction action) { 178 assert_lock_strong(SystemDictionary_lock); 179 SeenThread* threadQ = actionToQueue(action); 180 SeenThread* seen = threadQ; 181 SeenThread* prev = NULL; 182 while (seen) { 183 if (thread == seen->thread()) { 184 if (prev) { 185 prev->set_next(seen->next()); 186 } else { 187 set_threadQ(seen->next(), action); 188 } 189 if (seen->next()) { 190 seen->next()->set_prev(prev); 191 } 192 delete seen; 193 break; 194 } 195 prev = seen; 196 seen = seen->next(); 197 } 198 return (actionToQueue(action) == NULL); 199 } 200 201 202 // Placeholder methods 203 204 // Placeholder objects represent classes currently being loaded. 205 // All threads examining the placeholder table must hold the 206 // SystemDictionary_lock, so we don't need special precautions 207 // on store ordering here. 208 PlaceholderEntry* add_entry(Symbol* class_name, ClassLoaderData* loader_data, 209 Symbol* supername){ 210 assert_locked_or_safepoint(SystemDictionary_lock); 211 assert(class_name != NULL, "adding NULL obj"); 212 213 PlaceholderEntry entry; 214 entry.set_supername(supername); 215 PlaceholderKey key(class_name, loader_data); 216 bool created; 217 PlaceholderEntry* table_copy = _placeholders.put_if_absent(key, entry, &created); 218 assert(created, "better be absent"); 219 return table_copy; 220 } 221 222 // Remove a placeholder object. 223 void remove_entry(Symbol* class_name, ClassLoaderData* loader_data) { 224 assert_locked_or_safepoint(SystemDictionary_lock); 225 226 PlaceholderKey key(class_name, loader_data); 227 _placeholders.remove(key); 228 } 229 230 231 PlaceholderEntry* PlaceholderTable::get_entry(Symbol* class_name, ClassLoaderData* loader_data) { 232 assert_locked_or_safepoint(SystemDictionary_lock); 233 PlaceholderKey key(class_name, loader_data); 234 return _placeholders.get(key); 235 } 236 237 static const char* action_to_string(PlaceholderTable::classloadAction action) { 238 switch (action) { 239 case PlaceholderTable::LOAD_INSTANCE: return "LOAD_INSTANCE"; 240 case PlaceholderTable::LOAD_SUPER: return "LOAD_SUPER"; 241 case PlaceholderTable::DEFINE_CLASS: return "DEFINE_CLASS"; 242 case PlaceholderTable::PRIMITIVE_OBJECT_FIELD: return "PRIMITIVE_OBJECT_FIELD"; 243 } 244 return ""; 245 } 246 247 inline void log(Symbol* name, PlaceholderEntry* entry, const char* function, PlaceholderTable::classloadAction action) { 248 if (log_is_enabled(Debug, class, load, placeholders)) { 249 LogTarget(Debug, class, load, placeholders) lt; 250 ResourceMark rm; 251 LogStream ls(lt); 252 ls.print("entry %s : %s %s ", name->as_C_string(), function, action_to_string(action)); 253 entry->print_on(&ls); 254 } 255 } 256 257 // find_and_add returns probe pointer - old or new 258 // If no entry exists, add a placeholder entry 259 // If entry exists, reuse entry 260 // For both, push SeenThread for classloadAction 261 // If LOAD_SUPER, this is used for circularity detection for instanceklass loading. 262 PlaceholderEntry* PlaceholderTable::find_and_add(Symbol* name, 263 ClassLoaderData* loader_data, 264 classloadAction action, 265 Symbol* supername, 266 JavaThread* thread) { 267 assert(action != LOAD_SUPER || supername != NULL, "must have a super class name"); 268 PlaceholderEntry* probe = get_entry(name, loader_data); 269 if (probe == NULL) { 270 // Nothing found, add place holder 271 probe = add_entry(name, loader_data, supername); 272 } else { 273 if (action == LOAD_SUPER) { 274 probe->set_supername(supername); 275 } 276 } 277 probe->add_seen_thread(thread, action); 278 log(name, probe, "find_and_add", action); 279 return probe; 280 } 281 282 283 // placeholder is used to track class loading internal states 284 // placeholder existence now for loading superclass/superinterface 285 // superthreadQ tracks class circularity, while loading superclass/superinterface 286 // loadInstanceThreadQ tracks load_instance_class calls 287 // definer() tracks the single thread that owns define token 288 // defineThreadQ tracks waiters on defining thread's results 289 // 1st claimant creates placeholder 290 // find_and_add adds SeenThread entry for appropriate queue 291 // All claimants remove SeenThread after completing action 292 // On removal: if definer and all queues empty, remove entry 293 // Note: you can be in both placeholders and systemDictionary 294 // Therefore - must always check SD first 295 // Ignores the case where entry is not found 296 void PlaceholderTable::find_and_remove(Symbol* name, ClassLoaderData* loader_data, 297 classloadAction action, 298 JavaThread* thread) { 299 assert_locked_or_safepoint(SystemDictionary_lock); 300 PlaceholderEntry* probe = get_entry(name, loader_data); 301 if (probe != NULL) { 302 log(name, probe, "find_and_remove", action); 303 probe->remove_seen_thread(thread, action); 304 // If no other threads using this entry, and this thread is not using this entry for other states 305 if ((probe->superThreadQ() == NULL) && (probe->loadInstanceThreadQ() == NULL) 306 && (probe->defineThreadQ() == NULL) && (probe->definer() == NULL) 307 && (probe->inlineTypeFieldQ() == NULL)) { 308 probe->clear_supername(); 309 remove_entry(name, loader_data); 310 } 311 } 312 } 313 314 void PlaceholderKey::print_on(outputStream* st) const { 315 _name->print_value_on(st); 316 st->print(", loader "); 317 _loader_data->print_value_on(st); 318 } 319 320 void PlaceholderEntry::print_on(outputStream* st) const { 321 if (supername() != NULL) { 322 st->print(", supername "); 323 supername()->print_value_on(st); 324 } 325 if (definer() != NULL) { 326 st->print(", definer "); 327 definer()->print_value_on(st); 328 } 329 if (instance_klass() != NULL) { 330 st->print(", InstanceKlass "); 331 instance_klass()->print_value_on(st); 332 } 333 st->cr(); 334 st->print("loadInstanceThreadQ threads:"); 335 loadInstanceThreadQ()->print_action_queue(st); 336 st->cr(); 337 st->print("superThreadQ threads:"); 338 superThreadQ()->print_action_queue(st); 339 st->cr(); 340 st->print("defineThreadQ threads:"); 341 defineThreadQ()->print_action_queue(st); 342 st->cr(); 343 st->print("inlineTypeFieldQ threads:"); 344 inlineTypeFieldQ()->print_action_queue(st); 345 st->cr(); 346 } 347 348 void PlaceholderTable::print_on(outputStream* st) { 349 auto printer = [&] (PlaceholderKey& key, PlaceholderEntry& entry) { 350 st->print("placeholder "); 351 key.print_on(st); 352 entry.print_on(st); 353 return true; 354 }; 355 st->print_cr("Placeholder table (table_size=%d, placeholders=%d)", 356 _placeholders.table_size(), _placeholders.number_of_entries()); 357 _placeholders.iterate(printer); 358 } 359 360 void PlaceholderTable::print() { return print_on(tty); }