1 /*
   2  * Copyright (c) 2003, 2020, 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_CLASSFILE_DICTIONARY_HPP
  26 #define SHARE_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 "oops/oopHandle.hpp"
  33 #include "utilities/hashtable.hpp"
  34 #include "utilities/ostream.hpp"
  35 
  36 class DictionaryEntry;
  37 class BoolObjectClosure;
  38 
  39 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  40 // The data structure for the class loader data dictionaries.
  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 public:
  56   Dictionary(ClassLoaderData* loader_data, int table_size, bool resizable = false);
  57   Dictionary(ClassLoaderData* loader_data, int table_size, HashtableBucket<mtClass>* t, int number_of_entries, bool resizable = false);
  58   ~Dictionary();
  59 
  60   static bool does_any_dictionary_needs_resizing();
  61   bool resize_if_needed();
  62 
  63   void add_klass(unsigned int hash, Symbol* class_name, InstanceKlass* obj);
  64   // replace the existing class with k, return old class.
  65   InstanceKlass* replace_class(int index, unsigned hash, Symbol* class_name, InstanceKlass* k);
  66 
  67   InstanceKlass* find_class(int index, unsigned int hash, Symbol* name);
  68 
  69   void classes_do(void f(InstanceKlass*));
  70   void classes_do(void f(InstanceKlass*, TRAPS), TRAPS);
  71   void all_entries_do(KlassClosure* closure);
  72   void classes_do(MetaspaceClosure* it);
  73 
  74   void clean_cached_protection_domains();
  75 
  76   // Protection domains
  77   InstanceKlass* find(unsigned int hash, Symbol* name, Handle protection_domain);
  78   bool is_valid_protection_domain(unsigned int hash,
  79                                   Symbol* name,
  80                                   Handle protection_domain);
  81   void add_protection_domain(int index, unsigned int hash,
  82                              InstanceKlass* klass,
  83                              Handle protection_domain, TRAPS);
  84 
  85   void print_on(outputStream* st) const;
  86   void verify();
  87 
  88  private:
  89   DictionaryEntry* new_entry(unsigned int hash, InstanceKlass* klass);
  90 
  91   DictionaryEntry* bucket(int i) const {
  92     return (DictionaryEntry*)Hashtable<InstanceKlass*, mtClass>::bucket(i);
  93   }
  94 
  95   // The following method is not MT-safe and must be done under lock.
  96   DictionaryEntry** bucket_addr(int i) {
  97     return (DictionaryEntry**)Hashtable<InstanceKlass*, mtClass>::bucket_addr(i);
  98   }
  99 
 100   void add_entry(int index, DictionaryEntry* new_entry) {
 101     Hashtable<InstanceKlass*, mtClass>::add_entry(index, (HashtableEntry<InstanceKlass*, mtClass>*)new_entry);
 102   }
 103 
 104   void unlink_entry(DictionaryEntry* entry) {
 105     Hashtable<InstanceKlass*, mtClass>::unlink_entry((HashtableEntry<InstanceKlass*, mtClass>*)entry);
 106   }
 107 
 108   void free_entry(DictionaryEntry* entry);
 109 };
 110 
 111 // An entry in the class loader data dictionaries, this describes a class as
 112 // { InstanceKlass*, protection_domain }.
 113 
 114 class DictionaryEntry : public HashtableEntry<InstanceKlass*, mtClass> {
 115   friend class VMStructs;
 116  private:
 117   // Contains the set of approved protection domains that can access
 118   // this dictionary entry.
 119   //
 120   // This protection domain set is a set of tuples:
 121   //
 122   // (InstanceKlass C, initiating class loader ICL, Protection Domain PD)
 123   //
 124   // [Note that C.protection_domain(), which is stored in the java.lang.Class
 125   // mirror of C, is NOT the same as PD]
 126   //
 127   // If such an entry (C, ICL, PD) exists in the table, it means that
 128   // it is okay for a class Foo to reference C, where
 129   //
 130   //    Foo.protection_domain() == PD, and
 131   //    Foo's defining class loader == ICL
 132   //
 133   // The usage of the PD set can be seen in SystemDictionary::validate_protection_domain()
 134   // It is essentially a cache to avoid repeated Java up-calls to
 135   // ClassLoader.checkPackageAccess().
 136   //
 137   ProtectionDomainEntry* volatile _pd_set;
 138 
 139  public:
 140   // Tells whether a protection is in the approved set.
 141   bool contains_protection_domain(oop protection_domain) const;
 142   // Adds a protection domain to the approved set.
 143   void add_protection_domain(Dictionary* dict, Handle protection_domain);
 144 
 145   InstanceKlass* instance_klass() const { return literal(); }
 146   InstanceKlass** klass_addr() { return (InstanceKlass**)literal_addr(); }
 147 
 148   DictionaryEntry* next() const {
 149     return (DictionaryEntry*)HashtableEntry<InstanceKlass*, mtClass>::next();
 150   }
 151 
 152   DictionaryEntry** next_addr() {
 153     return (DictionaryEntry**)HashtableEntry<InstanceKlass*, mtClass>::next_addr();
 154   }
 155 
 156   ProtectionDomainEntry* pd_set() const            { return _pd_set; }
 157   void set_pd_set(ProtectionDomainEntry* new_head) {  _pd_set = new_head; }
 158 
 159   // Tells whether the initiating class' protection domain can access the klass in this entry
 160   bool is_valid_protection_domain(Handle protection_domain) {
 161     if (!ProtectionDomainVerification) return true;
 162 
 163     return protection_domain() == NULL
 164          ? true
 165          : contains_protection_domain(protection_domain());
 166   }
 167 
 168   void verify_protection_domain_set();
 169 
 170   bool equals(const Symbol* class_name) const {
 171     InstanceKlass* klass = (InstanceKlass*)literal();
 172     return (klass->name() == class_name);
 173   }
 174 
 175   void print_count(outputStream *st);
 176   void verify();
 177 };
 178 
 179 // Entry in a SymbolPropertyTable, mapping a single Symbol*
 180 // to a managed and an unmanaged pointer.
 181 class SymbolPropertyEntry : public HashtableEntry<Symbol*, mtSymbol> {
 182   friend class VMStructs;
 183  private:
 184   intptr_t _symbol_mode;  // secondary key
 185   Method*   _method;
 186   OopHandle _method_type;
 187 
 188  public:
 189   Symbol* symbol() const            { return literal(); }
 190 
 191   intptr_t symbol_mode() const      { return _symbol_mode; }
 192   void set_symbol_mode(intptr_t m)  { _symbol_mode = m; }
 193 
 194   Method*        method() const     { return _method; }
 195   void set_method(Method* p)        { _method = p; }
 196 
 197   oop      method_type() const;
 198   void set_method_type(oop p);
 199 
 200   // We need to clear the OopHandle because these hashtable entries are not constructed properly.
 201   void clear_method_type() { _method_type = OopHandle(); }
 202 
 203   void free_entry();
 204 
 205   SymbolPropertyEntry* next() const {
 206     return (SymbolPropertyEntry*)HashtableEntry<Symbol*, mtSymbol>::next();
 207   }
 208 
 209   SymbolPropertyEntry** next_addr() {
 210     return (SymbolPropertyEntry**)HashtableEntry<Symbol*, mtSymbol>::next_addr();
 211   }
 212 
 213   void print_entry(outputStream* st) const {
 214     symbol()->print_value_on(st);
 215     st->print("/mode=" INTX_FORMAT, symbol_mode());
 216     st->print(" -> ");
 217     bool printed = false;
 218     if (method() != NULL) {
 219       method()->print_value_on(st);
 220       printed = true;
 221     }
 222     if (method_type() != NULL) {
 223       if (printed)  st->print(" and ");
 224       st->print(INTPTR_FORMAT, p2i((void *)method_type()));
 225       printed = true;
 226     }
 227     st->print_cr(printed ? "" : "(empty)");
 228   }
 229 };
 230 
 231 // A system-internal mapping of symbols to pointers, both managed
 232 // and unmanaged.  Used to record the auto-generation of each method
 233 // MethodHandle.invoke(S)T, for all signatures (S)T.
 234 class SymbolPropertyTable : public Hashtable<Symbol*, mtSymbol> {
 235   friend class VMStructs;
 236 private:
 237   // The following method is not MT-safe and must be done under lock.
 238   SymbolPropertyEntry** bucket_addr(int i) {
 239     return (SymbolPropertyEntry**) Hashtable<Symbol*, mtSymbol>::bucket_addr(i);
 240   }
 241 
 242   void add_entry(int index, SymbolPropertyEntry* new_entry) {
 243     ShouldNotReachHere();
 244   }
 245   void set_entry(int index, SymbolPropertyEntry* new_entry) {
 246     ShouldNotReachHere();
 247   }
 248 
 249   SymbolPropertyEntry* new_entry(unsigned int hash, Symbol* symbol, intptr_t symbol_mode) {
 250     SymbolPropertyEntry* entry = (SymbolPropertyEntry*) Hashtable<Symbol*, mtSymbol>::new_entry(hash, symbol);
 251     // Hashtable with Symbol* literal must increment and decrement refcount.
 252     symbol->increment_refcount();
 253     entry->set_symbol_mode(symbol_mode);
 254     entry->set_method(NULL);
 255     entry->clear_method_type();
 256     return entry;
 257   }
 258 
 259 public:
 260   SymbolPropertyTable(int table_size);
 261   SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t, int number_of_entries);
 262 
 263   void free_entry(SymbolPropertyEntry* entry);
 264 
 265   unsigned int compute_hash(Symbol* sym, intptr_t symbol_mode) {
 266     // Use the regular identity_hash.
 267     return Hashtable<Symbol*, mtSymbol>::compute_hash(sym) ^ symbol_mode;
 268   }
 269 
 270   int index_for(Symbol* name, intptr_t symbol_mode) {
 271     return hash_to_index(compute_hash(name, symbol_mode));
 272   }
 273 
 274   // need not be locked; no state change
 275   SymbolPropertyEntry* find_entry(int index, unsigned int hash, Symbol* name, intptr_t name_mode);
 276 
 277   // must be done under SystemDictionary_lock
 278   SymbolPropertyEntry* add_entry(int index, unsigned int hash, Symbol* name, intptr_t name_mode);
 279 
 280   void methods_do(void f(Method*));
 281 
 282   void verify();
 283 
 284   SymbolPropertyEntry* bucket(int i) {
 285     return (SymbolPropertyEntry*) Hashtable<Symbol*, mtSymbol>::bucket(i);
 286   }
 287 };
 288 #endif // SHARE_CLASSFILE_DICTIONARY_HPP