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