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