1 /* 2 * Copyright (c) 2016, 2018, 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_MODULEENTRY_HPP 26 #define SHARE_VM_CLASSFILE_MODULEENTRY_HPP 27 28 #include "jni.h" 29 #include "classfile/classLoaderData.hpp" 30 #include "classfile/vmSymbols.hpp" 31 #include "oops/oopHandle.hpp" 32 #include "oops/symbol.hpp" 33 #include "runtime/jniHandles.hpp" 34 #include "runtime/mutexLocker.hpp" 35 #include "utilities/growableArray.hpp" 36 #include "utilities/hashtable.hpp" 37 #include "utilities/macros.hpp" 38 #include "utilities/ostream.hpp" 39 #if INCLUDE_JFR 40 #include "jfr/support/jfrTraceIdExtension.hpp" 41 #endif 42 43 #define UNNAMED_MODULE "Unnamed Module" 44 #define JAVAPKG "java" 45 #define JAVAPKG_LEN 4 46 #define JAVA_BASE_NAME "java.base" 47 48 class ModuleClosure; 49 50 // A ModuleEntry describes a module that has been defined by a call to JVM_DefineModule. 51 // It contains: 52 // - Symbol* containing the module's name. 53 // - pointer to the java.lang.Module for this module. 54 // - pointer to the java.security.ProtectionDomain shared by classes defined to this module. 55 // - ClassLoaderData*, class loader of this module. 56 // - a growable array containg other module entries that this module can read. 57 // - a flag indicating if this module can read all unnamed modules. 58 // 59 // The Mutex Module_lock is shared between ModuleEntry and PackageEntry, to lock either 60 // data structure. 61 class ModuleEntry : public HashtableEntry<Symbol*, mtModule> { 62 private: 63 OopHandle _module; // java.lang.Module 64 OopHandle _pd; // java.security.ProtectionDomain, cached 65 // for shared classes from this module 66 ClassLoaderData* _loader_data; 67 GrowableArray<ModuleEntry*>* _reads; // list of modules that are readable by this module 68 Symbol* _version; // module version number 69 Symbol* _location; // module location 70 bool _can_read_all_unnamed; 71 bool _has_default_read_edges; // JVMTI redefine/retransform support 72 bool _must_walk_reads; // walk module's reads list at GC safepoints to purge out dead modules 73 bool _is_open; // whether the packages in the module are all unqualifiedly exported 74 bool _is_patched; // whether the module is patched via --patch-module 75 JFR_ONLY(DEFINE_TRACE_ID_FIELD;) 76 enum {MODULE_READS_SIZE = 101}; // Initial size of list of modules that the module can read. 77 78 public: 79 void init() { 80 _module = NULL; 81 _loader_data = NULL; 82 _pd = NULL; 83 _reads = NULL; 84 _version = NULL; 85 _location = NULL; 86 _can_read_all_unnamed = false; 87 _has_default_read_edges = false; 88 _must_walk_reads = false; 89 _is_patched = false; 90 _is_open = false; 91 } 92 93 Symbol* name() const { return literal(); } 94 void set_name(Symbol* n) { set_literal(n); } 95 96 oop module() const; 97 OopHandle module_handle() const { return _module; } 98 void set_module(OopHandle j) { _module = j; } 99 100 // The shared ProtectionDomain reference is set once the VM loads a shared class 101 // originated from the current Module. The referenced ProtectionDomain object is 102 // created by the ClassLoader when loading a class (shared or non-shared) from the 103 // Module for the first time. This ProtectionDomain object is used for all 104 // classes from the Module loaded by the same ClassLoader. 105 oop shared_protection_domain(); 106 void set_shared_protection_domain(ClassLoaderData *loader_data, Handle pd); 107 108 ClassLoaderData* loader_data() const { return _loader_data; } 109 110 void set_loader_data(ClassLoaderData* cld) { 111 assert(!cld->is_anonymous(), "Unexpected anonymous class loader data"); 112 _loader_data = cld; 113 } 114 115 Symbol* version() const { return _version; } 116 void set_version(Symbol* version); 117 118 Symbol* location() const { return _location; } 119 void set_location(Symbol* location); 120 bool should_show_version(); 121 122 bool can_read(ModuleEntry* m) const; 123 bool has_reads_list() const; 124 void add_read(ModuleEntry* m); 125 void set_read_walk_required(ClassLoaderData* m_loader_data); 126 127 bool is_open() const { return _is_open; } 128 void set_is_open(bool is_open); 129 130 bool is_named() const { return (name() != NULL); } 131 132 bool can_read_all_unnamed() const { 133 assert(is_named() || _can_read_all_unnamed == true, 134 "unnamed modules can always read all unnamed modules"); 135 return _can_read_all_unnamed; 136 } 137 138 // Modules can only go from strict to loose. 139 void set_can_read_all_unnamed() { _can_read_all_unnamed = true; } 140 141 bool has_default_read_edges() const { 142 return _has_default_read_edges; 143 } 144 145 // Sets true and returns the previous value. 146 bool set_has_default_read_edges() { 147 MutexLocker ml(Module_lock); 148 bool prev = _has_default_read_edges; 149 _has_default_read_edges = true; 150 return prev; 151 } 152 153 void set_is_patched() { 154 _is_patched = true; 155 } 156 bool is_patched() { 157 return _is_patched; 158 } 159 160 ModuleEntry* next() const { 161 return (ModuleEntry*)HashtableEntry<Symbol*, mtModule>::next(); 162 } 163 ModuleEntry** next_addr() { 164 return (ModuleEntry**)HashtableEntry<Symbol*, mtModule>::next_addr(); 165 } 166 167 // iteration support for readability 168 void module_reads_do(ModuleClosure* const f); 169 170 // Purge dead weak references out of reads list when any given class loader is unloaded. 171 void purge_reads(); 172 void delete_reads(); 173 174 // Special handling for unnamed module, one per class loader 175 static ModuleEntry* create_unnamed_module(ClassLoaderData* cld); 176 static ModuleEntry* create_boot_unnamed_module(ClassLoaderData* cld); 177 static ModuleEntry* new_unnamed_module_entry(Handle module_handle, ClassLoaderData* cld); 178 void delete_unnamed_module(); 179 180 void print(outputStream* st = tty); 181 void verify(); 182 183 JFR_ONLY(DEFINE_TRACE_ID_METHODS;) 184 }; 185 186 // Iterator interface 187 class ModuleClosure: public StackObj { 188 public: 189 virtual void do_module(ModuleEntry* module) = 0; 190 }; 191 192 193 // The ModuleEntryTable is a Hashtable containing a list of all modules defined 194 // by a particular class loader. Each module is represented as a ModuleEntry node. 195 // 196 // Each ModuleEntryTable contains a _javabase_module field which allows for the 197 // creation of java.base's ModuleEntry very early in bootstrapping before the 198 // corresponding JVM_DefineModule call for java.base occurs during module system 199 // initialization. Setting up java.base's ModuleEntry early enables classes, 200 // loaded prior to the module system being initialized to be created with their 201 // PackageEntry node's correctly pointing at java.base's ModuleEntry. No class 202 // outside of java.base is allowed to be loaded pre-module system initialization. 203 // 204 // The ModuleEntryTable's lookup is lock free. 205 // 206 class ModuleEntryTable : public Hashtable<Symbol*, mtModule> { 207 friend class VMStructs; 208 public: 209 enum Constants { 210 _moduletable_entry_size = 109 // number of entries in module entry table 211 }; 212 213 private: 214 static ModuleEntry* _javabase_module; 215 216 ModuleEntry* new_entry(unsigned int hash, Handle module_handle, bool is_open, 217 Symbol* name, Symbol* version, Symbol* location, ClassLoaderData* loader_data); 218 void add_entry(int index, ModuleEntry* new_entry); 219 220 int entry_size() const { return BasicHashtable<mtModule>::entry_size(); } 221 222 ModuleEntry** bucket_addr(int i) { 223 return (ModuleEntry**)Hashtable<Symbol*, mtModule>::bucket_addr(i); 224 } 225 226 static unsigned int compute_hash(Symbol* name) { return ((name == NULL) ? 0 : (unsigned int)(name->identity_hash())); } 227 int index_for(Symbol* name) const { return hash_to_index(compute_hash(name)); } 228 229 public: 230 ModuleEntryTable(int table_size); 231 ~ModuleEntryTable(); 232 233 ModuleEntry* bucket(int i) { 234 return (ModuleEntry*)Hashtable<Symbol*, mtModule>::bucket(i); 235 } 236 237 // Create module in loader's module entry table, if already exists then 238 // return null. Assume Module_lock has been locked by caller. 239 ModuleEntry* locked_create_entry_or_null(Handle module_handle, 240 bool is_open, 241 Symbol* module_name, 242 Symbol* module_version, 243 Symbol* module_location, 244 ClassLoaderData* loader_data); 245 246 // Only lookup module within loader's module entry table. The table read is lock-free. 247 ModuleEntry* lookup_only(Symbol* name); 248 249 // purge dead weak references out of reads list 250 void purge_all_module_reads(); 251 252 // Special handling for java.base 253 static ModuleEntry* javabase_moduleEntry() { return _javabase_module; } 254 static void set_javabase_moduleEntry(ModuleEntry* java_base) { _javabase_module = java_base; } 255 256 static bool javabase_defined() { return ((_javabase_module != NULL) && 257 (_javabase_module->module() != NULL)); } 258 static void finalize_javabase(Handle module_handle, Symbol* version, Symbol* location); 259 static void patch_javabase_entries(Handle module_handle); 260 261 void print(outputStream* st = tty); 262 void verify(); 263 }; 264 265 #endif // SHARE_VM_CLASSFILE_MODULEENTRY_HPP