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