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