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