src/share/vm/classfile/dictionary.hpp

Print this page
rev 5310 : imported patch ioi_original_patch
rev 5311 : imported patch cleanup
   1 /*
   2  * Copyright (c) 2003, 2012, 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/systemDictionary.hpp"
  29 #include "oops/instanceKlass.hpp"
  30 #include "oops/oop.hpp"
  31 #include "utilities/hashtable.hpp"
  32 
  33 class DictionaryEntry;
  34 class PSPromotionManager;


  35 
  36 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  37 // The data structure for the system dictionary (and the shared system
  38 // dictionary).
  39 
  40 class Dictionary : public TwoOopHashtable<Klass*, mtClass> {
  41   friend class VMStructs;
  42 private:
  43   // current iteration index.
  44   static int                    _current_class_index;
  45   // pointer to the current hash table entry.
  46   static DictionaryEntry*       _current_class_entry;
  47 


  48   DictionaryEntry* get_entry(int index, unsigned int hash,
  49                              Symbol* name, ClassLoaderData* loader_data);
  50 
  51   DictionaryEntry* bucket(int i) {
  52     return (DictionaryEntry*)Hashtable<Klass*, 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<Klass*, mtClass>::bucket_addr(i);
  58   }
  59 
  60   void add_entry(int index, DictionaryEntry* new_entry) {
  61     Hashtable<Klass*, mtClass>::add_entry(index, (HashtableEntry<Klass*, mtClass>*)new_entry);
  62   }
  63 
  64 public:
  65   Dictionary(int table_size);
  66   Dictionary(int table_size, HashtableBucket<mtClass>* t, int number_of_entries);
  67 


 101     return (loader_data->is_the_null_class_loader_data() || !ClassUnloading);
 102   }
 103 
 104   // Unload (that is, break root links to) all unmarked classes and
 105   // loaders.  Returns "true" iff something was unloaded.
 106   bool do_unloading();
 107 
 108   // Protection domains
 109   Klass* find(int index, unsigned int hash, Symbol* name,
 110                 ClassLoaderData* loader_data, Handle protection_domain, TRAPS);
 111   bool is_valid_protection_domain(int index, unsigned int hash,
 112                                   Symbol* name, ClassLoaderData* loader_data,
 113                                   Handle protection_domain);
 114   void add_protection_domain(int index, unsigned int hash,
 115                              instanceKlassHandle klass, ClassLoaderData* loader_data,
 116                              Handle protection_domain, TRAPS);
 117 
 118   // Sharing support
 119   void reorder_dictionary();
 120 

 121 
 122 #ifndef PRODUCT
 123   void print();
 124 #endif
 125   void verify();
 126 };
 127 
 128 // The following classes can be in dictionary.cpp, but we need these
 129 // to be in header file so that SA's vmStructs can access.










































































































 130 
 131 class ProtectionDomainEntry :public CHeapObj<mtClass> {
 132   friend class VMStructs;
 133  public:
 134   ProtectionDomainEntry* _next;
 135   oop                    _protection_domain;
 136 
 137   ProtectionDomainEntry(oop protection_domain, ProtectionDomainEntry* next) {
 138     _protection_domain = protection_domain;
 139     _next              = next;

 140   }
 141 
 142   ProtectionDomainEntry* next() { return _next; }
 143   oop protection_domain() { return _protection_domain; }








 144 };
 145 
 146 // An entry in the system dictionary, this describes a class as
 147 // { Klass*, loader, protection_domain }.
 148 
 149 class DictionaryEntry : public HashtableEntry<Klass*, mtClass> {
 150   friend class VMStructs;
 151  private:
 152   // Contains the set of approved protection domains that can access
 153   // this system dictionary entry.
 154   ProtectionDomainEntry* _pd_set;
 155   ClassLoaderData*       _loader_data;
 156 
 157  public:
 158   // Tells whether a protection is in the approved set.
 159   bool contains_protection_domain(oop protection_domain) const;
 160   // Adds a protection domain to the approved set.
 161   void add_protection_domain(oop protection_domain);
 162 
 163   Klass* klass() const { return (Klass*)literal(); }
 164   Klass** klass_addr() { return (Klass**)literal_addr(); }
 165 
 166   DictionaryEntry* next() const {
 167     return (DictionaryEntry*)HashtableEntry<Klass*, mtClass>::next();
 168   }
 169 
 170   DictionaryEntry** next_addr() {
 171     return (DictionaryEntry**)HashtableEntry<Klass*, mtClass>::next_addr();
 172   }
 173 
 174   ClassLoaderData* loader_data() const { return _loader_data; }
 175   void set_loader_data(ClassLoaderData* loader_data) { _loader_data = loader_data; }
 176 
 177   ProtectionDomainEntry* pd_set() const { return _pd_set; }
 178   void set_pd_set(ProtectionDomainEntry* pd_set) { _pd_set = pd_set; }
 179 
 180   bool has_protection_domain() { return _pd_set != NULL; }
 181 
 182   // Tells whether the initiating class' protection can access the this _klass
 183   bool is_valid_protection_domain(Handle protection_domain) {
 184     if (!ProtectionDomainVerification) return true;
 185     if (!SystemDictionary::has_checkPackageAccess()) return true;
 186 
 187     return protection_domain() == NULL
 188          ? true
 189          : contains_protection_domain(protection_domain());
 190   }
 191 
 192 
 193   void protection_domain_set_oops_do(OopClosure* f) {
 194     for (ProtectionDomainEntry* current = _pd_set;
 195                                 current != NULL;
 196                                 current = current->_next) {
 197       f->do_oop(&(current->_protection_domain));
 198     }
 199   }
 200 
 201   void verify_protection_domain_set() {
 202     for (ProtectionDomainEntry* current = _pd_set;
 203                                 current != NULL;
 204                                 current = current->_next) {
 205       current->_protection_domain->verify();
 206     }
 207   }
 208 
 209   bool equals(Symbol* class_name, ClassLoaderData* loader_data) const {
 210     Klass* klass = (Klass*)literal();
 211     return (InstanceKlass::cast(klass)->name() == class_name &&
 212             _loader_data == loader_data);
 213   }
 214 
 215   void print() {
 216     int count = 0;
 217     for (ProtectionDomainEntry* current = _pd_set;
 218                                 current != NULL;
 219                                 current = current->_next) {
 220       count++;
 221     }
 222     tty->print_cr("pd set = #%d", count);
 223   }
 224 };
 225 


   1 /*
   2  * Copyright (c) 2003, 2013, 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/systemDictionary.hpp"
  29 #include "oops/instanceKlass.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "utilities/hashtable.hpp"
  32 
  33 class DictionaryEntry;
  34 class PSPromotionManager;
  35 class ProtectionDomainCacheTable;
  36 class ProtectionDomainCacheEntry;
  37 
  38 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  39 // The data structure for the system dictionary (and the shared system
  40 // dictionary).
  41 
  42 class Dictionary : public TwoOopHashtable<Klass*, mtClass> {
  43   friend class VMStructs;
  44 private:
  45   // current iteration index.
  46   static int                    _current_class_index;
  47   // pointer to the current hash table entry.
  48   static DictionaryEntry*       _current_class_entry;
  49 
  50   ProtectionDomainCacheTable*   _pd_cache_table;
  51 
  52   DictionaryEntry* get_entry(int index, unsigned int hash,
  53                              Symbol* name, ClassLoaderData* loader_data);
  54 
  55   DictionaryEntry* bucket(int i) {
  56     return (DictionaryEntry*)Hashtable<Klass*, mtClass>::bucket(i);
  57   }
  58 
  59   // The following method is not MT-safe and must be done under lock.
  60   DictionaryEntry** bucket_addr(int i) {
  61     return (DictionaryEntry**)Hashtable<Klass*, mtClass>::bucket_addr(i);
  62   }
  63 
  64   void add_entry(int index, DictionaryEntry* new_entry) {
  65     Hashtable<Klass*, mtClass>::add_entry(index, (HashtableEntry<Klass*, mtClass>*)new_entry);
  66   }
  67 
  68 public:
  69   Dictionary(int table_size);
  70   Dictionary(int table_size, HashtableBucket<mtClass>* t, int number_of_entries);
  71 


 105     return (loader_data->is_the_null_class_loader_data() || !ClassUnloading);
 106   }
 107 
 108   // Unload (that is, break root links to) all unmarked classes and
 109   // loaders.  Returns "true" iff something was unloaded.
 110   bool do_unloading();
 111 
 112   // Protection domains
 113   Klass* find(int index, unsigned int hash, Symbol* name,
 114                 ClassLoaderData* loader_data, Handle protection_domain, TRAPS);
 115   bool is_valid_protection_domain(int index, unsigned int hash,
 116                                   Symbol* name, ClassLoaderData* loader_data,
 117                                   Handle protection_domain);
 118   void add_protection_domain(int index, unsigned int hash,
 119                              instanceKlassHandle klass, ClassLoaderData* loader_data,
 120                              Handle protection_domain, TRAPS);
 121 
 122   // Sharing support
 123   void reorder_dictionary();
 124 
 125   ProtectionDomainCacheEntry* cache_get(oop protection_domain);
 126 
 127 #ifndef PRODUCT
 128   void print();
 129 #endif
 130   void verify();
 131 };
 132 
 133 // The following classes can be in dictionary.cpp, but we need these
 134 // to be in header file so that SA's vmStructs can access them.
 135 class ProtectionDomainCacheEntry : public HashtableEntry<oop, mtClass> {
 136   friend class VMStructs;
 137  private:
 138   // the number of system dictionary entries referring to this entry
 139   int _refcount;
 140   // flag indicating whether this protection domain entry is strongly reachable.
 141   // Used during iterating over the system dictionary to remember oops that need
 142   // to be updated.
 143   bool _strongly_reachable;
 144  public:
 145   oop protection_domain() { return literal(); }
 146 
 147   void increment_refcount() {
 148     assert(_refcount >= 0, "sanity");
 149     _refcount++;
 150   }
 151 
 152   int refcount() { return _refcount; }
 153   int decrement_refcount() {
 154     assert(_refcount > 0, "sanity");
 155     _refcount--;
 156     return _refcount;
 157   }
 158 
 159   void init() {
 160     _refcount = 0;
 161     _strongly_reachable = false;
 162   }
 163 
 164   ProtectionDomainCacheEntry* next() {
 165     return (ProtectionDomainCacheEntry*)HashtableEntry<oop, mtClass>::next();
 166   }
 167 
 168   ProtectionDomainCacheEntry** next_addr() {
 169     return (ProtectionDomainCacheEntry**)HashtableEntry<oop, mtClass>::next_addr();
 170   }
 171 
 172   void oops_do(OopClosure* f) {
 173     f->do_oop(literal_addr());
 174   }
 175 
 176   void set_strongly_reachable() { _strongly_reachable = true; }
 177 
 178   bool is_strongly_reachable() { return _strongly_reachable; }
 179 
 180   void reset_strongly_reachable() { _strongly_reachable = false; }
 181 
 182   void print() PRODUCT_RETURN;
 183   void verify();
 184 };
 185 
 186 // the ProtectionDomainCacheTable contains all protection domain oops. The system
 187 // dictionary entries reference its entries instead of having references to oops
 188 // directly.
 189 // This is used to speed up system dictionary iteration: the oops in the
 190 // protection domain are the only ones referring the Java heap. So when there is
 191 // need to update these, instead of going over every entry of the system dictionary,
 192 // we only need to iterate over this set.
 193 // The amount of different protection domains used is typically magnitudes smaller
 194 // than the number of system dictionary entries (loaded classes).
 195 class ProtectionDomainCacheTable : public Hashtable<oop, mtClass> {
 196   friend class VMStructs;
 197 private:
 198   ProtectionDomainCacheEntry* bucket(int i) {
 199     return (ProtectionDomainCacheEntry*) Hashtable<oop, mtClass>::bucket(i);
 200   }
 201 
 202   // The following method is not MT-safe and must be done under lock.
 203   ProtectionDomainCacheEntry** bucket_addr(int i) {
 204     return (ProtectionDomainCacheEntry**) Hashtable<oop, mtClass>::bucket_addr(i);
 205   }
 206 
 207   ProtectionDomainCacheEntry* new_entry(unsigned int hash, oop protection_domain) {
 208     ProtectionDomainCacheEntry* entry = (ProtectionDomainCacheEntry*) Hashtable<oop, mtClass>::new_entry(hash, protection_domain);
 209     entry->init();
 210     return entry;
 211   }
 212 
 213   static unsigned int compute_hash(oop protection_domain) {
 214     return (unsigned int)(protection_domain->identity_hash());
 215   }
 216 
 217   int index_for(oop protection_domain) {
 218     return hash_to_index(compute_hash(protection_domain));
 219   }
 220 
 221   ProtectionDomainCacheEntry* add_entry(int index, unsigned int hash, oop protection_domain);
 222   ProtectionDomainCacheEntry* find_entry(int index, oop protection_domain);
 223 
 224 public:
 225 
 226   ProtectionDomainCacheTable(int table_size);
 227 
 228   ProtectionDomainCacheEntry* get(oop protection_domain);
 229   void free(ProtectionDomainCacheEntry* entry);
 230 
 231   // GC support
 232   void oops_do(OopClosure* f);
 233   void always_strong_oops_do(OopClosure* f);
 234 
 235   static uint bucket_size();
 236 
 237   void print() PRODUCT_RETURN;
 238   void verify();
 239 };
 240 
 241 
 242 class ProtectionDomainEntry :public CHeapObj<mtClass> {
 243   friend class VMStructs;
 244  public:
 245   ProtectionDomainEntry* _next;
 246   ProtectionDomainCacheEntry* _pd_cache;
 247 
 248   ProtectionDomainEntry(ProtectionDomainCacheEntry* pd_cache, ProtectionDomainEntry* next) {
 249     _pd_cache = pd_cache;
 250     _next     = next;
 251     _pd_cache->increment_refcount();
 252   }
 253 
 254   ProtectionDomainEntry* next() { return _next; }
 255   oop protection_domain() { return _pd_cache->protection_domain(); }
 256 
 257   ProtectionDomainCacheEntry * release_pd_cache() {
 258     if (_pd_cache->decrement_refcount() == 0) {
 259       return _pd_cache;
 260     } else {
 261       return NULL;
 262     }
 263   }
 264 };
 265 
 266 // An entry in the system dictionary, this describes a class as
 267 // { Klass*, loader, protection_domain }.
 268 
 269 class DictionaryEntry : public HashtableEntry<Klass*, mtClass> {
 270   friend class VMStructs;
 271  private:
 272   // Contains the set of approved protection domains that can access
 273   // this system dictionary entry.
 274   ProtectionDomainEntry* _pd_set;
 275   ClassLoaderData*       _loader_data;
 276 
 277  public:
 278   // Tells whether a protection is in the approved set.
 279   bool contains_protection_domain(oop protection_domain) const;
 280   // Adds a protection domain to the approved set.
 281   void add_protection_domain(Dictionary* dict, oop protection_domain);
 282 
 283   Klass* klass() const { return (Klass*)literal(); }
 284   Klass** klass_addr() { return (Klass**)literal_addr(); }
 285 
 286   DictionaryEntry* next() const {
 287     return (DictionaryEntry*)HashtableEntry<Klass*, mtClass>::next();
 288   }
 289 
 290   DictionaryEntry** next_addr() {
 291     return (DictionaryEntry**)HashtableEntry<Klass*, mtClass>::next_addr();
 292   }
 293 
 294   ClassLoaderData* loader_data() const { return _loader_data; }
 295   void set_loader_data(ClassLoaderData* loader_data) { _loader_data = loader_data; }
 296 
 297   ProtectionDomainEntry* pd_set() const { return _pd_set; }
 298   void set_pd_set(ProtectionDomainEntry* pd_set) { _pd_set = pd_set; }
 299 
 300   bool has_protection_domain() { return _pd_set != NULL; }
 301 
 302   // Tells whether the initiating class' protection can access the this _klass
 303   bool is_valid_protection_domain(Handle protection_domain) {
 304     if (!ProtectionDomainVerification) return true;
 305     if (!SystemDictionary::has_checkPackageAccess()) return true;
 306 
 307     return protection_domain() == NULL
 308          ? true
 309          : contains_protection_domain(protection_domain());
 310   }
 311 
 312   void set_strongly_reachable() {

 313     for (ProtectionDomainEntry* current = _pd_set;
 314                                 current != NULL;
 315                                 current = current->_next) {
 316       current->_pd_cache->set_strongly_reachable();
 317     }
 318   }
 319 
 320   void verify_protection_domain_set() {
 321     for (ProtectionDomainEntry* current = _pd_set;
 322                                 current != NULL;
 323                                 current = current->_next) {
 324       current->_pd_cache->protection_domain()->verify();
 325     }
 326   }
 327 
 328   bool equals(Symbol* class_name, ClassLoaderData* loader_data) const {
 329     Klass* klass = (Klass*)literal();
 330     return (InstanceKlass::cast(klass)->name() == class_name &&
 331             _loader_data == loader_data);
 332   }
 333 
 334   void print() {
 335     int count = 0;
 336     for (ProtectionDomainEntry* current = _pd_set;
 337                                 current != NULL;
 338                                 current = current->_next) {
 339       count++;
 340     }
 341     tty->print_cr("pd set = #%d", count);
 342   }
 343 };
 344