1 /* 2 * Copyright (c) 2003, 2017, 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/sharedClassUtil.hpp" 28 #include "classfile/dictionary.hpp" 29 #include "classfile/protectionDomainCache.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "classfile/systemDictionaryShared.hpp" 32 #include "memory/iterator.hpp" 33 #include "memory/metaspaceClosure.hpp" 34 #include "memory/resourceArea.hpp" 35 #include "oops/oop.inline.hpp" 36 #include "runtime/orderAccess.inline.hpp" 37 #include "utilities/hashtable.inline.hpp" 38 39 DictionaryEntry* Dictionary::_current_class_entry = NULL; 40 int Dictionary::_current_class_index = 0; 41 42 size_t Dictionary::entry_size() { 43 if (DumpSharedSpaces) { 44 return SystemDictionaryShared::dictionary_entry_size(); 45 } else { 46 return sizeof(DictionaryEntry); 47 } 48 } 49 50 Dictionary::Dictionary(int table_size) 51 : TwoOopHashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size()) { 52 _current_class_index = 0; 53 _current_class_entry = NULL; 54 _pd_cache_table = new ProtectionDomainCacheTable(defaultProtectionDomainCacheSize); 55 }; 56 57 58 Dictionary::Dictionary(int table_size, HashtableBucket<mtClass>* t, 59 int number_of_entries) 60 : TwoOopHashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size(), t, number_of_entries) { 61 _current_class_index = 0; 62 _current_class_entry = NULL; 63 _pd_cache_table = new ProtectionDomainCacheTable(defaultProtectionDomainCacheSize); 64 }; 65 66 ProtectionDomainCacheEntry* Dictionary::cache_get(Handle protection_domain) { 67 return _pd_cache_table->get(protection_domain); 68 } 69 70 DictionaryEntry* Dictionary::new_entry(unsigned int hash, InstanceKlass* klass, 71 ClassLoaderData* loader_data) { 72 DictionaryEntry* entry = (DictionaryEntry*)Hashtable<InstanceKlass*, mtClass>::new_entry(hash, klass); 73 entry->set_loader_data(loader_data); 74 entry->set_pd_set(NULL); 75 assert(klass->is_instance_klass(), "Must be"); 76 if (DumpSharedSpaces) { 77 SystemDictionaryShared::init_shared_dictionary_entry(klass, entry); 78 } 79 return entry; 80 } 81 82 83 void Dictionary::free_entry(DictionaryEntry* entry) { 84 // avoid recursion when deleting linked list 85 while (entry->pd_set() != NULL) { 86 ProtectionDomainEntry* to_delete = entry->pd_set(); 87 entry->set_pd_set(to_delete->next()); 88 delete to_delete; 89 } 90 Hashtable<InstanceKlass*, mtClass>::free_entry(entry); 91 } 92 93 94 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const { 95 #ifdef ASSERT 96 if (protection_domain == klass()->protection_domain()) { 97 // Ensure this doesn't show up in the pd_set (invariant) 98 bool in_pd_set = false; 99 for (ProtectionDomainEntry* current = _pd_set; 100 current != NULL; 101 current = current->next()) { 102 if (current->protection_domain() == protection_domain) { 103 in_pd_set = true; 104 break; 105 } 106 } 107 if (in_pd_set) { 108 assert(false, "A klass's protection domain should not show up " 109 "in its sys. dict. PD set"); 110 } 111 } 112 #endif /* ASSERT */ 113 114 if (protection_domain == klass()->protection_domain()) { 115 // Succeeds trivially 116 return true; 117 } 118 119 for (ProtectionDomainEntry* current = _pd_set; 120 current != NULL; 121 current = current->next()) { 122 if (current->protection_domain() == protection_domain) return true; 123 } 124 return false; 125 } 126 127 128 void DictionaryEntry::add_protection_domain(Dictionary* dict, Handle protection_domain) { 129 assert_locked_or_safepoint(SystemDictionary_lock); 130 if (!contains_protection_domain(protection_domain())) { 131 ProtectionDomainCacheEntry* entry = dict->cache_get(protection_domain); 132 ProtectionDomainEntry* new_head = 133 new ProtectionDomainEntry(entry, _pd_set); 134 // Warning: Preserve store ordering. The SystemDictionary is read 135 // without locks. The new ProtectionDomainEntry must be 136 // complete before other threads can be allowed to see it 137 // via a store to _pd_set. 138 OrderAccess::release_store_ptr(&_pd_set, new_head); 139 } 140 if (log_is_enabled(Trace, protectiondomain)) { 141 ResourceMark rm; 142 outputStream* log = Log(protectiondomain)::trace_stream(); 143 print_count(log); 144 } 145 } 146 147 148 void Dictionary::do_unloading() { 149 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 150 151 // Remove unloadable entries and classes from system dictionary 152 DictionaryEntry* probe = NULL; 153 for (int index = 0; index < table_size(); index++) { 154 for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) { 155 probe = *p; 156 Klass* e = probe->klass(); 157 ClassLoaderData* loader_data = probe->loader_data(); 158 159 InstanceKlass* ik = InstanceKlass::cast(e); 160 161 // Only unload classes that are not strongly reachable 162 if (!is_strongly_reachable(loader_data, e)) { 163 // Entry was not visited in phase1 (negated test from phase1) 164 assert(!loader_data->is_the_null_class_loader_data(), "unloading entry with null class loader"); 165 ClassLoaderData* k_def_class_loader_data = ik->class_loader_data(); 166 167 // Do we need to delete this system dictionary entry? 168 bool purge_entry = false; 169 170 // Do we need to delete this system dictionary entry? 171 if (loader_data->is_unloading()) { 172 // If the loader is not live this entry should always be 173 // removed (will never be looked up again). 174 purge_entry = true; 175 } else { 176 // The loader in this entry is alive. If the klass is dead, 177 // (determined by checking the defining class loader) 178 // the loader must be an initiating loader (rather than the 179 // defining loader). Remove this entry. 180 if (k_def_class_loader_data->is_unloading()) { 181 // If we get here, the class_loader_data must not be the defining 182 // loader, it must be an initiating one. 183 assert(k_def_class_loader_data != loader_data, 184 "cannot have live defining loader and unreachable klass"); 185 // Loader is live, but class and its defining loader are dead. 186 // Remove the entry. The class is going away. 187 purge_entry = true; 188 } 189 } 190 191 if (purge_entry) { 192 *p = probe->next(); 193 if (probe == _current_class_entry) { 194 _current_class_entry = NULL; 195 } 196 free_entry(probe); 197 continue; 198 } 199 } 200 p = probe->next_addr(); 201 } 202 } 203 } 204 205 void Dictionary::roots_oops_do(OopClosure* strong, OopClosure* weak) { 206 // Do strong roots marking if the closures are the same. 207 if (strong == weak || !ClassUnloading) { 208 // Only the protection domain oops contain references into the heap. Iterate 209 // over all of them. 210 _pd_cache_table->oops_do(strong); 211 } else { 212 if (weak != NULL) { 213 _pd_cache_table->oops_do(weak); 214 } 215 } 216 } 217 218 219 void Dictionary::remove_classes_in_error_state() { 220 assert(DumpSharedSpaces, "supported only when dumping"); 221 DictionaryEntry* probe = NULL; 222 for (int index = 0; index < table_size(); index++) { 223 for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) { 224 probe = *p; 225 InstanceKlass* ik = InstanceKlass::cast(probe->klass()); 226 if (ik->is_in_error_state()) { // purge this entry 227 *p = probe->next(); 228 if (probe == _current_class_entry) { 229 _current_class_entry = NULL; 230 } 231 free_entry(probe); 232 ResourceMark rm; 233 tty->print_cr("Preload Warning: Removed error class: %s", ik->external_name()); 234 continue; 235 } 236 237 p = probe->next_addr(); 238 } 239 } 240 } 241 242 // Just the classes from defining class loaders 243 void Dictionary::classes_do(void f(Klass*)) { 244 for (int index = 0; index < table_size(); index++) { 245 for (DictionaryEntry* probe = bucket(index); 246 probe != NULL; 247 probe = probe->next()) { 248 Klass* k = probe->klass(); 249 if (probe->loader_data() == k->class_loader_data()) { 250 f(k); 251 } 252 } 253 } 254 } 255 256 // Added for initialize_itable_for_klass to handle exceptions 257 // Just the classes from defining class loaders 258 void Dictionary::classes_do(void f(Klass*, TRAPS), TRAPS) { 259 for (int index = 0; index < table_size(); index++) { 260 for (DictionaryEntry* probe = bucket(index); 261 probe != NULL; 262 probe = probe->next()) { 263 Klass* k = probe->klass(); 264 if (probe->loader_data() == k->class_loader_data()) { 265 f(k, CHECK); 266 } 267 } 268 } 269 } 270 271 // All classes, and their class loaders 272 // Don't iterate over placeholders 273 void Dictionary::classes_do(void f(Klass*, ClassLoaderData*)) { 274 for (int index = 0; index < table_size(); index++) { 275 for (DictionaryEntry* probe = bucket(index); 276 probe != NULL; 277 probe = probe->next()) { 278 Klass* k = probe->klass(); 279 f(k, probe->loader_data()); 280 } 281 } 282 } 283 284 // Used to scan and relocate the classes during CDS archive dump. 285 void Dictionary::classes_do(MetaspaceClosure* it) { 286 for (int index = 0; index < table_size(); index++) { 287 for (DictionaryEntry* probe = bucket(index); 288 probe != NULL; 289 probe = probe->next()) { 290 it->push(probe->klass_addr()); 291 if (DumpSharedSpaces) { 292 ((SharedDictionaryEntry*)probe)->metaspace_pointers_do(it); 293 } 294 } 295 } 296 } 297 298 299 void Dictionary::oops_do(OopClosure* f) { 300 // Only the protection domain oops contain references into the heap. Iterate 301 // over all of them. 302 _pd_cache_table->oops_do(f); 303 } 304 305 void Dictionary::unlink(BoolObjectClosure* is_alive) { 306 // Only the protection domain cache table may contain references to the heap 307 // that need to be unlinked. 308 _pd_cache_table->unlink(is_alive); 309 } 310 311 InstanceKlass* Dictionary::try_get_next_class() { 312 while (true) { 313 if (_current_class_entry != NULL) { 314 InstanceKlass* k = _current_class_entry->klass(); 315 _current_class_entry = _current_class_entry->next(); 316 return k; 317 } 318 _current_class_index = (_current_class_index + 1) % table_size(); 319 _current_class_entry = bucket(_current_class_index); 320 } 321 // never reached 322 } 323 324 // Add a loaded class to the system dictionary. 325 // Readers of the SystemDictionary aren't always locked, so _buckets 326 // is volatile. The store of the next field in the constructor is 327 // also cast to volatile; we do this to ensure store order is maintained 328 // by the compilers. 329 330 void Dictionary::add_klass(Symbol* class_name, ClassLoaderData* loader_data, 331 InstanceKlass* obj) { 332 assert_locked_or_safepoint(SystemDictionary_lock); 333 assert(obj != NULL, "adding NULL obj"); 334 assert(obj->name() == class_name, "sanity check on name"); 335 assert(loader_data != NULL, "Must be non-NULL"); 336 337 unsigned int hash = compute_hash(class_name, loader_data); 338 int index = hash_to_index(hash); 339 DictionaryEntry* entry = new_entry(hash, obj, loader_data); 340 add_entry(index, entry); 341 } 342 343 344 // This routine does not lock the system dictionary. 345 // 346 // Since readers don't hold a lock, we must make sure that system 347 // dictionary entries are only removed at a safepoint (when only one 348 // thread is running), and are added to in a safe way (all links must 349 // be updated in an MT-safe manner). 350 // 351 // Callers should be aware that an entry could be added just after 352 // _buckets[index] is read here, so the caller will not see the new entry. 353 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash, 354 Symbol* class_name, 355 ClassLoaderData* loader_data) { 356 for (DictionaryEntry* entry = bucket(index); 357 entry != NULL; 358 entry = entry->next()) { 359 if (entry->hash() == hash && entry->equals(class_name, loader_data)) { 360 return entry; 361 } 362 } 363 return NULL; 364 } 365 366 367 InstanceKlass* Dictionary::find(int index, unsigned int hash, Symbol* name, 368 ClassLoaderData* loader_data, Handle protection_domain, TRAPS) { 369 DictionaryEntry* entry = get_entry(index, hash, name, loader_data); 370 if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) { 371 return entry->klass(); 372 } else { 373 return NULL; 374 } 375 } 376 377 378 InstanceKlass* Dictionary::find_class(int index, unsigned int hash, 379 Symbol* name, ClassLoaderData* loader_data) { 380 assert_locked_or_safepoint(SystemDictionary_lock); 381 assert (index == index_for(name, loader_data), "incorrect index?"); 382 383 DictionaryEntry* entry = get_entry(index, hash, name, loader_data); 384 return (entry != NULL) ? entry->klass() : NULL; 385 } 386 387 388 // Variant of find_class for shared classes. No locking required, as 389 // that table is static. 390 391 InstanceKlass* Dictionary::find_shared_class(int index, unsigned int hash, 392 Symbol* name) { 393 assert (index == index_for(name, NULL), "incorrect index?"); 394 395 DictionaryEntry* entry = get_entry(index, hash, name, NULL); 396 return (entry != NULL) ? entry->klass() : NULL; 397 } 398 399 400 void Dictionary::add_protection_domain(int index, unsigned int hash, 401 InstanceKlass* klass, 402 ClassLoaderData* loader_data, Handle protection_domain, 403 TRAPS) { 404 Symbol* klass_name = klass->name(); 405 DictionaryEntry* entry = get_entry(index, hash, klass_name, loader_data); 406 407 assert(entry != NULL,"entry must be present, we just created it"); 408 assert(protection_domain() != NULL, 409 "real protection domain should be present"); 410 411 entry->add_protection_domain(this, protection_domain); 412 413 assert(entry->contains_protection_domain(protection_domain()), 414 "now protection domain should be present"); 415 } 416 417 418 bool Dictionary::is_valid_protection_domain(int index, unsigned int hash, 419 Symbol* name, 420 ClassLoaderData* loader_data, 421 Handle protection_domain) { 422 DictionaryEntry* entry = get_entry(index, hash, name, loader_data); 423 return entry->is_valid_protection_domain(protection_domain); 424 } 425 426 427 void Dictionary::reorder_dictionary_for_sharing() { 428 429 // Copy all the dictionary entries into a single master list. 430 431 DictionaryEntry* master_list = NULL; 432 for (int i = 0; i < table_size(); ++i) { 433 DictionaryEntry* p = bucket(i); 434 while (p != NULL) { 435 DictionaryEntry* tmp; 436 tmp = p->next(); 437 p->set_next(master_list); 438 master_list = p; 439 p = tmp; 440 } 441 set_entry(i, NULL); 442 } 443 444 // Add the dictionary entries back to the list in the correct buckets. 445 while (master_list != NULL) { 446 DictionaryEntry* p = master_list; 447 master_list = master_list->next(); 448 p->set_next(NULL); 449 Symbol* class_name = p->klass()->name(); 450 // Since the null class loader data isn't copied to the CDS archive, 451 // compute the hash with NULL for loader data. 452 unsigned int hash = compute_hash(class_name, NULL); 453 int index = hash_to_index(hash); 454 p->set_hash(hash); 455 p->set_loader_data(NULL); // loader_data isn't copied to CDS 456 p->set_next(bucket(index)); 457 set_entry(index, p); 458 } 459 } 460 461 462 SymbolPropertyTable::SymbolPropertyTable(int table_size) 463 : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry)) 464 { 465 } 466 SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t, 467 int number_of_entries) 468 : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries) 469 { 470 } 471 472 473 SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash, 474 Symbol* sym, 475 intptr_t sym_mode) { 476 assert(index == index_for(sym, sym_mode), "incorrect index?"); 477 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { 478 if (p->hash() == hash && p->symbol() == sym && p->symbol_mode() == sym_mode) { 479 return p; 480 } 481 } 482 return NULL; 483 } 484 485 486 SymbolPropertyEntry* SymbolPropertyTable::add_entry(int index, unsigned int hash, 487 Symbol* sym, intptr_t sym_mode) { 488 assert_locked_or_safepoint(SystemDictionary_lock); 489 assert(index == index_for(sym, sym_mode), "incorrect index?"); 490 assert(find_entry(index, hash, sym, sym_mode) == NULL, "no double entry"); 491 492 SymbolPropertyEntry* p = new_entry(hash, sym, sym_mode); 493 Hashtable<Symbol*, mtSymbol>::add_entry(index, p); 494 return p; 495 } 496 497 void SymbolPropertyTable::oops_do(OopClosure* f) { 498 for (int index = 0; index < table_size(); index++) { 499 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { 500 if (p->method_type() != NULL) { 501 f->do_oop(p->method_type_addr()); 502 } 503 } 504 } 505 } 506 507 void SymbolPropertyTable::methods_do(void f(Method*)) { 508 for (int index = 0; index < table_size(); index++) { 509 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { 510 Method* prop = p->method(); 511 if (prop != NULL) { 512 f((Method*)prop); 513 } 514 } 515 } 516 } 517 518 519 // ---------------------------------------------------------------------------- 520 521 void Dictionary::print(bool details) { 522 ResourceMark rm; 523 524 if (details) { 525 tty->print_cr("Java system dictionary (table_size=%d, classes=%d)", 526 table_size(), number_of_entries()); 527 tty->print_cr("^ indicates that initiating loader is different from " 528 "defining loader"); 529 } 530 531 for (int index = 0; index < table_size(); index++) { 532 for (DictionaryEntry* probe = bucket(index); 533 probe != NULL; 534 probe = probe->next()) { 535 Klass* e = probe->klass(); 536 ClassLoaderData* loader_data = probe->loader_data(); 537 bool is_defining_class = 538 (loader_data == e->class_loader_data()); 539 if (details) { 540 tty->print("%4d: ", index); 541 } 542 tty->print("%s%s", ((!details) || is_defining_class) ? " " : "^", 543 e->external_name()); 544 545 if (details) { 546 tty->print(", loader "); 547 if (loader_data != NULL) { 548 loader_data->print_value(); 549 } else { 550 tty->print("NULL"); 551 } 552 } 553 tty->cr(); 554 } 555 } 556 557 if (details) { 558 tty->cr(); 559 _pd_cache_table->print(); 560 } 561 tty->cr(); 562 } 563 564 void DictionaryEntry::verify() { 565 Klass* e = klass(); 566 ClassLoaderData* cld = loader_data(); 567 guarantee(e->is_instance_klass(), 568 "Verify of system dictionary failed"); 569 // class loader must be present; a null class loader is the 570 // boostrap loader 571 guarantee(cld != NULL || DumpSharedSpaces || 572 cld->class_loader() == NULL || 573 cld->class_loader()->is_instance(), 574 "checking type of class_loader"); 575 e->verify(); 576 verify_protection_domain_set(); 577 } 578 579 void Dictionary::verify() { 580 guarantee(number_of_entries() >= 0, "Verify of system dictionary failed"); 581 verify_table<DictionaryEntry>("System Dictionary"); 582 _pd_cache_table->verify(); 583 } 584