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 while (entry->pd_set() != NULL) { 89 ProtectionDomainEntry* to_delete = entry->pd_set(); 90 entry->set_pd_set(to_delete->next()); 91 delete to_delete; 92 } 93 // Unlink from the Hashtable prior to freeing 94 unlink_entry(entry); 95 FREE_C_HEAP_ARRAY(char, entry); 96 } 97 98 99 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const { 100 #ifdef ASSERT 101 if (protection_domain == instance_klass()->protection_domain()) { 102 // Ensure this doesn't show up in the pd_set (invariant) 103 bool in_pd_set = false; 104 for (ProtectionDomainEntry* current = _pd_set; 105 current != NULL; 106 current = current->next()) { 107 if (current->protection_domain() == protection_domain) { 108 in_pd_set = true; 109 break; 110 } 111 } 112 if (in_pd_set) { 113 assert(false, "A klass's protection domain should not show up " 114 "in its sys. dict. PD set"); 115 } 116 } 117 #endif /* ASSERT */ 118 119 if (protection_domain == instance_klass()->protection_domain()) { 120 // Succeeds trivially 121 return true; 122 } 123 124 for (ProtectionDomainEntry* current = _pd_set; 125 current != NULL; 126 current = current->next()) { 127 if (current->protection_domain() == protection_domain) return true; 128 } 129 return false; 130 } 131 132 133 void DictionaryEntry::add_protection_domain(Dictionary* dict, Handle protection_domain) { 134 assert_locked_or_safepoint(SystemDictionary_lock); 135 if (!contains_protection_domain(protection_domain())) { 136 ProtectionDomainCacheEntry* entry = SystemDictionary::cache_get(protection_domain); 137 ProtectionDomainEntry* new_head = 138 new ProtectionDomainEntry(entry, _pd_set); 139 // Warning: Preserve store ordering. The SystemDictionary is read 140 // without locks. The new ProtectionDomainEntry must be 141 // complete before other threads can be allowed to see it 142 // via a store to _pd_set. 143 OrderAccess::release_store_ptr(&_pd_set, new_head); 144 } 145 LogTarget(Trace, protectiondomain) lt; 146 if (lt.is_enabled()) { 147 LogStream ls(lt); 148 print_count(&ls); 149 } 150 } 151 152 153 void Dictionary::do_unloading() { 154 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 155 156 // The NULL class loader doesn't initiate loading classes from other class loaders 157 if (loader_data() == ClassLoaderData::the_null_class_loader_data()) { 158 return; 159 } 160 161 // Remove unloaded entries and classes from this dictionary 162 DictionaryEntry* probe = NULL; 163 for (int index = 0; index < table_size(); index++) { 164 for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) { 165 probe = *p; 166 InstanceKlass* ik = probe->instance_klass(); 167 ClassLoaderData* k_def_class_loader_data = ik->class_loader_data(); 168 169 // If the klass that this loader initiated is dead, 170 // (determined by checking the defining class loader) 171 // remove this entry. 172 if (k_def_class_loader_data->is_unloading()) { 173 assert(k_def_class_loader_data != loader_data(), 174 "cannot have live defining loader and unreachable klass"); 175 *p = probe->next(); 176 free_entry(probe); 177 continue; 178 } 179 p = probe->next_addr(); 180 } 181 } 182 } 183 184 void Dictionary::remove_classes_in_error_state() { 185 assert(DumpSharedSpaces, "supported only when dumping"); 186 DictionaryEntry* probe = NULL; 187 for (int index = 0; index < table_size(); index++) { 188 for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) { 189 probe = *p; 190 InstanceKlass* ik = probe->instance_klass(); 191 if (ik->is_in_error_state()) { // purge this entry 192 *p = probe->next(); 193 free_entry(probe); 194 ResourceMark rm; 195 tty->print_cr("Preload Warning: Removed error class: %s", ik->external_name()); 196 continue; 197 } 198 199 p = probe->next_addr(); 200 } 201 } 202 } 203 204 // Just the classes from defining class loaders 205 void Dictionary::classes_do(void f(InstanceKlass*)) { 206 for (int index = 0; index < table_size(); index++) { 207 for (DictionaryEntry* probe = bucket(index); 208 probe != NULL; 209 probe = probe->next()) { 210 InstanceKlass* k = probe->instance_klass(); 211 if (loader_data() == k->class_loader_data()) { 212 f(k); 213 } 214 } 215 } 216 } 217 218 // Added for initialize_itable_for_klass to handle exceptions 219 // Just the classes from defining class loaders 220 void Dictionary::classes_do(void f(InstanceKlass*, TRAPS), TRAPS) { 221 for (int index = 0; index < table_size(); index++) { 222 for (DictionaryEntry* probe = bucket(index); 223 probe != NULL; 224 probe = probe->next()) { 225 InstanceKlass* k = probe->instance_klass(); 226 if (loader_data() == k->class_loader_data()) { 227 f(k, CHECK); 228 } 229 } 230 } 231 } 232 233 // All classes, and their class loaders, including initiating class loaders 234 void Dictionary::all_entries_do(void f(InstanceKlass*, ClassLoaderData*)) { 235 for (int index = 0; index < table_size(); index++) { 236 for (DictionaryEntry* probe = bucket(index); 237 probe != NULL; 238 probe = probe->next()) { 239 InstanceKlass* k = probe->instance_klass(); 240 f(k, loader_data()); 241 } 242 } 243 } 244 245 // Used to scan and relocate the classes during CDS archive dump. 246 void Dictionary::classes_do(MetaspaceClosure* it) { 247 assert(DumpSharedSpaces, "dump-time only"); 248 for (int index = 0; index < table_size(); index++) { 249 for (DictionaryEntry* probe = bucket(index); 250 probe != NULL; 251 probe = probe->next()) { 252 it->push(probe->klass_addr()); 253 ((SharedDictionaryEntry*)probe)->metaspace_pointers_do(it); 254 } 255 } 256 } 257 258 259 260 // Add a loaded class to the dictionary. 261 // Readers of the SystemDictionary aren't always locked, so _buckets 262 // is volatile. The store of the next field in the constructor is 263 // also cast to volatile; we do this to ensure store order is maintained 264 // by the compilers. 265 266 void Dictionary::add_klass(int index, unsigned int hash, Symbol* class_name, 267 InstanceKlass* obj) { 268 assert_locked_or_safepoint(SystemDictionary_lock); 269 assert(obj != NULL, "adding NULL obj"); 270 assert(obj->name() == class_name, "sanity check on name"); 271 272 DictionaryEntry* entry = new_entry(hash, obj); 273 add_entry(index, entry); 274 } 275 276 277 // This routine does not lock the dictionary. 278 // 279 // Since readers don't hold a lock, we must make sure that system 280 // dictionary entries are only removed at a safepoint (when only one 281 // thread is running), and are added to in a safe way (all links must 282 // be updated in an MT-safe manner). 283 // 284 // Callers should be aware that an entry could be added just after 285 // _buckets[index] is read here, so the caller will not see the new entry. 286 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash, 287 Symbol* class_name) { 288 for (DictionaryEntry* entry = bucket(index); 289 entry != NULL; 290 entry = entry->next()) { 291 if (entry->hash() == hash && entry->equals(class_name)) { 292 if (!DumpSharedSpaces || SystemDictionaryShared::is_builtin(entry)) { 293 return entry; 294 } 295 } 296 } 297 return NULL; 298 } 299 300 301 InstanceKlass* Dictionary::find(int index, unsigned int hash, Symbol* name, 302 Handle protection_domain) { 303 DictionaryEntry* entry = get_entry(index, hash, name); 304 if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) { 305 return entry->instance_klass(); 306 } else { 307 return NULL; 308 } 309 } 310 311 312 InstanceKlass* Dictionary::find_class(int index, unsigned int hash, 313 Symbol* name) { 314 assert_locked_or_safepoint(SystemDictionary_lock); 315 assert (index == index_for(name), "incorrect index?"); 316 317 DictionaryEntry* entry = get_entry(index, hash, name); 318 return (entry != NULL) ? entry->instance_klass() : NULL; 319 } 320 321 322 // Variant of find_class for shared classes. No locking required, as 323 // that table is static. 324 325 InstanceKlass* Dictionary::find_shared_class(int index, unsigned int hash, 326 Symbol* name) { 327 assert (index == index_for(name), "incorrect index?"); 328 329 DictionaryEntry* entry = get_entry(index, hash, name); 330 return (entry != NULL) ? entry->instance_klass() : NULL; 331 } 332 333 334 void Dictionary::add_protection_domain(int index, unsigned int hash, 335 InstanceKlass* klass, 336 Handle protection_domain, 337 TRAPS) { 338 Symbol* klass_name = klass->name(); 339 DictionaryEntry* entry = get_entry(index, hash, klass_name); 340 341 assert(entry != NULL,"entry must be present, we just created it"); 342 assert(protection_domain() != NULL, 343 "real protection domain should be present"); 344 345 entry->add_protection_domain(this, protection_domain); 346 347 assert(entry->contains_protection_domain(protection_domain()), 348 "now protection domain should be present"); 349 } 350 351 352 bool Dictionary::is_valid_protection_domain(int index, unsigned int hash, 353 Symbol* name, 354 Handle protection_domain) { 355 DictionaryEntry* entry = get_entry(index, hash, name); 356 return entry->is_valid_protection_domain(protection_domain); 357 } 358 359 360 void Dictionary::reorder_dictionary_for_sharing() { 361 362 // Copy all the dictionary entries into a single master list. 363 364 DictionaryEntry* master_list = NULL; 365 for (int i = 0; i < table_size(); ++i) { 366 DictionaryEntry* p = bucket(i); 367 while (p != NULL) { 368 DictionaryEntry* tmp; 369 tmp = p->next(); 370 p->set_next(master_list); 371 master_list = p; 372 p = tmp; 373 } 374 set_entry(i, NULL); 375 } 376 377 // Add the dictionary entries back to the list in the correct buckets. 378 while (master_list != NULL) { 379 DictionaryEntry* p = master_list; 380 master_list = master_list->next(); 381 p->set_next(NULL); 382 Symbol* class_name = p->instance_klass()->name(); 383 // Since the null class loader data isn't copied to the CDS archive, 384 // compute the hash with NULL for loader data. 385 unsigned int hash = compute_hash(class_name); 386 int index = hash_to_index(hash); 387 p->set_hash(hash); 388 p->set_next(bucket(index)); 389 set_entry(index, p); 390 } 391 } 392 393 394 SymbolPropertyTable::SymbolPropertyTable(int table_size) 395 : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry)) 396 { 397 } 398 SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t, 399 int number_of_entries) 400 : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries) 401 { 402 } 403 404 405 SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash, 406 Symbol* sym, 407 intptr_t sym_mode) { 408 assert(index == index_for(sym, sym_mode), "incorrect index?"); 409 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { 410 if (p->hash() == hash && p->symbol() == sym && p->symbol_mode() == sym_mode) { 411 return p; 412 } 413 } 414 return NULL; 415 } 416 417 418 SymbolPropertyEntry* SymbolPropertyTable::add_entry(int index, unsigned int hash, 419 Symbol* sym, intptr_t sym_mode) { 420 assert_locked_or_safepoint(SystemDictionary_lock); 421 assert(index == index_for(sym, sym_mode), "incorrect index?"); 422 assert(find_entry(index, hash, sym, sym_mode) == NULL, "no double entry"); 423 424 SymbolPropertyEntry* p = new_entry(hash, sym, sym_mode); 425 Hashtable<Symbol*, mtSymbol>::add_entry(index, p); 426 return p; 427 } 428 429 void SymbolPropertyTable::oops_do(OopClosure* f) { 430 for (int index = 0; index < table_size(); index++) { 431 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { 432 if (p->method_type() != NULL) { 433 f->do_oop(p->method_type_addr()); 434 } 435 } 436 } 437 } 438 439 void SymbolPropertyTable::methods_do(void f(Method*)) { 440 for (int index = 0; index < table_size(); index++) { 441 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { 442 Method* prop = p->method(); 443 if (prop != NULL) { 444 f((Method*)prop); 445 } 446 } 447 } 448 } 449 450 451 // ---------------------------------------------------------------------------- 452 453 void Dictionary::print_on(outputStream* st) const { 454 ResourceMark rm; 455 456 assert(loader_data() != NULL, "loader data should not be null"); 457 st->print_cr("Java dictionary (table_size=%d, classes=%d)", 458 table_size(), number_of_entries()); 459 st->print_cr("^ indicates that initiating loader is different from defining loader"); 460 461 for (int index = 0; index < table_size(); index++) { 462 for (DictionaryEntry* probe = bucket(index); 463 probe != NULL; 464 probe = probe->next()) { 465 Klass* e = probe->instance_klass(); 466 bool is_defining_class = 467 (loader_data() == e->class_loader_data()); 468 st->print("%4d: %s%s, loader ", index, is_defining_class ? " " : "^", e->external_name()); 469 ClassLoaderData* loader_data = e->class_loader_data(); 470 if (loader_data == NULL) { 471 // Shared class not restored yet in shared dictionary 472 st->print("<shared, not restored>"); 473 } else { 474 loader_data->print_value_on(st); 475 } 476 st->cr(); 477 } 478 } 479 tty->cr(); 480 } 481 482 void DictionaryEntry::verify() { 483 Klass* e = instance_klass(); 484 guarantee(e->is_instance_klass(), 485 "Verify of dictionary failed"); 486 e->verify(); 487 verify_protection_domain_set(); 488 } 489 490 void Dictionary::verify() { 491 guarantee(number_of_entries() >= 0, "Verify of dictionary failed"); 492 493 ClassLoaderData* cld = loader_data(); 494 // class loader must be present; a null class loader is the 495 // boostrap loader 496 guarantee(cld != NULL || DumpSharedSpaces || 497 cld->class_loader() == NULL || 498 cld->class_loader()->is_instance(), 499 "checking type of class_loader"); 500 501 ResourceMark rm; 502 stringStream tempst; 503 tempst.print("System Dictionary for %s", cld->loader_name()); 504 verify_table<DictionaryEntry>(tempst.as_string()); 505 }