1 /*
   2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_CLASSFILE_DICTIONARY_HPP
  26 #define SHARE_VM_CLASSFILE_DICTIONARY_HPP
  27 
  28 #include "classfile/protectionDomainCache.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "oops/instanceKlass.hpp"
  31 #include "oops/oop.hpp"
  32 #include "utilities/hashtable.hpp"
  33 #include "utilities/ostream.hpp"
  34 
  35 class DictionaryEntry;
  36 class BoolObjectClosure;
  37 
  38 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  39 // The data structure for the system dictionary (and the shared system
  40 // dictionary).
  41 
  42 class Dictionary : public TwoOopHashtable<InstanceKlass*, mtClass> {
  43   friend class VMStructs;
  44 private:
  45   // current iteration index.
  46   static int                    _current_class_index;
  47   // pointer to the current hash table entry.
  48   static DictionaryEntry*       _current_class_entry;
  49 
  50   ProtectionDomainCacheTable*   _pd_cache_table;
  51 
  52   DictionaryEntry* get_entry(int index, unsigned int hash,
  53                              Symbol* name, ClassLoaderData* loader_data);
  54 
  55 protected:
  56   DictionaryEntry* bucket(int i) const {
  57     return (DictionaryEntry*)Hashtable<InstanceKlass*, mtClass>::bucket(i);
  58   }
  59 
  60   // The following method is not MT-safe and must be done under lock.
  61   DictionaryEntry** bucket_addr(int i) {
  62     return (DictionaryEntry**)Hashtable<InstanceKlass*, mtClass>::bucket_addr(i);
  63   }
  64 
  65   void add_entry(int index, DictionaryEntry* new_entry) {
  66     Hashtable<InstanceKlass*, mtClass>::add_entry(index, (HashtableEntry<InstanceKlass*, mtClass>*)new_entry);
  67   }
  68 
  69   static size_t entry_size();
  70 public:
  71   Dictionary(int table_size);
  72   Dictionary(int table_size, HashtableBucket<mtClass>* t, int number_of_entries);
  73 
  74   DictionaryEntry* new_entry(unsigned int hash, InstanceKlass* klass, ClassLoaderData* loader_data);
  75 
  76   void free_entry(DictionaryEntry* entry);
  77 
  78   void add_klass(Symbol* class_name, ClassLoaderData* loader_data, InstanceKlass* obj);
  79 
  80   InstanceKlass* find_class(int index, unsigned int hash,
  81                             Symbol* name, ClassLoaderData* loader_data);
  82 
  83   InstanceKlass* find_shared_class(int index, unsigned int hash, Symbol* name);
  84 
  85   // Compiler support
  86   InstanceKlass* try_get_next_class();
  87 
  88   // GC support
  89   void oops_do(OopClosure* f);
  90   void roots_oops_do(OopClosure* strong, OopClosure* weak);
  91 
  92   void classes_do(void f(Klass*));
  93   void classes_do(void f(Klass*, TRAPS), TRAPS);
  94   void classes_do(void f(Klass*, ClassLoaderData*));
  95 
  96   void unlink(BoolObjectClosure* is_alive);
  97   void remove_classes_in_error_state();
  98 
  99   // Classes loaded by the bootstrap loader are always strongly reachable.
 100   // If we're not doing class unloading, all classes are strongly reachable.
 101   static bool is_strongly_reachable(ClassLoaderData* loader_data, Klass* klass) {
 102     assert (klass != NULL, "should have non-null klass");
 103     return (loader_data->is_the_null_class_loader_data() || !ClassUnloading);
 104   }
 105 
 106   // Unload (that is, break root links to) all unmarked classes and loaders.
 107   void do_unloading();
 108 
 109   // Protection domains
 110   InstanceKlass* find(int index, unsigned int hash, Symbol* name,
 111                       ClassLoaderData* loader_data, Handle protection_domain, TRAPS);
 112   bool is_valid_protection_domain(int index, unsigned int hash,
 113                                   Symbol* name, ClassLoaderData* loader_data,
 114                                   Handle protection_domain);
 115   void add_protection_domain(int index, unsigned int hash,
 116                              InstanceKlass* klass, ClassLoaderData* loader_data,
 117                              Handle protection_domain, TRAPS);
 118 
 119   // Sharing support
 120   void reorder_dictionary();
 121 
 122   ProtectionDomainCacheEntry* cache_get(Handle protection_domain);
 123 
 124   void print(bool details = true);
 125 #ifdef ASSERT
 126   void printPerformanceInfoDetails();
 127 #endif // ASSERT
 128   void verify();
 129 };
 130 
 131 // An entry in the system dictionary, this describes a class as
 132 // { InstanceKlass*, loader, protection_domain }.
 133 
 134 class DictionaryEntry : public HashtableEntry<InstanceKlass*, mtClass> {
 135   friend class VMStructs;
 136  private:
 137   // Contains the set of approved protection domains that can access
 138   // this system dictionary entry.
 139   //
 140   // This protection domain set is a set of tuples:
 141   //
 142   // (InstanceKlass C, initiating class loader ICL, Protection Domain PD)
 143   //
 144   // [Note that C.protection_domain(), which is stored in the java.lang.Class
 145   // mirror of C, is NOT the same as PD]
 146   //
 147   // If such an entry (C, ICL, PD) exists in the table, it means that
 148   // it is okay for a class Foo to reference C, where
 149   //
 150   //    Foo.protection_domain() == PD, and
 151   //    Foo's defining class loader == ICL
 152   //
 153   // The usage of the PD set can be seen in SystemDictionary::validate_protection_domain()
 154   // It is essentially a cache to avoid repeated Java up-calls to
 155   // ClassLoader.checkPackageAccess().
 156   //
 157   ProtectionDomainEntry* _pd_set;
 158   ClassLoaderData*       _loader_data;
 159 
 160  public:
 161   // Tells whether a protection is in the approved set.
 162   bool contains_protection_domain(oop protection_domain) const;
 163   // Adds a protection domain to the approved set.
 164   void add_protection_domain(Dictionary* dict, Handle protection_domain);
 165 
 166   InstanceKlass* klass() const { return (InstanceKlass*)literal(); }
 167 
 168   DictionaryEntry* next() const {
 169     return (DictionaryEntry*)HashtableEntry<InstanceKlass*, mtClass>::next();
 170   }
 171 
 172   DictionaryEntry** next_addr() {
 173     return (DictionaryEntry**)HashtableEntry<InstanceKlass*, mtClass>::next_addr();
 174   }
 175 
 176   ClassLoaderData* loader_data() const { return _loader_data; }
 177   void set_loader_data(ClassLoaderData* loader_data) { _loader_data = loader_data; }
 178 
 179   ProtectionDomainEntry* pd_set() const { return _pd_set; }
 180   void set_pd_set(ProtectionDomainEntry* pd_set) { _pd_set = pd_set; }
 181 
 182   // Tells whether the initiating class' protection can access the this _klass
 183   bool is_valid_protection_domain(Handle protection_domain) {
 184     if (!ProtectionDomainVerification) return true;
 185     if (!SystemDictionary::has_checkPackageAccess()) return true;
 186 
 187     return protection_domain() == NULL
 188          ? true
 189          : contains_protection_domain(protection_domain());
 190   }
 191 
 192   void verify_protection_domain_set() {
 193     for (ProtectionDomainEntry* current = _pd_set;
 194                                 current != NULL;
 195                                 current = current->_next) {
 196       current->_pd_cache->protection_domain()->verify();
 197     }
 198   }
 199 
 200   bool equals(const Symbol* class_name, ClassLoaderData* loader_data) const {
 201     InstanceKlass* klass = (InstanceKlass*)literal();
 202     return (klass->name() == class_name && _loader_data == loader_data);
 203   }
 204 
 205   void print_count(outputStream *st) {
 206     int count = 0;
 207     for (ProtectionDomainEntry* current = _pd_set;
 208                                 current != NULL;
 209                                 current = current->_next) {
 210       count++;
 211     }
 212     st->print_cr("pd set count = #%d", count);
 213   }
 214 
 215   void verify();
 216 };
 217 
 218 // Entry in a SymbolPropertyTable, mapping a single Symbol*
 219 // to a managed and an unmanaged pointer.
 220 class SymbolPropertyEntry : public HashtableEntry<Symbol*, mtSymbol> {
 221   friend class VMStructs;
 222  private:
 223   intptr_t _symbol_mode;  // secondary key
 224   Method*   _method;
 225   oop       _method_type;
 226 
 227  public:
 228   Symbol* symbol() const            { return literal(); }
 229 
 230   intptr_t symbol_mode() const      { return _symbol_mode; }
 231   void set_symbol_mode(intptr_t m)  { _symbol_mode = m; }
 232 
 233   Method*        method() const     { return _method; }
 234   void set_method(Method* p)        { _method = p; }
 235 
 236   oop      method_type() const      { return _method_type; }
 237   oop*     method_type_addr()       { return &_method_type; }
 238   void set_method_type(oop p)       { _method_type = p; }
 239 
 240   SymbolPropertyEntry* next() const {
 241     return (SymbolPropertyEntry*)HashtableEntry<Symbol*, mtSymbol>::next();
 242   }
 243 
 244   SymbolPropertyEntry** next_addr() {
 245     return (SymbolPropertyEntry**)HashtableEntry<Symbol*, mtSymbol>::next_addr();
 246   }
 247 
 248   void print_on(outputStream* st) const {
 249     symbol()->print_value_on(st);
 250     st->print("/mode=" INTX_FORMAT, symbol_mode());
 251     st->print(" -> ");
 252     bool printed = false;
 253     if (method() != NULL) {
 254       method()->print_value_on(st);
 255       printed = true;
 256     }
 257     if (method_type() != NULL) {
 258       if (printed)  st->print(" and ");
 259       st->print(INTPTR_FORMAT, p2i((void *)method_type()));
 260       printed = true;
 261     }
 262     st->print_cr(printed ? "" : "(empty)");
 263   }
 264 };
 265 
 266 // A system-internal mapping of symbols to pointers, both managed
 267 // and unmanaged.  Used to record the auto-generation of each method
 268 // MethodHandle.invoke(S)T, for all signatures (S)T.
 269 class SymbolPropertyTable : public Hashtable<Symbol*, mtSymbol> {
 270   friend class VMStructs;
 271 private:
 272   SymbolPropertyEntry* bucket(int i) {
 273     return (SymbolPropertyEntry*) Hashtable<Symbol*, mtSymbol>::bucket(i);
 274   }
 275 
 276   // The following method is not MT-safe and must be done under lock.
 277   SymbolPropertyEntry** bucket_addr(int i) {
 278     return (SymbolPropertyEntry**) Hashtable<Symbol*, mtSymbol>::bucket_addr(i);
 279   }
 280 
 281   void add_entry(int index, SymbolPropertyEntry* new_entry) {
 282     ShouldNotReachHere();
 283   }
 284   void set_entry(int index, SymbolPropertyEntry* new_entry) {
 285     ShouldNotReachHere();
 286   }
 287 
 288   SymbolPropertyEntry* new_entry(unsigned int hash, Symbol* symbol, intptr_t symbol_mode) {
 289     SymbolPropertyEntry* entry = (SymbolPropertyEntry*) Hashtable<Symbol*, mtSymbol>::new_entry(hash, symbol);
 290     // Hashtable with Symbol* literal must increment and decrement refcount.
 291     symbol->increment_refcount();
 292     entry->set_symbol_mode(symbol_mode);
 293     entry->set_method(NULL);
 294     entry->set_method_type(NULL);
 295     return entry;
 296   }
 297 
 298 public:
 299   SymbolPropertyTable(int table_size);
 300   SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t, int number_of_entries);
 301 
 302   void free_entry(SymbolPropertyEntry* entry) {
 303     // decrement Symbol refcount here because hashtable doesn't.
 304     entry->literal()->decrement_refcount();
 305     Hashtable<Symbol*, mtSymbol>::free_entry(entry);
 306   }
 307 
 308   unsigned int compute_hash(Symbol* sym, intptr_t symbol_mode) {
 309     // Use the regular identity_hash.
 310     return Hashtable<Symbol*, mtSymbol>::compute_hash(sym) ^ symbol_mode;
 311   }
 312 
 313   int index_for(Symbol* name, intptr_t symbol_mode) {
 314     return hash_to_index(compute_hash(name, symbol_mode));
 315   }
 316 
 317   // need not be locked; no state change
 318   SymbolPropertyEntry* find_entry(int index, unsigned int hash, Symbol* name, intptr_t name_mode);
 319 
 320   // must be done under SystemDictionary_lock
 321   SymbolPropertyEntry* add_entry(int index, unsigned int hash, Symbol* name, intptr_t name_mode);
 322 
 323   // GC support
 324   void oops_do(OopClosure* f);
 325 
 326   void methods_do(void f(Method*));
 327 
 328   // Sharing support
 329   void reorder_dictionary();
 330 
 331 #ifndef PRODUCT
 332   void print();
 333 #endif
 334   void verify();
 335 };
 336 #endif // SHARE_VM_CLASSFILE_DICTIONARY_HPP