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/metaspaceClosure.hpp"
  36 #include "memory/resourceArea.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "runtime/atomic.hpp"
  39 #include "runtime/orderAccess.inline.hpp"
  40 #include "utilities/hashtable.inline.hpp"
  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(ClassLoaderData* loader_data, int table_size)
  51   : _loader_data(loader_data), Hashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size()) {

  52 };
  53 
  54 
  55 Dictionary::Dictionary(ClassLoaderData* loader_data,
  56                        int table_size, HashtableBucket<mtClass>* t,
  57                        int number_of_entries)
  58   : _loader_data(loader_data), Hashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size(), t, number_of_entries) {

  59 };
  60 
  61 Dictionary::~Dictionary() {
  62   DictionaryEntry* probe = NULL;
  63   for (int index = 0; index < table_size(); index++) {
  64     for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
  65       probe = *p;
  66       *p = probe->next();
  67       free_entry(probe);
  68     }
  69   }
  70   assert(number_of_entries() == 0, "should have removed all entries");
  71   assert(new_entry_free_list() == NULL, "entry present on Dictionary's free list");
  72   free_buckets();
  73 }
  74 
  75 DictionaryEntry* Dictionary::new_entry(unsigned int hash, InstanceKlass* klass) {
  76   DictionaryEntry* entry = (DictionaryEntry*)Hashtable<InstanceKlass*, mtClass>::allocate_new_entry(hash, klass);
  77   entry->set_pd_set(NULL);
  78   assert(klass->is_instance_klass(), "Must be");
  79   if (DumpSharedSpaces) {
  80     SystemDictionaryShared::init_shared_dictionary_entry(klass, entry);
  81   }
  82   return entry;
  83 }
  84 
  85 
  86 void Dictionary::free_entry(DictionaryEntry* entry) {
  87   // avoid recursion when deleting linked list
  88   // pd_set is accessed during a safepoint.
  89   while (entry->pd_set() != NULL) {
  90     ProtectionDomainEntry* to_delete = entry->pd_set();
  91     entry->set_pd_set(to_delete->next());
  92     delete to_delete;
  93   }
  94   // Unlink from the Hashtable prior to freeing
  95   unlink_entry(entry);
  96   FREE_C_HEAP_ARRAY(char, entry);
  97 }
  98 






















































  99 
 100 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const {
 101 #ifdef ASSERT
 102   if (protection_domain == instance_klass()->protection_domain()) {
 103     // Ensure this doesn't show up in the pd_set (invariant)
 104     bool in_pd_set = false;
 105     for (ProtectionDomainEntry* current = pd_set_acquire();
 106                                 current != NULL;
 107                                 current = current->next()) {
 108       if (current->protection_domain() == protection_domain) {
 109         in_pd_set = true;
 110         break;
 111       }
 112     }
 113     if (in_pd_set) {
 114       assert(false, "A klass's protection domain should not show up "
 115                     "in its sys. dict. PD set");
 116     }
 117   }
 118 #endif /* ASSERT */
 119 
 120   if (protection_domain == instance_klass()->protection_domain()) {
 121     // Succeeds trivially
 122     return true;
 123   }
 124 
 125   for (ProtectionDomainEntry* current = pd_set_acquire();
 126                               current != NULL;
 127                               current = current->next()) {
 128     if (current->protection_domain() == protection_domain) return true;
 129   }
 130   return false;
 131 }
 132 
 133 
 134 void DictionaryEntry::add_protection_domain(Dictionary* dict, Handle protection_domain) {
 135   assert_locked_or_safepoint(SystemDictionary_lock);
 136   if (!contains_protection_domain(protection_domain())) {
 137     ProtectionDomainCacheEntry* entry = SystemDictionary::cache_get(protection_domain);
 138     ProtectionDomainEntry* new_head =
 139                 new ProtectionDomainEntry(entry, pd_set());
 140     // Warning: Preserve store ordering.  The SystemDictionary is read
 141     //          without locks.  The new ProtectionDomainEntry must be
 142     //          complete before other threads can be allowed to see it
 143     //          via a store to _pd_set.
 144     release_set_pd_set(new_head);
 145   }
 146   LogTarget(Trace, protectiondomain) lt;
 147   if (lt.is_enabled()) {
 148     LogStream ls(lt);
 149     print_count(&ls);
 150   }
 151 }
 152 
 153 
 154 void Dictionary::do_unloading() {
 155   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 156 
 157   // The NULL class loader doesn't initiate loading classes from other class loaders
 158   if (loader_data() == ClassLoaderData::the_null_class_loader_data()) {
 159     return;
 160   }
 161 
 162   // Remove unloaded entries and classes from this dictionary
 163   DictionaryEntry* probe = NULL;
 164   for (int index = 0; index < table_size(); index++) {
 165     for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
 166       probe = *p;
 167       InstanceKlass* ik = probe->instance_klass();
 168       ClassLoaderData* k_def_class_loader_data = ik->class_loader_data();
 169 
 170       // If the klass that this loader initiated is dead,
 171       // (determined by checking the defining class loader)
 172       // remove this entry.
 173       if (k_def_class_loader_data->is_unloading()) {
 174         assert(k_def_class_loader_data != loader_data(),
 175                "cannot have live defining loader and unreachable klass");
 176         *p = probe->next();
 177         free_entry(probe);
 178         continue;
 179       }
 180       p = probe->next_addr();
 181     }
 182   }
 183 }
 184 
 185 void Dictionary::remove_classes_in_error_state() {
 186   assert(DumpSharedSpaces, "supported only when dumping");
 187   DictionaryEntry* probe = NULL;
 188   for (int index = 0; index < table_size(); index++) {
 189     for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
 190       probe = *p;
 191       InstanceKlass* ik = probe->instance_klass();
 192       if (ik->is_in_error_state()) { // purge this entry
 193         *p = probe->next();
 194         free_entry(probe);
 195         ResourceMark rm;
 196         tty->print_cr("Preload Warning: Removed error class: %s", ik->external_name());
 197         continue;
 198       }
 199 
 200       p = probe->next_addr();
 201     }
 202   }
 203 }
 204 
 205 //   Just the classes from defining class loaders
 206 void Dictionary::classes_do(void f(InstanceKlass*)) {
 207   for (int index = 0; index < table_size(); index++) {
 208     for (DictionaryEntry* probe = bucket(index);
 209                           probe != NULL;
 210                           probe = probe->next()) {
 211       InstanceKlass* k = probe->instance_klass();
 212       if (loader_data() == k->class_loader_data()) {
 213         f(k);
 214       }
 215     }
 216   }
 217 }
 218 
 219 // Added for initialize_itable_for_klass to handle exceptions
 220 //   Just the classes from defining class loaders
 221 void Dictionary::classes_do(void f(InstanceKlass*, TRAPS), TRAPS) {
 222   for (int index = 0; index < table_size(); index++) {
 223     for (DictionaryEntry* probe = bucket(index);
 224                           probe != NULL;
 225                           probe = probe->next()) {
 226       InstanceKlass* k = probe->instance_klass();
 227       if (loader_data() == k->class_loader_data()) {
 228         f(k, CHECK);
 229       }
 230     }
 231   }
 232 }
 233 
 234 // All classes, and their class loaders, including initiating class loaders
 235 void Dictionary::all_entries_do(void f(InstanceKlass*, ClassLoaderData*)) {
 236   for (int index = 0; index < table_size(); index++) {
 237     for (DictionaryEntry* probe = bucket(index);
 238                           probe != NULL;
 239                           probe = probe->next()) {
 240       InstanceKlass* k = probe->instance_klass();
 241       f(k, loader_data());
 242     }
 243   }
 244 }
 245 
 246 // Used to scan and relocate the classes during CDS archive dump.
 247 void Dictionary::classes_do(MetaspaceClosure* it) {
 248   assert(DumpSharedSpaces, "dump-time only");
 249   for (int index = 0; index < table_size(); index++) {
 250     for (DictionaryEntry* probe = bucket(index);
 251                           probe != NULL;
 252                           probe = probe->next()) {
 253       it->push(probe->klass_addr());
 254       ((SharedDictionaryEntry*)probe)->metaspace_pointers_do(it);
 255     }
 256   }
 257 }
 258 
 259 
 260 
 261 // Add a loaded class to the dictionary.
 262 // Readers of the SystemDictionary aren't always locked, so _buckets
 263 // is volatile. The store of the next field in the constructor is
 264 // also cast to volatile;  we do this to ensure store order is maintained
 265 // by the compilers.
 266 
 267 void Dictionary::add_klass(int index, unsigned int hash, Symbol* class_name,
 268                            InstanceKlass* obj) {
 269   assert_locked_or_safepoint(SystemDictionary_lock);
 270   assert(obj != NULL, "adding NULL obj");
 271   assert(obj->name() == class_name, "sanity check on name");
 272 
 273   DictionaryEntry* entry = new_entry(hash, obj);

 274   add_entry(index, entry);

 275 }
 276 
 277 
 278 // This routine does not lock the dictionary.
 279 //
 280 // Since readers don't hold a lock, we must make sure that system
 281 // dictionary entries are only removed at a safepoint (when only one
 282 // thread is running), and are added to in a safe way (all links must
 283 // be updated in an MT-safe manner).
 284 //
 285 // Callers should be aware that an entry could be added just after
 286 // _buckets[index] is read here, so the caller will not see the new entry.
 287 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash,
 288                                        Symbol* class_name) {
 289   for (DictionaryEntry* entry = bucket(index);
 290                         entry != NULL;
 291                         entry = entry->next()) {
 292     if (entry->hash() == hash && entry->equals(class_name)) {
 293       if (!DumpSharedSpaces || SystemDictionaryShared::is_builtin(entry)) {
 294         return entry;
 295       }
 296     }
 297   }
 298   return NULL;
 299 }
 300 
 301 
 302 InstanceKlass* Dictionary::find(int index, unsigned int hash, Symbol* name,
 303                                 Handle protection_domain) {



 304   DictionaryEntry* entry = get_entry(index, hash, name);
 305   if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) {
 306     return entry->instance_klass();
 307   } else {
 308     return NULL;
 309   }
 310 }
 311 
 312 
 313 InstanceKlass* Dictionary::find_class(int index, unsigned int hash,
 314                                       Symbol* name) {
 315   assert_locked_or_safepoint(SystemDictionary_lock);
 316   assert (index == index_for(name), "incorrect index?");
 317 
 318   DictionaryEntry* entry = get_entry(index, hash, name);
 319   return (entry != NULL) ? entry->instance_klass() : NULL;
 320 }
 321 
 322 
 323 // Variant of find_class for shared classes.  No locking required, as
 324 // that table is static.
 325 
 326 InstanceKlass* Dictionary::find_shared_class(int index, unsigned int hash,
 327                                              Symbol* name) {
 328   assert (index == index_for(name), "incorrect index?");
 329 
 330   DictionaryEntry* entry = get_entry(index, hash, name);
 331   return (entry != NULL) ? entry->instance_klass() : NULL;
 332 }
 333 
 334 
 335 void Dictionary::add_protection_domain(int index, unsigned int hash,
 336                                        InstanceKlass* klass,
 337                                        Handle protection_domain,
 338                                        TRAPS) {
 339   Symbol*  klass_name = klass->name();
 340   DictionaryEntry* entry = get_entry(index, hash, klass_name);
 341 
 342   assert(entry != NULL,"entry must be present, we just created it");
 343   assert(protection_domain() != NULL,
 344          "real protection domain should be present");
 345 
 346   entry->add_protection_domain(this, protection_domain);
 347 
 348   assert(entry->contains_protection_domain(protection_domain()),
 349          "now protection domain should be present");
 350 }
 351 
 352 
 353 bool Dictionary::is_valid_protection_domain(int index, unsigned int hash,
 354                                             Symbol* name,
 355                                             Handle protection_domain) {

 356   DictionaryEntry* entry = get_entry(index, hash, name);
 357   return entry->is_valid_protection_domain(protection_domain);
 358 }
 359 
 360 
 361 void Dictionary::reorder_dictionary_for_sharing() {
 362 
 363   // Copy all the dictionary entries into a single master list.
 364 
 365   DictionaryEntry* master_list = NULL;
 366   for (int i = 0; i < table_size(); ++i) {
 367     DictionaryEntry* p = bucket(i);
 368     while (p != NULL) {
 369       DictionaryEntry* next = p->next();
 370       InstanceKlass*ik = p->instance_klass();
 371       // we cannot include signed classes in the archive because the certificates
 372       // used during dump time may be different than those used during
 373       // runtime (due to expiration, etc).
 374       if (ik->signers() != NULL) {
 375         ResourceMark rm;
 376         tty->print_cr("Preload Warning: Skipping %s from signed JAR",
 377                        ik->name()->as_C_string());
 378         free_entry(p);
 379       } else {
 380         p->set_next(master_list);
 381         master_list = p;
 382       }
 383       p = next;
 384     }
 385     set_entry(i, NULL);
 386   }
 387 
 388   // Add the dictionary entries back to the list in the correct buckets.
 389   while (master_list != NULL) {
 390     DictionaryEntry* p = master_list;
 391     master_list = master_list->next();
 392     p->set_next(NULL);
 393     Symbol* class_name = p->instance_klass()->name();
 394     // Since the null class loader data isn't copied to the CDS archive,
 395     // compute the hash with NULL for loader data.
 396     unsigned int hash = compute_hash(class_name);
 397     int index = hash_to_index(hash);
 398     p->set_hash(hash);
 399     p->set_next(bucket(index));
 400     set_entry(index, p);
 401   }
 402 }
 403 
 404 
 405 SymbolPropertyTable::SymbolPropertyTable(int table_size)
 406   : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry))
 407 {
 408 }
 409 SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t,
 410                                          int number_of_entries)
 411   : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries)
 412 {
 413 }
 414 
 415 
 416 SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash,
 417                                                      Symbol* sym,
 418                                                      intptr_t sym_mode) {
 419   assert(index == index_for(sym, sym_mode), "incorrect index?");
 420   for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
 421     if (p->hash() == hash && p->symbol() == sym && p->symbol_mode() == sym_mode) {
 422       return p;
 423     }
 424   }
 425   return NULL;
 426 }
 427 
 428 
 429 SymbolPropertyEntry* SymbolPropertyTable::add_entry(int index, unsigned int hash,
 430                                                     Symbol* sym, intptr_t sym_mode) {
 431   assert_locked_or_safepoint(SystemDictionary_lock);
 432   assert(index == index_for(sym, sym_mode), "incorrect index?");
 433   assert(find_entry(index, hash, sym, sym_mode) == NULL, "no double entry");
 434 
 435   SymbolPropertyEntry* p = new_entry(hash, sym, sym_mode);
 436   Hashtable<Symbol*, mtSymbol>::add_entry(index, p);
 437   return p;
 438 }
 439 
 440 void SymbolPropertyTable::oops_do(OopClosure* f) {
 441   for (int index = 0; index < table_size(); index++) {
 442     for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
 443       if (p->method_type() != NULL) {
 444         f->do_oop(p->method_type_addr());
 445       }
 446     }
 447   }
 448 }
 449 
 450 void SymbolPropertyTable::methods_do(void f(Method*)) {
 451   for (int index = 0; index < table_size(); index++) {
 452     for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
 453       Method* prop = p->method();
 454       if (prop != NULL) {
 455         f((Method*)prop);
 456       }
 457     }
 458   }
 459 }
 460 
 461 
 462 // ----------------------------------------------------------------------------
 463 
 464 void Dictionary::print_on(outputStream* st) const {
 465   ResourceMark rm;
 466 
 467   assert(loader_data() != NULL, "loader data should not be null");
 468   st->print_cr("Java dictionary (table_size=%d, classes=%d)",
 469                table_size(), number_of_entries());
 470   st->print_cr("^ indicates that initiating loader is different from defining loader");
 471 
 472   for (int index = 0; index < table_size(); index++) {
 473     for (DictionaryEntry* probe = bucket(index);
 474                           probe != NULL;
 475                           probe = probe->next()) {
 476       Klass* e = probe->instance_klass();
 477       bool is_defining_class =
 478          (loader_data() == e->class_loader_data());
 479       st->print("%4d: %s%s, loader ", index, is_defining_class ? " " : "^", e->external_name());
 480       ClassLoaderData* loader_data = e->class_loader_data();
 481       if (loader_data == NULL) {
 482         // Shared class not restored yet in shared dictionary
 483         st->print("<shared, not restored>");
 484       } else {
 485         loader_data->print_value_on(st);
 486       }
 487       st->cr();
 488     }
 489   }
 490   tty->cr();
 491 }
 492 
 493 void DictionaryEntry::verify() {
 494   Klass* e = instance_klass();
 495   guarantee(e->is_instance_klass(),
 496                           "Verify of dictionary failed");
 497   e->verify();
 498   verify_protection_domain_set();
 499 }
 500 
 501 void Dictionary::verify() {
 502   guarantee(number_of_entries() >= 0, "Verify of dictionary failed");
 503 
 504   ClassLoaderData* cld = loader_data();
 505   // class loader must be present;  a null class loader is the
 506   // boostrap loader
 507   guarantee(cld != NULL || DumpSharedSpaces ||
 508             cld->class_loader() == NULL ||
 509             cld->class_loader()->is_instance(),
 510             "checking type of class_loader");
 511 
 512   ResourceMark rm;
 513   stringStream tempst;
 514   tempst.print("System Dictionary for %s", cld->loader_name());
 515   verify_table<DictionaryEntry>(tempst.as_string());
 516 }
--- EOF ---