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