dictionary.cpp 20.0 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30
#include "precompiled.hpp"
#include "classfile/dictionary.hpp"
#include "classfile/systemDictionary.hpp"
#include "oops/oop.inline.hpp"
#include "prims/jvmtiRedefineClassesTrace.hpp"
#include "utilities/hashtable.inline.hpp"
D
duke 已提交
31 32 33 34 35 36 37


DictionaryEntry*  Dictionary::_current_class_entry = NULL;
int               Dictionary::_current_class_index =    0;


Dictionary::Dictionary(int table_size)
38
  : TwoOopHashtable<Klass*, mtClass>(table_size, sizeof(DictionaryEntry)) {
D
duke 已提交
39 40 41 42 43 44
  _current_class_index = 0;
  _current_class_entry = NULL;
};



Z
zgu 已提交
45
Dictionary::Dictionary(int table_size, HashtableBucket<mtClass>* t,
D
duke 已提交
46
                       int number_of_entries)
47
  : TwoOopHashtable<Klass*, mtClass>(table_size, sizeof(DictionaryEntry), t, number_of_entries) {
D
duke 已提交
48 49 50 51 52
  _current_class_index = 0;
  _current_class_entry = NULL;
};


53 54 55 56
DictionaryEntry* Dictionary::new_entry(unsigned int hash, Klass* klass,
                                       ClassLoaderData* loader_data) {
  DictionaryEntry* entry = (DictionaryEntry*)Hashtable<Klass*, mtClass>::new_entry(hash, klass);
  entry->set_loader_data(loader_data);
D
duke 已提交
57
  entry->set_pd_set(NULL);
58
  assert(klass->oop_is_instance(), "Must be");
D
duke 已提交
59 60 61 62 63 64 65 66 67 68 69
  return entry;
}


void Dictionary::free_entry(DictionaryEntry* entry) {
  // avoid recursion when deleting linked list
  while (entry->pd_set() != NULL) {
    ProtectionDomainEntry* to_delete = entry->pd_set();
    entry->set_pd_set(to_delete->next());
    delete to_delete;
  }
70
  Hashtable<Klass*, mtClass>::free_entry(entry);
D
duke 已提交
71 72 73 74 75
}


bool DictionaryEntry::contains_protection_domain(oop protection_domain) const {
#ifdef ASSERT
76
  if (protection_domain == InstanceKlass::cast(klass())->protection_domain()) {
D
duke 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    // Ensure this doesn't show up in the pd_set (invariant)
    bool in_pd_set = false;
    for (ProtectionDomainEntry* current = _pd_set;
                                current != NULL;
                                current = current->next()) {
      if (current->protection_domain() == protection_domain) {
        in_pd_set = true;
        break;
      }
    }
    if (in_pd_set) {
      assert(false, "A klass's protection domain should not show up "
                    "in its sys. dict. PD set");
    }
  }
#endif /* ASSERT */

94
  if (protection_domain == InstanceKlass::cast(klass())->protection_domain()) {
D
duke 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    // Succeeds trivially
    return true;
  }

  for (ProtectionDomainEntry* current = _pd_set;
                              current != NULL;
                              current = current->next()) {
    if (current->protection_domain() == protection_domain) return true;
  }
  return false;
}


void DictionaryEntry::add_protection_domain(oop protection_domain) {
  assert_locked_or_safepoint(SystemDictionary_lock);
  if (!contains_protection_domain(protection_domain)) {
    ProtectionDomainEntry* new_head =
                new ProtectionDomainEntry(protection_domain, _pd_set);
    // Warning: Preserve store ordering.  The SystemDictionary is read
    //          without locks.  The new ProtectionDomainEntry must be
    //          complete before other threads can be allowed to see it
    //          via a store to _pd_set.
    OrderAccess::release_store_ptr(&_pd_set, new_head);
  }
  if (TraceProtectionDomainVerification && WizardMode) {
    print();
  }
}


