aoqi@0: /* iklam@8497: * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_CLASSFILE_DICTIONARY_HPP aoqi@0: #define SHARE_VM_CLASSFILE_DICTIONARY_HPP aoqi@0: aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "oops/instanceKlass.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "utilities/hashtable.hpp" aoqi@0: aoqi@0: class DictionaryEntry; aoqi@0: class PSPromotionManager; aoqi@0: class ProtectionDomainCacheTable; aoqi@0: class ProtectionDomainCacheEntry; aoqi@0: class BoolObjectClosure; aoqi@0: aoqi@0: //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ aoqi@0: // The data structure for the system dictionary (and the shared system aoqi@0: // dictionary). aoqi@0: aoqi@0: class Dictionary : public TwoOopHashtable { aoqi@0: friend class VMStructs; aoqi@0: private: aoqi@0: // current iteration index. aoqi@0: static int _current_class_index; aoqi@0: // pointer to the current hash table entry. aoqi@0: static DictionaryEntry* _current_class_entry; aoqi@0: aoqi@0: ProtectionDomainCacheTable* _pd_cache_table; aoqi@0: aoqi@0: DictionaryEntry* get_entry(int index, unsigned int hash, aoqi@0: Symbol* name, ClassLoaderData* loader_data); aoqi@0: iklam@8497: protected: aoqi@0: DictionaryEntry* bucket(int i) { aoqi@0: return (DictionaryEntry*)Hashtable::bucket(i); aoqi@0: } aoqi@0: aoqi@0: // The following method is not MT-safe and must be done under lock. aoqi@0: DictionaryEntry** bucket_addr(int i) { aoqi@0: return (DictionaryEntry**)Hashtable::bucket_addr(i); aoqi@0: } aoqi@0: aoqi@0: void add_entry(int index, DictionaryEntry* new_entry) { aoqi@0: Hashtable::add_entry(index, (HashtableEntry*)new_entry); aoqi@0: } aoqi@0: iklam@8497: static size_t entry_size(); iklam@8497: aoqi@0: public: aoqi@0: Dictionary(int table_size); aoqi@0: Dictionary(int table_size, HashtableBucket* t, int number_of_entries); aoqi@0: aoqi@0: DictionaryEntry* new_entry(unsigned int hash, Klass* klass, ClassLoaderData* loader_data); aoqi@0: aoqi@0: DictionaryEntry* new_entry(); aoqi@0: aoqi@0: void free_entry(DictionaryEntry* entry); aoqi@0: aoqi@0: void add_klass(Symbol* class_name, ClassLoaderData* loader_data,KlassHandle obj); aoqi@0: aoqi@0: Klass* find_class(int index, unsigned int hash, aoqi@0: Symbol* name, ClassLoaderData* loader_data); aoqi@0: aoqi@0: Klass* find_shared_class(int index, unsigned int hash, Symbol* name); aoqi@0: aoqi@0: // Compiler support aoqi@0: Klass* try_get_next_class(); aoqi@0: aoqi@0: // GC support aoqi@0: void oops_do(OopClosure* f); aoqi@0: void always_strong_oops_do(OopClosure* blk); stefank@6992: void roots_oops_do(OopClosure* strong, OopClosure* weak); aoqi@0: aoqi@0: void always_strong_classes_do(KlassClosure* closure); aoqi@0: aoqi@0: void classes_do(void f(Klass*)); aoqi@0: void classes_do(void f(Klass*, TRAPS), TRAPS); aoqi@0: void classes_do(void f(Klass*, ClassLoaderData*)); aoqi@0: aoqi@0: void methods_do(void f(Method*)); aoqi@0: aoqi@0: void unlink(BoolObjectClosure* is_alive); iklam@7089: void remove_classes_in_error_state(); aoqi@0: aoqi@0: // Classes loaded by the bootstrap loader are always strongly reachable. aoqi@0: // If we're not doing class unloading, all classes are strongly reachable. aoqi@0: static bool is_strongly_reachable(ClassLoaderData* loader_data, Klass* klass) { aoqi@0: assert (klass != NULL, "should have non-null klass"); aoqi@0: return (loader_data->is_the_null_class_loader_data() || !ClassUnloading); aoqi@0: } aoqi@0: thartmann@7064: // Unload (that is, break root links to) all unmarked classes and loaders. thartmann@7064: void do_unloading(); aoqi@0: aoqi@0: // Protection domains aoqi@0: Klass* find(int index, unsigned int hash, Symbol* name, aoqi@0: ClassLoaderData* loader_data, Handle protection_domain, TRAPS); aoqi@0: bool is_valid_protection_domain(int index, unsigned int hash, aoqi@0: Symbol* name, ClassLoaderData* loader_data, aoqi@0: Handle protection_domain); aoqi@0: void add_protection_domain(int index, unsigned int hash, aoqi@0: instanceKlassHandle klass, ClassLoaderData* loader_data, aoqi@0: Handle protection_domain, TRAPS); aoqi@0: aoqi@0: // Sharing support aoqi@0: void reorder_dictionary(); aoqi@0: aoqi@0: ProtectionDomainCacheEntry* cache_get(oop protection_domain); aoqi@0: iklam@7089: void print(bool details = true); aoqi@0: void verify(); aoqi@0: }; aoqi@0: aoqi@0: // The following classes can be in dictionary.cpp, but we need these aoqi@0: // to be in header file so that SA's vmStructs can access them. aoqi@0: class ProtectionDomainCacheEntry : public HashtableEntry { aoqi@0: friend class VMStructs; aoqi@0: private: aoqi@0: // Flag indicating whether this protection domain entry is strongly reachable. aoqi@0: // Used during iterating over the system dictionary to remember oops that need aoqi@0: // to be updated. aoqi@0: bool _strongly_reachable; aoqi@0: public: aoqi@0: oop protection_domain() { return literal(); } aoqi@0: aoqi@0: void init() { aoqi@0: _strongly_reachable = false; aoqi@0: } aoqi@0: aoqi@0: ProtectionDomainCacheEntry* next() { aoqi@0: return (ProtectionDomainCacheEntry*)HashtableEntry::next(); aoqi@0: } aoqi@0: aoqi@0: ProtectionDomainCacheEntry** next_addr() { aoqi@0: return (ProtectionDomainCacheEntry**)HashtableEntry::next_addr(); aoqi@0: } aoqi@0: aoqi@0: void oops_do(OopClosure* f) { aoqi@0: f->do_oop(literal_addr()); aoqi@0: } aoqi@0: aoqi@0: void set_strongly_reachable() { _strongly_reachable = true; } aoqi@0: bool is_strongly_reachable() { return _strongly_reachable; } aoqi@0: void reset_strongly_reachable() { _strongly_reachable = false; } aoqi@0: aoqi@0: void print() PRODUCT_RETURN; aoqi@0: void verify(); aoqi@0: }; aoqi@0: aoqi@0: // The ProtectionDomainCacheTable contains all protection domain oops. The system aoqi@0: // dictionary entries reference its entries instead of having references to oops aoqi@0: // directly. aoqi@0: // This is used to speed up system dictionary iteration: the oops in the aoqi@0: // protection domain are the only ones referring the Java heap. So when there is aoqi@0: // need to update these, instead of going over every entry of the system dictionary, aoqi@0: // we only need to iterate over this set. aoqi@0: // The amount of different protection domains used is typically magnitudes smaller aoqi@0: // than the number of system dictionary entries (loaded classes). aoqi@0: class ProtectionDomainCacheTable : public Hashtable { aoqi@0: friend class VMStructs; aoqi@0: private: aoqi@0: ProtectionDomainCacheEntry* bucket(int i) { aoqi@0: return (ProtectionDomainCacheEntry*) Hashtable::bucket(i); aoqi@0: } aoqi@0: aoqi@0: // The following method is not MT-safe and must be done under lock. aoqi@0: ProtectionDomainCacheEntry** bucket_addr(int i) { aoqi@0: return (ProtectionDomainCacheEntry**) Hashtable::bucket_addr(i); aoqi@0: } aoqi@0: aoqi@0: ProtectionDomainCacheEntry* new_entry(unsigned int hash, oop protection_domain) { aoqi@0: ProtectionDomainCacheEntry* entry = (ProtectionDomainCacheEntry*) Hashtable::new_entry(hash, protection_domain); aoqi@0: entry->init(); aoqi@0: return entry; aoqi@0: } aoqi@0: aoqi@0: static unsigned int compute_hash(oop protection_domain) { aoqi@0: return (unsigned int)(protection_domain->identity_hash()); aoqi@0: } aoqi@0: aoqi@0: int index_for(oop protection_domain) { aoqi@0: return hash_to_index(compute_hash(protection_domain)); aoqi@0: } aoqi@0: aoqi@0: ProtectionDomainCacheEntry* add_entry(int index, unsigned int hash, oop protection_domain); aoqi@0: ProtectionDomainCacheEntry* find_entry(int index, oop protection_domain); aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: ProtectionDomainCacheTable(int table_size); aoqi@0: aoqi@0: ProtectionDomainCacheEntry* get(oop protection_domain); aoqi@0: void free(ProtectionDomainCacheEntry* entry); aoqi@0: aoqi@0: void unlink(BoolObjectClosure* cl); aoqi@0: aoqi@0: // GC support aoqi@0: void oops_do(OopClosure* f); aoqi@0: void always_strong_oops_do(OopClosure* f); stefank@6992: void roots_oops_do(OopClosure* strong, OopClosure* weak); aoqi@0: aoqi@0: static uint bucket_size(); aoqi@0: aoqi@0: void print() PRODUCT_RETURN; aoqi@0: void verify(); aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class ProtectionDomainEntry :public CHeapObj { aoqi@0: friend class VMStructs; aoqi@0: public: aoqi@0: ProtectionDomainEntry* _next; aoqi@0: ProtectionDomainCacheEntry* _pd_cache; aoqi@0: aoqi@0: ProtectionDomainEntry(ProtectionDomainCacheEntry* pd_cache, ProtectionDomainEntry* next) { aoqi@0: _pd_cache = pd_cache; aoqi@0: _next = next; aoqi@0: } aoqi@0: aoqi@0: ProtectionDomainEntry* next() { return _next; } aoqi@0: oop protection_domain() { return _pd_cache->protection_domain(); } aoqi@0: }; aoqi@0: aoqi@0: // An entry in the system dictionary, this describes a class as aoqi@0: // { Klass*, loader, protection_domain }. aoqi@0: aoqi@0: class DictionaryEntry : public HashtableEntry { aoqi@0: friend class VMStructs; aoqi@0: private: aoqi@0: // Contains the set of approved protection domains that can access aoqi@0: // this system dictionary entry. aoqi@0: // aoqi@0: // This protection domain set is a set of tuples: aoqi@0: // aoqi@0: // (InstanceKlass C, initiating class loader ICL, Protection Domain PD) aoqi@0: // aoqi@0: // [Note that C.protection_domain(), which is stored in the java.lang.Class aoqi@0: // mirror of C, is NOT the same as PD] aoqi@0: // aoqi@0: // If such an entry (C, ICL, PD) exists in the table, it means that aoqi@0: // it is okay for a class Foo to reference C, where aoqi@0: // aoqi@0: // Foo.protection_domain() == PD, and aoqi@0: // Foo's defining class loader == ICL aoqi@0: // aoqi@0: // The usage of the PD set can be seen in SystemDictionary::validate_protection_domain() aoqi@0: // It is essentially a cache to avoid repeated Java up-calls to aoqi@0: // ClassLoader.checkPackageAccess(). aoqi@0: // aoqi@0: ProtectionDomainEntry* _pd_set; aoqi@0: ClassLoaderData* _loader_data; aoqi@0: aoqi@0: public: aoqi@0: // Tells whether a protection is in the approved set. aoqi@0: bool contains_protection_domain(oop protection_domain) const; aoqi@0: // Adds a protection domain to the approved set. aoqi@0: void add_protection_domain(Dictionary* dict, oop protection_domain); aoqi@0: aoqi@0: Klass* klass() const { return (Klass*)literal(); } aoqi@0: Klass** klass_addr() { return (Klass**)literal_addr(); } aoqi@0: aoqi@0: DictionaryEntry* next() const { aoqi@0: return (DictionaryEntry*)HashtableEntry::next(); aoqi@0: } aoqi@0: aoqi@0: DictionaryEntry** next_addr() { aoqi@0: return (DictionaryEntry**)HashtableEntry::next_addr(); aoqi@0: } aoqi@0: aoqi@0: ClassLoaderData* loader_data() const { return _loader_data; } aoqi@0: void set_loader_data(ClassLoaderData* loader_data) { _loader_data = loader_data; } aoqi@0: aoqi@0: ProtectionDomainEntry* pd_set() const { return _pd_set; } aoqi@0: void set_pd_set(ProtectionDomainEntry* pd_set) { _pd_set = pd_set; } aoqi@0: aoqi@0: bool has_protection_domain() { return _pd_set != NULL; } aoqi@0: aoqi@0: // Tells whether the initiating class' protection can access the this _klass aoqi@0: bool is_valid_protection_domain(Handle protection_domain) { aoqi@0: if (!ProtectionDomainVerification) return true; aoqi@0: if (!SystemDictionary::has_checkPackageAccess()) return true; aoqi@0: aoqi@0: return protection_domain() == NULL aoqi@0: ? true aoqi@0: : contains_protection_domain(protection_domain()); aoqi@0: } aoqi@0: aoqi@0: void set_strongly_reachable() { aoqi@0: for (ProtectionDomainEntry* current = _pd_set; aoqi@0: current != NULL; aoqi@0: current = current->_next) { aoqi@0: current->_pd_cache->set_strongly_reachable(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void verify_protection_domain_set() { aoqi@0: for (ProtectionDomainEntry* current = _pd_set; aoqi@0: current != NULL; aoqi@0: current = current->_next) { aoqi@0: current->_pd_cache->protection_domain()->verify(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool equals(Symbol* class_name, ClassLoaderData* loader_data) const { aoqi@0: Klass* klass = (Klass*)literal(); aoqi@0: return (InstanceKlass::cast(klass)->name() == class_name && aoqi@0: _loader_data == loader_data); aoqi@0: } aoqi@0: aoqi@0: void print() { aoqi@0: int count = 0; aoqi@0: for (ProtectionDomainEntry* current = _pd_set; aoqi@0: current != NULL; aoqi@0: current = current->_next) { aoqi@0: count++; aoqi@0: } aoqi@0: tty->print_cr("pd set = #%d", count); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // Entry in a SymbolPropertyTable, mapping a single Symbol* aoqi@0: // to a managed and an unmanaged pointer. aoqi@0: class SymbolPropertyEntry : public HashtableEntry { aoqi@0: friend class VMStructs; aoqi@0: private: aoqi@0: intptr_t _symbol_mode; // secondary key aoqi@0: Method* _method; aoqi@0: oop _method_type; aoqi@0: aoqi@0: public: aoqi@0: Symbol* symbol() const { return literal(); } aoqi@0: aoqi@0: intptr_t symbol_mode() const { return _symbol_mode; } aoqi@0: void set_symbol_mode(intptr_t m) { _symbol_mode = m; } aoqi@0: aoqi@0: Method* method() const { return _method; } aoqi@0: void set_method(Method* p) { _method = p; } aoqi@0: aoqi@0: oop method_type() const { return _method_type; } aoqi@0: oop* method_type_addr() { return &_method_type; } aoqi@0: void set_method_type(oop p) { _method_type = p; } aoqi@0: aoqi@0: SymbolPropertyEntry* next() const { aoqi@0: return (SymbolPropertyEntry*)HashtableEntry::next(); aoqi@0: } aoqi@0: aoqi@0: SymbolPropertyEntry** next_addr() { aoqi@0: return (SymbolPropertyEntry**)HashtableEntry::next_addr(); aoqi@0: } aoqi@0: aoqi@0: void print_on(outputStream* st) const { aoqi@0: symbol()->print_value_on(st); aoqi@0: st->print("/mode="INTX_FORMAT, symbol_mode()); aoqi@0: st->print(" -> "); aoqi@0: bool printed = false; aoqi@0: if (method() != NULL) { aoqi@0: method()->print_value_on(st); aoqi@0: printed = true; aoqi@0: } aoqi@0: if (method_type() != NULL) { aoqi@0: if (printed) st->print(" and "); aoqi@0: st->print(INTPTR_FORMAT, p2i((void *)method_type())); aoqi@0: printed = true; aoqi@0: } aoqi@0: st->print_cr(printed ? "" : "(empty)"); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // A system-internal mapping of symbols to pointers, both managed aoqi@0: // and unmanaged. Used to record the auto-generation of each method aoqi@0: // MethodHandle.invoke(S)T, for all signatures (S)T. aoqi@0: class SymbolPropertyTable : public Hashtable { aoqi@0: friend class VMStructs; aoqi@0: private: aoqi@0: SymbolPropertyEntry* bucket(int i) { aoqi@0: return (SymbolPropertyEntry*) Hashtable::bucket(i); aoqi@0: } aoqi@0: aoqi@0: // The following method is not MT-safe and must be done under lock. aoqi@0: SymbolPropertyEntry** bucket_addr(int i) { aoqi@0: return (SymbolPropertyEntry**) Hashtable::bucket_addr(i); aoqi@0: } aoqi@0: aoqi@0: void add_entry(int index, SymbolPropertyEntry* new_entry) { aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: void set_entry(int index, SymbolPropertyEntry* new_entry) { aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: aoqi@0: SymbolPropertyEntry* new_entry(unsigned int hash, Symbol* symbol, intptr_t symbol_mode) { aoqi@0: SymbolPropertyEntry* entry = (SymbolPropertyEntry*) Hashtable::new_entry(hash, symbol); aoqi@0: // Hashtable with Symbol* literal must increment and decrement refcount. aoqi@0: symbol->increment_refcount(); aoqi@0: entry->set_symbol_mode(symbol_mode); aoqi@0: entry->set_method(NULL); aoqi@0: entry->set_method_type(NULL); aoqi@0: return entry; aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: SymbolPropertyTable(int table_size); aoqi@0: SymbolPropertyTable(int table_size, HashtableBucket* t, int number_of_entries); aoqi@0: aoqi@0: void free_entry(SymbolPropertyEntry* entry) { aoqi@0: // decrement Symbol refcount here because hashtable doesn't. aoqi@0: entry->literal()->decrement_refcount(); aoqi@0: Hashtable::free_entry(entry); aoqi@0: } aoqi@0: aoqi@0: unsigned int compute_hash(Symbol* sym, intptr_t symbol_mode) { aoqi@0: // Use the regular identity_hash. aoqi@0: return Hashtable::compute_hash(sym) ^ symbol_mode; aoqi@0: } aoqi@0: aoqi@0: int index_for(Symbol* name, intptr_t symbol_mode) { aoqi@0: return hash_to_index(compute_hash(name, symbol_mode)); aoqi@0: } aoqi@0: aoqi@0: // need not be locked; no state change aoqi@0: SymbolPropertyEntry* find_entry(int index, unsigned int hash, Symbol* name, intptr_t name_mode); aoqi@0: aoqi@0: // must be done under SystemDictionary_lock aoqi@0: SymbolPropertyEntry* add_entry(int index, unsigned int hash, Symbol* name, intptr_t name_mode); aoqi@0: aoqi@0: // GC support aoqi@0: void oops_do(OopClosure* f); aoqi@0: aoqi@0: void methods_do(void f(Method*)); aoqi@0: aoqi@0: // Sharing support aoqi@0: void reorder_dictionary(); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void print(); aoqi@0: #endif aoqi@0: void verify(); aoqi@0: }; aoqi@0: #endif // SHARE_VM_CLASSFILE_DICTIONARY_HPP