1 /* 2 * Copyright (c) 2003, 2020, 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/dictionary.hpp" 28 #include "classfile/protectionDomainCache.hpp" 29 #include "classfile/systemDictionary.hpp" 30 #include "logging/log.hpp" 31 #include "logging/logStream.hpp" 32 #include "memory/iterator.hpp" 33 #include "memory/metaspaceClosure.hpp" 34 #include "memory/resourceArea.hpp" 35 #include "oops/oop.inline.hpp" 36 #include "runtime/mutexLocker.hpp" 37 #include "runtime/safepointVerifiers.hpp" 38 #include "utilities/hashtable.inline.hpp" 39 40 // Optimization: if any dictionary needs resizing, we set this flag, 41 // so that we dont't have to walk all dictionaries to check if any actually 42 // needs resizing, which is costly to do at Safepoint. 43 bool Dictionary::_some_dictionary_needs_resizing = false; 44 45 Dictionary::Dictionary(ClassLoaderData* loader_data, int table_size, bool resizable) 46 : Hashtable<InstanceKlass*, mtClass>(table_size, (int)sizeof(DictionaryEntry)), 47 _resizable(resizable), _needs_resizing(false), _loader_data(loader_data) { 48 }; 49 50 51 Dictionary::Dictionary(ClassLoaderData* loader_data, 52 int table_size, HashtableBucket<mtClass>* t, 53 int number_of_entries, bool resizable) 54 : Hashtable<InstanceKlass*, mtClass>(table_size, (int)sizeof(DictionaryEntry), t, number_of_entries), 55 _resizable(resizable), _needs_resizing(false), _loader_data(loader_data) { 56 }; 57 58 Dictionary::~Dictionary() { 59 DictionaryEntry* probe = NULL; 60 for (int index = 0; index < table_size(); index++) { 61 for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) { 62 probe = *p; 63 *p = probe->next(); 64 free_entry(probe); 65 } 66 } 67 assert(number_of_entries() == 0, "should have removed all entries"); 68 assert(new_entry_free_list() == NULL, "entry present on Dictionary's free list"); 69 } 70 71 DictionaryEntry* Dictionary::new_entry(unsigned int hash, InstanceKlass* klass) { 72 DictionaryEntry* entry = (DictionaryEntry*)Hashtable<InstanceKlass*, mtClass>::allocate_new_entry(hash, klass); 73 entry->set_pd_set(NULL); 74 assert(klass->is_instance_klass(), "Must be"); 75 return entry; 76 } 77 78 79 void Dictionary::free_entry(DictionaryEntry* entry) { 80 // avoid recursion when deleting linked list 81 // pd_set is accessed during a safepoint. 82 // This doesn't require a lock because nothing is reading this 83 // entry anymore. The ClassLoader is dead. 84 while (entry->pd_set() != NULL) { 85 ProtectionDomainEntry* to_delete = entry->pd_set(); 86 entry->set_pd_set(to_delete->next()); 87 delete to_delete; 88 } 89 // Unlink from the Hashtable prior to freeing 90 unlink_entry(entry); 91 FREE_C_HEAP_ARRAY(char, entry); 92 } 93 94 const int _resize_load_trigger = 5; // load factor that will trigger the resize 95 const double _resize_factor = 2.0; // by how much we will resize using current number of entries 96 const int _resize_max_size = 40423; // the max dictionary size allowed 97 const int _primelist[] = {107, 1009, 2017, 4049, 5051, 10103, 20201, _resize_max_size}; 98 const int _prime_array_size = sizeof(_primelist)/sizeof(int); 99 100 // Calculate next "good" dictionary size based on requested count 101 static int calculate_dictionary_size(int requested) { 102 int newsize = _primelist[0]; 103 int index = 0; 104 for (newsize = _primelist[index]; index < (_prime_array_size - 1); 105 newsize = _primelist[++index]) { 106 if (requested <= newsize) { 107 break; 108 } 109 } 110 return newsize; 111 } 112 113 bool Dictionary::does_any_dictionary_needs_resizing() { 114 return Dictionary::_some_dictionary_needs_resizing; 115 } 116 117 void Dictionary::check_if_needs_resize() { 118 if (_resizable == true) { 119 if (number_of_entries() > (_resize_load_trigger*table_size())) { 120 _needs_resizing = true; 121 Dictionary::_some_dictionary_needs_resizing = true; 122 } 123 } 124 } 125 126 bool Dictionary::resize_if_needed() { 127 int desired_size = 0; 128 if (_needs_resizing == true) { 129 desired_size = calculate_dictionary_size((int)(_resize_factor*number_of_entries())); 130 if (desired_size >= _resize_max_size) { 131 desired_size = _resize_max_size; 132 // We have reached the limit, turn resizing off 133 _resizable = false; 134 } 135 if ((desired_size != 0) && (desired_size != table_size())) { 136 if (!resize(desired_size)) { 137 // Something went wrong, turn resizing off 138 _resizable = false; 139 } 140 } 141 } 142 143 _needs_resizing = false; 144 Dictionary::_some_dictionary_needs_resizing = false; 145 146 return (desired_size != 0); 147 } 148 149 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const { 150 // Lock the pd_set list. This lock cannot safepoint since the caller holds 151 // a Dictionary entry, which can be moved if the Dictionary is resized. 152 MutexLocker ml(ProtectionDomainSet_lock, Mutex::_no_safepoint_check_flag); 153 #ifdef ASSERT 154 if (protection_domain == instance_klass()->protection_domain()) { 155 // Ensure this doesn't show up in the pd_set (invariant) 156 bool in_pd_set = false; 157 for (ProtectionDomainEntry* current = pd_set(); 158 current != NULL; 159 current = current->next()) { 160 if (current->object_no_keepalive() == protection_domain) { 161 in_pd_set = true; 162 break; 163 } 164 } 165 if (in_pd_set) { 166 assert(false, "A klass's protection domain should not show up " 167 "in its sys. dict. PD set"); 168 } 169 } 170 #endif /* ASSERT */ 171 172 if (protection_domain == instance_klass()->protection_domain()) { 173 // Succeeds trivially 174 return true; 175 } 176 177 for (ProtectionDomainEntry* current = pd_set(); 178 current != NULL; 179 current = current->next()) { 180 if (current->object_no_keepalive() == protection_domain) return true; 181 } 182 return false; 183 } 184 185 186 void DictionaryEntry::add_protection_domain(Dictionary* dict, Handle protection_domain) { 187 assert_locked_or_safepoint(SystemDictionary_lock); 188 if (!contains_protection_domain(protection_domain())) { 189 ProtectionDomainCacheEntry* entry = SystemDictionary::cache_get(protection_domain); 190 // The pd_set in the dictionary entry is protected by a low level lock. 191 // With concurrent PD table cleanup, these links could be broken. 192 MutexLocker ml(ProtectionDomainSet_lock, Mutex::_no_safepoint_check_flag); 193 ProtectionDomainEntry* new_head = 194 new ProtectionDomainEntry(entry, pd_set()); 195 set_pd_set(new_head); 196 } 197 LogTarget(Trace, protectiondomain) lt; 198 if (lt.is_enabled()) { 199 LogStream ls(lt); 200 print_count(&ls); 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(KlassClosure* closure) { 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 closure->do_klass(k); 241 } 242 } 243 } 244 245 // Used to scan and relocate the classes during CDS archive dump. 246 void Dictionary::classes_do(MetaspaceClosure* it) { 247 Arguments::assert_is_dumping_archive(); 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 } 254 } 255 } 256 257 258 259 // Add a loaded class to the dictionary. 260 // Readers of the SystemDictionary aren't always locked, so _buckets 261 // is volatile. The store of the next field in the constructor is 262 // also cast to volatile; we do this to ensure store order is maintained 263 // by the compilers. 264 265 void Dictionary::add_klass(unsigned int hash, Symbol* class_name, 266 InstanceKlass* obj) { 267 assert_locked_or_safepoint(SystemDictionary_lock); 268 assert(obj != NULL, "adding NULL obj"); 269 assert(obj->name() == class_name, "sanity check on name"); 270 271 DictionaryEntry* entry = new_entry(hash, obj); 272 int index = hash_to_index(hash); 273 add_entry(index, entry); 274 check_if_needs_resize(); 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 return entry; 294 } 295 } 296 return NULL; 297 } 298 299 300 InstanceKlass* Dictionary::find(unsigned int hash, Symbol* name, 301 Handle protection_domain) { 302 NoSafepointVerifier nsv; 303 304 int index = hash_to_index(hash); 305 DictionaryEntry* entry = get_entry(index, hash, name); 306 if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) { 307 return entry->instance_klass(); 308 } else { 309 return NULL; 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 void Dictionary::add_protection_domain(int index, unsigned int hash, 324 InstanceKlass* klass, 325 Handle protection_domain, 326 TRAPS) { 327 Symbol* klass_name = klass->name(); 328 DictionaryEntry* entry = get_entry(index, hash, klass_name); 329 330 assert(entry != NULL,"entry must be present, we just created it"); 331 assert(protection_domain() != NULL, 332 "real protection domain should be present"); 333 334 entry->add_protection_domain(this, protection_domain); 335 336 #ifdef ASSERT 337 assert(loader_data() != ClassLoaderData::the_null_class_loader_data(), "doesn't make sense"); 338 #endif 339 340 assert(entry->contains_protection_domain(protection_domain()), 341 "now protection domain should be present"); 342 } 343 344 345 bool Dictionary::is_valid_protection_domain(unsigned int hash, 346 Symbol* name, 347 Handle protection_domain) { 348 int index = hash_to_index(hash); 349 DictionaryEntry* entry = get_entry(index, hash, name); 350 return entry->is_valid_protection_domain(protection_domain); 351 } 352 353 // During class loading we may have cached a protection domain that has 354 // since been unreferenced, so this entry should be cleared. 355 void Dictionary::clean_cached_protection_domains() { 356 assert_locked_or_safepoint(SystemDictionary_lock); 357 assert(!loader_data()->has_class_mirror_holder(), "cld should have a ClassLoader holder not a Class holder"); 358 359 if (loader_data()->is_the_null_class_loader_data()) { 360 // Classes in the boot loader are not loaded with protection domains 361 return; 362 } 363 364 for (int index = 0; index < table_size(); index++) { 365 for (DictionaryEntry* probe = bucket(index); 366 probe != NULL; 367 probe = probe->next()) { 368 Klass* e = probe->instance_klass(); 369 370 MutexLocker ml(ProtectionDomainSet_lock, Mutex::_no_safepoint_check_flag); 371 ProtectionDomainEntry* current = probe->pd_set(); 372 ProtectionDomainEntry* prev = NULL; 373 while (current != NULL) { 374 if (current->object_no_keepalive() == NULL) { 375 LogTarget(Debug, protectiondomain) lt; 376 if (lt.is_enabled()) { 377 ResourceMark rm; 378 // Print out trace information 379 LogStream ls(lt); 380 ls.print_cr("PD in set is not alive:"); 381 ls.print("class loader: "); loader_data()->class_loader()->print_value_on(&ls); 382 ls.print(" loading: "); probe->instance_klass()->print_value_on(&ls); 383 ls.cr(); 384 } 385 if (probe->pd_set() == current) { 386 probe->set_pd_set(current->next()); 387 } else { 388 assert(prev != NULL, "should be set by alive entry"); 389 prev->set_next(current->next()); 390 } 391 ProtectionDomainEntry* to_delete = current; 392 current = current->next(); 393 delete to_delete; 394 } else { 395 prev = current; 396 current = current->next(); 397 } 398 } 399 } 400 } 401 } 402 403 404 SymbolPropertyTable::SymbolPropertyTable(int table_size) 405 : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry)) 406 { 407 } 408 SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t, 409 int number_of_entries) 410 : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries) 411 { 412 } 413 414 415 SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash, 416 Symbol* sym, 417 intptr_t sym_mode) { 418 assert(index == index_for(sym, sym_mode), "incorrect index?"); 419 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { 420 if (p->hash() == hash && p->symbol() == sym && p->symbol_mode() == sym_mode) { 421 return p; 422 } 423 } 424 return NULL; 425 } 426 427 428 SymbolPropertyEntry* SymbolPropertyTable::add_entry(int index, unsigned int hash, 429 Symbol* sym, intptr_t sym_mode) { 430 assert_locked_or_safepoint(SystemDictionary_lock); 431 assert(index == index_for(sym, sym_mode), "incorrect index?"); 432 assert(find_entry(index, hash, sym, sym_mode) == NULL, "no double entry"); 433 434 SymbolPropertyEntry* p = new_entry(hash, sym, sym_mode); 435 Hashtable<Symbol*, mtSymbol>::add_entry(index, p); 436 return p; 437 } 438 439 void SymbolPropertyTable::oops_do(OopClosure* f) { 440 for (int index = 0; index < table_size(); index++) { 441 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { 442 if (p->method_type() != NULL) { 443 f->do_oop(p->method_type_addr()); 444 } 445 } 446 } 447 } 448 449 void SymbolPropertyTable::methods_do(void f(Method*)) { 450 for (int index = 0; index < table_size(); index++) { 451 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { 452 Method* prop = p->method(); 453 if (prop != NULL) { 454 f((Method*)prop); 455 } 456 } 457 } 458 } 459 460 void DictionaryEntry::verify_protection_domain_set() { 461 MutexLocker ml(ProtectionDomainSet_lock, Mutex::_no_safepoint_check_flag); 462 for (ProtectionDomainEntry* current = pd_set(); // accessed at a safepoint 463 current != NULL; 464 current = current->_next) { 465 guarantee(oopDesc::is_oop_or_null(current->_pd_cache->object_no_keepalive()), "Invalid oop"); 466 } 467 } 468 469 void DictionaryEntry::print_count(outputStream *st) { 470 MutexLocker ml(ProtectionDomainSet_lock, Mutex::_no_safepoint_check_flag); 471 int count = 0; 472 for (ProtectionDomainEntry* current = pd_set(); // accessed inside SD lock 473 current != NULL; 474 current = current->_next) { 475 count++; 476 } 477 st->print_cr("pd set count = #%d", count); 478 } 479 480 // ---------------------------------------------------------------------------- 481 482 void Dictionary::print_on(outputStream* st) const { 483 ResourceMark rm; 484 485 assert(loader_data() != NULL, "loader data should not be null"); 486 assert(!loader_data()->has_class_mirror_holder(), "cld should have a ClassLoader holder not a Class holder"); 487 st->print_cr("Java dictionary (table_size=%d, classes=%d, resizable=%s)", 488 table_size(), number_of_entries(), BOOL_TO_STR(_resizable)); 489 st->print_cr("^ indicates that initiating loader is different from defining loader"); 490 491 for (int index = 0; index < table_size(); index++) { 492 for (DictionaryEntry* probe = bucket(index); 493 probe != NULL; 494 probe = probe->next()) { 495 Klass* e = probe->instance_klass(); 496 bool is_defining_class = 497 (loader_data() == e->class_loader_data()); 498 st->print("%4d: %s%s", index, is_defining_class ? " " : "^", e->external_name()); 499 ClassLoaderData* cld = e->class_loader_data(); 500 if (!loader_data()->is_the_null_class_loader_data()) { 501 // Class loader output for the dictionary for the null class loader data is 502 // redundant and obvious. 503 st->print(", "); 504 cld->print_value_on(st); 505 } 506 st->cr(); 507 } 508 } 509 tty->cr(); 510 } 511 512 void DictionaryEntry::verify() { 513 Klass* e = instance_klass(); 514 guarantee(e->is_instance_klass(), 515 "Verify of dictionary failed"); 516 e->verify(); 517 verify_protection_domain_set(); 518 } 519 520 void Dictionary::verify() { 521 guarantee(number_of_entries() >= 0, "Verify of dictionary failed"); 522 523 ClassLoaderData* cld = loader_data(); 524 // class loader must be present; a null class loader is the 525 // boostrap loader 526 guarantee(cld != NULL || 527 cld->class_loader() == NULL || 528 cld->class_loader()->is_instance(), 529 "checking type of class_loader"); 530 531 ResourceMark rm; 532 stringStream tempst; 533 tempst.print("System Dictionary for %s class loader", cld->loader_name_and_id()); 534 verify_table<DictionaryEntry>(tempst.as_string()); 535 }