125
bool Dictionary::do_unloading() {
126
  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
D
duke 已提交
127 128 129 130 131 132 133 134 135
  bool class_was_unloaded = false;
  int  index = 0; // Defined here for portability! Do not move

  // Remove unloadable entries and classes from system dictionary
  // The placeholder array has been handled in always_strong_oops_do.
  DictionaryEntry* probe = NULL;
  for (index = 0; index < table_size(); index++) {
    for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
      probe = *p;
136 137
      Klass* e = probe->klass();
      ClassLoaderData* loader_data = probe->loader_data();
D
duke 已提交
138

139
      InstanceKlass* ik = InstanceKlass::cast(e);
D
duke 已提交
140 141

      // Non-unloadable classes were handled in always_strong_oops_do
142
      if (!is_strongly_reachable(loader_data, e)) {
D
duke 已提交
143
        // Entry was not visited in phase1 (negated test from phase1)
144 145
        assert(!loader_data->is_the_null_class_loader_data(), "unloading entry with null class loader");
        ClassLoaderData* k_def_class_loader_data = ik->class_loader_data();
D
duke 已提交
146 147 148 149 150

        // Do we need to delete this system dictionary entry?
        bool purge_entry = false;

        // Do we need to delete this system dictionary entry?
151
        if (loader_data->is_unloading()) {
D
duke 已提交
152 153 154
          // If the loader is not live this entry should always be
          // removed (will never be looked up again). Note that this is
          // not the same as unloading the referred class.
155
          if (k_def_class_loader_data == loader_data) {
D
duke 已提交
156 157 158 159 160 161 162 163 164
            // This is the defining entry, so the referred class is about
            // to be unloaded.
            class_was_unloaded = true;
          }
          // Also remove this system dictionary entry.
          purge_entry = true;

        } else {
          // The loader in this entry is alive. If the klass is dead,
165
          // (determined by checking the defining class loader)
D
duke 已提交
166 167
          // the loader must be an initiating loader (rather than the
          // defining loader). Remove this entry.
168 169
          if (k_def_class_loader_data->is_unloading()) {
            // If we get here, the class_loader_data must not be the defining
D
duke 已提交
170
            // loader, it must be an initiating one.
171
            assert(k_def_class_loader_data != loader_data,
D
duke 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
                   "cannot have live defining loader and unreachable klass");
            // Loader is live, but class and its defining loader are dead.
            // Remove the entry. The class is going away.
            purge_entry = true;
          }
        }

        if (purge_entry) {
          *p = probe->next();
          if (probe == _current_class_entry) {
            _current_class_entry = NULL;
          }
          free_entry(probe);
          continue;
        }
      }
      p = probe->next_addr();
    }
  }
  return class_was_unloaded;
}


195
void Dictionary::always_strong_oops_do(OopClosure* blk) {
D
duke 已提交
196 197 198 199 200
  // Follow all system classes and temporary placeholders in dictionary
  for (int index = 0; index < table_size(); index++) {
    for (DictionaryEntry *probe = bucket(index);
                          probe != NULL;
                          probe = probe->next()) {
201 202 203
      Klass* e = probe->klass();
      ClassLoaderData* loader_data = probe->loader_data();
      if (is_strongly_reachable(loader_data, e)) {
D
duke 已提交
204 205 206 207 208 209 210
        probe->protection_domain_set_oops_do(blk);
      }
    }
  }
}


211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
void Dictionary::always_strong_classes_do(KlassClosure* closure) {
  // Follow all system classes and temporary placeholders in dictionary
  for (int index = 0; index < table_size(); index++) {
    for (DictionaryEntry* probe = bucket(index);
                          probe != NULL;
                          probe = probe->next()) {
      Klass* e = probe->klass();
      ClassLoaderData* loader_data = probe->loader_data();
      if (is_strongly_reachable(loader_data, e)) {
        closure->do_klass(e);
      }
    }
  }
}


D
duke 已提交
227
//   Just the classes from defining class loaders
228
void Dictionary::classes_do(void f(Klass*)) {
D
duke 已提交
229 230 231 232
  for (int index = 0; index < table_size(); index++) {
    for (DictionaryEntry* probe = bucket(index);
                          probe != NULL;
                          probe = probe->next()) {
233 234
      Klass* k = probe->klass();
      if (probe->loader_data() == InstanceKlass::cast(k)->class_loader_data()) {
D
duke 已提交
235 236 237 238 239 240 241 242
        f(k);
      }
    }
  }
}

