1 /*
   2  * Copyright (c) 2016, 2017, 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/classLoaderData.hpp"
  27 #include "classfile/javaClasses.hpp"
  28 #include "classfile/moduleEntry.hpp"
  29 #include "logging/log.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/symbol.hpp"
  32 #include "prims/jni.h"
  33 #include "runtime/handles.inline.hpp"
  34 #include "runtime/safepoint.hpp"
  35 #include "trace/traceMacros.hpp"
  36 #include "utilities/events.hpp"
  37 #include "utilities/growableArray.hpp"
  38 #include "utilities/hashtable.inline.hpp"
  39 #include "utilities/ostream.hpp"
  40 
  41 ModuleEntry* ModuleEntryTable::_javabase_module = NULL;
  42 
  43 void ModuleEntry::set_location(Symbol* location) {
  44   if (_location != NULL) {
  45     // _location symbol's refcounts are managed by ModuleEntry,
  46     // must decrement the old one before updating.
  47     _location->decrement_refcount();
  48   }
  49 
  50   _location = location;
  51 
  52   if (location != NULL) {
  53     location->increment_refcount();
  54   }
  55 }
  56 
  57 bool ModuleEntry::is_non_jdk_module() {
  58   ResourceMark rm;
  59   if (location() != NULL) {
  60     const char* loc = location()->as_C_string();
  61     if (strncmp(loc, "jrt:/java.", 10) != 0 && strncmp(loc, "jrt:/jdk.", 9) != 0) {
  62       return true;
  63     }
  64   }
  65   return false;
  66 }
  67 
  68 void ModuleEntry::set_version(Symbol* version) {
  69   if (_version != NULL) {
  70     // _version symbol's refcounts are managed by ModuleEntry,
  71     // must decrement the old one before updating.
  72     _version->decrement_refcount();
  73   }
  74 
  75   _version = version;
  76 
  77   if (version != NULL) {
  78     version->increment_refcount();
  79   }
  80 }
  81 
  82 // Returns the shared ProtectionDomain
  83 Handle ModuleEntry::shared_protection_domain() {
  84   return Handle(Thread::current(), JNIHandles::resolve(_pd));
  85 }
  86 
  87 // Set the shared ProtectionDomain atomically
  88 void ModuleEntry::set_shared_protection_domain(ClassLoaderData *loader_data,
  89                                                Handle pd_h) {
  90   // Create a handle for the shared ProtectionDomain and save it atomically.
  91   // If someone beats us setting the _pd cache, the created handle is destroyed.
  92   jobject obj = loader_data->add_handle(pd_h);
  93   if (Atomic::cmpxchg_ptr(obj, &_pd, NULL) != NULL) {
  94     loader_data->remove_handle_unsafe(obj);
  95   }
  96 }
  97 
  98 // Returns true if this module can read module m
  99 bool ModuleEntry::can_read(ModuleEntry* m) const {
 100   assert(m != NULL, "No module to lookup in this module's reads list");
 101 
 102   // Unnamed modules read everyone and all modules
 103   // read java.base.  If either of these conditions
 104   // hold, readability has been established.
 105   if (!this->is_named() ||
 106       (m == ModuleEntryTable::javabase_moduleEntry())) {
 107     return true;
 108   }
 109 
 110   MutexLocker m1(Module_lock);
 111   // This is a guard against possible race between agent threads that redefine
 112   // or retransform classes in this module. Only one of them is adding the
 113   // default read edges to the unnamed modules of the boot and app class loaders
 114   // with an upcall to jdk.internal.module.Modules.transformedByAgent.
 115   // At the same time, another thread can instrument the module classes by
 116   // injecting dependencies that require the default read edges for resolution.
 117   if (this->has_default_read_edges() && !m->is_named()) {
 118     ClassLoaderData* cld = m->loader_data();
 119     if (cld->is_the_null_class_loader_data() || cld->is_system_class_loader_data()) {
 120       return true; // default read edge
 121     }
 122   }
 123   if (!has_reads_list()) {
 124     return false;
 125   } else {
 126     return _reads->contains(m);
 127   }
 128 }
 129 
 130 // Add a new module to this module's reads list
 131 void ModuleEntry::add_read(ModuleEntry* m) {
 132   // Unnamed module is special cased and can read all modules
 133   if (!is_named()) {
 134     return;
 135   }
 136 
 137   MutexLocker m1(Module_lock);
 138   if (m == NULL) {
 139     set_can_read_all_unnamed();
 140   } else {
 141     if (_reads == NULL) {
 142       // Lazily create a module's reads list
 143       _reads = new (ResourceObj::C_HEAP, mtModule)GrowableArray<ModuleEntry*>(MODULE_READS_SIZE, true);
 144     }
 145 
 146     // Determine, based on this newly established read edge to module m,
 147     // if this module's read list should be walked at a GC safepoint.
 148     set_read_walk_required(m->loader_data());
 149 
 150     // Establish readability to module m
 151     _reads->append_if_missing(m);
 152   }
 153 }
 154 
 155 // If the module's loader, that a read edge is being established to, is
 156 // not the same loader as this module's and is not one of the 3 builtin
 157 // class loaders, then this module's reads list must be walked at GC
 158 // safepoint. Modules have the same life cycle as their defining class
 159 // loaders and should be removed if dead.
 160 void ModuleEntry::set_read_walk_required(ClassLoaderData* m_loader_data) {
 161   assert(is_named(), "Cannot call set_read_walk_required on unnamed module");
 162   assert_locked_or_safepoint(Module_lock);
 163   if (!_must_walk_reads &&
 164       loader_data() != m_loader_data &&
 165       !m_loader_data->is_builtin_class_loader_data()) {
 166     _must_walk_reads = true;
 167     if (log_is_enabled(Trace, module)) {
 168       ResourceMark rm;
 169       log_trace(module)("ModuleEntry::set_read_walk_required(): module %s reads list must be walked",
 170                         (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE);
 171     }
 172   }
 173 }
 174 
 175 // Set whether the module is open, i.e. all its packages are unqualifiedly exported
 176 void ModuleEntry::set_is_open(bool is_open) {
 177   assert_lock_strong(Module_lock);
 178   _is_open = is_open;
 179 }
 180 
 181 // Returns true if the module has a non-empty reads list. As such, the unnamed
 182 // module will return false.
 183 bool ModuleEntry::has_reads_list() const {
 184   assert_locked_or_safepoint(Module_lock);
 185   return ((_reads != NULL) && !_reads->is_empty());
 186 }
 187 
 188 // Purge dead module entries out of reads list.
 189 void ModuleEntry::purge_reads() {
 190   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 191 
 192   if (_must_walk_reads && has_reads_list()) {
 193     // This module's _must_walk_reads flag will be reset based
 194     // on the remaining live modules on the reads list.
 195     _must_walk_reads = false;
 196 
 197     if (log_is_enabled(Trace, module)) {
 198       ResourceMark rm;
 199       log_trace(module)("ModuleEntry::purge_reads(): module %s reads list being walked",
 200                         (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE);
 201     }
 202 
 203     // Go backwards because this removes entries that are dead.
 204     int len = _reads->length();
 205     for (int idx = len - 1; idx >= 0; idx--) {
 206       ModuleEntry* module_idx = _reads->at(idx);
 207       ClassLoaderData* cld_idx = module_idx->loader_data();
 208       if (cld_idx->is_unloading()) {
 209         _reads->delete_at(idx);
 210       } else {
 211         // Update the need to walk this module's reads based on live modules
 212         set_read_walk_required(cld_idx);
 213       }
 214     }
 215   }
 216 }
 217 
 218 void ModuleEntry::module_reads_do(ModuleClosure* const f) {
 219   assert_locked_or_safepoint(Module_lock);
 220   assert(f != NULL, "invariant");
 221 
 222   if (has_reads_list()) {
 223     int reads_len = _reads->length();
 224     for (int i = 0; i < reads_len; ++i) {
 225       f->do_module(_reads->at(i));
 226     }
 227   }
 228 }
 229 
 230 void ModuleEntry::delete_reads() {
 231   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 232   delete _reads;
 233   _reads = NULL;
 234 }
 235 
 236 ModuleEntry* ModuleEntry::create_unnamed_module(ClassLoaderData* cld) {
 237   // The java.lang.Module for this loader's
 238   // corresponding unnamed module can be found in the java.lang.ClassLoader object.
 239   oop module = java_lang_ClassLoader::unnamedModule(cld->class_loader());
 240   ModuleEntry* unnamed_module = new_unnamed_module_entry(Handle(Thread::current(), module), cld);
 241 
 242   // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module
 243   // object.
 244   java_lang_Module::set_module_entry(module, unnamed_module);
 245 
 246   return unnamed_module;
 247 }
 248 
 249 ModuleEntry* ModuleEntry::create_boot_unnamed_module(ClassLoaderData* cld) {
 250   // For the boot loader, the java.lang.Module for the unnamed module
 251   // is not known until a call to JVM_SetBootLoaderUnnamedModule is made. At
 252   // this point initially create the ModuleEntry for the unnamed module.
 253   ModuleEntry* unnamed_module = new_unnamed_module_entry(Handle(), cld);
 254   assert(unnamed_module != NULL, "boot loader unnamed module should not be null");
 255   return unnamed_module;
 256 }
 257 
 258 // When creating an unnamed module, this is called without holding the Module_lock.
 259 // This is okay because the unnamed module gets created before the ClassLoaderData
 260 // is available to other threads.
 261 ModuleEntry* ModuleEntry::new_unnamed_module_entry(Handle module_handle, ClassLoaderData* cld) {
 262   ModuleEntry* entry = (ModuleEntry*) NEW_C_HEAP_ARRAY(char, sizeof(ModuleEntry), mtModule);
 263 
 264   // Initialize everything BasicHashtable would
 265   entry->set_next(NULL);
 266   entry->set_hash(0);
 267   entry->set_literal(NULL);
 268 
 269   // Initialize fields specific to a ModuleEntry
 270   entry->init();
 271 
 272   // Unnamed modules can read all other unnamed modules.
 273   entry->set_can_read_all_unnamed();
 274 
 275   if (!module_handle.is_null()) {
 276     entry->set_module(cld->add_handle(module_handle));
 277   }
 278 
 279   entry->set_loader_data(cld);
 280   entry->_is_open = true;
 281 
 282   TRACE_INIT_ID(entry);
 283 
 284   return entry;
 285 }
 286 
 287 void ModuleEntry::delete_unnamed_module() {
 288   // Do not need unlink_entry() since the unnamed module is not in the hashtable
 289   FREE_C_HEAP_ARRAY(char, this);
 290 }
 291 
 292 ModuleEntryTable::ModuleEntryTable(int table_size)
 293   : Hashtable<Symbol*, mtModule>(table_size, sizeof(ModuleEntry))
 294 {
 295 }
 296 
 297 ModuleEntryTable::~ModuleEntryTable() {
 298   assert_locked_or_safepoint(Module_lock);
 299 
 300   // Walk through all buckets and all entries in each bucket,
 301   // freeing each entry.
 302   for (int i = 0; i < table_size(); ++i) {
 303     for (ModuleEntry* m = bucket(i); m != NULL;) {
 304       ModuleEntry* to_remove = m;
 305       // read next before freeing.
 306       m = m->next();
 307 
 308       ResourceMark rm;
 309       if (to_remove->name() != NULL) {
 310         log_info(module, unload)("unloading module %s", to_remove->name()->as_C_string());
 311       }
 312       log_debug(module)("ModuleEntryTable: deleting module: %s", to_remove->name() != NULL ?
 313                         to_remove->name()->as_C_string() : UNNAMED_MODULE);
 314 
 315       // Clean out the C heap allocated reads list first before freeing the entry
 316       to_remove->delete_reads();
 317       if (to_remove->name() != NULL) {
 318         to_remove->name()->decrement_refcount();
 319       }
 320       if (to_remove->version() != NULL) {
 321         to_remove->version()->decrement_refcount();
 322       }
 323       if (to_remove->location() != NULL) {
 324         to_remove->location()->decrement_refcount();
 325       }
 326 
 327       // Unlink from the Hashtable prior to freeing
 328       unlink_entry(to_remove);
 329       FREE_C_HEAP_ARRAY(char, to_remove);
 330     }
 331   }
 332   assert(number_of_entries() == 0, "should have removed all entries");
 333   assert(new_entry_free_list() == NULL, "entry present on ModuleEntryTable's free list");
 334   free_buckets();
 335 }
 336 
 337 ModuleEntry* ModuleEntryTable::new_entry(unsigned int hash, Handle module_handle,
 338                                          bool is_open, Symbol* name,
 339                                          Symbol* version, Symbol* location,
 340                                          ClassLoaderData* loader_data) {
 341   assert(Module_lock->owned_by_self(), "should have the Module_lock");
 342   ModuleEntry* entry = (ModuleEntry*)Hashtable<Symbol*, mtModule>::allocate_new_entry(hash, name);
 343 
 344   // Initialize fields specific to a ModuleEntry
 345   entry->init();
 346   if (name != NULL) {
 347     name->increment_refcount();
 348   } else {
 349     // Unnamed modules can read all other unnamed modules.
 350     entry->set_can_read_all_unnamed();
 351   }
 352 
 353   if (!module_handle.is_null()) {
 354     entry->set_module(loader_data->add_handle(module_handle));
 355   }
 356 
 357   entry->set_loader_data(loader_data);
 358   entry->set_version(version);
 359   entry->set_location(location);
 360   entry->set_is_open(is_open);
 361 
 362   if (ClassLoader::is_in_patch_mod_entries(name)) {
 363     entry->set_is_patched();
 364     if (log_is_enabled(Trace, module, patch)) {
 365       ResourceMark rm;
 366       log_trace(module, patch)("Marked module %s as patched from --patch-module", name->as_C_string());
 367     }
 368   }
 369 
 370   TRACE_INIT_ID(entry);
 371 
 372   return entry;
 373 }
 374 
 375 void ModuleEntryTable::add_entry(int index, ModuleEntry* new_entry) {
 376   assert(Module_lock->owned_by_self(), "should have the Module_lock");
 377   Hashtable<Symbol*, mtModule>::add_entry(index, (HashtableEntry<Symbol*, mtModule>*)new_entry);
 378 }
 379 
 380 ModuleEntry* ModuleEntryTable::locked_create_entry_or_null(Handle module_handle,
 381                                                            bool is_open,
 382                                                            Symbol* module_name,
 383                                                            Symbol* module_version,
 384                                                            Symbol* module_location,
 385                                                            ClassLoaderData* loader_data) {
 386   assert(module_name != NULL, "ModuleEntryTable locked_create_entry_or_null should never be called for unnamed module.");
 387   assert(Module_lock->owned_by_self(), "should have the Module_lock");
 388   // Check if module already exists.
 389   if (lookup_only(module_name) != NULL) {
 390     return NULL;
 391   } else {
 392     ModuleEntry* entry = new_entry(compute_hash(module_name), module_handle, is_open, module_name,
 393                                    module_version, module_location, loader_data);
 394     add_entry(index_for(module_name), entry);
 395     return entry;
 396   }
 397 }
 398 
 399 // lookup_only by Symbol* to find a ModuleEntry.
 400 ModuleEntry* ModuleEntryTable::lookup_only(Symbol* name) {
 401   assert(name != NULL, "name cannot be NULL");
 402   int index = index_for(name);
 403   for (ModuleEntry* m = bucket(index); m != NULL; m = m->next()) {
 404     if (m->name()->fast_compare(name) == 0) {
 405       return m;
 406     }
 407   }
 408   return NULL;
 409 }
 410 
 411 // Remove dead modules from all other alive modules' reads list.
 412 // This should only occur at class unloading.
 413 void ModuleEntryTable::purge_all_module_reads() {
 414   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 415   for (int i = 0; i < table_size(); i++) {
 416     for (ModuleEntry* entry = bucket(i);
 417                       entry != NULL;
 418                       entry = entry->next()) {
 419       entry->purge_reads();
 420     }
 421   }
 422 }
 423 
 424 void ModuleEntryTable::finalize_javabase(Handle module_handle, Symbol* version, Symbol* location) {
 425   assert(Module_lock->owned_by_self(), "should have the Module_lock");
 426   ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data();
 427   ModuleEntryTable* module_table = boot_loader_data->modules();
 428 
 429   assert(module_table != NULL, "boot loader's ModuleEntryTable not defined");
 430 
 431   if (module_handle.is_null()) {
 432     fatal("Unable to finalize module definition for " JAVA_BASE_NAME);
 433   }
 434 
 435   // Set java.lang.Module, version and location for java.base
 436   ModuleEntry* jb_module = javabase_moduleEntry();
 437   assert(jb_module != NULL, JAVA_BASE_NAME " ModuleEntry not defined");
 438   jb_module->set_version(version);
 439   jb_module->set_location(location);
 440   // Once java.base's ModuleEntry _module field is set with the known
 441   // java.lang.Module, java.base is considered "defined" to the VM.
 442   jb_module->set_module(boot_loader_data->add_handle(module_handle));
 443 
 444   // Store pointer to the ModuleEntry for java.base in the java.lang.Module object.
 445   java_lang_Module::set_module_entry(module_handle(), jb_module);
 446 }
 447 
 448 // Within java.lang.Class instances there is a java.lang.Module field that must
 449 // be set with the defining module.  During startup, prior to java.base's definition,
 450 // classes needing their module field set are added to the fixup_module_list.
 451 // Their module field is set once java.base's java.lang.Module is known to the VM.
 452 void ModuleEntryTable::patch_javabase_entries(Handle module_handle) {
 453   if (module_handle.is_null()) {
 454     fatal("Unable to patch the module field of classes loaded prior to "
 455           JAVA_BASE_NAME "'s definition, invalid java.lang.Module");
 456   }
 457 
 458   // Do the fixups for the basic primitive types
 459   java_lang_Class::set_module(Universe::int_mirror(), module_handle());
 460   java_lang_Class::set_module(Universe::float_mirror(), module_handle());
 461   java_lang_Class::set_module(Universe::double_mirror(), module_handle());
 462   java_lang_Class::set_module(Universe::byte_mirror(), module_handle());
 463   java_lang_Class::set_module(Universe::bool_mirror(), module_handle());
 464   java_lang_Class::set_module(Universe::char_mirror(), module_handle());
 465   java_lang_Class::set_module(Universe::long_mirror(), module_handle());
 466   java_lang_Class::set_module(Universe::short_mirror(), module_handle());
 467   java_lang_Class::set_module(Universe::void_mirror(), module_handle());
 468 
 469   // Do the fixups for classes that have already been created.
 470   GrowableArray <Klass*>* list = java_lang_Class::fixup_module_field_list();
 471   int list_length = list->length();
 472   for (int i = 0; i < list_length; i++) {
 473     Klass* k = list->at(i);
 474     assert(k->is_klass(), "List should only hold classes");
 475     java_lang_Class::fixup_module_field(k, module_handle);
 476     k->class_loader_data()->dec_keep_alive();
 477   }
 478 
 479   delete java_lang_Class::fixup_module_field_list();
 480   java_lang_Class::set_fixup_module_field_list(NULL);
 481 }
 482 
 483 void ModuleEntryTable::print(outputStream* st) {
 484   st->print_cr("Module Entry Table (table_size=%d, entries=%d)",
 485                table_size(), number_of_entries());
 486   for (int i = 0; i < table_size(); i++) {
 487     for (ModuleEntry* probe = bucket(i);
 488                               probe != NULL;
 489                               probe = probe->next()) {
 490       probe->print(st);
 491     }
 492   }
 493 }
 494 
 495 void ModuleEntry::print(outputStream* st) {
 496   ResourceMark rm;
 497   st->print_cr("entry " PTR_FORMAT " name %s module " PTR_FORMAT " loader %s version %s location %s strict %s next " PTR_FORMAT,
 498                p2i(this),
 499                name() == NULL ? UNNAMED_MODULE : name()->as_C_string(),
 500                p2i(module()),
 501                loader_data()->loader_name(),
 502                version() != NULL ? version()->as_C_string() : "NULL",
 503                location() != NULL ? location()->as_C_string() : "NULL",
 504                BOOL_TO_STR(!can_read_all_unnamed()), p2i(next()));
 505 }
 506 
 507 void ModuleEntryTable::verify() {
 508   verify_table<ModuleEntry>("Module Entry Table");
 509 }
 510 
 511 void ModuleEntry::verify() {
 512   guarantee(loader_data() != NULL, "A module entry must be associated with a loader.");
 513 }