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