// Added for initialize_itable_for_klass to handle exceptions
//   Just the classes from defining class loaders
243
void Dictionary::classes_do(void f(Klass*, TRAPS), TRAPS) {
D
duke 已提交
244 245 246 247
  for (int index = 0; index < table_size(); index++) {
    for (DictionaryEntry* probe = bucket(index);
                          probe != NULL;
                          probe = probe->next()) {
248 249
      Klass* k = probe->klass();
      if (probe->loader_data() == InstanceKlass::cast(k)->class_loader_data()) {
D
duke 已提交
250 251 252 253 254 255 256 257 258 259
        f(k, CHECK);
      }
    }
  }
}


//   All classes, and their class loaders
//   (added for helpers that use HandleMarks and ResourceMarks)
// Don't iterate over placeholders
260
void Dictionary::classes_do(void f(Klass*, ClassLoaderData*, TRAPS), TRAPS) {
D
duke 已提交
261 262 263 264
  for (int index = 0; index < table_size(); index++) {
    for (DictionaryEntry* probe = bucket(index);
                          probe != NULL;
                          probe = probe->next()) {
265 266
      Klass* k = probe->klass();
      f(k, probe->loader_data(), CHECK);
D
duke 已提交
267 268 269 270 271 272 273
    }
  }
}


//   All classes, and their class loaders
// Don't iterate over placeholders
274
void Dictionary::classes_do(void f(Klass*, ClassLoaderData*)) {
D
duke 已提交
275 276 277 278
  for (int index = 0; index < table_size(); index++) {
    for (DictionaryEntry* probe = bucket(index);
                          probe != NULL;
                          probe = probe->next()) {
279 280
      Klass* k = probe->klass();
      f(k, probe->loader_data());
D
duke 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    }
  }
}


void Dictionary::oops_do(OopClosure* f) {
  for (int index = 0; index < table_size(); index++) {
    for (DictionaryEntry* probe = bucket(index);
                          probe != NULL;
                          probe = probe->next()) {
      probe->protection_domain_set_oops_do(f);
    }
  }
}


297
void Dictionary::methods_do(void f(Method*)) {
D
duke 已提交
298 299 300 301
  for (int index = 0; index < table_size(); index++) {
    for (DictionaryEntry* probe = bucket(index);
                          probe != NULL;
                          probe = probe->next()) {
302 303
      Klass* k = probe->klass();
      if (probe->loader_data() == InstanceKlass::cast(k)->class_loader_data()) {
D
duke 已提交
304
        // only take klass is we have the entry with the defining class loader
305
        InstanceKlass::cast(k)->methods_do(f);
D
duke 已提交
306 307 308 309 310 311
      }
    }
  }
}


312
Klass* Dictionary::try_get_next_class() {
D
duke 已提交
313 314
  while (true) {
    if (_current_class_entry != NULL) {
315
      Klass* k = _current_class_entry->klass();
D
duke 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
      _current_class_entry = _current_class_entry->next();
      return k;
    }
    _current_class_index = (_current_class_index + 1) % table_size();
    _current_class_entry = bucket(_current_class_index);
  }
  // never reached
}


// Add a loaded class to the system dictionary.
// Readers of the SystemDictionary aren't always locked, so _buckets
// is volatile. The store of the next field in the constructor is
// also cast to volatile;  we do this to ensure store order is maintained
// by the compilers.

332
void Dictionary::add_klass(Symbol* class_name, ClassLoaderData* loader_data,
D
duke 已提交
333 334 335
                           KlassHandle obj) {
  assert_locked_or_safepoint(SystemDictionary_lock);
  assert(obj() != NULL, "adding NULL obj");
H
hseigel 已提交
336
  assert(obj()->name() == class_name, "sanity check on name");
337
  assert(loader_data != NULL, "Must be non-NULL");
D
duke 已提交
338

339
  unsigned int hash = compute_hash(class_name, loader_data);
D
duke 已提交
340
  int index = hash_to_index(hash);
341
  DictionaryEntry* entry = new_entry(hash, obj(), loader_data);
D
duke 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355
  add_entry(index, entry);
}


// This routine does not lock the system dictionary.
//
// Since readers don't hold a lock, we must make sure that system
// dictionary entries are only removed at a safepoint (when only one
// thread is running), and are added to in a safe way (all links must
// be updated in an MT-safe manner).
//
// Callers should be aware that an entry could be added just after
// _buckets[index] is read here, so the caller will not see the new entry.
DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash,
356
                                       Symbol* class_name,
