1 /* 2 * Copyright (c) 2003, 2010, 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/dictionary.hpp" 27 #include "classfile/systemDictionary.hpp" 28 #include "oops/oop.inline.hpp" 29 #include "prims/jvmtiRedefineClassesTrace.hpp" 30 #include "services/classLoadingService.hpp" 31 #include "utilities/hashtable.inline.hpp" 32 33 34 DictionaryEntry* Dictionary::_current_class_entry = NULL; 35 int Dictionary::_current_class_index = 0; 36 37 38 Dictionary::Dictionary(int table_size) 39 : TwoOopHashtable(table_size, sizeof(DictionaryEntry)) { 40 _current_class_index = 0; 41 _current_class_entry = NULL; 42 }; 43 44 45 46 Dictionary::Dictionary(int table_size, HashtableBucket* t, 47 int number_of_entries) 48 : TwoOopHashtable(table_size, sizeof(DictionaryEntry), t, number_of_entries) { 49 _current_class_index = 0; 50 _current_class_entry = NULL; 51 }; 52 53 54 DictionaryEntry* Dictionary::new_entry(unsigned int hash, klassOop klass, 55 oop loader) { 56 DictionaryEntry* entry; 57 entry = (DictionaryEntry*)Hashtable::new_entry(hash, klass); 58 entry->set_loader(loader); 59 entry->set_pd_set(NULL); 60 return entry; 61 } 62 63 64 DictionaryEntry* Dictionary::new_entry() { 65 DictionaryEntry* entry = (DictionaryEntry*)Hashtable::new_entry(0L, NULL); 66 entry->set_loader(NULL); 67 entry->set_pd_set(NULL); 68 return entry; 69 } 70 71 72 void Dictionary::free_entry(DictionaryEntry* entry) { 73 // avoid recursion when deleting linked list 74 while (entry->pd_set() != NULL) { 75 ProtectionDomainEntry* to_delete = entry->pd_set(); 76 entry->set_pd_set(to_delete->next()); 77 delete to_delete; 78 } 79 Hashtable::free_entry(entry); 80 } 81 82 83 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const { 84 #ifdef ASSERT 85 if (protection_domain == instanceKlass::cast(klass())->protection_domain()) { 86 // Ensure this doesn't show up in the pd_set (invariant) 87 bool in_pd_set = false; 88 for (ProtectionDomainEntry* current = _pd_set; 89 current != NULL; 90 current = current->next()) { 91 if (current->protection_domain() == protection_domain) { 92 in_pd_set = true; 93 break; 94 } 95 } 96 if (in_pd_set) { 97 assert(false, "A klass's protection domain should not show up " 98 "in its sys. dict. PD set"); 99 } 100 } 101 #endif /* ASSERT */ 102 103 if (protection_domain == instanceKlass::cast(klass())->protection_domain()) { 104 // Succeeds trivially 105 return true; 106 } 107 108 for (ProtectionDomainEntry* current = _pd_set; 109 current != NULL; 110 current = current->next()) { 111 if (current->protection_domain() == protection_domain) return true; 112 } 113 return false; 114 } 115 116 117 void DictionaryEntry::add_protection_domain(oop protection_domain) { 118 assert_locked_or_safepoint(SystemDictionary_lock); 119 if (!contains_protection_domain(protection_domain)) { 120 ProtectionDomainEntry* new_head = 121 new ProtectionDomainEntry(protection_domain, _pd_set); 122 // Warning: Preserve store ordering. The SystemDictionary is read 123 // without locks. The new ProtectionDomainEntry must be 124 // complete before other threads can be allowed to see it 125 // via a store to _pd_set. 126 OrderAccess::release_store_ptr(&_pd_set, new_head); 127 } 128 if (TraceProtectionDomainVerification && WizardMode) { 129 print(); 130 } 131 } 132 133 134 bool Dictionary::do_unloading(BoolObjectClosure* is_alive) { 135 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 136 bool class_was_unloaded = false; 137 int index = 0; // Defined here for portability! Do not move 138 139 // Remove unloadable entries and classes from system dictionary 140 // The placeholder array has been handled in always_strong_oops_do. 141 DictionaryEntry* probe = NULL; 142 for (index = 0; index < table_size(); index++) { 143 for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) { 144 probe = *p; 145 klassOop e = probe->klass(); 146 oop class_loader = probe->loader(); 147 148 instanceKlass* ik = instanceKlass::cast(e); 149 if (ik->previous_versions() != NULL) { 150 // This klass has previous versions so see what we can cleanup 151 // while it is safe to do so. 152 153 int gc_count = 0; // leave debugging breadcrumbs 154 int live_count = 0; 155 156 // RC_TRACE macro has an embedded ResourceMark 157 RC_TRACE(0x00000200, ("unload: %s: previous version length=%d", 158 ik->external_name(), ik->previous_versions()->length())); 159 160 for (int i = ik->previous_versions()->length() - 1; i >= 0; i--) { 161 // check the previous versions array for GC'ed weak refs 162 PreviousVersionNode * pv_node = ik->previous_versions()->at(i); 163 jobject cp_ref = pv_node->prev_constant_pool(); 164 assert(cp_ref != NULL, "cp ref was unexpectedly cleared"); 165 if (cp_ref == NULL) { 166 delete pv_node; 167 ik->previous_versions()->remove_at(i); 168 // Since we are traversing the array backwards, we don't have to 169 // do anything special with the index. 170 continue; // robustness 171 } 172 173 constantPoolOop pvcp = (constantPoolOop)JNIHandles::resolve(cp_ref); 174 if (pvcp == NULL) { 175 // this entry has been GC'ed so remove it 176 delete pv_node; 177 ik->previous_versions()->remove_at(i); 178 // Since we are traversing the array backwards, we don't have to 179 // do anything special with the index. 180 gc_count++; 181 continue; 182 } else { 183 RC_TRACE(0x00000200, ("unload: previous version @%d is alive", i)); 184 if (is_alive->do_object_b(pvcp)) { 185 live_count++; 186 } else { 187 guarantee(false, "sanity check"); 188 } 189 } 190 191 GrowableArray<jweak>* method_refs = pv_node->prev_EMCP_methods(); 192 if (method_refs != NULL) { 193 RC_TRACE(0x00000200, ("unload: previous methods length=%d", 194 method_refs->length())); 195 for (int j = method_refs->length() - 1; j >= 0; j--) { 196 jweak method_ref = method_refs->at(j); 197 assert(method_ref != NULL, "weak method ref was unexpectedly cleared"); 198 if (method_ref == NULL) { 199 method_refs->remove_at(j); 200 // Since we are traversing the array backwards, we don't have to 201 // do anything special with the index. 202 continue; // robustness 203 } 204 205 methodOop method = (methodOop)JNIHandles::resolve(method_ref); 206 if (method == NULL) { 207 // this method entry has been GC'ed so remove it 208 JNIHandles::destroy_weak_global(method_ref); 209 method_refs->remove_at(j); 210 } else { 211 // RC_TRACE macro has an embedded ResourceMark 212 RC_TRACE(0x00000200, 213 ("unload: %s(%s): prev method @%d in version @%d is alive", 214 method->name()->as_C_string(), 215 method->signature()->as_C_string(), j, i)); 216 } 217 } 218 } 219 } 220 assert(ik->previous_versions()->length() == live_count, "sanity check"); 221 RC_TRACE(0x00000200, 222 ("unload: previous version stats: live=%d, GC'ed=%d", live_count, 223 gc_count)); 224 } 225 226 // Non-unloadable classes were handled in always_strong_oops_do 227 if (!is_strongly_reachable(class_loader, e)) { 228 // Entry was not visited in phase1 (negated test from phase1) 229 assert(class_loader != NULL, "unloading entry with null class loader"); 230 oop k_def_class_loader = ik->class_loader(); 231 232 // Do we need to delete this system dictionary entry? 233 bool purge_entry = false; 234 235 // Do we need to delete this system dictionary entry? 236 if (!is_alive->do_object_b(class_loader)) { 237 // If the loader is not live this entry should always be 238 // removed (will never be looked up again). Note that this is 239 // not the same as unloading the referred class. 240 if (k_def_class_loader == class_loader) { 241 // This is the defining entry, so the referred class is about 242 // to be unloaded. 243 // Notify the debugger and clean up the class. 244 guarantee(!is_alive->do_object_b(e), 245 "klass should not be live if defining loader is not"); 246 class_was_unloaded = true; 247 // notify the debugger 248 if (JvmtiExport::should_post_class_unload()) { 249 JvmtiExport::post_class_unload(ik->as_klassOop()); 250 } 251 252 // notify ClassLoadingService of class unload 253 ClassLoadingService::notify_class_unloaded(ik); 254 255 // Clean up C heap 256 ik->release_C_heap_structures(); 257 } 258 // Also remove this system dictionary entry. 259 purge_entry = true; 260 261 } else { 262 // The loader in this entry is alive. If the klass is dead, 263 // the loader must be an initiating loader (rather than the 264 // defining loader). Remove this entry. 265 if (!is_alive->do_object_b(e)) { 266 guarantee(!is_alive->do_object_b(k_def_class_loader), 267 "defining loader should not be live if klass is not"); 268 // If we get here, the class_loader must not be the defining 269 // loader, it must be an initiating one. 270 assert(k_def_class_loader != class_loader, 271 "cannot have live defining loader and unreachable klass"); 272 273 // Loader is live, but class and its defining loader are dead. 274 // Remove the entry. The class is going away. 275 purge_entry = true; 276 } 277 } 278 279 if (purge_entry) { 280 *p = probe->next(); 281 if (probe == _current_class_entry) { 282 _current_class_entry = NULL; 283 } 284 free_entry(probe); 285 continue; 286 } 287 } 288 p = probe->next_addr(); 289 } 290 } 291 return class_was_unloaded; 292 } 293 294 295 void Dictionary::always_strong_classes_do(OopClosure* blk) { 296 // Follow all system classes and temporary placeholders in dictionary 297 for (int index = 0; index < table_size(); index++) { 298 for (DictionaryEntry *probe = bucket(index); 299 probe != NULL; 300 probe = probe->next()) { 301 oop e = probe->klass(); 302 oop class_loader = probe->loader(); 303 if (is_strongly_reachable(class_loader, e)) { 304 blk->do_oop((oop*)probe->klass_addr()); 305 if (class_loader != NULL) { 306 blk->do_oop(probe->loader_addr()); 307 } 308 probe->protection_domain_set_oops_do(blk); 309 } 310 } 311 } 312 } 313 314 315 // Just the classes from defining class loaders 316 void Dictionary::classes_do(void f(klassOop)) { 317 for (int index = 0; index < table_size(); index++) { 318 for (DictionaryEntry* probe = bucket(index); 319 probe != NULL; 320 probe = probe->next()) { 321 klassOop k = probe->klass(); 322 if (probe->loader() == instanceKlass::cast(k)->class_loader()) { 323 f(k); 324 } 325 } 326 } 327 } 328 329 // Added for initialize_itable_for_klass to handle exceptions 330 // Just the classes from defining class loaders 331 void Dictionary::classes_do(void f(klassOop, TRAPS), TRAPS) { 332 for (int index = 0; index < table_size(); index++) { 333 for (DictionaryEntry* probe = bucket(index); 334 probe != NULL; 335 probe = probe->next()) { 336 klassOop k = probe->klass(); 337 if (probe->loader() == instanceKlass::cast(k)->class_loader()) { 338 f(k, CHECK); 339 } 340 } 341 } 342 } 343 344 345 // All classes, and their class loaders 346 // (added for helpers that use HandleMarks and ResourceMarks) 347 // Don't iterate over placeholders 348 void Dictionary::classes_do(void f(klassOop, oop, TRAPS), TRAPS) { 349 for (int index = 0; index < table_size(); index++) { 350 for (DictionaryEntry* probe = bucket(index); 351 probe != NULL; 352 probe = probe->next()) { 353 klassOop k = probe->klass(); 354 f(k, probe->loader(), CHECK); 355 } 356 } 357 } 358 359 360 // All classes, and their class loaders 361 // Don't iterate over placeholders 362 void Dictionary::classes_do(void f(klassOop, oop)) { 363 for (int index = 0; index < table_size(); index++) { 364 for (DictionaryEntry* probe = bucket(index); 365 probe != NULL; 366 probe = probe->next()) { 367 klassOop k = probe->klass(); 368 f(k, probe->loader()); 369 } 370 } 371 } 372 373 374 void Dictionary::oops_do(OopClosure* f) { 375 for (int index = 0; index < table_size(); index++) { 376 for (DictionaryEntry* probe = bucket(index); 377 probe != NULL; 378 probe = probe->next()) { 379 f->do_oop((oop*)probe->klass_addr()); 380 if (probe->loader() != NULL) { 381 f->do_oop(probe->loader_addr()); 382 } 383 probe->protection_domain_set_oops_do(f); 384 } 385 } 386 } 387 388 389 void Dictionary::methods_do(void f(methodOop)) { 390 for (int index = 0; index < table_size(); index++) { 391 for (DictionaryEntry* probe = bucket(index); 392 probe != NULL; 393 probe = probe->next()) { 394 klassOop k = probe->klass(); 395 if (probe->loader() == instanceKlass::cast(k)->class_loader()) { 396 // only take klass is we have the entry with the defining class loader 397 instanceKlass::cast(k)->methods_do(f); 398 } 399 } 400 } 401 } 402 403 404 klassOop Dictionary::try_get_next_class() { 405 while (true) { 406 if (_current_class_entry != NULL) { 407 klassOop k = _current_class_entry->klass(); 408 _current_class_entry = _current_class_entry->next(); 409 return k; 410 } 411 _current_class_index = (_current_class_index + 1) % table_size(); 412 _current_class_entry = bucket(_current_class_index); 413 } 414 // never reached 415 } 416 417 418 // Add a loaded class to the system dictionary. 419 // Readers of the SystemDictionary aren't always locked, so _buckets 420 // is volatile. The store of the next field in the constructor is 421 // also cast to volatile; we do this to ensure store order is maintained 422 // by the compilers. 423 424 void Dictionary::add_klass(symbolHandle class_name, Handle class_loader, 425 KlassHandle obj) { 426 assert_locked_or_safepoint(SystemDictionary_lock); 427 assert(obj() != NULL, "adding NULL obj"); 428 assert(Klass::cast(obj())->name() == class_name(), "sanity check on name"); 429 430 unsigned int hash = compute_hash(class_name, class_loader); 431 int index = hash_to_index(hash); 432 DictionaryEntry* entry = new_entry(hash, obj(), class_loader()); 433 add_entry(index, entry); 434 } 435 436 437 // This routine does not lock the system dictionary. 438 // 439 // Since readers don't hold a lock, we must make sure that system 440 // dictionary entries are only removed at a safepoint (when only one 441 // thread is running), and are added to in a safe way (all links must 442 // be updated in an MT-safe manner). 443 // 444 // Callers should be aware that an entry could be added just after 445 // _buckets[index] is read here, so the caller will not see the new entry. 446 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash, 447 symbolHandle class_name, 448 Handle class_loader) { 449 symbolOop name_ = class_name(); 450 oop loader_ = class_loader(); 451 debug_only(_lookup_count++); 452 for (DictionaryEntry* entry = bucket(index); 453 entry != NULL; 454 entry = entry->next()) { 455 if (entry->hash() == hash && entry->equals(name_, loader_)) { 456 return entry; 457 } 458 debug_only(_lookup_length++); 459 } 460 return NULL; 461 } 462 463 464 klassOop Dictionary::find(int index, unsigned int hash, symbolHandle name, 465 Handle loader, Handle protection_domain, TRAPS) { 466 DictionaryEntry* entry = get_entry(index, hash, name, loader); 467 if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) { 468 return entry->klass(); 469 } else { 470 return NULL; 471 } 472 } 473 474 475 klassOop Dictionary::find_class(int index, unsigned int hash, 476 symbolHandle name, Handle loader) { 477 assert_locked_or_safepoint(SystemDictionary_lock); 478 assert (index == index_for(name, loader), "incorrect index?"); 479 480 DictionaryEntry* entry = get_entry(index, hash, name, loader); 481 return (entry != NULL) ? entry->klass() : (klassOop)NULL; 482 } 483 484 485 // Variant of find_class for shared classes. No locking required, as 486 // that table is static. 487 488 klassOop Dictionary::find_shared_class(int index, unsigned int hash, 489 symbolHandle name) { 490 assert (index == index_for(name, Handle()), "incorrect index?"); 491 492 DictionaryEntry* entry = get_entry(index, hash, name, Handle()); 493 return (entry != NULL) ? entry->klass() : (klassOop)NULL; 494 } 495 496 497 void Dictionary::add_protection_domain(int index, unsigned int hash, 498 instanceKlassHandle klass, 499 Handle loader, Handle protection_domain, 500 TRAPS) { 501 symbolHandle klass_name(THREAD, klass->name()); 502 DictionaryEntry* entry = get_entry(index, hash, klass_name, loader); 503 504 assert(entry != NULL,"entry must be present, we just created it"); 505 assert(protection_domain() != NULL, 506 "real protection domain should be present"); 507 508 entry->add_protection_domain(protection_domain()); 509 510 assert(entry->contains_protection_domain(protection_domain()), 511 "now protection domain should be present"); 512 } 513 514 515 bool Dictionary::is_valid_protection_domain(int index, unsigned int hash, 516 symbolHandle name, 517 Handle loader, 518 Handle protection_domain) { 519 DictionaryEntry* entry = get_entry(index, hash, name, loader); 520 return entry->is_valid_protection_domain(protection_domain); 521 } 522 523 524 void Dictionary::reorder_dictionary() { 525 526 // Copy all the dictionary entries into a single master list. 527 528 DictionaryEntry* master_list = NULL; 529 for (int i = 0; i < table_size(); ++i) { 530 DictionaryEntry* p = bucket(i); 531 while (p != NULL) { 532 DictionaryEntry* tmp; 533 tmp = p->next(); 534 p->set_next(master_list); 535 master_list = p; 536 p = tmp; 537 } 538 set_entry(i, NULL); 539 } 540 541 // Add the dictionary entries back to the list in the correct buckets. 542 Thread *thread = Thread::current(); 543 544 while (master_list != NULL) { 545 DictionaryEntry* p = master_list; 546 master_list = master_list->next(); 547 p->set_next(NULL); 548 symbolHandle class_name (thread, instanceKlass::cast((klassOop)(p->klass()))->name()); 549 unsigned int hash = compute_hash(class_name, Handle(thread, p->loader())); 550 int index = hash_to_index(hash); 551 p->set_hash(hash); 552 p->set_next(bucket(index)); 553 set_entry(index, p); 554 } 555 } 556 557 SymbolPropertyTable::SymbolPropertyTable(int table_size) 558 : Hashtable(table_size, sizeof(SymbolPropertyEntry)) 559 { 560 } 561 SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket* t, 562 int number_of_entries) 563 : Hashtable(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries) 564 { 565 } 566 567 568 SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash, 569 symbolHandle sym, 570 intptr_t sym_mode) { 571 assert(index == index_for(sym, sym_mode), "incorrect index?"); 572 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { 573 if (p->hash() == hash && p->symbol() == sym() && p->symbol_mode() == sym_mode) { 574 return p; 575 } 576 } 577 return NULL; 578 } 579 580 581 SymbolPropertyEntry* SymbolPropertyTable::add_entry(int index, unsigned int hash, 582 symbolHandle sym, intptr_t sym_mode) { 583 assert_locked_or_safepoint(SystemDictionary_lock); 584 assert(index == index_for(sym, sym_mode), "incorrect index?"); 585 assert(find_entry(index, hash, sym, sym_mode) == NULL, "no double entry"); 586 587 SymbolPropertyEntry* p = new_entry(hash, sym(), sym_mode); 588 Hashtable::add_entry(index, p); 589 return p; 590 } 591 592 593 void SymbolPropertyTable::oops_do(OopClosure* f) { 594 for (int index = 0; index < table_size(); index++) { 595 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { 596 f->do_oop((oop*) p->symbol_addr()); 597 if (p->property_oop() != NULL) { 598 f->do_oop(p->property_oop_addr()); 599 } 600 } 601 } 602 } 603 604 void SymbolPropertyTable::methods_do(void f(methodOop)) { 605 for (int index = 0; index < table_size(); index++) { 606 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { 607 oop prop = p->property_oop(); 608 if (prop != NULL && prop->is_method()) { 609 f((methodOop)prop); 610 } 611 } 612 } 613 } 614 615 616 // ---------------------------------------------------------------------------- 617 #ifndef PRODUCT 618 619 void Dictionary::print() { 620 ResourceMark rm; 621 HandleMark hm; 622 623 tty->print_cr("Java system dictionary (classes=%d)", number_of_entries()); 624 tty->print_cr("^ indicates that initiating loader is different from " 625 "defining loader"); 626 627 for (int index = 0; index < table_size(); index++) { 628 for (DictionaryEntry* probe = bucket(index); 629 probe != NULL; 630 probe = probe->next()) { 631 if (Verbose) tty->print("%4d: ", index); 632 klassOop e = probe->klass(); 633 oop class_loader = probe->loader(); 634 bool is_defining_class = 635 (class_loader == instanceKlass::cast(e)->class_loader()); 636 tty->print("%s%s", is_defining_class ? " " : "^", 637 Klass::cast(e)->external_name()); 638 if (class_loader != NULL) { 639 tty->print(", loader "); 640 class_loader->print_value(); 641 } 642 tty->cr(); 643 } 644 } 645 } 646 647 #endif 648 649 void Dictionary::verify() { 650 guarantee(number_of_entries() >= 0, "Verify of system dictionary failed"); 651 int element_count = 0; 652 for (int index = 0; index < table_size(); index++) { 653 for (DictionaryEntry* probe = bucket(index); 654 probe != NULL; 655 probe = probe->next()) { 656 klassOop e = probe->klass(); 657 oop class_loader = probe->loader(); 658 guarantee(Klass::cast(e)->oop_is_instance(), 659 "Verify of system dictionary failed"); 660 // class loader must be present; a null class loader is the 661 // boostrap loader 662 guarantee(class_loader == NULL || class_loader->is_instance(), 663 "checking type of class_loader"); 664 e->verify(); 665 probe->verify_protection_domain_set(); 666 element_count++; 667 } 668 } 669 guarantee(number_of_entries() == element_count, 670 "Verify of system dictionary failed"); 671 debug_only(verify_lookup_length((double)number_of_entries() / table_size())); 672 }