1 /*
   2  * Copyright (c) 2003, 2016, 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 #ifndef SHARE_VM_CLASSFILE_DICTIONARY_HPP
  26 #define SHARE_VM_CLASSFILE_DICTIONARY_HPP
  27 
  28 #include "classfile/systemDictionary.hpp"
  29 #include "oops/instanceKlass.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "utilities/hashtable.hpp"
  32 
  33 class DictionaryEntry;
  34 class PSPromotionManager;
  35 class ProtectionDomainCacheTable;
  36 class ProtectionDomainCacheEntry;
  37 class BoolObjectClosure;
  38 
  39 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  40 // The data structure for the system dictionary (and the shared system
  41 // dictionary).
  42 
  43 class Dictionary : public TwoOopHashtable<Klass*, mtClass> {
  44   friend class VMStructs;
  45 private:
  46   // current iteration index.
  47   static int                    _current_class_index;
  48   // pointer to the current hash table entry.
  49   static DictionaryEntry*       _current_class_entry;
  50 
  51   ProtectionDomainCacheTable*   _pd_cache_table;
  52 
  53   DictionaryEntry* get_entry(int index, unsigned int hash,
  54                              Symbol* name, ClassLoaderData* loader_data);
  55 
  56 protected:
  57   DictionaryEntry* bucket(int i) {
  58     return (DictionaryEntry*)Hashtable<Klass*, mtClass>::bucket(i);
  59   }
  60 
  61   // The following method is not MT-safe and must be done under lock.
  62   DictionaryEntry** bucket_addr(int i) {
  63     return (DictionaryEntry**)Hashtable<Klass*, mtClass>::bucket_addr(i);
  64   }
  65 
  66   void add_entry(int index, DictionaryEntry* new_entry) {
  67     Hashtable<Klass*, mtClass>::add_entry(index, (HashtableEntry<Klass*, mtClass>*)new_entry);
  68   }
  69 
  70   static size_t entry_size();
  71 
  72 public:
  73   Dictionary(int table_size);
  74   Dictionary(int table_size, HashtableBucket<mtClass>* t, int number_of_entries);
  75 
  76   DictionaryEntry* new_entry(unsigned int hash, Klass* klass, ClassLoaderData* loader_data);
  77 
  78   DictionaryEntry* new_entry();
  79 
  80   void free_entry(DictionaryEntry* entry);
  81 
  82   void add_klass(Symbol* class_name, ClassLoaderData* loader_data,KlassHandle obj);
  83 
  84   Klass* find_class(int index, unsigned int hash,
  85                       Symbol* name, ClassLoaderData* loader_data);
  86 
  87   Klass* find_shared_class(int index, unsigned int hash, Symbol* name);
  88 
  89   // Compiler support
  90   Klass* try_get_next_class();
  91 
  92   // GC support
  93   void oops_do(OopClosure* f);
  94   void always_strong_oops_do(OopClosure* blk);
  95   void roots_oops_do(OopClosure* strong, OopClosure* weak);
  96 
  97   void always_strong_classes_do(KlassClosure* closure);
  98 
  99   void classes_do(void f(Klass*));
 100   void classes_do(void f(Klass*, TRAPS), TRAPS);
 101   void classes_do(void f(Klass*, ClassLoaderData*));
 102 
 103   void methods_do(void f(Method*));
 104 
 105   void unlink(BoolObjectClosure* is_alive);
 106   void remove_classes_in_error_state();
 107 
 108   // Classes loaded by the bootstrap loader are always strongly reachable.
 109   // If we're not doing class unloading, all classes are strongly reachable.
 110   static bool is_strongly_reachable(ClassLoaderData* loader_data, Klass* klass) {
 111     assert (klass != NULL, "should have non-null klass");
 112     return (loader_data->is_the_null_class_loader_data() || !ClassUnloading);
 113   }
 114 
 115   // Unload (that is, break root links to) all unmarked classes and loaders.
 116   void do_unloading();
 117 
 118   // Protection domains
 119   Klass* find(int index, unsigned int hash, Symbol* name,
 120                 ClassLoaderData* loader_data, Handle protection_domain, TRAPS);
 121   bool is_valid_protection_domain(int index, unsigned int hash,
 122                                   Symbol* name, ClassLoaderData* loader_data,
 123                                   Handle protection_domain);
 124   void add_protection_domain(int index, unsigned int hash,
 125                              instanceKlassHandle klass, ClassLoaderData* loader_data,
 126                              Handle protection_domain, TRAPS);
 127 
 128   // Sharing support
 129   void reorder_dictionary();
 130 
 131   ProtectionDomainCacheEntry* cache_get(oop protection_domain);
 132   ProtectionDomainCacheTable* pd_cache_table() { return _pd_cache_table; }
 133 
 134   void print(bool details = true);
 135   void verify();
 136 };
 137 
 138 // The following classes can be in dictionary.cpp, but we need these
 139 // to be in header file so that SA's vmStructs can access them.
 140 class ProtectionDomainCacheEntry : public HashtableEntry<oop, mtClass> {
 141   friend class VMStructs;
 142  private:
 143   // Flag indicating whether this protection domain entry is strongly reachable.
 144   // Used during iterating over the system dictionary to remember oops that need
 145   // to be updated.
 146   bool _strongly_reachable;
 147  public:
 148   oop protection_domain() { return literal(); }
 149 
 150   void init() {
 151     _strongly_reachable = false;
 152   }
 153 
 154   ProtectionDomainCacheEntry* next() {
 155     return (ProtectionDomainCacheEntry*)HashtableEntry<oop, mtClass>::next();
 156   }
 157 
 158   ProtectionDomainCacheEntry** next_addr() {
 159     return (ProtectionDomainCacheEntry**)HashtableEntry<oop, mtClass>::next_addr();
 160   }
 161 
 162   void oops_do(OopClosure* f) {
 163     f->do_oop(literal_addr());
 164   }
 165 
 166   void set_strongly_reachable()   { _strongly_reachable = true; }
 167   bool is_strongly_reachable()    { return _strongly_reachable; }
 168   void reset_strongly_reachable() { _strongly_reachable = false; }
 169 
 170   void print() PRODUCT_RETURN;
 171   void verify();
 172 };
 173 
 174 // The ProtectionDomainCacheTable contains all protection domain oops. The system
 175 // dictionary entries reference its entries instead of having references to oops
 176 // directly.
 177 // This is used to speed up system dictionary iteration: the oops in the
 178 // protection domain are the only ones referring the Java heap. So when there is
 179 // need to update these, instead of going over every entry of the system dictionary,
 180 // we only need to iterate over this set.
 181 // The amount of different protection domains used is typically magnitudes smaller
 182 // than the number of system dictionary entries (loaded classes).
 183 class ProtectionDomainCacheTable : public Hashtable<oop, mtClass> {
 184   friend class VMStructs;
 185 private:
 186   ProtectionDomainCacheEntry* bucket(int i) {
 187     return (ProtectionDomainCacheEntry*) Hashtable<oop, mtClass>::bucket(i);
 188   }
 189 
 190   // The following method is not MT-safe and must be done under lock.
 191   ProtectionDomainCacheEntry** bucket_addr(int i) {
 192     return (ProtectionDomainCacheEntry**) Hashtable<oop, mtClass>::bucket_addr(i);
 193   }
 194 
 195   ProtectionDomainCacheEntry* new_entry(unsigned int hash, oop protection_domain) {
 196     ProtectionDomainCacheEntry* entry = (ProtectionDomainCacheEntry*) Hashtable<oop, mtClass>::new_entry(hash, protection_domain);
 197     entry->init();
 198     return entry;
 199   }
 200 
 201   static unsigned int compute_hash(oop protection_domain) {
 202     return (unsigned int)(protection_domain->identity_hash());
 203   }
 204 
 205   int index_for(oop protection_domain) {
 206     return hash_to_index(compute_hash(protection_domain));
 207   }
 208 
 209   ProtectionDomainCacheEntry* add_entry(int index, unsigned int hash, oop protection_domain);
 210   ProtectionDomainCacheEntry* find_entry(int index, oop protection_domain);
 211 
 212 public:
 213 
 214   ProtectionDomainCacheTable(int table_size);
 215 
 216   ProtectionDomainCacheEntry* get(oop protection_domain);
 217   void free(ProtectionDomainCacheEntry* entry);
 218 
 219   void unlink(BoolObjectClosure* cl);
 220 
 221   // GC support
 222   void oops_do(OopClosure* f);
 223   void always_strong_oops_do(OopClosure* f);
 224   void roots_oops_do(OopClosure* strong, OopClosure* weak);
 225 
 226   static uint bucket_size();
 227 
 228   void print() PRODUCT_RETURN;
 229   void verify();
 230 };
 231 
 232 
 233 class ProtectionDomainEntry :public CHeapObj<mtClass> {
 234   friend class VMStructs;
 235  public:
 236   ProtectionDomainEntry* _next;
 237   ProtectionDomainCacheEntry* _pd_cache;
 238 
 239   ProtectionDomainEntry(ProtectionDomainCacheEntry* pd_cache, ProtectionDomainEntry* next) {
 240     _pd_cache = pd_cache;
 241     _next     = next;
 242   }
 243 
 244   ProtectionDomainEntry* next() { return _next; }
 245   oop protection_domain() { return _pd_cache->protection_domain(); }
 246 };
 247 
 248 // An entry in the system dictionary, this describes a class as
 249 // { Klass*, loader, protection_domain }.
 250 
 251 class DictionaryEntry : public HashtableEntry<Klass*, mtClass> {
 252   friend class VMStructs;
 253  private:
 254   // Contains the set of approved protection domains that can access
 255   // this system dictionary entry.
 256   //
 257   // This protection domain set is a set of tuples:
 258   //
 259   // (InstanceKlass C, initiating class loader ICL, Protection Domain PD)
 260   //
 261   // [Note that C.protection_domain(), which is stored in the java.lang.Class
 262   // mirror of C, is NOT the same as PD]
 263   //
 264   // If such an entry (C, ICL, PD) exists in the table, it means that
 265   // it is okay for a class Foo to reference C, where
 266   //
 267   //    Foo.protection_domain() == PD, and
 268   //    Foo's defining class loader == ICL
 269   //
 270   // The usage of the PD set can be seen in SystemDictionary::validate_protection_domain()
 271   // It is essentially a cache to avoid repeated Java up-calls to
 272   // ClassLoader.checkPackageAccess().
 273   //
 274   ProtectionDomainEntry* _pd_set;
 275   ClassLoaderData*       _loader_data;
 276 
 277  public:
 278   // Tells whether a protection is in the approved set.
 279   bool contains_protection_domain(oop protection_domain) const;
 280   // Adds a protection domain to the approved set.
 281   void add_protection_domain(Dictionary* dict, oop protection_domain);
 282 
 283   Klass* klass() const { return (Klass*)literal(); }
 284   Klass** klass_addr() { return (Klass**)literal_addr(); }
 285 
 286   DictionaryEntry* next() const {
 287     return (DictionaryEntry*)HashtableEntry<Klass*, mtClass>::next();
 288   }
 289 
 290   DictionaryEntry** next_addr() {
 291     return (DictionaryEntry**)HashtableEntry<Klass*, mtClass>::next_addr();
 292   }
 293 
 294   ClassLoaderData* loader_data() const { return _loader_data; }
 295   void set_loader_data(ClassLoaderData* loader_data) { _loader_data = loader_data; }
 296 
 297   ProtectionDomainEntry* pd_set() const { return _pd_set; }
 298   void set_pd_set(ProtectionDomainEntry* pd_set) { _pd_set = pd_set; }
 299 
 300   bool has_protection_domain() { return _pd_set != NULL; }
 301 
 302   // Tells whether the initiating class' protection can access the this _klass
 303   bool is_valid_protection_domain(Handle protection_domain) {
 304     if (!ProtectionDomainVerification) return true;
 305     if (!SystemDictionary::has_checkPackageAccess()) return true;
 306 
 307     return protection_domain() == NULL
 308          ? true
 309          : contains_protection_domain(protection_domain());
 310   }
 311 
 312   void set_strongly_reachable() {
 313     for (ProtectionDomainEntry* current = _pd_set;
 314                                 current != NULL;
 315                                 current = current->_next) {
 316       current->_pd_cache->set_strongly_reachable();
 317     }
 318   }
 319 
 320   void verify_protection_domain_set() {
 321     for (ProtectionDomainEntry* current = _pd_set;
 322                                 current != NULL;
 323                                 current = current->_next) {
 324       current->_pd_cache->protection_domain()->verify();
 325     }
 326   }
 327 
 328   bool equals(Symbol* class_name, ClassLoaderData* loader_data) const {
 329     Klass* klass = (Klass*)literal();
 330     return (InstanceKlass::cast(klass)->name() == class_name &&
 331             _loader_data == loader_data);
 332   }
 333 
 334   void print() {
 335     int count = 0;
 336     for (ProtectionDomainEntry* current = _pd_set;
 337                                 current != NULL;
 338                                 current = current->_next) {
 339       count++;
 340     }
 341     tty->print_cr("pd set = #%d", count);
 342   }
 343 };
 344 
 345 // Entry in a SymbolPropertyTable, mapping a single Symbol*
 346 // to a managed and an unmanaged pointer.
 347 class SymbolPropertyEntry : public HashtableEntry<Symbol*, mtSymbol> {
 348   friend class VMStructs;
 349  private:
 350   intptr_t _symbol_mode;  // secondary key
 351   Method*   _method;
 352   oop       _method_type;
 353 
 354  public:
 355   Symbol* symbol() const            { return literal(); }
 356 
 357   intptr_t symbol_mode() const      { return _symbol_mode; }
 358   void set_symbol_mode(intptr_t m)  { _symbol_mode = m; }
 359 
 360   Method*        method() const     { return _method; }
 361   void set_method(Method* p)        { _method = p; }
 362 
 363   oop      method_type() const      { return _method_type; }
 364   oop*     method_type_addr()       { return &_method_type; }
 365   void set_method_type(oop p)       { _method_type = p; }
 366 
 367   SymbolPropertyEntry* next() const {
 368     return (SymbolPropertyEntry*)HashtableEntry<Symbol*, mtSymbol>::next();
 369   }
 370 
 371   SymbolPropertyEntry** next_addr() {
 372     return (SymbolPropertyEntry**)HashtableEntry<Symbol*, mtSymbol>::next_addr();
 373   }
 374 
 375   void print_on(outputStream* st) const {
 376     symbol()->print_value_on(st);
 377     st->print("/mode=" INTX_FORMAT, symbol_mode());
 378     st->print(" -> ");
 379     bool printed = false;
 380     if (method() != NULL) {
 381       method()->print_value_on(st);
 382       printed = true;
 383     }
 384     if (method_type() != NULL) {
 385       if (printed)  st->print(" and ");
 386       st->print(INTPTR_FORMAT, p2i((void *)method_type()));
 387       printed = true;
 388     }
 389     st->print_cr(printed ? "" : "(empty)");
 390   }
 391 };
 392 
 393 // A system-internal mapping of symbols to pointers, both managed
 394 // and unmanaged.  Used to record the auto-generation of each method
 395 // MethodHandle.invoke(S)T, for all signatures (S)T.
 396 class SymbolPropertyTable : public Hashtable<Symbol*, mtSymbol> {
 397   friend class VMStructs;
 398 private:
 399   SymbolPropertyEntry* bucket(int i) {
 400     return (SymbolPropertyEntry*) Hashtable<Symbol*, mtSymbol>::bucket(i);
 401   }
 402 
 403   // The following method is not MT-safe and must be done under lock.
 404   SymbolPropertyEntry** bucket_addr(int i) {
 405     return (SymbolPropertyEntry**) Hashtable<Symbol*, mtSymbol>::bucket_addr(i);
 406   }
 407 
 408   void add_entry(int index, SymbolPropertyEntry* new_entry) {
 409     ShouldNotReachHere();
 410   }
 411   void set_entry(int index, SymbolPropertyEntry* new_entry) {
 412     ShouldNotReachHere();
 413   }
 414 
 415   SymbolPropertyEntry* new_entry(unsigned int hash, Symbol* symbol, intptr_t symbol_mode) {
 416     SymbolPropertyEntry* entry = (SymbolPropertyEntry*) Hashtable<Symbol*, mtSymbol>::new_entry(hash, symbol);
 417     // Hashtable with Symbol* literal must increment and decrement refcount.
 418     symbol->increment_refcount();
 419     entry->set_symbol_mode(symbol_mode);
 420     entry->set_method(NULL);
 421     entry->set_method_type(NULL);
 422     return entry;
 423   }
 424 
 425 public:
 426   SymbolPropertyTable(int table_size);
 427   SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t, int number_of_entries);
 428 
 429   void free_entry(SymbolPropertyEntry* entry) {
 430     // decrement Symbol refcount here because hashtable doesn't.
 431     entry->literal()->decrement_refcount();
 432     Hashtable<Symbol*, mtSymbol>::free_entry(entry);
 433   }
 434 
 435   unsigned int compute_hash(Symbol* sym, intptr_t symbol_mode) {
 436     // Use the regular identity_hash.
 437     return Hashtable<Symbol*, mtSymbol>::compute_hash(sym) ^ symbol_mode;
 438   }
 439 
 440   int index_for(Symbol* name, intptr_t symbol_mode) {
 441     return hash_to_index(compute_hash(name, symbol_mode));
 442   }
 443 
 444   // need not be locked; no state change
 445   SymbolPropertyEntry* find_entry(int index, unsigned int hash, Symbol* name, intptr_t name_mode);
 446 
 447   // must be done under SystemDictionary_lock
 448   SymbolPropertyEntry* add_entry(int index, unsigned int hash, Symbol* name, intptr_t name_mode);
 449 
 450   // GC support
 451   void oops_do(OopClosure* f);
 452 
 453   void methods_do(void f(Method*));
 454 
 455   // Sharing support
 456   void reorder_dictionary();
 457 
 458 #ifndef PRODUCT
 459   void print();
 460 #endif
 461   void verify();
 462 };
 463 #endif // SHARE_VM_CLASSFILE_DICTIONARY_HPP