1 /*
   2  * Copyright (c) 2003, 2012, 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 "utilities/hashtable.inline.hpp"
  31 
  32 
  33 DictionaryEntry*  Dictionary::_current_class_entry = NULL;
  34 int               Dictionary::_current_class_index =    0;
  35 
  36 
  37 Dictionary::Dictionary(int table_size)
  38   : TwoOopHashtable<Klass*, mtClass>(table_size, sizeof(DictionaryEntry)) {
  39   _current_class_index = 0;
  40   _current_class_entry = NULL;
  41 };
  42 
  43 
  44 
  45 Dictionary::Dictionary(int table_size, HashtableBucket<mtClass>* t,
  46                        int number_of_entries)
  47   : TwoOopHashtable<Klass*, mtClass>(table_size, sizeof(DictionaryEntry), t, number_of_entries) {
  48   _current_class_index = 0;
  49   _current_class_entry = NULL;
  50 };
  51 
  52 
  53 DictionaryEntry* Dictionary::new_entry(unsigned int hash, Klass* klass,
  54                                        ClassLoaderData* loader_data) {
  55   DictionaryEntry* entry = (DictionaryEntry*)Hashtable<Klass*, mtClass>::new_entry(hash, klass);
  56   entry->set_loader_data(loader_data);
  57   entry->set_pd_set(NULL);
  58   assert(klass->oop_is_instance(), "Must be");
  59   return entry;
  60 }
  61 
  62 
  63 void Dictionary::free_entry(DictionaryEntry* entry) {
  64   // avoid recursion when deleting linked list
  65   while (entry->pd_set() != NULL) {
  66     ProtectionDomainEntry* to_delete = entry->pd_set();
  67     entry->set_pd_set(to_delete->next());
  68     delete to_delete;
  69   }
  70   Hashtable<Klass*, mtClass>::free_entry(entry);
  71 }
  72 
  73 
  74 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const {
  75 #ifdef ASSERT
  76   if (protection_domain == InstanceKlass::cast(klass())->protection_domain()) {
  77     // Ensure this doesn't show up in the pd_set (invariant)
  78     bool in_pd_set = false;
  79     for (ProtectionDomainEntry* current = _pd_set;
  80                                 current != NULL;
  81                                 current = current->next()) {
  82       if (current->protection_domain() == protection_domain) {
  83         in_pd_set = true;
  84         break;
  85       }
  86     }
  87     if (in_pd_set) {
  88       assert(false, "A klass's protection domain should not show up "
  89                     "in its sys. dict. PD set");
  90     }
  91   }
  92 #endif /* ASSERT */
  93 
  94   if (protection_domain == InstanceKlass::cast(klass())->protection_domain()) {
  95     // Succeeds trivially
  96     return true;
  97   }
  98 
  99   for (ProtectionDomainEntry* current = _pd_set;
 100                               current != NULL;
 101                               current = current->next()) {
 102     if (current->protection_domain() == protection_domain) return true;
 103   }
 104   return false;
 105 }
 106 
 107 
 108 void DictionaryEntry::add_protection_domain(oop protection_domain) {
 109   assert_locked_or_safepoint(SystemDictionary_lock);
 110   if (!contains_protection_domain(protection_domain)) {
 111     ProtectionDomainEntry* new_head =
 112                 new ProtectionDomainEntry(protection_domain, _pd_set);
 113     // Warning: Preserve store ordering.  The SystemDictionary is read
 114     //          without locks.  The new ProtectionDomainEntry must be
 115     //          complete before other threads can be allowed to see it
 116     //          via a store to _pd_set.
 117     OrderAccess::release_store_ptr(&_pd_set, new_head);
 118   }
 119   if (TraceProtectionDomainVerification && WizardMode) {
 120     print();
 121   }
 122 }
 123 
 124 
 125 bool Dictionary::do_unloading() {
 126   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 127   bool class_was_unloaded = false;
 128   int  index = 0; // Defined here for portability! Do not move
 129 
 130   // Remove unloadable entries and classes from system dictionary
 131   // The placeholder array has been handled in always_strong_oops_do.
 132   DictionaryEntry* probe = NULL;
 133   for (index = 0; index < table_size(); index++) {
 134     for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
 135       probe = *p;
 136       Klass* e = probe->klass();
 137       ClassLoaderData* loader_data = probe->loader_data();
 138 
 139       InstanceKlass* ik = InstanceKlass::cast(e);
 140 
 141       // Non-unloadable classes were handled in always_strong_oops_do
 142       if (!is_strongly_reachable(loader_data, e)) {
 143         // Entry was not visited in phase1 (negated test from phase1)
 144         assert(!loader_data->is_the_null_class_loader_data(), "unloading entry with null class loader");
 145         ClassLoaderData* k_def_class_loader_data = ik->class_loader_data();
 146 
 147         // Do we need to delete this system dictionary entry?
 148         bool purge_entry = false;
 149 
 150         // Do we need to delete this system dictionary entry?
 151         if (loader_data->is_unloading()) {
 152           // If the loader is not live this entry should always be
 153           // removed (will never be looked up again). Note that this is
 154           // not the same as unloading the referred class.
 155           if (k_def_class_loader_data == loader_data) {
 156             // This is the defining entry, so the referred class is about
 157             // to be unloaded.
 158             class_was_unloaded = true;
 159           }
 160           // Also remove this system dictionary entry.
 161           purge_entry = true;
 162 
 163         } else {
 164           // The loader in this entry is alive. If the klass is dead,
 165           // (determined by checking the defining class loader)
 166           // the loader must be an initiating loader (rather than the
 167           // defining loader). Remove this entry.
 168           if (k_def_class_loader_data->is_unloading()) {
 169             // If we get here, the class_loader_data must not be the defining
 170             // loader, it must be an initiating one.
 171             assert(k_def_class_loader_data != loader_data,
 172                    "cannot have live defining loader and unreachable klass");
 173             // Loader is live, but class and its defining loader are dead.
 174             // Remove the entry. The class is going away.
 175             purge_entry = true;
 176           }
 177         }
 178 
 179         if (purge_entry) {
 180           *p = probe->next();
 181           if (probe == _current_class_entry) {
 182             _current_class_entry = NULL;
 183           }
 184           free_entry(probe);
 185           continue;
 186         }
 187       }
 188       p = probe->next_addr();
 189     }
 190   }
 191   return class_was_unloaded;
 192 }
 193 
 194 
 195 void Dictionary::always_strong_oops_do(OopClosure* blk) {
 196   // Follow all system classes and temporary placeholders in dictionary
 197   for (int index = 0; index < table_size(); index++) {
 198     for (DictionaryEntry *probe = bucket(index);
 199                           probe != NULL;
 200                           probe = probe->next()) {
 201       Klass* e = probe->klass();
 202       ClassLoaderData* loader_data = probe->loader_data();
 203       if (is_strongly_reachable(loader_data, e)) {
 204         probe->protection_domain_set_oops_do(blk);
 205       }
 206     }
 207   }
 208 }
 209 
 210 
 211 void Dictionary::always_strong_classes_do(KlassClosure* closure) {
 212   // Follow all system classes and temporary placeholders in dictionary
 213   for (int index = 0; index < table_size(); index++) {
 214     for (DictionaryEntry* probe = bucket(index);
 215                           probe != NULL;
 216                           probe = probe->next()) {
 217       Klass* e = probe->klass();
 218       ClassLoaderData* loader_data = probe->loader_data();
 219       if (is_strongly_reachable(loader_data, e)) {
 220         closure->do_klass(e);
 221       }
 222     }
 223   }
 224 }
 225 
 226 
 227 //   Just the classes from defining class loaders
 228 void Dictionary::classes_do(void f(Klass*)) {
 229   for (int index = 0; index < table_size(); index++) {
 230     for (DictionaryEntry* probe = bucket(index);
 231                           probe != NULL;
 232                           probe = probe->next()) {
 233       Klass* k = probe->klass();
 234       if (probe->loader_data() == InstanceKlass::cast(k)->class_loader_data()) {
 235         f(k);
 236       }
 237     }
 238   }
 239 }
 240 
 241 // Added for initialize_itable_for_klass to handle exceptions
 242 //   Just the classes from defining class loaders
 243 void Dictionary::classes_do(void f(Klass*, TRAPS), TRAPS) {
 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() == InstanceKlass::cast(k)->class_loader_data()) {
 250         f(k, CHECK);
 251       }
 252     }
 253   }
 254 }
 255 
 256 //   All classes, and their class loaders
 257 // Don't iterate over placeholders
 258 void Dictionary::classes_do(void f(Klass*, ClassLoaderData*)) {
 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       f(k, probe->loader_data());
 265     }
 266   }
 267 }
 268 
 269 
 270 void Dictionary::oops_do(OopClosure* f) {
 271   for (int index = 0; index < table_size(); index++) {
 272     for (DictionaryEntry* probe = bucket(index);
 273                           probe != NULL;
 274                           probe = probe->next()) {
 275       probe->protection_domain_set_oops_do(f);
 276     }
 277   }
 278 }
 279 
 280 
 281 void Dictionary::methods_do(void f(Method*)) {
 282   for (int index = 0; index < table_size(); index++) {
 283     for (DictionaryEntry* probe = bucket(index);
 284                           probe != NULL;
 285                           probe = probe->next()) {
 286       Klass* k = probe->klass();
 287       if (probe->loader_data() == InstanceKlass::cast(k)->class_loader_data()) {
 288         // only take klass is we have the entry with the defining class loader
 289         InstanceKlass::cast(k)->methods_do(f);
 290       }
 291     }
 292   }
 293 }
 294 
 295 
 296 Klass* Dictionary::try_get_next_class() {
 297   while (true) {
 298     if (_current_class_entry != NULL) {
 299       Klass* k = _current_class_entry->klass();
 300       _current_class_entry = _current_class_entry->next();
 301       return k;
 302     }
 303     _current_class_index = (_current_class_index + 1) % table_size();
 304     _current_class_entry = bucket(_current_class_index);
 305   }
 306   // never reached
 307 }
 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                            KlassHandle 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   debug_only(_lookup_count++);
 343   for (DictionaryEntry* entry = bucket(index);
 344                         entry != NULL;
 345                         entry = entry->next()) {
 346     if (entry->hash() == hash && entry->equals(class_name, loader_data)) {
 347       return entry;
 348     }
 349     debug_only(_lookup_length++);
 350   }
 351   return NULL;
 352 }
 353 
 354 
 355 Klass* Dictionary::find(int index, unsigned int hash, Symbol* name,
 356                           ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
 357   DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
 358   if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) {
 359     return entry->klass();
 360   } else {
 361     return NULL;
 362   }
 363 }
 364 
 365 
 366 Klass* Dictionary::find_class(int index, unsigned int hash,
 367                                 Symbol* name, ClassLoaderData* loader_data) {
 368   assert_locked_or_safepoint(SystemDictionary_lock);
 369   assert (index == index_for(name, loader_data), "incorrect index?");
 370 
 371   DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
 372   return (entry != NULL) ? entry->klass() : (Klass*)NULL;
 373 }
 374 
 375 
 376 // Variant of find_class for shared classes.  No locking required, as
 377 // that table is static.
 378 
 379 Klass* Dictionary::find_shared_class(int index, unsigned int hash,
 380                                        Symbol* name) {
 381   assert (index == index_for(name, NULL), "incorrect index?");
 382 
 383   DictionaryEntry* entry = get_entry(index, hash, name, NULL);
 384   return (entry != NULL) ? entry->klass() : (Klass*)NULL;
 385 }
 386 
 387 
 388 void Dictionary::add_protection_domain(int index, unsigned int hash,
 389                                        instanceKlassHandle klass,
 390                                        ClassLoaderData* loader_data, Handle protection_domain,
 391                                        TRAPS) {
 392   Symbol*  klass_name = klass->name();
 393   DictionaryEntry* entry = get_entry(index, hash, klass_name, loader_data);
 394 
 395   assert(entry != NULL,"entry must be present, we just created it");
 396   assert(protection_domain() != NULL,
 397          "real protection domain should be present");
 398 
 399   entry->add_protection_domain(protection_domain());
 400 
 401   assert(entry->contains_protection_domain(protection_domain()),
 402          "now protection domain should be present");
 403 }
 404 
 405 
 406 bool Dictionary::is_valid_protection_domain(int index, unsigned int hash,
 407                                             Symbol* name,
 408                                             ClassLoaderData* loader_data,
 409                                             Handle protection_domain) {
 410   DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
 411   return entry->is_valid_protection_domain(protection_domain);
 412 }
 413 
 414 
 415 void Dictionary::reorder_dictionary() {
 416 
 417   // Copy all the dictionary entries into a single master list.
 418 
 419   DictionaryEntry* master_list = NULL;
 420   for (int i = 0; i < table_size(); ++i) {
 421     DictionaryEntry* p = bucket(i);
 422     while (p != NULL) {
 423       DictionaryEntry* tmp;
 424       tmp = p->next();
 425       p->set_next(master_list);
 426       master_list = p;
 427       p = tmp;
 428     }
 429     set_entry(i, NULL);
 430   }
 431 
 432   // Add the dictionary entries back to the list in the correct buckets.
 433   while (master_list != NULL) {
 434     DictionaryEntry* p = master_list;
 435     master_list = master_list->next();
 436     p->set_next(NULL);
 437     Symbol* class_name = InstanceKlass::cast((Klass*)(p->klass()))->name();
 438     // Since the null class loader data isn't copied to the CDS archive,
 439     // compute the hash with NULL for loader data.
 440     unsigned int hash = compute_hash(class_name, NULL);
 441     int index = hash_to_index(hash);
 442     p->set_hash(hash);
 443     p->set_loader_data(NULL);   // loader_data isn't copied to CDS
 444     p->set_next(bucket(index));
 445     set_entry(index, p);
 446   }
 447 }
 448 
 449 SymbolPropertyTable::SymbolPropertyTable(int table_size)
 450   : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry))
 451 {
 452 }
 453 SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t,
 454                                          int number_of_entries)
 455   : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries)
 456 {
 457 }
 458 
 459 
 460 SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash,
 461                                                      Symbol* sym,
 462                                                      intptr_t sym_mode) {
 463   assert(index == index_for(sym, sym_mode), "incorrect index?");
 464   for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
 465     if (p->hash() == hash && p->symbol() == sym && p->symbol_mode() == sym_mode) {
 466       return p;
 467     }
 468   }
 469   return NULL;
 470 }
 471 
 472 
 473 SymbolPropertyEntry* SymbolPropertyTable::add_entry(int index, unsigned int hash,
 474                                                     Symbol* sym, intptr_t sym_mode) {
 475   assert_locked_or_safepoint(SystemDictionary_lock);
 476   assert(index == index_for(sym, sym_mode), "incorrect index?");
 477   assert(find_entry(index, hash, sym, sym_mode) == NULL, "no double entry");
 478 
 479   SymbolPropertyEntry* p = new_entry(hash, sym, sym_mode);
 480   Hashtable<Symbol*, mtSymbol>::add_entry(index, p);
 481   return p;
 482 }
 483 
 484 void SymbolPropertyTable::oops_do(OopClosure* f) {
 485   for (int index = 0; index < table_size(); index++) {
 486     for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
 487       if (p->method_type() != NULL) {
 488         f->do_oop(p->method_type_addr());
 489       }
 490     }
 491   }
 492 }
 493 
 494 void SymbolPropertyTable::methods_do(void f(Method*)) {
 495   for (int index = 0; index < table_size(); index++) {
 496     for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
 497       Method* prop = p->method();
 498       if (prop != NULL) {
 499         f((Method*)prop);
 500       }
 501     }
 502   }
 503 }
 504 
 505 
 506 // ----------------------------------------------------------------------------
 507 #ifndef PRODUCT
 508 
 509 void Dictionary::print() {
 510   ResourceMark rm;
 511   HandleMark   hm;
 512 
 513   tty->print_cr("Java system dictionary (table_size=%d, classes=%d)",
 514                  table_size(), number_of_entries());
 515   tty->print_cr("^ indicates that initiating loader is different from "
 516                 "defining loader");
 517 
 518   for (int index = 0; index < table_size(); index++) {
 519     for (DictionaryEntry* probe = bucket(index);
 520                           probe != NULL;
 521                           probe = probe->next()) {
 522       if (Verbose) tty->print("%4d: ", index);
 523       Klass* e = probe->klass();
 524       ClassLoaderData* loader_data =  probe->loader_data();
 525       bool is_defining_class =
 526          (loader_data == InstanceKlass::cast(e)->class_loader_data());
 527       tty->print("%s%s", is_defining_class ? " " : "^",
 528                    e->external_name());
 529 
 530         tty->print(", loader ");
 531       loader_data->print_value();
 532       tty->cr();
 533     }
 534   }
 535 }
 536 
 537 #endif
 538 
 539 
 540 void Dictionary::verify() {
 541   guarantee(number_of_entries() >= 0, "Verify of system dictionary failed");
 542 
 543   int element_count = 0;
 544   for (int index = 0; index < table_size(); index++) {
 545     for (DictionaryEntry* probe = bucket(index);
 546                           probe != NULL;
 547                           probe = probe->next()) {
 548       Klass* e = probe->klass();
 549       ClassLoaderData* loader_data = probe->loader_data();
 550       guarantee(e->oop_is_instance(),
 551                               "Verify of system dictionary failed");
 552       // class loader must be present;  a null class loader is the
 553       // boostrap loader
 554       guarantee(loader_data != NULL || DumpSharedSpaces ||
 555                 loader_data->class_loader() == NULL ||
 556                 loader_data->class_loader()->is_instance(),
 557                 "checking type of class_loader");
 558       e->verify(/*check_dictionary*/false);
 559       probe->verify_protection_domain_set();
 560       element_count++;
 561     }
 562   }
 563   guarantee(number_of_entries() == element_count,
 564             "Verify of system dictionary failed");
 565   debug_only(verify_lookup_length((double)number_of_entries() / table_size()));
 566 }
 567