1 /* 2 * Copyright (c) 2016, 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 #include "precompiled.hpp" 26 #include "classfile/moduleEntry.hpp" 27 #include "classfile/packageEntry.hpp" 28 #include "memory/resourceArea.hpp" 29 #include "oops/symbol.hpp" 30 #include "runtime/handles.inline.hpp" 31 #include "trace/traceMacros.hpp" 32 #include "utilities/events.hpp" 33 #include "utilities/growableArray.hpp" 34 #include "utilities/hashtable.inline.hpp" 35 #include "utilities/ostream.hpp" 36 37 // Returns true if this package specifies m as a qualified export, including through an unnamed export 38 bool PackageEntry::is_qexported_to(ModuleEntry* m) const { 39 assert(m != NULL, "No module to lookup in this package's qualified exports list"); 40 MutexLocker m1(Module_lock); 41 if (is_exported_allUnnamed() && !m->is_named()) { 42 return true; 43 } else if (!has_qual_exports_list()) { 44 return false; 45 } else { 46 return _qualified_exports->contains(m); 47 } 48 } 49 50 // Add a module to the package's qualified export list. 51 void PackageEntry::add_qexport(ModuleEntry* m) { 52 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 53 if (!has_qual_exports_list()) { 54 // Lazily create a package's qualified exports list. 55 // Initial size is small, do not anticipate export lists to be large. 56 _qualified_exports = 57 new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleEntry*>(QUAL_EXP_SIZE, true); 58 } 59 _qualified_exports->append_if_missing(m); 60 } 61 62 // Set the package's exported states based on the value of the ModuleEntry. 63 void PackageEntry::set_exported(ModuleEntry* m) { 64 MutexLocker m1(Module_lock); 65 if (is_unqual_exported()) { 66 // An exception could be thrown, but choose to simply ignore. 67 // Illegal to convert an unqualified exported package to be qualifiedly exported 68 return; 69 } 70 71 if (m == NULL) { 72 // NULL indicates the package is being unqualifiedly exported 73 if (has_qual_exports_list()) { 74 // Legit to transition a package from being qualifiedly exported 75 // to unqualified. Clean up the qualified lists at the next 76 // safepoint. 77 _exported_pending_delete = _qualified_exports; 78 } 79 80 // Mark package as unqualifiedly exported 81 set_unqual_exported(); 82 83 } else { 84 // Add the exported module 85 add_qexport(m); 86 } 87 } 88 89 void PackageEntry::set_is_exported_allUnnamed() { 90 MutexLocker m1(Module_lock); 91 if (!is_unqual_exported()) { 92 _is_exported_allUnnamed = true; 93 } 94 } 95 96 // Remove dead module entries within the package's exported list. 97 void PackageEntry::purge_qualified_exports() { 98 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 99 if (_qualified_exports != NULL) { 100 // Go backwards because this removes entries that are dead. 101 int len = _qualified_exports->length(); 102 for (int idx = len - 1; idx >= 0; idx--) { 103 ModuleEntry* module_idx = _qualified_exports->at(idx); 104 ClassLoaderData* cld = module_idx->loader(); 105 if (cld->is_unloading()) { 106 _qualified_exports->delete_at(idx); 107 } 108 } 109 } 110 } 111 112 void PackageEntry::delete_qualified_exports() { 113 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 114 if (_exported_pending_delete != NULL) { 115 // If a transition occurred from qualified to unqualified, the _qualified_exports 116 // field should have been NULL'ed out. 117 assert(_qualified_exports == NULL, "Package's exported pending delete, exported list should not be active"); 118 delete _exported_pending_delete; 119 } 120 121 if (_qualified_exports != NULL) { 122 delete _qualified_exports; 123 } 124 125 _exported_pending_delete = NULL; 126 _qualified_exports = NULL; 127 } 128 129 PackageEntryTable::PackageEntryTable(int table_size) 130 : Hashtable<Symbol*, mtModule>(table_size, sizeof(PackageEntry)) 131 { 132 } 133 134 PackageEntryTable::~PackageEntryTable() { 135 assert_locked_or_safepoint(Module_lock); 136 137 // Walk through all buckets and all entries in each bucket, 138 // freeing each entry. 139 for (int i = 0; i < table_size(); ++i) { 140 for (PackageEntry* p = bucket(i); p != NULL;) { 141 PackageEntry* to_remove = p; 142 // read next before freeing. 143 p = p->next(); 144 145 // Clean out the C heap allocated qualified exports list first before freeing the entry 146 to_remove->delete_qualified_exports(); 147 to_remove->name()->decrement_refcount(); 148 149 // Unlink from the Hashtable prior to freeing 150 unlink_entry(to_remove); 151 FREE_C_HEAP_ARRAY(char, to_remove); 152 } 153 } 154 assert(number_of_entries() == 0, "should have removed all entries"); 155 assert(new_entry_free_list() == NULL, "entry present on PackageEntryTable's free list"); 156 free_buckets(); 157 } 158 159 PackageEntry* PackageEntryTable::new_entry(unsigned int hash, Symbol* name, ModuleEntry* module) { 160 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 161 PackageEntry* entry = (PackageEntry*) NEW_C_HEAP_ARRAY(char, entry_size(), mtModule); 162 163 // Initialize everything BasicHashtable would 164 entry->set_next(NULL); 165 entry->set_hash(hash); 166 entry->set_literal(name); 167 168 TRACE_INIT_PACKAGE_ID(entry); 169 170 // Initialize fields specific to a PackageEntry 171 entry->init(); 172 entry->name()->increment_refcount(); 173 if (!module->is_named()) { 174 // Set the exported state to true because all packages 175 // within the unnamed module are unqualifiedly exported 176 entry->set_unqual_exported(); 177 } 178 entry->set_module(module); 179 return entry; 180 } 181 182 void PackageEntryTable::add_entry(int index, PackageEntry* new_entry) { 183 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 184 Hashtable<Symbol*, mtModule>::add_entry(index, (HashtableEntry<Symbol*, mtModule>*)new_entry); 185 } 186 187 // Create package in loader's package entry table and return the entry. 188 // If entry already exists, return null. Assume Module lock was taken by caller. 189 PackageEntry* PackageEntryTable::locked_create_entry_or_null(Symbol* name, ModuleEntry* module) { 190 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 191 // Check if package already exists. Return NULL if it does. 192 if (lookup_only(name) != NULL) { 193 return NULL; 194 } else { 195 PackageEntry* entry = new_entry(compute_hash(name), name, module); 196 add_entry(index_for(name), entry); 197 return entry; 198 } 199 } 200 201 PackageEntry* PackageEntryTable::lookup(Symbol* name, ModuleEntry* module) { 202 PackageEntry* p = lookup_only(name); 203 if (p != NULL) { 204 return p; 205 } else { 206 // If not found, add to table. Grab the PackageEntryTable lock first. 207 MutexLocker ml(Module_lock); 208 209 // Since look-up was done lock-free, we need to check if another thread beat 210 // us in the race to insert the package. 211 PackageEntry* test = lookup_only(name); 212 if (test != NULL) { 213 // A race occurred and another thread introduced the package. 214 return test; 215 } else { 216 assert(module != NULL, "module should never be null"); 217 PackageEntry* entry = new_entry(compute_hash(name), name, module); 218 add_entry(index_for(name), entry); 219 return entry; 220 } 221 } 222 } 223 224 PackageEntry* PackageEntryTable::lookup_only(Symbol* name) { 225 int index = index_for(name); 226 for (PackageEntry* p = bucket(index); p != NULL; p = p->next()) { 227 if (p->name()->fast_compare(name) == 0) { 228 return p; 229 } 230 } 231 return NULL; 232 } 233 234 // Called when a define module for java.base is being processed. 235 // Verify the packages loaded thus far are in java.base's package list. 236 void PackageEntryTable::verify_javabase_packages(GrowableArray<Symbol*> *pkg_list) { 237 for (int i = 0; i < table_size(); i++) { 238 for (PackageEntry* entry = bucket(i); 239 entry != NULL; 240 entry = entry->next()) { 241 ModuleEntry* m = entry->module(); 242 Symbol* module_name = (m == NULL ? NULL : m->name()); 243 if (module_name != NULL && 244 (module_name->fast_compare(vmSymbols::java_base()) == 0) && 245 !pkg_list->contains(entry->name())) { 246 ResourceMark rm; 247 vm_exit_during_initialization("A non-java.base package was loaded prior to module system initialization", entry->name()->as_C_string()); 248 } 249 } 250 } 251 252 } 253 254 // iteration of qualified exports 255 void PackageEntry::package_exports_do(ModuleClosure* const f) { 256 assert_locked_or_safepoint(Module_lock); 257 assert(f != NULL, "invariant"); 258 259 if (has_qual_exports_list()) { 260 int qe_len = _qualified_exports->length(); 261 262 for (int i = 0; i < qe_len; ++i) { 263 f->do_module(_qualified_exports->at(i)); 264 } 265 } 266 } 267 268 // Remove dead entries from all packages' exported list 269 void PackageEntryTable::purge_all_package_exports() { 270 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 271 for (int i = 0; i < table_size(); i++) { 272 for (PackageEntry* entry = bucket(i); 273 entry != NULL; 274 entry = entry->next()) { 275 if (entry->exported_pending_delete()) { 276 // exported list is pending deletion due to a transition 277 // from qualified to unqualified 278 entry->delete_qualified_exports(); 279 } else if (entry->is_qual_exported()) { 280 entry->purge_qualified_exports(); 281 } 282 } 283 } 284 } 285 286 void PackageEntryTable::print(outputStream* st) { 287 st->print_cr("Package Entry Table (table_size=%d, entries=%d)", 288 table_size(), number_of_entries()); 289 for (int i = 0; i < table_size(); i++) { 290 for (PackageEntry* probe = bucket(i); 291 probe != NULL; 292 probe = probe->next()) { 293 probe->print(st); 294 } 295 } 296 } 297 298 void PackageEntry::print(outputStream* st) { 299 ResourceMark rm; 300 st->print_cr("package entry " PTR_FORMAT " name %s module %s classpath_index " 301 INT32_FORMAT " is_exported_unqualified %d is_exported_allUnnamed %d " "next " PTR_FORMAT, 302 p2i(this), name()->as_C_string(), 303 (module()->is_named() ? module()->name()->as_C_string() : UNNAMED_MODULE), 304 _classpath_index, _is_exported_unqualified, _is_exported_allUnnamed, p2i(next())); 305 } 306 307 void PackageEntryTable::verify() { 308 int element_count = 0; 309 for (int index = 0; index < table_size(); index++) { 310 for (PackageEntry* probe = bucket(index); 311 probe != NULL; 312 probe = probe->next()) { 313 probe->verify(); 314 element_count++; 315 } 316 } 317 guarantee(number_of_entries() == element_count, 318 "Verify of Package Entry Table failed"); 319 debug_only(verify_lookup_length((double)number_of_entries() / table_size())); 320 } 321 322 void PackageEntry::verify() { 323 guarantee(name() != NULL, "A package entry must have a corresponding symbol name."); 324 }