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 Hashtable<InstanceKlass*, mtClass> {
  43   friend class VMStructs;
  44 
  45   ClassLoaderData* _loader_data;  // backpointer to owning loader
  46   ClassLoaderData* loader_data() const { return _loader_data; }
  47 
  48   DictionaryEntry* get_entry(int index, unsigned int hash, Symbol* name);
  49 
  50 protected:
  51   DictionaryEntry* bucket(int i) const {
  52     return (DictionaryEntry*)Hashtable<InstanceKlass*, mtClass>::bucket(i);
  53   }
  54 
  55   // The following method is not MT-safe and must be done under lock.
  56   DictionaryEntry** bucket_addr(int i) {
  57     return (DictionaryEntry**)Hashtable<InstanceKlass*, mtClass>::bucket_addr(i);
  58   }
  59 
  60   void add_entry(int index, DictionaryEntry* new_entry) {
  61     Hashtable<InstanceKlass*, mtClass>::add_entry(index, (HashtableEntry<InstanceKlass*, mtClass>*)new_entry);
  62   }
  63 
  64   void free_entry(DictionaryEntry* entry);
  65 
  66   static size_t entry_size();
  67 public:
  68   Dictionary(ClassLoaderData* loader_data, int table_size);
  69   Dictionary(ClassLoaderData* loader_data, int table_size, HashtableBucket<mtClass>* t, int number_of_entries);
  70   ~Dictionary();
  71 
  72   DictionaryEntry* new_entry(unsigned int hash, InstanceKlass* klass);
  73 
  74   void add_klass(int index, unsigned int hash, Symbol* class_name, InstanceKlass* obj);
  75 
  76   InstanceKlass* find_class(int index, unsigned int hash, Symbol* name);
  77 
  78   InstanceKlass* find_shared_class(int index, unsigned int hash, Symbol* name);
  79 
  80   // GC support
  81   void oops_do(OopClosure* f);
  82   void roots_oops_do(OopClosure* strong, OopClosure* weak);
  83 
  84   void classes_do(void f(InstanceKlass*));
  85   void classes_do(void f(InstanceKlass*, TRAPS), TRAPS);
  86   void all_entries_do(void f(InstanceKlass*, ClassLoaderData*));
  87 
  88   void unlink(BoolObjectClosure* is_alive);
  89   void remove_classes_in_error_state();
  90 
  91   // Unload classes whose defining loaders are unloaded
  92   void do_unloading();
  93 
  94   // Protection domains
  95   InstanceKlass* find(int index, unsigned int hash, Symbol* name, Handle protection_domain);
  96   bool is_valid_protection_domain(int index, unsigned int hash,
  97                                   Symbol* name,
  98                                   Handle protection_domain);
  99   void add_protection_domain(int index, unsigned int hash,
 100                              InstanceKlass* klass,
 101                              Handle protection_domain, TRAPS);
 102 
 103   // Sharing support
 104   void reorder_dictionary();
 105 
 106   void print(bool details = true);
 107 #ifdef ASSERT
 108   void printPerformanceInfoDetails();
 109 #endif // ASSERT
 110   void verify();
 111 };
 112 
 113 // An entry in the system dictionary, this describes a class as
 114 // { InstanceKlass*, loader, protection_domain }.
 115 
 116 class DictionaryEntry : public HashtableEntry<InstanceKlass*, mtClass> {
 117   friend class VMStructs;
 118  private:
 119   // Contains the set of approved protection domains that can access
 120   // this system dictionary entry.
 121   //
 122   // This protection domain set is a set of tuples:
 123   //
 124   // (InstanceKlass C, initiating class loader ICL, Protection Domain PD)
 125   //
 126   // [Note that C.protection_domain(), which is stored in the java.lang.Class
 127   // mirror of C, is NOT the same as PD]
 128   //
 129   // If such an entry (C, ICL, PD) exists in the table, it means that
 130   // it is okay for a class Foo to reference C, where
 131   //
 132   //    Foo.protection_domain() == PD, and
 133   //    Foo's defining class loader == ICL
 134   //
 135   // The usage of the PD set can be seen in SystemDictionary::validate_protection_domain()
 136   // It is essentially a cache to avoid repeated Java up-calls to
 137   // ClassLoader.checkPackageAccess().
 138   //
 139   ProtectionDomainEntry* _pd_set;
 140 
 141  public:
 142   // Tells whether a protection is in the approved set.
 143   bool contains_protection_domain(oop protection_domain) const;
 144   // Adds a protection domain to the approved set.
 145   void add_protection_domain(Dictionary* dict, Handle protection_domain);
 146 
 147   InstanceKlass* klass() const { return (InstanceKlass*)literal(); }
 148 
 149   DictionaryEntry* next() const {
 150     return (DictionaryEntry*)HashtableEntry<InstanceKlass*, mtClass>::next();
 151   }
 152 
 153   DictionaryEntry** next_addr() {
 154     return (DictionaryEntry**)HashtableEntry<InstanceKlass*, mtClass>::next_addr();
 155   }
 156 
 157   ProtectionDomainEntry* pd_set() const { return _pd_set; }
 158   void set_pd_set(ProtectionDomainEntry* pd_set) { _pd_set = pd_set; }
 159 
 160   // Tells whether the initiating class' protection can access the this _klass
 161   bool is_valid_protection_domain(Handle protection_domain) {
 162     if (!ProtectionDomainVerification) return true;
 163     if (!SystemDictionary::has_checkPackageAccess()) return true;
 164 
 165     return protection_domain() == NULL
 166          ? true
 167          : contains_protection_domain(protection_domain());
 168   }
 169 
 170   void verify_protection_domain_set() {
 171     for (ProtectionDomainEntry* current = _pd_set;
 172                                 current != NULL;
 173                                 current = current->_next) {
 174       current->_pd_cache->protection_domain()->verify();
 175     }
 176   }
 177 
 178   bool equals(const Symbol* class_name) const {
 179     InstanceKlass* klass = (InstanceKlass*)literal();
 180     return (klass->name() == class_name);
 181   }
 182 
 183   void print_count(outputStream *st) {
 184     int count = 0;
 185     for (ProtectionDomainEntry* current = _pd_set;
 186                                 current != NULL;
 187                                 current = current->_next) {
 188       count++;
 189     }
 190     st->print_cr("pd set count = #%d", count);
 191   }
 192 
 193   void verify();
 194 };
 195 
 196 // Entry in a SymbolPropertyTable, mapping a single Symbol*
 197 // to a managed and an unmanaged pointer.
 198 class SymbolPropertyEntry : public HashtableEntry<Symbol*, mtSymbol> {
 199   friend class VMStructs;
 200  private:
 201   intptr_t _symbol_mode;  // secondary key
 202   Method*   _method;
 203   oop       _method_type;
 204 
 205  public:
 206   Symbol* symbol() const            { return literal(); }
 207 
 208   intptr_t symbol_mode() const      { return _symbol_mode; }
 209   void set_symbol_mode(intptr_t m)  { _symbol_mode = m; }
 210 
 211   Method*        method() const     { return _method; }
 212   void set_method(Method* p)        { _method = p; }
 213 
 214   oop      method_type() const      { return _method_type; }
 215   oop*     method_type_addr()       { return &_method_type; }
 216   void set_method_type(oop p)       { _method_type = p; }
 217 
 218   SymbolPropertyEntry* next() const {
 219     return (SymbolPropertyEntry*)HashtableEntry<Symbol*, mtSymbol>::next();
 220   }
 221 
 222   SymbolPropertyEntry** next_addr() {
 223     return (SymbolPropertyEntry**)HashtableEntry<Symbol*, mtSymbol>::next_addr();
 224   }
 225 
 226   void print_on(outputStream* st) const {
 227     symbol()->print_value_on(st);
 228     st->print("/mode=" INTX_FORMAT, symbol_mode());
 229     st->print(" -> ");
 230     bool printed = false;
 231     if (method() != NULL) {
 232       method()->print_value_on(st);
 233       printed = true;
 234     }
 235     if (method_type() != NULL) {
 236       if (printed)  st->print(" and ");
 237       st->print(INTPTR_FORMAT, p2i((void *)method_type()));
 238       printed = true;
 239     }
 240     st->print_cr(printed ? "" : "(empty)");
 241   }
 242 };
 243 
 244 // A system-internal mapping of symbols to pointers, both managed
 245 // and unmanaged.  Used to record the auto-generation of each method
 246 // MethodHandle.invoke(S)T, for all signatures (S)T.
 247 class SymbolPropertyTable : public Hashtable<Symbol*, mtSymbol> {
 248   friend class VMStructs;
 249 private:
 250   SymbolPropertyEntry* bucket(int i) {
 251     return (SymbolPropertyEntry*) Hashtable<Symbol*, mtSymbol>::bucket(i);
 252   }
 253 
 254   // The following method is not MT-safe and must be done under lock.
 255   SymbolPropertyEntry** bucket_addr(int i) {
 256     return (SymbolPropertyEntry**) Hashtable<Symbol*, mtSymbol>::bucket_addr(i);
 257   }
 258 
 259   void add_entry(int index, SymbolPropertyEntry* new_entry) {
 260     ShouldNotReachHere();
 261   }
 262   void set_entry(int index, SymbolPropertyEntry* new_entry) {
 263     ShouldNotReachHere();
 264   }
 265 
 266   SymbolPropertyEntry* new_entry(unsigned int hash, Symbol* symbol, intptr_t symbol_mode) {
 267     SymbolPropertyEntry* entry = (SymbolPropertyEntry*) Hashtable<Symbol*, mtSymbol>::new_entry(hash, symbol);
 268     // Hashtable with Symbol* literal must increment and decrement refcount.
 269     symbol->increment_refcount();
 270     entry->set_symbol_mode(symbol_mode);
 271     entry->set_method(NULL);
 272     entry->set_method_type(NULL);
 273     return entry;
 274   }
 275 
 276 public:
 277   SymbolPropertyTable(int table_size);
 278   SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t, int number_of_entries);
 279 
 280   void free_entry(SymbolPropertyEntry* entry) {
 281     // decrement Symbol refcount here because hashtable doesn't.
 282     entry->literal()->decrement_refcount();
 283     Hashtable<Symbol*, mtSymbol>::free_entry(entry);
 284   }
 285 
 286   unsigned int compute_hash(Symbol* sym, intptr_t symbol_mode) {
 287     // Use the regular identity_hash.
 288     return Hashtable<Symbol*, mtSymbol>::compute_hash(sym) ^ symbol_mode;
 289   }
 290 
 291   int index_for(Symbol* name, intptr_t symbol_mode) {
 292     return hash_to_index(compute_hash(name, symbol_mode));
 293   }
 294 
 295   // need not be locked; no state change
 296   SymbolPropertyEntry* find_entry(int index, unsigned int hash, Symbol* name, intptr_t name_mode);
 297 
 298   // must be done under SystemDictionary_lock
 299   SymbolPropertyEntry* add_entry(int index, unsigned int hash, Symbol* name, intptr_t name_mode);
 300 
 301   // GC support
 302   void oops_do(OopClosure* f);
 303 
 304   void methods_do(void f(Method*));
 305 
 306   // Sharing support
 307   void reorder_dictionary();
 308 
 309 #ifndef PRODUCT
 310   void print();
 311 #endif
 312   void verify();
 313 };
 314 #endif // SHARE_VM_CLASSFILE_DICTIONARY_HPP