Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/classfile/dictionary.hpp
32285 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_CLASSFILE_DICTIONARY_HPP25#define SHARE_VM_CLASSFILE_DICTIONARY_HPP2627#include "classfile/systemDictionary.hpp"28#include "oops/instanceKlass.hpp"29#include "oops/oop.inline.hpp"30#include "utilities/hashtable.hpp"3132class DictionaryEntry;33class PSPromotionManager;34class ProtectionDomainCacheTable;35class ProtectionDomainCacheEntry;36class BoolObjectClosure;3738//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~39// The data structure for the system dictionary (and the shared system40// dictionary).4142class Dictionary : public TwoOopHashtable<Klass*, mtClass> {43friend class VMStructs;44private:45// current iteration index.46static int _current_class_index;47// pointer to the current hash table entry.48static DictionaryEntry* _current_class_entry;4950ProtectionDomainCacheTable* _pd_cache_table;5152DictionaryEntry* get_entry(int index, unsigned int hash,53Symbol* name, ClassLoaderData* loader_data);5455protected:56DictionaryEntry* bucket(int i) {57return (DictionaryEntry*)Hashtable<Klass*, mtClass>::bucket(i);58}5960// The following method is not MT-safe and must be done under lock.61DictionaryEntry** bucket_addr(int i) {62return (DictionaryEntry**)Hashtable<Klass*, mtClass>::bucket_addr(i);63}6465void add_entry(int index, DictionaryEntry* new_entry) {66Hashtable<Klass*, mtClass>::add_entry(index, (HashtableEntry<Klass*, mtClass>*)new_entry);67}6869static size_t entry_size();7071public:72Dictionary(int table_size);73Dictionary(int table_size, HashtableBucket<mtClass>* t, int number_of_entries);7475DictionaryEntry* new_entry(unsigned int hash, Klass* klass, ClassLoaderData* loader_data);7677DictionaryEntry* new_entry();7879void free_entry(DictionaryEntry* entry);8081void add_klass(Symbol* class_name, ClassLoaderData* loader_data,KlassHandle obj);8283Klass* find_class(int index, unsigned int hash,84Symbol* name, ClassLoaderData* loader_data);8586Klass* find_shared_class(int index, unsigned int hash, Symbol* name);8788// Compiler support89Klass* try_get_next_class();9091// GC support92void oops_do(OopClosure* f);93void always_strong_oops_do(OopClosure* blk);94void roots_oops_do(OopClosure* strong, OopClosure* weak);9596void always_strong_classes_do(KlassClosure* closure);9798void classes_do(void f(Klass*));99void classes_do(void f(Klass*, TRAPS), TRAPS);100void classes_do(void f(Klass*, ClassLoaderData*));101102void methods_do(void f(Method*));103104void unlink(BoolObjectClosure* is_alive);105void remove_classes_in_error_state();106107// Classes loaded by the bootstrap loader are always strongly reachable.108// If we're not doing class unloading, all classes are strongly reachable.109static bool is_strongly_reachable(ClassLoaderData* loader_data, Klass* klass) {110assert (klass != NULL, "should have non-null klass");111return (loader_data->is_the_null_class_loader_data() || !ClassUnloading);112}113114// Unload (that is, break root links to) all unmarked classes and loaders.115void do_unloading();116117// Protection domains118Klass* find(int index, unsigned int hash, Symbol* name,119ClassLoaderData* loader_data, Handle protection_domain, TRAPS);120bool is_valid_protection_domain(int index, unsigned int hash,121Symbol* name, ClassLoaderData* loader_data,122Handle protection_domain);123void add_protection_domain(int index, unsigned int hash,124instanceKlassHandle klass, ClassLoaderData* loader_data,125Handle protection_domain, TRAPS);126127// Sharing support128void reorder_dictionary();129130ProtectionDomainCacheEntry* cache_get(oop protection_domain);131132void print(bool details = true);133void verify();134};135136// The following classes can be in dictionary.cpp, but we need these137// to be in header file so that SA's vmStructs can access them.138class ProtectionDomainCacheEntry : public HashtableEntry<oop, mtClass> {139friend class VMStructs;140private:141// Flag indicating whether this protection domain entry is strongly reachable.142// Used during iterating over the system dictionary to remember oops that need143// to be updated.144bool _strongly_reachable;145public:146oop protection_domain() { return literal(); }147148void init() {149_strongly_reachable = false;150}151152ProtectionDomainCacheEntry* next() {153return (ProtectionDomainCacheEntry*)HashtableEntry<oop, mtClass>::next();154}155156ProtectionDomainCacheEntry** next_addr() {157return (ProtectionDomainCacheEntry**)HashtableEntry<oop, mtClass>::next_addr();158}159160void oops_do(OopClosure* f) {161f->do_oop(literal_addr());162}163164void set_strongly_reachable() { _strongly_reachable = true; }165bool is_strongly_reachable() { return _strongly_reachable; }166void reset_strongly_reachable() { _strongly_reachable = false; }167168void print() PRODUCT_RETURN;169void verify();170};171172// The ProtectionDomainCacheTable contains all protection domain oops. The system173// dictionary entries reference its entries instead of having references to oops174// directly.175// This is used to speed up system dictionary iteration: the oops in the176// protection domain are the only ones referring the Java heap. So when there is177// need to update these, instead of going over every entry of the system dictionary,178// we only need to iterate over this set.179// The amount of different protection domains used is typically magnitudes smaller180// than the number of system dictionary entries (loaded classes).181class ProtectionDomainCacheTable : public Hashtable<oop, mtClass> {182friend class VMStructs;183private:184ProtectionDomainCacheEntry* bucket(int i) {185return (ProtectionDomainCacheEntry*) Hashtable<oop, mtClass>::bucket(i);186}187188// The following method is not MT-safe and must be done under lock.189ProtectionDomainCacheEntry** bucket_addr(int i) {190return (ProtectionDomainCacheEntry**) Hashtable<oop, mtClass>::bucket_addr(i);191}192193ProtectionDomainCacheEntry* new_entry(unsigned int hash, oop protection_domain) {194ProtectionDomainCacheEntry* entry = (ProtectionDomainCacheEntry*) Hashtable<oop, mtClass>::new_entry(hash, protection_domain);195entry->init();196return entry;197}198199static unsigned int compute_hash(oop protection_domain) {200return (unsigned int)(protection_domain->identity_hash());201}202203int index_for(oop protection_domain) {204return hash_to_index(compute_hash(protection_domain));205}206207ProtectionDomainCacheEntry* add_entry(int index, unsigned int hash, oop protection_domain);208ProtectionDomainCacheEntry* find_entry(int index, oop protection_domain);209210public:211212ProtectionDomainCacheTable(int table_size);213214ProtectionDomainCacheEntry* get(oop protection_domain);215void free(ProtectionDomainCacheEntry* entry);216217void unlink(BoolObjectClosure* cl);218219// GC support220void oops_do(OopClosure* f);221void always_strong_oops_do(OopClosure* f);222void roots_oops_do(OopClosure* strong, OopClosure* weak);223224static uint bucket_size();225226void print() PRODUCT_RETURN;227void verify();228};229230231class ProtectionDomainEntry :public CHeapObj<mtClass> {232friend class VMStructs;233public:234ProtectionDomainEntry* _next;235ProtectionDomainCacheEntry* _pd_cache;236237ProtectionDomainEntry(ProtectionDomainCacheEntry* pd_cache, ProtectionDomainEntry* next) {238_pd_cache = pd_cache;239_next = next;240}241242ProtectionDomainEntry* next() { return _next; }243oop protection_domain() { return _pd_cache->protection_domain(); }244};245246// An entry in the system dictionary, this describes a class as247// { Klass*, loader, protection_domain }.248249class DictionaryEntry : public HashtableEntry<Klass*, mtClass> {250friend class VMStructs;251private:252// Contains the set of approved protection domains that can access253// this system dictionary entry.254//255// This protection domain set is a set of tuples:256//257// (InstanceKlass C, initiating class loader ICL, Protection Domain PD)258//259// [Note that C.protection_domain(), which is stored in the java.lang.Class260// mirror of C, is NOT the same as PD]261//262// If such an entry (C, ICL, PD) exists in the table, it means that263// it is okay for a class Foo to reference C, where264//265// Foo.protection_domain() == PD, and266// Foo's defining class loader == ICL267//268// The usage of the PD set can be seen in SystemDictionary::validate_protection_domain()269// It is essentially a cache to avoid repeated Java up-calls to270// ClassLoader.checkPackageAccess().271//272ProtectionDomainEntry* _pd_set;273ClassLoaderData* _loader_data;274275public:276// Tells whether a protection is in the approved set.277bool contains_protection_domain(oop protection_domain) const;278// Adds a protection domain to the approved set.279void add_protection_domain(Dictionary* dict, oop protection_domain);280281Klass* klass() const { return (Klass*)literal(); }282Klass** klass_addr() { return (Klass**)literal_addr(); }283284DictionaryEntry* next() const {285return (DictionaryEntry*)HashtableEntry<Klass*, mtClass>::next();286}287288DictionaryEntry** next_addr() {289return (DictionaryEntry**)HashtableEntry<Klass*, mtClass>::next_addr();290}291292ClassLoaderData* loader_data() const { return _loader_data; }293void set_loader_data(ClassLoaderData* loader_data) { _loader_data = loader_data; }294295ProtectionDomainEntry* pd_set() const { return _pd_set; }296void set_pd_set(ProtectionDomainEntry* pd_set) { _pd_set = pd_set; }297298bool has_protection_domain() { return _pd_set != NULL; }299300// Tells whether the initiating class' protection can access the this _klass301bool is_valid_protection_domain(Handle protection_domain) {302if (!ProtectionDomainVerification) return true;303if (!SystemDictionary::has_checkPackageAccess()) return true;304305return protection_domain() == NULL306? true307: contains_protection_domain(protection_domain());308}309310void set_strongly_reachable() {311for (ProtectionDomainEntry* current = _pd_set;312current != NULL;313current = current->_next) {314current->_pd_cache->set_strongly_reachable();315}316}317318void verify_protection_domain_set() {319for (ProtectionDomainEntry* current = _pd_set;320current != NULL;321current = current->_next) {322current->_pd_cache->protection_domain()->verify();323}324}325326bool equals(Symbol* class_name, ClassLoaderData* loader_data) const {327Klass* klass = (Klass*)literal();328return (InstanceKlass::cast(klass)->name() == class_name &&329_loader_data == loader_data);330}331332void print() {333int count = 0;334for (ProtectionDomainEntry* current = _pd_set;335current != NULL;336current = current->_next) {337count++;338}339tty->print_cr("pd set = #%d", count);340}341};342343// Entry in a SymbolPropertyTable, mapping a single Symbol*344// to a managed and an unmanaged pointer.345class SymbolPropertyEntry : public HashtableEntry<Symbol*, mtSymbol> {346friend class VMStructs;347private:348intptr_t _symbol_mode; // secondary key349Method* _method;350oop _method_type;351352public:353Symbol* symbol() const { return literal(); }354355intptr_t symbol_mode() const { return _symbol_mode; }356void set_symbol_mode(intptr_t m) { _symbol_mode = m; }357358Method* method() const { return _method; }359void set_method(Method* p) { _method = p; }360361oop method_type() const { return _method_type; }362oop* method_type_addr() { return &_method_type; }363void set_method_type(oop p) { _method_type = p; }364365SymbolPropertyEntry* next() const {366return (SymbolPropertyEntry*)HashtableEntry<Symbol*, mtSymbol>::next();367}368369SymbolPropertyEntry** next_addr() {370return (SymbolPropertyEntry**)HashtableEntry<Symbol*, mtSymbol>::next_addr();371}372373void print_on(outputStream* st) const {374symbol()->print_value_on(st);375st->print("/mode=" INTX_FORMAT, symbol_mode());376st->print(" -> ");377bool printed = false;378if (method() != NULL) {379method()->print_value_on(st);380printed = true;381}382if (method_type() != NULL) {383if (printed) st->print(" and ");384st->print(INTPTR_FORMAT, p2i((void *)method_type()));385printed = true;386}387st->print_cr(printed ? "" : "(empty)");388}389};390391// A system-internal mapping of symbols to pointers, both managed392// and unmanaged. Used to record the auto-generation of each method393// MethodHandle.invoke(S)T, for all signatures (S)T.394class SymbolPropertyTable : public Hashtable<Symbol*, mtSymbol> {395friend class VMStructs;396private:397SymbolPropertyEntry* bucket(int i) {398return (SymbolPropertyEntry*) Hashtable<Symbol*, mtSymbol>::bucket(i);399}400401// The following method is not MT-safe and must be done under lock.402SymbolPropertyEntry** bucket_addr(int i) {403return (SymbolPropertyEntry**) Hashtable<Symbol*, mtSymbol>::bucket_addr(i);404}405406void add_entry(int index, SymbolPropertyEntry* new_entry) {407ShouldNotReachHere();408}409void set_entry(int index, SymbolPropertyEntry* new_entry) {410ShouldNotReachHere();411}412413SymbolPropertyEntry* new_entry(unsigned int hash, Symbol* symbol, intptr_t symbol_mode) {414SymbolPropertyEntry* entry = (SymbolPropertyEntry*) Hashtable<Symbol*, mtSymbol>::new_entry(hash, symbol);415// Hashtable with Symbol* literal must increment and decrement refcount.416symbol->increment_refcount();417entry->set_symbol_mode(symbol_mode);418entry->set_method(NULL);419entry->set_method_type(NULL);420return entry;421}422423public:424SymbolPropertyTable(int table_size);425SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t, int number_of_entries);426427void free_entry(SymbolPropertyEntry* entry) {428// decrement Symbol refcount here because hashtable doesn't.429entry->literal()->decrement_refcount();430Hashtable<Symbol*, mtSymbol>::free_entry(entry);431}432433unsigned int compute_hash(Symbol* sym, intptr_t symbol_mode) {434// Use the regular identity_hash.435return Hashtable<Symbol*, mtSymbol>::compute_hash(sym) ^ symbol_mode;436}437438int index_for(Symbol* name, intptr_t symbol_mode) {439return hash_to_index(compute_hash(name, symbol_mode));440}441442// need not be locked; no state change443SymbolPropertyEntry* find_entry(int index, unsigned int hash, Symbol* name, intptr_t name_mode);444445// must be done under SystemDictionary_lock446SymbolPropertyEntry* add_entry(int index, unsigned int hash, Symbol* name, intptr_t name_mode);447448// GC support449void oops_do(OopClosure* f);450451void methods_do(void f(Method*));452453// Sharing support454void reorder_dictionary();455456#ifndef PRODUCT457void print();458#endif459void verify();460};461#endif // SHARE_VM_CLASSFILE_DICTIONARY_HPP462463464