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