357
                                       ClassLoaderData* loader_data) {
D
duke 已提交
358 359 360 361
  debug_only(_lookup_count++);
  for (DictionaryEntry* entry = bucket(index);
                        entry != NULL;
                        entry = entry->next()) {
362
    if (entry->hash() == hash && entry->equals(class_name, loader_data)) {
D
duke 已提交
363 364 365 366 367 368 369 370
      return entry;
    }
    debug_only(_lookup_length++);
  }
  return NULL;
}


371 372 373
Klass* Dictionary::find(int index, unsigned int hash, Symbol* name,
                          ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
  DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
D
duke 已提交
374 375 376 377 378 379 380 381
  if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) {
    return entry->klass();
  } else {
    return NULL;
  }
}


382 383
Klass* Dictionary::find_class(int index, unsigned int hash,
                                Symbol* name, ClassLoaderData* loader_data) {
D
duke 已提交
384
  assert_locked_or_safepoint(SystemDictionary_lock);
385
  assert (index == index_for(name, loader_data), "incorrect index?");
D
duke 已提交
386

387 388
  DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
  return (entry != NULL) ? entry->klass() : (Klass*)NULL;
D
duke 已提交
389 390 391 392 393 394
}


// Variant of find_class for shared classes.  No locking required, as
// that table is static.

395
Klass* Dictionary::find_shared_class(int index, unsigned int hash,
396
                                       Symbol* name) {
397
  assert (index == index_for(name, NULL), "incorrect index?");
D
duke 已提交
398

399 400
  DictionaryEntry* entry = get_entry(index, hash, name, NULL);
  return (entry != NULL) ? entry->klass() : (Klass*)NULL;
D
duke 已提交
401 402 403 404 405
}


void Dictionary::add_protection_domain(int index, unsigned int hash,
                                       instanceKlassHandle klass,
406
                                       ClassLoaderData* loader_data, Handle protection_domain,
D
duke 已提交
407
                                       TRAPS) {
408
  Symbol*  klass_name = klass->name();
409
  DictionaryEntry* entry = get_entry(index, hash, klass_name, loader_data);
D
duke 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422

  assert(entry != NULL,"entry must be present, we just created it");
  assert(protection_domain() != NULL,
         "real protection domain should be present");

  entry->add_protection_domain(protection_domain());

  assert(entry->contains_protection_domain(protection_domain()),
         "now protection domain should be present");
}


bool Dictionary::is_valid_protection_domain(int index, unsigned int hash,
423
                                            Symbol* name,
424
                                            ClassLoaderData* loader_data,
D
duke 已提交
425
                                            Handle protection_domain) {
426
  DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
D
duke 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
  return entry->is_valid_protection_domain(protection_domain);
}


void Dictionary::reorder_dictionary() {

  // Copy all the dictionary entries into a single master list.

  DictionaryEntry* master_list = NULL;
  for (int i = 0; i < table_size(); ++i) {
    DictionaryEntry* p = bucket(i);
    while (p != NULL) {
      DictionaryEntry* tmp;
      tmp = p->next();
      p->set_next(master_list);
      master_list = p;
      p = tmp;
    }
    set_entry(i, NULL);
  }

  // Add the dictionary entries back to the list in the correct buckets.
  while (master_list != NULL) {
    DictionaryEntry* p = master_list;
    master_list = master_list->next();
    p->set_next(NULL);
453 454 455 456
    Symbol* class_name = InstanceKlass::cast((Klass*)(p->klass()))->name();
    // Since the null class loader data isn't copied to the CDS archive,
    // compute the hash with NULL for loader data.
    unsigned int hash = compute_hash(class_name, NULL);
D
duke 已提交
457 458
    int index = hash_to_index(hash);
    p->set_hash(hash);
459
    p->set_loader_data(NULL);   // loader_data isn't copied to CDS
D
duke 已提交
460 461 462
    p->set_next(bucket(index));
    set_entry(index, p);
  }
463 464 465
}

SymbolPropertyTable::SymbolPropertyTable(int table_size)
Z
zgu 已提交
466
  : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry))
467 468
{
}
Z
zgu 已提交
469
SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t,
470
                                         int number_of_entries)
