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