1 /* 2 * Copyright (c) 2003, 2016, 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 class ProtectionDomainCacheTable; 36 class ProtectionDomainCacheEntry; 37 class BoolObjectClosure; 38 39 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 40 // The data structure for the system dictionary (and the shared system 41 // dictionary). 42 43 class Dictionary : public TwoOopHashtable<Klass*, mtClass> { 44 friend class VMStructs; 45 private: 46 // current iteration index. 47 static int _current_class_index; 48 // pointer to the current hash table entry. 49 static DictionaryEntry* _current_class_entry; 50 51 ProtectionDomainCacheTable* _pd_cache_table; 52 53 DictionaryEntry* get_entry(int index, unsigned int hash, 54 Symbol* name, ClassLoaderData* loader_data); 55 56 protected: 57 DictionaryEntry* bucket(int i) const { 58 return (DictionaryEntry*)Hashtable<Klass*, mtClass>::bucket(i); 59 } 60 61 // The following method is not MT-safe and must be done under lock. 62 DictionaryEntry** bucket_addr(int i) { 63 return (DictionaryEntry**)Hashtable<Klass*, mtClass>::bucket_addr(i); 64 } 65 66 void add_entry(int index, DictionaryEntry* new_entry) { 67 Hashtable<Klass*, mtClass>::add_entry(index, (HashtableEntry<Klass*, mtClass>*)new_entry); 68 } 69 70 static size_t entry_size(); 71 public: 72 Dictionary(int table_size); 73 Dictionary(int table_size, HashtableBucket<mtClass>* t, int number_of_entries); 74 75 DictionaryEntry* new_entry(unsigned int hash, Klass* klass, ClassLoaderData* loader_data); 76 77 DictionaryEntry* new_entry(); 78 79 void free_entry(DictionaryEntry* entry); 80 81 void add_klass(Symbol* class_name, ClassLoaderData* loader_data,KlassHandle obj); 82 83 Klass* find_class(int index, unsigned int hash, 84 Symbol* name, ClassLoaderData* loader_data); 85 86 Klass* find_shared_class(int index, unsigned int hash, Symbol* name); 87 88 // Compiler support 89 Klass* try_get_next_class(); 90 91 // GC support 92 void oops_do(OopClosure* f); 93 void always_strong_oops_do(OopClosure* blk); 94 void roots_oops_do(OopClosure* strong, OopClosure* weak); 95 96 void always_strong_classes_do(KlassClosure* closure); 97 98 void classes_do(void f(Klass*)); 99 void classes_do(void f(Klass*, TRAPS), TRAPS); 100 void classes_do(void f(Klass*, ClassLoaderData*)); 101 102 void methods_do(void f(Method*)); 103 104 void unlink(BoolObjectClosure* is_alive); 105 void remove_classes_in_error_state(); 106 107 // Classes loaded by the bootstrap loader are always strongly reachable. 108 // If we're not doing class unloading, all classes are strongly reachable. 109 static bool is_strongly_reachable(ClassLoaderData* loader_data, Klass* klass) { 110 assert (klass != NULL, "should have non-null klass"); 111 return (loader_data->is_the_null_class_loader_data() || !ClassUnloading); 112 } 113 114 // Unload (that is, break root links to) all unmarked classes and loaders. 115 void do_unloading(); 116 117 // Protection domains 118 Klass* find(int index, unsigned int hash, Symbol* name, 119 ClassLoaderData* loader_data, Handle protection_domain, TRAPS); 120 bool is_valid_protection_domain(int index, unsigned int hash, 121 Symbol* name, ClassLoaderData* loader_data, 122 Handle protection_domain); 123 void add_protection_domain(int index, unsigned int hash, 124 instanceKlassHandle klass, ClassLoaderData* loader_data, 125 Handle protection_domain, TRAPS); 126 127 // Sharing support 128 void reorder_dictionary(); 129 130 ProtectionDomainCacheEntry* cache_get(oop protection_domain); 131 132 void print(bool details = true); 133 void verify(); 134 }; 135 136 // The following classes can be in dictionary.cpp, but we need these 137 // to be in header file so that SA's vmStructs can access them. 138 class ProtectionDomainCacheEntry : public HashtableEntry<oop, mtClass> { 139 friend class VMStructs; 140 private: 141 // Flag indicating whether this protection domain entry is strongly reachable. 142 // Used during iterating over the system dictionary to remember oops that need 143 // to be updated. 144 bool _strongly_reachable; 145 public: 146 oop protection_domain() { return literal(); } 147 148 void init() { 149 _strongly_reachable = false; 150 } 151 152 ProtectionDomainCacheEntry* next() { 153 return (ProtectionDomainCacheEntry*)HashtableEntry<oop, mtClass>::next(); 154 } 155 156 ProtectionDomainCacheEntry** next_addr() { 157 return (ProtectionDomainCacheEntry**)HashtableEntry<oop, mtClass>::next_addr(); 158 } 159 160 void oops_do(OopClosure* f) { 161 f->do_oop(literal_addr()); 162 } 163 164 void set_strongly_reachable() { _strongly_reachable = true; } 165 bool is_strongly_reachable() { return _strongly_reachable; } 166 void reset_strongly_reachable() { _strongly_reachable = false; } 167 168 void print() PRODUCT_RETURN; 169 void verify(); 170 }; 171 172 // The ProtectionDomainCacheTable contains all protection domain oops. The system 173 // dictionary entries reference its entries instead of having references to oops 174 // directly. 175 // This is used to speed up system dictionary iteration: the oops in the 176 // protection domain are the only ones referring the Java heap. So when there is 177 // need to update these, instead of going over every entry of the system dictionary, 178 // we only need to iterate over this set. 179 // The amount of different protection domains used is typically magnitudes smaller 180 // than the number of system dictionary entries (loaded classes). 181 class ProtectionDomainCacheTable : public Hashtable<oop, mtClass> { 182 friend class VMStructs; 183 private: 184 ProtectionDomainCacheEntry* bucket(int i) { 185 return (ProtectionDomainCacheEntry*) Hashtable<oop, mtClass>::bucket(i); 186 } 187 188 // The following method is not MT-safe and must be done under lock. 189 ProtectionDomainCacheEntry** bucket_addr(int i) { 190 return (ProtectionDomainCacheEntry**) Hashtable<oop, mtClass>::bucket_addr(i); 191 } 192 193 ProtectionDomainCacheEntry* new_entry(unsigned int hash, oop protection_domain) { 194 ProtectionDomainCacheEntry* entry = (ProtectionDomainCacheEntry*) Hashtable<oop, mtClass>::new_entry(hash, protection_domain); 195 entry->init(); 196 return entry; 197 } 198 199 static unsigned int compute_hash(oop protection_domain); 200 201 int index_for(oop protection_domain); 202 ProtectionDomainCacheEntry* add_entry(int index, unsigned int hash, oop protection_domain); 203 ProtectionDomainCacheEntry* find_entry(int index, oop protection_domain); 204 205 public: 206 207 ProtectionDomainCacheTable(int table_size); 208 209 ProtectionDomainCacheEntry* get(oop protection_domain); 210 void free(ProtectionDomainCacheEntry* entry); 211 212 void unlink(BoolObjectClosure* cl); 213 214 // GC support 215 void oops_do(OopClosure* f); 216 void always_strong_oops_do(OopClosure* f); 217 void roots_oops_do(OopClosure* strong, OopClosure* weak); 218 219 static uint bucket_size(); 220 221 void print() PRODUCT_RETURN; 222 void verify(); 223 }; 224 225 226 class ProtectionDomainEntry :public CHeapObj<mtClass> { 227 friend class VMStructs; 228 public: 229 ProtectionDomainEntry* _next; 230 ProtectionDomainCacheEntry* _pd_cache; 231 232 ProtectionDomainEntry(ProtectionDomainCacheEntry* pd_cache, ProtectionDomainEntry* next) { 233 _pd_cache = pd_cache; 234 _next = next; 235 } 236 237 ProtectionDomainEntry* next() { return _next; } 238 oop protection_domain() { return _pd_cache->protection_domain(); } 239 }; 240 241 // An entry in the system dictionary, this describes a class as 242 // { Klass*, loader, protection_domain }. 243 244 class DictionaryEntry : public HashtableEntry<Klass*, mtClass> { 245 friend class VMStructs; 246 private: 247 // Contains the set of approved protection domains that can access 248 // this system dictionary entry. 249 // 250 // This protection domain set is a set of tuples: 251 // 252 // (InstanceKlass C, initiating class loader ICL, Protection Domain PD) 253 // 254 // [Note that C.protection_domain(), which is stored in the java.lang.Class 255 // mirror of C, is NOT the same as PD] 256 // 257 // If such an entry (C, ICL, PD) exists in the table, it means that 258 // it is okay for a class Foo to reference C, where 259 // 260 // Foo.protection_domain() == PD, and 261 // Foo's defining class loader == ICL 262 // 263 // The usage of the PD set can be seen in SystemDictionary::validate_protection_domain() 264 // It is essentially a cache to avoid repeated Java up-calls to 265 // ClassLoader.checkPackageAccess(). 266 // 267 ProtectionDomainEntry* _pd_set; 268 ClassLoaderData* _loader_data; 269 270 public: 271 // Tells whether a protection is in the approved set. 272 bool contains_protection_domain(oop protection_domain) const; 273 // Adds a protection domain to the approved set. 274 void add_protection_domain(Dictionary* dict, oop protection_domain); 275 276 Klass* klass() const { return (Klass*)literal(); } 277 Klass** klass_addr() { return (Klass**)literal_addr(); } 278 279 DictionaryEntry* next() const { 280 return (DictionaryEntry*)HashtableEntry<Klass*, mtClass>::next(); 281 } 282 283 DictionaryEntry** next_addr() { 284 return (DictionaryEntry**)HashtableEntry<Klass*, mtClass>::next_addr(); 285 } 286 287 ClassLoaderData* loader_data() const { return _loader_data; } 288 void set_loader_data(ClassLoaderData* loader_data) { _loader_data = loader_data; } 289 290 ProtectionDomainEntry* pd_set() const { return _pd_set; } 291 void set_pd_set(ProtectionDomainEntry* pd_set) { _pd_set = pd_set; } 292 293 bool has_protection_domain() { return _pd_set != NULL; } 294 295 // Tells whether the initiating class' protection can access the this _klass 296 bool is_valid_protection_domain(Handle protection_domain) { 297 if (!ProtectionDomainVerification) return true; 298 if (!SystemDictionary::has_checkPackageAccess()) return true; 299 300 return protection_domain() == NULL 301 ? true 302 : contains_protection_domain(protection_domain()); 303 } 304 305 void set_strongly_reachable() { 306 for (ProtectionDomainEntry* current = _pd_set; 307 current != NULL; 308 current = current->_next) { 309 current->_pd_cache->set_strongly_reachable(); 310 } 311 } 312 313 void verify_protection_domain_set() { 314 for (ProtectionDomainEntry* current = _pd_set; 315 current != NULL; 316 current = current->_next) { 317 current->_pd_cache->protection_domain()->verify(); 318 } 319 } 320 321 bool equals(const Symbol* class_name, ClassLoaderData* loader_data) const { 322 Klass* klass = (Klass*)literal(); 323 return (klass->name() == class_name && _loader_data == loader_data); 324 } 325 326 void print() { 327 int count = 0; 328 for (ProtectionDomainEntry* current = _pd_set; 329 current != NULL; 330 current = current->_next) { 331 count++; 332 } 333 tty->print_cr("pd set = #%d", count); 334 } 335 }; 336 337 // Entry in a SymbolPropertyTable, mapping a single Symbol* 338 // to a managed and an unmanaged pointer. 339 class SymbolPropertyEntry : public HashtableEntry<Symbol*, mtSymbol> { 340 friend class VMStructs; 341 private: 342 intptr_t _symbol_mode; // secondary key 343 Method* _method; 344 oop _method_type; 345 346 public: 347 Symbol* symbol() const { return literal(); } 348 349 intptr_t symbol_mode() const { return _symbol_mode; } 350 void set_symbol_mode(intptr_t m) { _symbol_mode = m; } 351 352 Method* method() const { return _method; } 353 void set_method(Method* p) { _method = p; } 354 355 oop method_type() const { return _method_type; } 356 oop* method_type_addr() { return &_method_type; } 357 void set_method_type(oop p) { _method_type = p; } 358 359 SymbolPropertyEntry* next() const { 360 return (SymbolPropertyEntry*)HashtableEntry<Symbol*, mtSymbol>::next(); 361 } 362 363 SymbolPropertyEntry** next_addr() { 364 return (SymbolPropertyEntry**)HashtableEntry<Symbol*, mtSymbol>::next_addr(); 365 } 366 367 void print_on(outputStream* st) const { 368 symbol()->print_value_on(st); 369 st->print("/mode=" INTX_FORMAT, symbol_mode()); 370 st->print(" -> "); 371 bool printed = false; 372 if (method() != NULL) { 373 method()->print_value_on(st); 374 printed = true; 375 } 376 if (method_type() != NULL) { 377 if (printed) st->print(" and "); 378 st->print(INTPTR_FORMAT, p2i((void *)method_type())); 379 printed = true; 380 } 381 st->print_cr(printed ? "" : "(empty)"); 382 } 383 }; 384 385 // A system-internal mapping of symbols to pointers, both managed 386 // and unmanaged. Used to record the auto-generation of each method 387 // MethodHandle.invoke(S)T, for all signatures (S)T. 388 class SymbolPropertyTable : public Hashtable<Symbol*, mtSymbol> { 389 friend class VMStructs; 390 private: 391 SymbolPropertyEntry* bucket(int i) { 392 return (SymbolPropertyEntry*) Hashtable<Symbol*, mtSymbol>::bucket(i); 393 } 394 395 // The following method is not MT-safe and must be done under lock. 396 SymbolPropertyEntry** bucket_addr(int i) { 397 return (SymbolPropertyEntry**) Hashtable<Symbol*, mtSymbol>::bucket_addr(i); 398 } 399 400 void add_entry(int index, SymbolPropertyEntry* new_entry) { 401 ShouldNotReachHere(); 402 } 403 void set_entry(int index, SymbolPropertyEntry* new_entry) { 404 ShouldNotReachHere(); 405 } 406 407 SymbolPropertyEntry* new_entry(unsigned int hash, Symbol* symbol, intptr_t symbol_mode) { 408 SymbolPropertyEntry* entry = (SymbolPropertyEntry*) Hashtable<Symbol*, mtSymbol>::new_entry(hash, symbol); 409 // Hashtable with Symbol* literal must increment and decrement refcount. 410 symbol->increment_refcount(); 411 entry->set_symbol_mode(symbol_mode); 412 entry->set_method(NULL); 413 entry->set_method_type(NULL); 414 return entry; 415 } 416 417 public: 418 SymbolPropertyTable(int table_size); 419 SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t, int number_of_entries); 420 421 void free_entry(SymbolPropertyEntry* entry) { 422 // decrement Symbol refcount here because hashtable doesn't. 423 entry->literal()->decrement_refcount(); 424 Hashtable<Symbol*, mtSymbol>::free_entry(entry); 425 } 426 427 unsigned int compute_hash(Symbol* sym, intptr_t symbol_mode) { 428 // Use the regular identity_hash. 429 return Hashtable<Symbol*, mtSymbol>::compute_hash(sym) ^ symbol_mode; 430 } 431 432 int index_for(Symbol* name, intptr_t symbol_mode) { 433 return hash_to_index(compute_hash(name, symbol_mode)); 434 } 435 436 // need not be locked; no state change 437 SymbolPropertyEntry* find_entry(int index, unsigned int hash, Symbol* name, intptr_t name_mode); 438 439 // must be done under SystemDictionary_lock 440 SymbolPropertyEntry* add_entry(int index, unsigned int hash, Symbol* name, intptr_t name_mode); 441 442 // GC support 443 void oops_do(OopClosure* f); 444 445 void methods_do(void f(Method*)); 446 447 // Sharing support 448 void reorder_dictionary(); 449 450 #ifndef PRODUCT 451 void print(); 452 #endif 453 void verify(); 454 }; 455 #endif // SHARE_VM_CLASSFILE_DICTIONARY_HPP