Z
zgu 已提交
471
  : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries)
472 473 474 475 476
{
}


SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash,
477
                                                     Symbol* sym,
478 479
                                                     intptr_t sym_mode) {
  assert(index == index_for(sym, sym_mode), "incorrect index?");
480
  for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
481
    if (p->hash() == hash && p->symbol() == sym && p->symbol_mode() == sym_mode) {
482 483 484 485 486 487 488 489
      return p;
    }
  }
  return NULL;
}


SymbolPropertyEntry* SymbolPropertyTable::add_entry(int index, unsigned int hash,
490
                                                    Symbol* sym, intptr_t sym_mode) {
491
  assert_locked_or_safepoint(SystemDictionary_lock);
492 493
  assert(index == index_for(sym, sym_mode), "incorrect index?");
  assert(find_entry(index, hash, sym, sym_mode) == NULL, "no double entry");
494

495
  SymbolPropertyEntry* p = new_entry(hash, sym, sym_mode);
Z
zgu 已提交
496
  Hashtable<Symbol*, mtSymbol>::add_entry(index, p);
497 498 499 500 501 502
  return p;
}

void SymbolPropertyTable::oops_do(OopClosure* f) {
  for (int index = 0; index < table_size(); index++) {
    for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
503 504
      if (p->method_type() != NULL) {
        f->do_oop(p->method_type_addr());
505 506 507 508 509
      }
    }
  }
}

510
void SymbolPropertyTable::methods_do(void f(Method*)) {
511 512
  for (int index = 0; index < table_size(); index++) {
    for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
513 514 515
      Method* prop = p->method();
      if (prop != NULL) {
        f((Method*)prop);
516 517 518
      }
    }
  }
D
duke 已提交
519 520 521 522 523 524 525 526 527 528
}


// ----------------------------------------------------------------------------
#ifndef PRODUCT

void Dictionary::print() {
  ResourceMark rm;
  HandleMark   hm;

529 530
  tty->print_cr("Java system dictionary (table_size=%d, classes=%d)",
                 table_size(), number_of_entries());
D
duke 已提交
531 532 533 534 535 536 537 538
  tty->print_cr("^ indicates that initiating loader is different from "
                "defining loader");

  for (int index = 0; index < table_size(); index++) {
    for (DictionaryEntry* probe = bucket(index);
                          probe != NULL;
                          probe = probe->next()) {
      if (Verbose) tty->print("%4d: ", index);
539 540
      Klass* e = probe->klass();
      ClassLoaderData* loader_data =  probe->loader_data();
D
duke 已提交
541
      bool is_defining_class =
542
         (loader_data == InstanceKlass::cast(e)->class_loader_data());
D
duke 已提交
543
      tty->print("%s%s", is_defining_class ? " " : "^",
H
hseigel 已提交
544
                   e->external_name());
545

D
duke 已提交
546
        tty->print(", loader ");
547
      loader_data->print_value();
D
duke 已提交
548 549 550 551 552 553 554
      tty->cr();
    }
  }
}

#endif

555

D
duke 已提交
556 557
void Dictionary::verify() {
  guarantee(number_of_entries() >= 0, "Verify of system dictionary failed");
558

D
duke 已提交
559 560 561 562 563
  int element_count = 0;
  for (int index = 0; index < table_size(); index++) {
    for (DictionaryEntry* probe = bucket(index);
                          probe != NULL;
                          probe = probe->next()) {
564 565
      Klass* e = probe->klass();
      ClassLoaderData* loader_data = probe->loader_data();
H
hseigel 已提交
566
      guarantee(e->oop_is_instance(),
D
duke 已提交
567 568 569
                              "Verify of system dictionary failed");
      // class loader must be present;  a null class loader is the
      // boostrap loader
570
      guarantee(loader_data != NULL || DumpSharedSpaces ||
571
                loader_data->class_loader() == NULL ||
572
                loader_data->class_loader()->is_instance(),
D
duke 已提交
573 574 575 576 577 578 579 580 581 582
                "checking type of class_loader");
      e->verify();
      probe->verify_protection_domain_set();
      element_count++;
    }
  }
  guarantee(number_of_entries() == element_count,
            "Verify of system dictionary failed");
  debug_only(verify_lookup_length((double)number_of_entries() / table_size()));
}
583