src/share/vm/classfile/dictionary.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7363
3375833a603e
parent 6876
710a3c8b516e
child 8604
04d83ba48607
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/dictionary.hpp"
aoqi@0 27 #include "classfile/systemDictionary.hpp"
aoqi@0 28 #include "memory/iterator.hpp"
aoqi@0 29 #include "oops/oop.inline.hpp"
aoqi@0 30 #include "prims/jvmtiRedefineClassesTrace.hpp"
goetz@6911 31 #include "runtime/orderAccess.inline.hpp"
aoqi@0 32 #include "utilities/hashtable.inline.hpp"
aoqi@0 33
aoqi@0 34 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 35
aoqi@0 36 DictionaryEntry* Dictionary::_current_class_entry = NULL;
aoqi@0 37 int Dictionary::_current_class_index = 0;
aoqi@0 38
aoqi@0 39
aoqi@0 40 Dictionary::Dictionary(int table_size)
aoqi@0 41 : TwoOopHashtable<Klass*, mtClass>(table_size, sizeof(DictionaryEntry)) {
aoqi@0 42 _current_class_index = 0;
aoqi@0 43 _current_class_entry = NULL;
aoqi@0 44 _pd_cache_table = new ProtectionDomainCacheTable(defaultProtectionDomainCacheSize);
aoqi@0 45 };
aoqi@0 46
aoqi@0 47
aoqi@0 48 Dictionary::Dictionary(int table_size, HashtableBucket<mtClass>* t,
aoqi@0 49 int number_of_entries)
aoqi@0 50 : TwoOopHashtable<Klass*, mtClass>(table_size, sizeof(DictionaryEntry), t, number_of_entries) {
aoqi@0 51 _current_class_index = 0;
aoqi@0 52 _current_class_entry = NULL;
aoqi@0 53 _pd_cache_table = new ProtectionDomainCacheTable(defaultProtectionDomainCacheSize);
aoqi@0 54 };
aoqi@0 55
aoqi@0 56 ProtectionDomainCacheEntry* Dictionary::cache_get(oop protection_domain) {
aoqi@0 57 return _pd_cache_table->get(protection_domain);
aoqi@0 58 }
aoqi@0 59
aoqi@0 60 DictionaryEntry* Dictionary::new_entry(unsigned int hash, Klass* klass,
aoqi@0 61 ClassLoaderData* loader_data) {
aoqi@0 62 DictionaryEntry* entry = (DictionaryEntry*)Hashtable<Klass*, mtClass>::new_entry(hash, klass);
aoqi@0 63 entry->set_loader_data(loader_data);
aoqi@0 64 entry->set_pd_set(NULL);
aoqi@0 65 assert(klass->oop_is_instance(), "Must be");
aoqi@0 66 return entry;
aoqi@0 67 }
aoqi@0 68
aoqi@0 69
aoqi@0 70 void Dictionary::free_entry(DictionaryEntry* entry) {
aoqi@0 71 // avoid recursion when deleting linked list
aoqi@0 72 while (entry->pd_set() != NULL) {
aoqi@0 73 ProtectionDomainEntry* to_delete = entry->pd_set();
aoqi@0 74 entry->set_pd_set(to_delete->next());
aoqi@0 75 delete to_delete;
aoqi@0 76 }
aoqi@0 77 Hashtable<Klass*, mtClass>::free_entry(entry);
aoqi@0 78 }
aoqi@0 79
aoqi@0 80
aoqi@0 81 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const {
aoqi@0 82 #ifdef ASSERT
aoqi@0 83 if (protection_domain == InstanceKlass::cast(klass())->protection_domain()) {
aoqi@0 84 // Ensure this doesn't show up in the pd_set (invariant)
aoqi@0 85 bool in_pd_set = false;
aoqi@0 86 for (ProtectionDomainEntry* current = _pd_set;
aoqi@0 87 current != NULL;
aoqi@0 88 current = current->next()) {
aoqi@0 89 if (current->protection_domain() == protection_domain) {
aoqi@0 90 in_pd_set = true;
aoqi@0 91 break;
aoqi@0 92 }
aoqi@0 93 }
aoqi@0 94 if (in_pd_set) {
aoqi@0 95 assert(false, "A klass's protection domain should not show up "
aoqi@0 96 "in its sys. dict. PD set");
aoqi@0 97 }
aoqi@0 98 }
aoqi@0 99 #endif /* ASSERT */
aoqi@0 100
aoqi@0 101 if (protection_domain == InstanceKlass::cast(klass())->protection_domain()) {
aoqi@0 102 // Succeeds trivially
aoqi@0 103 return true;
aoqi@0 104 }
aoqi@0 105
aoqi@0 106 for (ProtectionDomainEntry* current = _pd_set;
aoqi@0 107 current != NULL;
aoqi@0 108 current = current->next()) {
aoqi@0 109 if (current->protection_domain() == protection_domain) return true;
aoqi@0 110 }
aoqi@0 111 return false;
aoqi@0 112 }
aoqi@0 113
aoqi@0 114
aoqi@0 115 void DictionaryEntry::add_protection_domain(Dictionary* dict, oop protection_domain) {
aoqi@0 116 assert_locked_or_safepoint(SystemDictionary_lock);
aoqi@0 117 if (!contains_protection_domain(protection_domain)) {
aoqi@0 118 ProtectionDomainCacheEntry* entry = dict->cache_get(protection_domain);
aoqi@0 119 ProtectionDomainEntry* new_head =
aoqi@0 120 new ProtectionDomainEntry(entry, _pd_set);
aoqi@0 121 // Warning: Preserve store ordering. The SystemDictionary is read
aoqi@0 122 // without locks. The new ProtectionDomainEntry must be
aoqi@0 123 // complete before other threads can be allowed to see it
aoqi@0 124 // via a store to _pd_set.
aoqi@0 125 OrderAccess::release_store_ptr(&_pd_set, new_head);
aoqi@0 126 }
aoqi@0 127 if (TraceProtectionDomainVerification && WizardMode) {
aoqi@0 128 print();
aoqi@0 129 }
aoqi@0 130 }
aoqi@0 131
aoqi@0 132
thartmann@7064 133 void Dictionary::do_unloading() {
aoqi@0 134 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
aoqi@0 135
aoqi@0 136 // Remove unloadable entries and classes from system dictionary
aoqi@0 137 // The placeholder array has been handled in always_strong_oops_do.
aoqi@0 138 DictionaryEntry* probe = NULL;
thartmann@7064 139 for (int index = 0; index < table_size(); index++) {
aoqi@0 140 for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
aoqi@0 141 probe = *p;
aoqi@0 142 Klass* e = probe->klass();
aoqi@0 143 ClassLoaderData* loader_data = probe->loader_data();
aoqi@0 144
aoqi@0 145 InstanceKlass* ik = InstanceKlass::cast(e);
aoqi@0 146
aoqi@0 147 // Non-unloadable classes were handled in always_strong_oops_do
aoqi@0 148 if (!is_strongly_reachable(loader_data, e)) {
aoqi@0 149 // Entry was not visited in phase1 (negated test from phase1)
aoqi@0 150 assert(!loader_data->is_the_null_class_loader_data(), "unloading entry with null class loader");
aoqi@0 151 ClassLoaderData* k_def_class_loader_data = ik->class_loader_data();
aoqi@0 152
aoqi@0 153 // Do we need to delete this system dictionary entry?
aoqi@0 154 bool purge_entry = false;
aoqi@0 155
aoqi@0 156 // Do we need to delete this system dictionary entry?
aoqi@0 157 if (loader_data->is_unloading()) {
aoqi@0 158 // If the loader is not live this entry should always be
thartmann@7064 159 // removed (will never be looked up again).
aoqi@0 160 purge_entry = true;
aoqi@0 161 } else {
aoqi@0 162 // The loader in this entry is alive. If the klass is dead,
aoqi@0 163 // (determined by checking the defining class loader)
aoqi@0 164 // the loader must be an initiating loader (rather than the
aoqi@0 165 // defining loader). Remove this entry.
aoqi@0 166 if (k_def_class_loader_data->is_unloading()) {
aoqi@0 167 // If we get here, the class_loader_data must not be the defining
aoqi@0 168 // loader, it must be an initiating one.
aoqi@0 169 assert(k_def_class_loader_data != loader_data,
aoqi@0 170 "cannot have live defining loader and unreachable klass");
aoqi@0 171 // Loader is live, but class and its defining loader are dead.
aoqi@0 172 // Remove the entry. The class is going away.
aoqi@0 173 purge_entry = true;
aoqi@0 174 }
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 if (purge_entry) {
aoqi@0 178 *p = probe->next();
aoqi@0 179 if (probe == _current_class_entry) {
aoqi@0 180 _current_class_entry = NULL;
aoqi@0 181 }
aoqi@0 182 free_entry(probe);
aoqi@0 183 continue;
aoqi@0 184 }
aoqi@0 185 }
aoqi@0 186 p = probe->next_addr();
aoqi@0 187 }
aoqi@0 188 }
aoqi@0 189 }
aoqi@0 190
stefank@6992 191 void Dictionary::roots_oops_do(OopClosure* strong, OopClosure* weak) {
stefank@6992 192 // Skip the strong roots probe marking if the closures are the same.
stefank@6992 193 if (strong == weak) {
stefank@6992 194 oops_do(strong);
stefank@6992 195 return;
stefank@6992 196 }
stefank@6992 197
stefank@6992 198 for (int index = 0; index < table_size(); index++) {
stefank@6992 199 for (DictionaryEntry *probe = bucket(index);
stefank@6992 200 probe != NULL;
stefank@6992 201 probe = probe->next()) {
stefank@6992 202 Klass* e = probe->klass();
stefank@6992 203 ClassLoaderData* loader_data = probe->loader_data();
stefank@6992 204 if (is_strongly_reachable(loader_data, e)) {
stefank@6992 205 probe->set_strongly_reachable();
stefank@6992 206 }
stefank@6992 207 }
stefank@6992 208 }
stefank@6992 209 _pd_cache_table->roots_oops_do(strong, weak);
stefank@6992 210 }
drchase@6680 211
iklam@7089 212 void Dictionary::remove_classes_in_error_state() {
iklam@7089 213 assert(DumpSharedSpaces, "supported only when dumping");
iklam@7089 214 DictionaryEntry* probe = NULL;
iklam@7089 215 for (int index = 0; index < table_size(); index++) {
iklam@7089 216 for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
iklam@7089 217 probe = *p;
iklam@7089 218 InstanceKlass* ik = InstanceKlass::cast(probe->klass());
iklam@7089 219 if (ik->is_in_error_state()) { // purge this entry
iklam@7089 220 *p = probe->next();
iklam@7089 221 if (probe == _current_class_entry) {
iklam@7089 222 _current_class_entry = NULL;
iklam@7089 223 }
iklam@7089 224 free_entry(probe);
iklam@7089 225 ResourceMark rm;
jiangli@7363 226 tty->print_cr("Preload Warning: Removed error class: %s", ik->external_name());
iklam@7089 227 continue;
iklam@7089 228 }
iklam@7089 229
iklam@7089 230 p = probe->next_addr();
iklam@7089 231 }
iklam@7089 232 }
iklam@7089 233 }
aoqi@0 234
aoqi@0 235 void Dictionary::always_strong_oops_do(OopClosure* blk) {
aoqi@0 236 // Follow all system classes and temporary placeholders in dictionary; only
aoqi@0 237 // protection domain oops contain references into the heap. In a first
aoqi@0 238 // pass over the system dictionary determine which need to be treated as
aoqi@0 239 // strongly reachable and mark them as such.
aoqi@0 240 for (int index = 0; index < table_size(); index++) {
aoqi@0 241 for (DictionaryEntry *probe = bucket(index);
aoqi@0 242 probe != NULL;
aoqi@0 243 probe = probe->next()) {
aoqi@0 244 Klass* e = probe->klass();
aoqi@0 245 ClassLoaderData* loader_data = probe->loader_data();
aoqi@0 246 if (is_strongly_reachable(loader_data, e)) {
aoqi@0 247 probe->set_strongly_reachable();
aoqi@0 248 }
aoqi@0 249 }
aoqi@0 250 }
aoqi@0 251 // Then iterate over the protection domain cache to apply the closure on the
aoqi@0 252 // previously marked ones.
aoqi@0 253 _pd_cache_table->always_strong_oops_do(blk);
aoqi@0 254 }
aoqi@0 255
aoqi@0 256
aoqi@0 257 void Dictionary::always_strong_classes_do(KlassClosure* closure) {
aoqi@0 258 // Follow all system classes and temporary placeholders in dictionary
aoqi@0 259 for (int index = 0; index < table_size(); index++) {
aoqi@0 260 for (DictionaryEntry* probe = bucket(index);
aoqi@0 261 probe != NULL;
aoqi@0 262 probe = probe->next()) {
aoqi@0 263 Klass* e = probe->klass();
aoqi@0 264 ClassLoaderData* loader_data = probe->loader_data();
aoqi@0 265 if (is_strongly_reachable(loader_data, e)) {
aoqi@0 266 closure->do_klass(e);
aoqi@0 267 }
aoqi@0 268 }
aoqi@0 269 }
aoqi@0 270 }
aoqi@0 271
aoqi@0 272
aoqi@0 273 // Just the classes from defining class loaders
aoqi@0 274 void Dictionary::classes_do(void f(Klass*)) {
aoqi@0 275 for (int index = 0; index < table_size(); index++) {
aoqi@0 276 for (DictionaryEntry* probe = bucket(index);
aoqi@0 277 probe != NULL;
aoqi@0 278 probe = probe->next()) {
aoqi@0 279 Klass* k = probe->klass();
aoqi@0 280 if (probe->loader_data() == InstanceKlass::cast(k)->class_loader_data()) {
aoqi@0 281 f(k);
aoqi@0 282 }
aoqi@0 283 }
aoqi@0 284 }
aoqi@0 285 }
aoqi@0 286
aoqi@0 287 // Added for initialize_itable_for_klass to handle exceptions
aoqi@0 288 // Just the classes from defining class loaders
aoqi@0 289 void Dictionary::classes_do(void f(Klass*, TRAPS), TRAPS) {
aoqi@0 290 for (int index = 0; index < table_size(); index++) {
aoqi@0 291 for (DictionaryEntry* probe = bucket(index);
aoqi@0 292 probe != NULL;
aoqi@0 293 probe = probe->next()) {
aoqi@0 294 Klass* k = probe->klass();
aoqi@0 295 if (probe->loader_data() == InstanceKlass::cast(k)->class_loader_data()) {
aoqi@0 296 f(k, CHECK);
aoqi@0 297 }
aoqi@0 298 }
aoqi@0 299 }
aoqi@0 300 }
aoqi@0 301
aoqi@0 302 // All classes, and their class loaders
aoqi@0 303 // Don't iterate over placeholders
aoqi@0 304 void Dictionary::classes_do(void f(Klass*, ClassLoaderData*)) {
aoqi@0 305 for (int index = 0; index < table_size(); index++) {
aoqi@0 306 for (DictionaryEntry* probe = bucket(index);
aoqi@0 307 probe != NULL;
aoqi@0 308 probe = probe->next()) {
aoqi@0 309 Klass* k = probe->klass();
aoqi@0 310 f(k, probe->loader_data());
aoqi@0 311 }
aoqi@0 312 }
aoqi@0 313 }
aoqi@0 314
aoqi@0 315 void Dictionary::oops_do(OopClosure* f) {
aoqi@0 316 // Only the protection domain oops contain references into the heap. Iterate
aoqi@0 317 // over all of them.
aoqi@0 318 _pd_cache_table->oops_do(f);
aoqi@0 319 }
aoqi@0 320
aoqi@0 321 void Dictionary::methods_do(void f(Method*)) {
aoqi@0 322 for (int index = 0; index < table_size(); index++) {
aoqi@0 323 for (DictionaryEntry* probe = bucket(index);
aoqi@0 324 probe != NULL;
aoqi@0 325 probe = probe->next()) {
aoqi@0 326 Klass* k = probe->klass();
aoqi@0 327 if (probe->loader_data() == InstanceKlass::cast(k)->class_loader_data()) {
aoqi@0 328 // only take klass is we have the entry with the defining class loader
aoqi@0 329 InstanceKlass::cast(k)->methods_do(f);
aoqi@0 330 }
aoqi@0 331 }
aoqi@0 332 }
aoqi@0 333 }
aoqi@0 334
aoqi@0 335 void Dictionary::unlink(BoolObjectClosure* is_alive) {
aoqi@0 336 // Only the protection domain cache table may contain references to the heap
aoqi@0 337 // that need to be unlinked.
aoqi@0 338 _pd_cache_table->unlink(is_alive);
aoqi@0 339 }
aoqi@0 340
aoqi@0 341 Klass* Dictionary::try_get_next_class() {
aoqi@0 342 while (true) {
aoqi@0 343 if (_current_class_entry != NULL) {
aoqi@0 344 Klass* k = _current_class_entry->klass();
aoqi@0 345 _current_class_entry = _current_class_entry->next();
aoqi@0 346 return k;
aoqi@0 347 }
aoqi@0 348 _current_class_index = (_current_class_index + 1) % table_size();
aoqi@0 349 _current_class_entry = bucket(_current_class_index);
aoqi@0 350 }
aoqi@0 351 // never reached
aoqi@0 352 }
aoqi@0 353
aoqi@0 354 // Add a loaded class to the system dictionary.
aoqi@0 355 // Readers of the SystemDictionary aren't always locked, so _buckets
aoqi@0 356 // is volatile. The store of the next field in the constructor is
aoqi@0 357 // also cast to volatile; we do this to ensure store order is maintained
aoqi@0 358 // by the compilers.
aoqi@0 359
aoqi@0 360 void Dictionary::add_klass(Symbol* class_name, ClassLoaderData* loader_data,
aoqi@0 361 KlassHandle obj) {
aoqi@0 362 assert_locked_or_safepoint(SystemDictionary_lock);
aoqi@0 363 assert(obj() != NULL, "adding NULL obj");
aoqi@0 364 assert(obj()->name() == class_name, "sanity check on name");
aoqi@0 365 assert(loader_data != NULL, "Must be non-NULL");
aoqi@0 366
aoqi@0 367 unsigned int hash = compute_hash(class_name, loader_data);
aoqi@0 368 int index = hash_to_index(hash);
aoqi@0 369 DictionaryEntry* entry = new_entry(hash, obj(), loader_data);
aoqi@0 370 add_entry(index, entry);
aoqi@0 371 }
aoqi@0 372
aoqi@0 373
aoqi@0 374 // This routine does not lock the system dictionary.
aoqi@0 375 //
aoqi@0 376 // Since readers don't hold a lock, we must make sure that system
aoqi@0 377 // dictionary entries are only removed at a safepoint (when only one
aoqi@0 378 // thread is running), and are added to in a safe way (all links must
aoqi@0 379 // be updated in an MT-safe manner).
aoqi@0 380 //
aoqi@0 381 // Callers should be aware that an entry could be added just after
aoqi@0 382 // _buckets[index] is read here, so the caller will not see the new entry.
aoqi@0 383 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash,
aoqi@0 384 Symbol* class_name,
aoqi@0 385 ClassLoaderData* loader_data) {
aoqi@0 386 debug_only(_lookup_count++);
aoqi@0 387 for (DictionaryEntry* entry = bucket(index);
aoqi@0 388 entry != NULL;
aoqi@0 389 entry = entry->next()) {
aoqi@0 390 if (entry->hash() == hash && entry->equals(class_name, loader_data)) {
aoqi@0 391 return entry;
aoqi@0 392 }
aoqi@0 393 debug_only(_lookup_length++);
aoqi@0 394 }
aoqi@0 395 return NULL;
aoqi@0 396 }
aoqi@0 397
aoqi@0 398
aoqi@0 399 Klass* Dictionary::find(int index, unsigned int hash, Symbol* name,
aoqi@0 400 ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
aoqi@0 401 DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
aoqi@0 402 if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) {
aoqi@0 403 return entry->klass();
aoqi@0 404 } else {
aoqi@0 405 return NULL;
aoqi@0 406 }
aoqi@0 407 }
aoqi@0 408
aoqi@0 409
aoqi@0 410 Klass* Dictionary::find_class(int index, unsigned int hash,
aoqi@0 411 Symbol* name, ClassLoaderData* loader_data) {
aoqi@0 412 assert_locked_or_safepoint(SystemDictionary_lock);
aoqi@0 413 assert (index == index_for(name, loader_data), "incorrect index?");
aoqi@0 414
aoqi@0 415 DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
aoqi@0 416 return (entry != NULL) ? entry->klass() : (Klass*)NULL;
aoqi@0 417 }
aoqi@0 418
aoqi@0 419
aoqi@0 420 // Variant of find_class for shared classes. No locking required, as
aoqi@0 421 // that table is static.
aoqi@0 422
aoqi@0 423 Klass* Dictionary::find_shared_class(int index, unsigned int hash,
aoqi@0 424 Symbol* name) {
aoqi@0 425 assert (index == index_for(name, NULL), "incorrect index?");
aoqi@0 426
aoqi@0 427 DictionaryEntry* entry = get_entry(index, hash, name, NULL);
aoqi@0 428 return (entry != NULL) ? entry->klass() : (Klass*)NULL;
aoqi@0 429 }
aoqi@0 430
aoqi@0 431
aoqi@0 432 void Dictionary::add_protection_domain(int index, unsigned int hash,
aoqi@0 433 instanceKlassHandle klass,
aoqi@0 434 ClassLoaderData* loader_data, Handle protection_domain,
aoqi@0 435 TRAPS) {
aoqi@0 436 Symbol* klass_name = klass->name();
aoqi@0 437 DictionaryEntry* entry = get_entry(index, hash, klass_name, loader_data);
aoqi@0 438
aoqi@0 439 assert(entry != NULL,"entry must be present, we just created it");
aoqi@0 440 assert(protection_domain() != NULL,
aoqi@0 441 "real protection domain should be present");
aoqi@0 442
aoqi@0 443 entry->add_protection_domain(this, protection_domain());
aoqi@0 444
aoqi@0 445 assert(entry->contains_protection_domain(protection_domain()),
aoqi@0 446 "now protection domain should be present");
aoqi@0 447 }
aoqi@0 448
aoqi@0 449
aoqi@0 450 bool Dictionary::is_valid_protection_domain(int index, unsigned int hash,
aoqi@0 451 Symbol* name,
aoqi@0 452 ClassLoaderData* loader_data,
aoqi@0 453 Handle protection_domain) {
aoqi@0 454 DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
aoqi@0 455 return entry->is_valid_protection_domain(protection_domain);
aoqi@0 456 }
aoqi@0 457
aoqi@0 458
aoqi@0 459 void Dictionary::reorder_dictionary() {
aoqi@0 460
aoqi@0 461 // Copy all the dictionary entries into a single master list.
aoqi@0 462
aoqi@0 463 DictionaryEntry* master_list = NULL;
aoqi@0 464 for (int i = 0; i < table_size(); ++i) {
aoqi@0 465 DictionaryEntry* p = bucket(i);
aoqi@0 466 while (p != NULL) {
aoqi@0 467 DictionaryEntry* tmp;
aoqi@0 468 tmp = p->next();
aoqi@0 469 p->set_next(master_list);
aoqi@0 470 master_list = p;
aoqi@0 471 p = tmp;
aoqi@0 472 }
aoqi@0 473 set_entry(i, NULL);
aoqi@0 474 }
aoqi@0 475
aoqi@0 476 // Add the dictionary entries back to the list in the correct buckets.
aoqi@0 477 while (master_list != NULL) {
aoqi@0 478 DictionaryEntry* p = master_list;
aoqi@0 479 master_list = master_list->next();
aoqi@0 480 p->set_next(NULL);
aoqi@0 481 Symbol* class_name = InstanceKlass::cast((Klass*)(p->klass()))->name();
aoqi@0 482 // Since the null class loader data isn't copied to the CDS archive,
aoqi@0 483 // compute the hash with NULL for loader data.
aoqi@0 484 unsigned int hash = compute_hash(class_name, NULL);
aoqi@0 485 int index = hash_to_index(hash);
aoqi@0 486 p->set_hash(hash);
aoqi@0 487 p->set_loader_data(NULL); // loader_data isn't copied to CDS
aoqi@0 488 p->set_next(bucket(index));
aoqi@0 489 set_entry(index, p);
aoqi@0 490 }
aoqi@0 491 }
aoqi@0 492
aoqi@0 493 ProtectionDomainCacheTable::ProtectionDomainCacheTable(int table_size)
aoqi@0 494 : Hashtable<oop, mtClass>(table_size, sizeof(ProtectionDomainCacheEntry))
aoqi@0 495 {
aoqi@0 496 }
aoqi@0 497
aoqi@0 498 void ProtectionDomainCacheTable::unlink(BoolObjectClosure* is_alive) {
aoqi@0 499 assert(SafepointSynchronize::is_at_safepoint(), "must be");
aoqi@0 500 for (int i = 0; i < table_size(); ++i) {
aoqi@0 501 ProtectionDomainCacheEntry** p = bucket_addr(i);
aoqi@0 502 ProtectionDomainCacheEntry* entry = bucket(i);
aoqi@0 503 while (entry != NULL) {
aoqi@0 504 if (is_alive->do_object_b(entry->literal())) {
aoqi@0 505 p = entry->next_addr();
aoqi@0 506 } else {
aoqi@0 507 *p = entry->next();
aoqi@0 508 free_entry(entry);
aoqi@0 509 }
aoqi@0 510 entry = *p;
aoqi@0 511 }
aoqi@0 512 }
aoqi@0 513 }
aoqi@0 514
aoqi@0 515 void ProtectionDomainCacheTable::oops_do(OopClosure* f) {
aoqi@0 516 for (int index = 0; index < table_size(); index++) {
aoqi@0 517 for (ProtectionDomainCacheEntry* probe = bucket(index);
aoqi@0 518 probe != NULL;
aoqi@0 519 probe = probe->next()) {
aoqi@0 520 probe->oops_do(f);
aoqi@0 521 }
aoqi@0 522 }
aoqi@0 523 }
aoqi@0 524
stefank@6992 525 void ProtectionDomainCacheTable::roots_oops_do(OopClosure* strong, OopClosure* weak) {
stefank@6992 526 for (int index = 0; index < table_size(); index++) {
stefank@6992 527 for (ProtectionDomainCacheEntry* probe = bucket(index);
stefank@6992 528 probe != NULL;
stefank@6992 529 probe = probe->next()) {
stefank@6992 530 if (probe->is_strongly_reachable()) {
stefank@6992 531 probe->reset_strongly_reachable();
stefank@6992 532 probe->oops_do(strong);
stefank@6992 533 } else {
stefank@6992 534 if (weak != NULL) {
stefank@6992 535 probe->oops_do(weak);
stefank@6992 536 }
stefank@6992 537 }
stefank@6992 538 }
stefank@6992 539 }
stefank@6992 540 }
stefank@6992 541
aoqi@0 542 uint ProtectionDomainCacheTable::bucket_size() {
aoqi@0 543 return sizeof(ProtectionDomainCacheEntry);
aoqi@0 544 }
aoqi@0 545
aoqi@0 546 #ifndef PRODUCT
aoqi@0 547 void ProtectionDomainCacheTable::print() {
aoqi@0 548 tty->print_cr("Protection domain cache table (table_size=%d, classes=%d)",
aoqi@0 549 table_size(), number_of_entries());
aoqi@0 550 for (int index = 0; index < table_size(); index++) {
aoqi@0 551 for (ProtectionDomainCacheEntry* probe = bucket(index);
aoqi@0 552 probe != NULL;
aoqi@0 553 probe = probe->next()) {
aoqi@0 554 probe->print();
aoqi@0 555 }
aoqi@0 556 }
aoqi@0 557 }
aoqi@0 558
aoqi@0 559 void ProtectionDomainCacheEntry::print() {
aoqi@0 560 tty->print_cr("entry "PTR_FORMAT" value "PTR_FORMAT" strongly_reachable %d next "PTR_FORMAT,
aoqi@0 561 this, (void*)literal(), _strongly_reachable, next());
aoqi@0 562 }
aoqi@0 563 #endif
aoqi@0 564
aoqi@0 565 void ProtectionDomainCacheTable::verify() {
aoqi@0 566 int element_count = 0;
aoqi@0 567 for (int index = 0; index < table_size(); index++) {
aoqi@0 568 for (ProtectionDomainCacheEntry* probe = bucket(index);
aoqi@0 569 probe != NULL;
aoqi@0 570 probe = probe->next()) {
aoqi@0 571 probe->verify();
aoqi@0 572 element_count++;
aoqi@0 573 }
aoqi@0 574 }
aoqi@0 575 guarantee(number_of_entries() == element_count,
aoqi@0 576 "Verify of protection domain cache table failed");
aoqi@0 577 debug_only(verify_lookup_length((double)number_of_entries() / table_size()));
aoqi@0 578 }
aoqi@0 579
aoqi@0 580 void ProtectionDomainCacheEntry::verify() {
aoqi@0 581 guarantee(literal()->is_oop(), "must be an oop");
aoqi@0 582 }
aoqi@0 583
aoqi@0 584 void ProtectionDomainCacheTable::always_strong_oops_do(OopClosure* f) {
aoqi@0 585 // the caller marked the protection domain cache entries that we need to apply
aoqi@0 586 // the closure on. Only process them.
aoqi@0 587 for (int index = 0; index < table_size(); index++) {
aoqi@0 588 for (ProtectionDomainCacheEntry* probe = bucket(index);
aoqi@0 589 probe != NULL;
aoqi@0 590 probe = probe->next()) {
aoqi@0 591 if (probe->is_strongly_reachable()) {
aoqi@0 592 probe->reset_strongly_reachable();
aoqi@0 593 probe->oops_do(f);
aoqi@0 594 }
aoqi@0 595 }
aoqi@0 596 }
aoqi@0 597 }
aoqi@0 598
aoqi@0 599 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::get(oop protection_domain) {
aoqi@0 600 unsigned int hash = compute_hash(protection_domain);
aoqi@0 601 int index = hash_to_index(hash);
aoqi@0 602
aoqi@0 603 ProtectionDomainCacheEntry* entry = find_entry(index, protection_domain);
aoqi@0 604 if (entry == NULL) {
aoqi@0 605 entry = add_entry(index, hash, protection_domain);
aoqi@0 606 }
aoqi@0 607 return entry;
aoqi@0 608 }
aoqi@0 609
aoqi@0 610 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::find_entry(int index, oop protection_domain) {
aoqi@0 611 for (ProtectionDomainCacheEntry* e = bucket(index); e != NULL; e = e->next()) {
aoqi@0 612 if (e->protection_domain() == protection_domain) {
aoqi@0 613 return e;
aoqi@0 614 }
aoqi@0 615 }
aoqi@0 616
aoqi@0 617 return NULL;
aoqi@0 618 }
aoqi@0 619
aoqi@0 620 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::add_entry(int index, unsigned int hash, oop protection_domain) {
aoqi@0 621 assert_locked_or_safepoint(SystemDictionary_lock);
aoqi@0 622 assert(index == index_for(protection_domain), "incorrect index?");
aoqi@0 623 assert(find_entry(index, protection_domain) == NULL, "no double entry");
aoqi@0 624
aoqi@0 625 ProtectionDomainCacheEntry* p = new_entry(hash, protection_domain);
aoqi@0 626 Hashtable<oop, mtClass>::add_entry(index, p);
aoqi@0 627 return p;
aoqi@0 628 }
aoqi@0 629
aoqi@0 630 void ProtectionDomainCacheTable::free(ProtectionDomainCacheEntry* to_delete) {
aoqi@0 631 unsigned int hash = compute_hash(to_delete->protection_domain());
aoqi@0 632 int index = hash_to_index(hash);
aoqi@0 633
aoqi@0 634 ProtectionDomainCacheEntry** p = bucket_addr(index);
aoqi@0 635 ProtectionDomainCacheEntry* entry = bucket(index);
aoqi@0 636 while (true) {
aoqi@0 637 assert(entry != NULL, "sanity");
aoqi@0 638
aoqi@0 639 if (entry == to_delete) {
aoqi@0 640 *p = entry->next();
aoqi@0 641 Hashtable<oop, mtClass>::free_entry(entry);
aoqi@0 642 break;
aoqi@0 643 } else {
aoqi@0 644 p = entry->next_addr();
aoqi@0 645 entry = *p;
aoqi@0 646 }
aoqi@0 647 }
aoqi@0 648 }
aoqi@0 649
aoqi@0 650 SymbolPropertyTable::SymbolPropertyTable(int table_size)
aoqi@0 651 : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry))
aoqi@0 652 {
aoqi@0 653 }
aoqi@0 654 SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t,
aoqi@0 655 int number_of_entries)
aoqi@0 656 : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries)
aoqi@0 657 {
aoqi@0 658 }
aoqi@0 659
aoqi@0 660
aoqi@0 661 SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash,
aoqi@0 662 Symbol* sym,
aoqi@0 663 intptr_t sym_mode) {
aoqi@0 664 assert(index == index_for(sym, sym_mode), "incorrect index?");
aoqi@0 665 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
aoqi@0 666 if (p->hash() == hash && p->symbol() == sym && p->symbol_mode() == sym_mode) {
aoqi@0 667 return p;
aoqi@0 668 }
aoqi@0 669 }
aoqi@0 670 return NULL;
aoqi@0 671 }
aoqi@0 672
aoqi@0 673
aoqi@0 674 SymbolPropertyEntry* SymbolPropertyTable::add_entry(int index, unsigned int hash,
aoqi@0 675 Symbol* sym, intptr_t sym_mode) {
aoqi@0 676 assert_locked_or_safepoint(SystemDictionary_lock);
aoqi@0 677 assert(index == index_for(sym, sym_mode), "incorrect index?");
aoqi@0 678 assert(find_entry(index, hash, sym, sym_mode) == NULL, "no double entry");
aoqi@0 679
aoqi@0 680 SymbolPropertyEntry* p = new_entry(hash, sym, sym_mode);
aoqi@0 681 Hashtable<Symbol*, mtSymbol>::add_entry(index, p);
aoqi@0 682 return p;
aoqi@0 683 }
aoqi@0 684
aoqi@0 685 void SymbolPropertyTable::oops_do(OopClosure* f) {
aoqi@0 686 for (int index = 0; index < table_size(); index++) {
aoqi@0 687 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
aoqi@0 688 if (p->method_type() != NULL) {
aoqi@0 689 f->do_oop(p->method_type_addr());
aoqi@0 690 }
aoqi@0 691 }
aoqi@0 692 }
aoqi@0 693 }
aoqi@0 694
aoqi@0 695 void SymbolPropertyTable::methods_do(void f(Method*)) {
aoqi@0 696 for (int index = 0; index < table_size(); index++) {
aoqi@0 697 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
aoqi@0 698 Method* prop = p->method();
aoqi@0 699 if (prop != NULL) {
aoqi@0 700 f((Method*)prop);
aoqi@0 701 }
aoqi@0 702 }
aoqi@0 703 }
aoqi@0 704 }
aoqi@0 705
aoqi@0 706
aoqi@0 707 // ----------------------------------------------------------------------------
aoqi@0 708
iklam@7089 709 void Dictionary::print(bool details) {
aoqi@0 710 ResourceMark rm;
aoqi@0 711 HandleMark hm;
aoqi@0 712
iklam@7089 713 if (details) {
iklam@7089 714 tty->print_cr("Java system dictionary (table_size=%d, classes=%d)",
iklam@7089 715 table_size(), number_of_entries());
iklam@7089 716 tty->print_cr("^ indicates that initiating loader is different from "
iklam@7089 717 "defining loader");
iklam@7089 718 }
aoqi@0 719
aoqi@0 720 for (int index = 0; index < table_size(); index++) {
aoqi@0 721 for (DictionaryEntry* probe = bucket(index);
aoqi@0 722 probe != NULL;
aoqi@0 723 probe = probe->next()) {
aoqi@0 724 if (Verbose) tty->print("%4d: ", index);
aoqi@0 725 Klass* e = probe->klass();
aoqi@0 726 ClassLoaderData* loader_data = probe->loader_data();
aoqi@0 727 bool is_defining_class =
aoqi@0 728 (loader_data == InstanceKlass::cast(e)->class_loader_data());
iklam@7089 729 tty->print("%s%s", ((!details) || is_defining_class) ? " " : "^",
aoqi@0 730 e->external_name());
aoqi@0 731
iklam@7089 732 if (details) {
aoqi@0 733 tty->print(", loader ");
iklam@7089 734 if (loader_data != NULL) {
iklam@7089 735 loader_data->print_value();
iklam@7089 736 } else {
iklam@7089 737 tty->print("NULL");
iklam@7089 738 }
iklam@7089 739 }
aoqi@0 740 tty->cr();
aoqi@0 741 }
aoqi@0 742 }
iklam@7089 743
iklam@7089 744 if (details) {
iklam@7089 745 tty->cr();
iklam@7089 746 _pd_cache_table->print();
iklam@7089 747 }
aoqi@0 748 tty->cr();
aoqi@0 749 }
aoqi@0 750
aoqi@0 751 void Dictionary::verify() {
aoqi@0 752 guarantee(number_of_entries() >= 0, "Verify of system dictionary failed");
aoqi@0 753
aoqi@0 754 int element_count = 0;
aoqi@0 755 for (int index = 0; index < table_size(); index++) {
aoqi@0 756 for (DictionaryEntry* probe = bucket(index);
aoqi@0 757 probe != NULL;
aoqi@0 758 probe = probe->next()) {
aoqi@0 759 Klass* e = probe->klass();
aoqi@0 760 ClassLoaderData* loader_data = probe->loader_data();
aoqi@0 761 guarantee(e->oop_is_instance(),
aoqi@0 762 "Verify of system dictionary failed");
aoqi@0 763 // class loader must be present; a null class loader is the
aoqi@0 764 // boostrap loader
aoqi@0 765 guarantee(loader_data != NULL || DumpSharedSpaces ||
aoqi@0 766 loader_data->class_loader() == NULL ||
aoqi@0 767 loader_data->class_loader()->is_instance(),
aoqi@0 768 "checking type of class_loader");
aoqi@0 769 e->verify();
aoqi@0 770 probe->verify_protection_domain_set();
aoqi@0 771 element_count++;
aoqi@0 772 }
aoqi@0 773 }
aoqi@0 774 guarantee(number_of_entries() == element_count,
aoqi@0 775 "Verify of system dictionary failed");
aoqi@0 776 debug_only(verify_lookup_length((double)number_of_entries() / table_size()));
aoqi@0 777
aoqi@0 778 _pd_cache_table->verify();
aoqi@0 779 }
aoqi@0 780

mercurial