1 /*
   2 * Copyright (c) 2016, 2024, 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 "cds/archiveBuilder.hpp"
  27 #include "cds/cdsConfig.hpp"
  28 #include "cds/metaspaceShared.hpp"
  29 #include "classfile/classFileParser.hpp"
  30 #include "classfile/classLoader.hpp"
  31 #include "classfile/classLoaderData.inline.hpp"
  32 #include "classfile/classLoaderDataShared.hpp"
  33 #include "classfile/classLoaderExt.hpp"
  34 #include "classfile/javaAssertions.hpp"
  35 #include "classfile/javaClasses.hpp"
  36 #include "classfile/javaClasses.inline.hpp"
  37 #include "classfile/moduleEntry.hpp"
  38 #include "classfile/modules.hpp"
  39 #include "classfile/packageEntry.hpp"
  40 #include "classfile/stringTable.hpp"
  41 #include "classfile/symbolTable.hpp"
  42 #include "classfile/systemDictionary.hpp"
  43 #include "classfile/vmClasses.hpp"
  44 #include "classfile/vmSymbols.hpp"
  45 #include "jvm.h"
  46 #include "logging/log.hpp"
  47 #include "logging/logStream.hpp"
  48 #include "memory/resourceArea.hpp"
  49 #include "prims/jvmtiExport.hpp"
  50 #include "runtime/arguments.hpp"
  51 #include "runtime/globals_extension.hpp"
  52 #include "runtime/handles.inline.hpp"
  53 #include "runtime/javaCalls.hpp"
  54 #include "runtime/jniHandles.inline.hpp"
  55 #include "utilities/formatBuffer.hpp"
  56 #include "utilities/stringUtils.hpp"
  57 #include "utilities/utf8.hpp"
  58 
  59 static bool verify_module_name(const char *module_name, int len) {
  60   assert(module_name != nullptr, "invariant");
  61   return (len > 0 && len <= Symbol::max_length());
  62 }
  63 
  64 static bool verify_package_name(const char* package_name, int len) {
  65   assert(package_name != nullptr, "Package name derived from non-null jstring can't be null");
  66   return (len > 0 && len <= Symbol::max_length() &&
  67     ClassFileParser::verify_unqualified_name(package_name, len,
  68     ClassFileParser::LegalClass));
  69 }
  70 
  71 static char* get_module_name(oop module, int& len, TRAPS) {
  72   oop name_oop = java_lang_Module::name(module);
  73   if (name_oop == nullptr) {
  74     THROW_MSG_NULL(vmSymbols::java_lang_NullPointerException(), "Null module name");
  75   }
  76   size_t utf8_len;
  77   char* module_name = java_lang_String::as_utf8_string(name_oop, utf8_len);
  78   len = checked_cast<int>(utf8_len); // module names are < 64K
  79   if (!verify_module_name(module_name, len)) {
  80     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
  81                    err_msg("Invalid module name: %s", module_name));
  82   }
  83   return module_name;
  84 }
  85 
  86 static Symbol* as_symbol(jstring str_object) {
  87   if (str_object == nullptr) {
  88     return nullptr;
  89   }
  90   size_t len;
  91   char* str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(str_object), len);
  92   return SymbolTable::new_symbol(str, checked_cast<int>(len));
  93 }
  94 
  95 ModuleEntryTable* Modules::get_module_entry_table(Handle h_loader) {
  96   // This code can be called during start-up, before the classLoader's classLoader data got
  97   // created.  So, call register_loader() to make sure the classLoader data gets created.
  98   ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader);
  99   return loader_cld->modules();
 100 }
 101 
 102 static PackageEntryTable* get_package_entry_table(Handle h_loader) {
 103   // This code can be called during start-up, before the classLoader's classLoader data got
 104   // created.  So, call register_loader() to make sure the classLoader data gets created.
 105   ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader);
 106   return loader_cld->packages();
 107 }
 108 
 109 static ModuleEntry* get_module_entry(Handle module, TRAPS) {
 110   if (!java_lang_Module::is_instance(module())) {
 111     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
 112                    "module is not an instance of type java.lang.Module");
 113   }
 114   return java_lang_Module::module_entry(module());
 115 }
 116 
 117 
 118 static PackageEntry* get_locked_package_entry(ModuleEntry* module_entry, const char* package_name, int len) {
 119   assert(Module_lock->owned_by_self(), "should have the Module_lock");
 120   assert(package_name != nullptr, "Precondition");
 121   TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name, len);
 122   PackageEntryTable* package_entry_table = module_entry->loader_data()->packages();
 123   assert(package_entry_table != nullptr, "Unexpected null package entry table");
 124   PackageEntry* package_entry = package_entry_table->locked_lookup_only(pkg_symbol);
 125   assert(package_entry == nullptr || package_entry->module() == module_entry, "Unexpectedly found a package linked to another module");
 126   return package_entry;
 127 }
 128 
 129 static PackageEntry* get_package_entry_by_name(Symbol* package, Handle h_loader) {
 130   if (package != nullptr) {
 131     PackageEntryTable* const package_entry_table =
 132       get_package_entry_table(h_loader);
 133     assert(package_entry_table != nullptr, "Unexpected null package entry table");
 134     return package_entry_table->lookup_only(package);
 135   }
 136   return nullptr;
 137 }
 138 
 139 bool Modules::is_package_defined(Symbol* package, Handle h_loader) {
 140   PackageEntry* res = get_package_entry_by_name(package, h_loader);
 141   return res != nullptr;
 142 }
 143 
 144 // Converts the String oop to an internal package
 145 // Will use the provided buffer if it's sufficiently large, otherwise allocates
 146 // a resource array
 147 // The length of the resulting string will be assigned to utf8_len
 148 static const char* as_internal_package(oop package_string, char* buf, size_t buflen, int& utf8_len) {
 149   size_t full_utf8_len;
 150   char* package_name = java_lang_String::as_utf8_string_full(package_string, buf, buflen, full_utf8_len);
 151   utf8_len = checked_cast<int>(full_utf8_len); // package names are < 64K
 152 
 153   // Turn all '/'s into '.'s
 154   for (int index = 0; index < utf8_len; index++) {
 155     if (package_name[index] == JVM_SIGNATURE_DOT) {
 156       package_name[index] = JVM_SIGNATURE_SLASH;
 157     }
 158   }
 159   return package_name;
 160 }
 161 
 162 static void define_javabase_module(Handle module_handle, jstring version, jstring location,
 163                                    objArrayHandle pkgs, int num_packages, TRAPS) {
 164   ResourceMark rm(THREAD);
 165 
 166   // Obtain java.base's module version
 167   TempNewSymbol version_symbol = as_symbol(version);
 168 
 169   // Obtain java.base's location
 170   TempNewSymbol location_symbol = as_symbol(location);
 171 
 172   // Check that the packages are syntactically ok.
 173   char buf[128];
 174   GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages);
 175   for (int x = 0; x < num_packages; x++) {
 176     oop pkg_str = pkgs->obj_at(x);
 177 
 178     if (pkg_str == nullptr || pkg_str->klass() != vmClasses::String_klass()) {
 179       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 180                 err_msg("Bad package name"));
 181     }
 182 
 183     int package_len;
 184     const char* package_name = as_internal_package(pkg_str, buf, sizeof(buf), package_len);
 185     if (!verify_package_name(package_name, package_len)) {
 186       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 187                 err_msg("Invalid package name: %s for module: " JAVA_BASE_NAME, package_name));
 188     }
 189     Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, package_len);
 190     pkg_list->append(pkg_symbol);
 191   }
 192 
 193   // Validate java_base's loader is the boot loader.
 194   oop loader = java_lang_Module::loader(module_handle());
 195   if (loader != nullptr) {
 196     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 197               "Class loader must be the boot class loader");
 198   }
 199   Handle h_loader(THREAD, loader);
 200 
 201   // Ensure the boot loader's PackageEntryTable has been created
 202   PackageEntryTable* package_table = get_package_entry_table(h_loader);
 203   assert(pkg_list->length() == 0 || package_table != nullptr, "Bad package_table");
 204 
 205   // Ensure java.base's ModuleEntry has been created
 206   assert(ModuleEntryTable::javabase_moduleEntry() != nullptr, "No ModuleEntry for " JAVA_BASE_NAME);
 207 
 208   bool duplicate_javabase = false;
 209   {
 210     MutexLocker m1(THREAD, Module_lock);
 211 
 212     if (ModuleEntryTable::javabase_defined()) {
 213       duplicate_javabase = true;
 214     } else {
 215 
 216       // Verify that all java.base packages created during bootstrapping are in
 217       // pkg_list.  If any are not in pkg_list, than a non-java.base class was
 218       // loaded erroneously pre java.base module definition.
 219       package_table->verify_javabase_packages(pkg_list);
 220 
 221       // loop through and add any new packages for java.base
 222       for (int x = 0; x < pkg_list->length(); x++) {
 223         // Some of java.base's packages were added early in bootstrapping, ignore duplicates.
 224         package_table->locked_create_entry_if_absent(pkg_list->at(x),
 225                                                      ModuleEntryTable::javabase_moduleEntry());
 226         assert(package_table->locked_lookup_only(pkg_list->at(x)) != nullptr,
 227                "Unable to create a " JAVA_BASE_NAME " package entry");
 228         // Unable to have a GrowableArray of TempNewSymbol.  Must decrement the refcount of
 229         // the Symbol* that was created above for each package. The refcount was incremented
 230         // by SymbolTable::new_symbol and as well by the PackageEntry creation.
 231         pkg_list->at(x)->decrement_refcount();
 232       }
 233 
 234       // Finish defining java.base's ModuleEntry
 235       ModuleEntryTable::finalize_javabase(module_handle, version_symbol, location_symbol);
 236     }
 237   }
 238   if (duplicate_javabase) {
 239     THROW_MSG(vmSymbols::java_lang_InternalError(),
 240               "Module " JAVA_BASE_NAME " is already defined");
 241   }
 242 
 243   // Only the thread that actually defined the base module will get here,
 244   // so no locking is needed.
 245 
 246   // Patch any previously loaded class's module field with java.base's java.lang.Module.
 247   ModuleEntryTable::patch_javabase_entries(THREAD, module_handle);
 248 
 249   log_info(module, load)(JAVA_BASE_NAME " location: %s",
 250                          location_symbol != nullptr ? location_symbol->as_C_string() : "nullptr");
 251   log_debug(module)("define_javabase_module(): Definition of module: "
 252                     JAVA_BASE_NAME ", version: %s, location: %s, package #: %d",
 253                     version_symbol != nullptr ? version_symbol->as_C_string() : "nullptr",
 254                     location_symbol != nullptr ? location_symbol->as_C_string() : "nullptr",
 255                     pkg_list->length());
 256 
 257   // packages defined to java.base
 258   if (log_is_enabled(Trace, module)) {
 259     for (int x = 0; x < pkg_list->length(); x++) {
 260       log_trace(module)("define_javabase_module(): creation of package %s for module " JAVA_BASE_NAME,
 261                         (pkg_list->at(x))->as_C_string());
 262     }
 263   }
 264 }
 265 
 266 // Caller needs ResourceMark.
 267 static void throw_dup_pkg_exception(const char* module_name, PackageEntry* package, TRAPS) {
 268   const char* package_name = package->name()->as_C_string();
 269   if (package->module()->is_named()) {
 270     THROW_MSG(vmSymbols::java_lang_IllegalStateException(),
 271       err_msg("Package %s for module %s is already in another module, %s, defined to the class loader",
 272               package_name, module_name, package->module()->name()->as_C_string()));
 273   } else {
 274     THROW_MSG(vmSymbols::java_lang_IllegalStateException(),
 275       err_msg("Package %s for module %s is already in the unnamed module defined to the class loader",
 276               package_name, module_name));
 277   }
 278 }
 279 
 280 void Modules::define_module(Handle module, jboolean is_open, jstring version,
 281                             jstring location, jobjectArray packages, TRAPS) {
 282   check_cds_restrictions(CHECK);
 283   ResourceMark rm(THREAD);
 284 
 285   if (module.is_null()) {
 286     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
 287   }
 288 
 289   if (!java_lang_Module::is_instance(module())) {
 290     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 291               "module is not an instance of type java.lang.Module");
 292   }
 293 
 294   int module_name_len;
 295   char* module_name = get_module_name(module(), module_name_len, CHECK);
 296   if (module_name == nullptr) {
 297     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 298               "Module name cannot be null");
 299   }
 300 
 301   // Resolve packages
 302   objArrayHandle packages_h(THREAD, objArrayOop(JNIHandles::resolve(packages)));
 303   int num_packages = (packages_h.is_null() ? 0 : packages_h->length());
 304 
 305   // Special handling of java.base definition
 306   if (strcmp(module_name, JAVA_BASE_NAME) == 0) {
 307     assert(is_open == JNI_FALSE, "java.base module cannot be open");
 308     define_javabase_module(module, version, location, packages_h, num_packages, CHECK);
 309     return;
 310   }
 311 
 312   oop loader = java_lang_Module::loader(module());
 313   Handle h_loader = Handle(THREAD, loader);
 314   // define_module can be called during start-up, before the class loader's ClassLoaderData
 315   // has been created.  SystemDictionary::register_loader ensures creation, if needed.
 316   ClassLoaderData* loader_data = SystemDictionary::register_loader(h_loader);
 317   assert(loader_data != nullptr, "class loader data shouldn't be null");
 318 
 319   // Only modules defined to either the boot or platform class loader, can define a "java/" package.
 320   bool java_pkg_disallowed = !h_loader.is_null() &&
 321         !SystemDictionary::is_platform_class_loader(h_loader());
 322 
 323   // Check that the list of packages has no duplicates and that the
 324   // packages are syntactically ok.
 325   char buf[128];
 326   GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages);
 327   for (int x = 0; x < num_packages; x++) {
 328     oop pkg_str = packages_h->obj_at(x);
 329     if (pkg_str == nullptr || pkg_str->klass() != vmClasses::String_klass()) {
 330       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 331                 err_msg("Bad package name"));
 332     }
 333 
 334     int package_len;
 335     const char* package_name = as_internal_package(pkg_str, buf, sizeof(buf), package_len);
 336     if (!verify_package_name(package_name, package_len)) {
 337       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 338                 err_msg("Invalid package name: %s for module: %s",
 339                         package_name, module_name));
 340     }
 341 
 342     // Only modules defined to either the boot or platform class loader, can define a "java/" package.
 343     if (java_pkg_disallowed &&
 344         (strncmp(package_name, JAVAPKG, JAVAPKG_LEN) == 0 &&
 345           (package_name[JAVAPKG_LEN] == JVM_SIGNATURE_SLASH || package_name[JAVAPKG_LEN] == '\0'))) {
 346       const char* class_loader_name = loader_data->loader_name_and_id();
 347       size_t pkg_len = strlen(package_name);
 348       char* pkg_name = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, pkg_len + 1);
 349       strncpy(pkg_name, package_name, pkg_len + 1);
 350       StringUtils::replace_no_expand(pkg_name, "/", ".");
 351       const char* msg_text1 = "Class loader (instance of): ";
 352       const char* msg_text2 = " tried to define prohibited package name: ";
 353       size_t len = strlen(msg_text1) + strlen(class_loader_name) + strlen(msg_text2) + pkg_len + 1;
 354       char* message = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
 355       jio_snprintf(message, len, "%s%s%s%s", msg_text1, class_loader_name, msg_text2, pkg_name);
 356       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), message);
 357     }
 358 
 359     Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, package_len);
 360     pkg_list->append(pkg_symbol);
 361   }
 362 
 363   ModuleEntryTable* module_table = get_module_entry_table(h_loader);
 364   assert(module_table != nullptr, "module entry table shouldn't be null");
 365 
 366   // Create symbol* entry for module name.
 367   TempNewSymbol module_symbol = SymbolTable::new_symbol(module_name, module_name_len);
 368 
 369   bool dupl_modules = false;
 370 
 371   // Create symbol for module version.
 372   TempNewSymbol version_symbol = as_symbol(version);
 373 
 374   // Create symbol* entry for module location.
 375   TempNewSymbol location_symbol = as_symbol(location);
 376 
 377   PackageEntryTable* package_table = nullptr;
 378   PackageEntry* existing_pkg = nullptr;
 379   {
 380     MutexLocker ml(THREAD, Module_lock);
 381 
 382     if (num_packages > 0) {
 383       package_table = get_package_entry_table(h_loader);
 384       assert(package_table != nullptr, "Missing package_table");
 385 
 386       // Check that none of the packages exist in the class loader's package table.
 387       for (int x = 0; x < pkg_list->length(); x++) {
 388         existing_pkg = package_table->locked_lookup_only(pkg_list->at(x));
 389         if (existing_pkg != nullptr) {
 390           // This could be because the module was already defined.  If so,
 391           // report that error instead of the package error.
 392           if (module_table->lookup_only(module_symbol) != nullptr) {
 393             dupl_modules = true;
 394           }
 395           break;
 396         }
 397       }
 398     }  // if (num_packages > 0)...
 399 
 400     // Add the module and its packages.
 401     if (!dupl_modules && existing_pkg == nullptr) {
 402       if (module_table->lookup_only(module_symbol) == nullptr) {
 403         // Create the entry for this module in the class loader's module entry table.
 404         ModuleEntry* module_entry = module_table->locked_create_entry(module,
 405                                     (is_open == JNI_TRUE), module_symbol,
 406                                     version_symbol, location_symbol, loader_data);
 407         assert(module_entry != nullptr, "module_entry creation failed");
 408 
 409         // Add the packages.
 410         assert(pkg_list->length() == 0 || package_table != nullptr, "Bad package table");
 411         for (int y = 0; y < pkg_list->length(); y++) {
 412           package_table->locked_create_entry(pkg_list->at(y), module_entry);
 413 
 414           // Unable to have a GrowableArray of TempNewSymbol.  Must decrement the refcount of
 415           // the Symbol* that was created above for each package. The refcount was incremented
 416           // by SymbolTable::new_symbol and as well by the PackageEntry creation.
 417           pkg_list->at(y)->decrement_refcount();
 418         }
 419 
 420         // Store pointer to ModuleEntry record in java.lang.Module object.
 421         java_lang_Module::set_module_entry(module(), module_entry);
 422       } else {
 423          dupl_modules = true;
 424       }
 425     }
 426   }  // Release the lock
 427 
 428   // any errors ?
 429   if (dupl_modules) {
 430      THROW_MSG(vmSymbols::java_lang_IllegalStateException(),
 431                err_msg("Module %s is already defined", module_name));
 432   } else if (existing_pkg != nullptr) {
 433       throw_dup_pkg_exception(module_name, existing_pkg, CHECK);
 434   }
 435 
 436   log_info(module, load)("%s location: %s", module_name,
 437                          location_symbol != nullptr ? location_symbol->as_C_string() : "null");
 438   LogTarget(Debug, module) lt;
 439   if (lt.is_enabled()) {
 440     LogStream ls(lt);
 441     ls.print("define_module(): creation of module: %s, version: %s, location: %s, ",
 442                  module_name, version_symbol != nullptr ? version_symbol->as_C_string() : "null",
 443                  location_symbol != nullptr ? location_symbol->as_C_string() : "null");
 444     loader_data->print_value_on(&ls);
 445     ls.print_cr(", package #: %d", pkg_list->length());
 446     for (int y = 0; y < pkg_list->length(); y++) {
 447       log_trace(module)("define_module(): creation of package %s for module %s",
 448                         (pkg_list->at(y))->as_C_string(), module_name);
 449     }
 450   }
 451 
 452   // If the module is defined to the boot loader and an exploded build is being
 453   // used, prepend <java.home>/modules/modules_name to the boot class path.
 454   if (h_loader.is_null() && !ClassLoader::has_jrt_entry()) {
 455     ClassLoader::add_to_exploded_build_list(THREAD, module_symbol);
 456   }
 457 
 458 #if COMPILER2_OR_JVMCI
 459   // Special handling of jdk.incubator.vector
 460   if (strcmp(module_name, "jdk.incubator.vector") == 0) {
 461     if (FLAG_IS_DEFAULT(EnableVectorSupport)) {
 462       FLAG_SET_DEFAULT(EnableVectorSupport, true);
 463     }
 464     if (EnableVectorSupport && FLAG_IS_DEFAULT(EnableVectorReboxing)) {
 465       FLAG_SET_DEFAULT(EnableVectorReboxing, true);
 466     }
 467     if (EnableVectorSupport && EnableVectorReboxing && FLAG_IS_DEFAULT(EnableVectorAggressiveReboxing)) {
 468       FLAG_SET_DEFAULT(EnableVectorAggressiveReboxing, true);
 469     }
 470     if (EnableVectorSupport && FLAG_IS_DEFAULT(UseVectorStubs)) {
 471       FLAG_SET_DEFAULT(UseVectorStubs, true);
 472     }
 473     log_info(compilation)("EnableVectorSupport=%s",            (EnableVectorSupport            ? "true" : "false"));
 474     log_info(compilation)("EnableVectorReboxing=%s",           (EnableVectorReboxing           ? "true" : "false"));
 475     log_info(compilation)("EnableVectorAggressiveReboxing=%s", (EnableVectorAggressiveReboxing ? "true" : "false"));
 476     log_info(compilation)("UseVectorStubs=%s",                 (UseVectorStubs                 ? "true" : "false"));
 477   }
 478 #endif // COMPILER2_OR_JVMCI
 479 }
 480 
 481 #if INCLUDE_CDS_JAVA_HEAP
 482 static bool _seen_platform_unnamed_module = false;
 483 static bool _seen_system_unnamed_module = false;
 484 
 485 // Validate the states of an java.lang.Module oop to be archived.
 486 //
 487 // Returns true iff the oop has an archived ModuleEntry.
 488 bool Modules::check_archived_module_oop(oop orig_module_obj) {
 489   assert(CDSConfig::is_dumping_full_module_graph(), "must be");
 490   assert(java_lang_Module::is_instance(orig_module_obj), "must be");
 491 
 492   ModuleEntry* orig_module_ent = java_lang_Module::module_entry_raw(orig_module_obj);
 493   if (orig_module_ent == nullptr) {
 494     // These special java.lang.Module oops are created in Java code. They are not
 495     // defined via Modules::define_module(), so they don't have a ModuleEntry:
 496     //     java.lang.Module::ALL_UNNAMED_MODULE
 497     //     java.lang.Module::EVERYONE_MODULE
 498     //     jdk.internal.loader.ClassLoaders$BootClassLoader::unnamedModule
 499     log_info(cds, module)("Archived java.lang.Module oop " PTR_FORMAT " with no ModuleEntry*", p2i(orig_module_obj));
 500     assert(java_lang_Module::name(orig_module_obj) == nullptr, "must be unnamed");
 501     return false;
 502   } else {
 503     // This java.lang.Module oop has an ModuleEntry*. Check if the latter is archived.
 504     if (log_is_enabled(Info, cds, module)) {
 505       ResourceMark rm;
 506       LogStream ls(Log(cds, module)::info());
 507       ls.print("Archived java.lang.Module oop " PTR_FORMAT " for ", p2i(orig_module_obj));
 508       orig_module_ent->print(&ls);
 509     }
 510 
 511     // We only archive the default module graph, which should contain only java.lang.Module oops
 512     // for the 3 built-in loaders (boot/platform/system)
 513     ClassLoaderData* loader_data = orig_module_ent->loader_data();
 514     assert(loader_data->is_builtin_class_loader_data(), "must be");
 515 
 516     if (orig_module_ent->name() != nullptr) {
 517       // For each named module, we archive both the java.lang.Module oop and the ModuleEntry.
 518       assert(orig_module_ent->has_been_archived(), "sanity");
 519       return true;
 520     } else {
 521       // We only archive two unnamed module oops (for platform and system loaders). These do NOT have an archived
 522       // ModuleEntry.
 523       //
 524       // At runtime, these oops are fetched from java_lang_ClassLoader::unnamedModule(loader) and
 525       // are initialized in ClassLoaderData::ClassLoaderData() => ModuleEntry::create_unnamed_module(), where
 526       // a new ModuleEntry is allocated.
 527       assert(!loader_data->is_boot_class_loader_data(), "unnamed module for boot loader should be not archived");
 528       assert(!orig_module_ent->has_been_archived(), "sanity");
 529 
 530       if (SystemDictionary::is_platform_class_loader(loader_data->class_loader())) {
 531         assert(!_seen_platform_unnamed_module, "only once");
 532         _seen_platform_unnamed_module = true;
 533       } else if (SystemDictionary::is_system_class_loader(loader_data->class_loader())) {
 534         assert(!_seen_system_unnamed_module, "only once");
 535         _seen_system_unnamed_module = true;
 536       } else {
 537         // The java.lang.Module oop and ModuleEntry of the unnamed module of the boot loader are
 538         // not in the archived module graph. These are always allocated at runtime.
 539         ShouldNotReachHere();
 540       }
 541       return false;
 542     }
 543   }
 544 }
 545 
 546 void Modules::update_oops_in_archived_module(oop orig_module_obj, int archived_module_root_index) {
 547   // This java.lang.Module oop must have an archived ModuleEntry
 548   assert(check_archived_module_oop(orig_module_obj) == true, "sanity");
 549 
 550   // We remember the oop inside the ModuleEntry::_archived_module_index. At runtime, we use
 551   // this index to reinitialize the ModuleEntry inside ModuleEntry::restore_archived_oops().
 552   //
 553   // ModuleEntry::verify_archived_module_entries(), called below, ensures that every archived
 554   // ModuleEntry has been assigned an _archived_module_index.
 555   ModuleEntry* orig_module_ent = java_lang_Module::module_entry_raw(orig_module_obj);
 556   ModuleEntry::get_archived_entry(orig_module_ent)->update_oops_in_archived_module(archived_module_root_index);
 557 }
 558 
 559 void Modules::verify_archived_modules() {
 560   ModuleEntry::verify_archived_module_entries();
 561 }
 562 
 563 char* Modules::_archived_main_module_name = nullptr;
 564 char* Modules::_archived_addmods_names = nullptr;
 565 char* Modules::_archived_native_access_flags = nullptr;
 566 
 567 void Modules::dump_main_module_name() {
 568   const char* module_name = Arguments::get_property("jdk.module.main");
 569   if (module_name != nullptr) {
 570     _archived_main_module_name = ArchiveBuilder::current()->ro_strdup(module_name);
 571   }
 572 }
 573 
 574 void Modules::check_archived_flag_consistency(char* archived_flag, const char* runtime_flag, const char* property) {
 575   log_info(cds)("%s %s", property,
 576     archived_flag != nullptr ? archived_flag : "(null)");
 577   bool disable = false;
 578   if (runtime_flag == nullptr) {
 579     if (archived_flag != nullptr) {
 580       log_info(cds)("Mismatched values for property %s: %s specified during dump time but not during runtime", property, archived_flag);
 581       disable = true;
 582     }
 583   } else {
 584     if (archived_flag == nullptr) {
 585       log_info(cds)("Mismatched values for property %s: %s specified during runtime but not during dump time", property, runtime_flag);
 586       disable = true;
 587     } else if (strcmp(runtime_flag, archived_flag) != 0) {
 588       log_info(cds)("Mismatched values for property %s: runtime %s dump time %s", property, runtime_flag, archived_flag);
 589       disable = true;
 590     }
 591   }
 592 
 593   if (disable) {
 594     log_info(cds)("Disabling optimized module handling");
 595     CDSConfig::stop_using_optimized_module_handling();
 596   }
 597   log_info(cds)("optimized module handling: %s", CDSConfig::is_using_optimized_module_handling() ? "enabled" : "disabled");
 598   log_info(cds)("full module graph: %s", CDSConfig::is_using_full_module_graph() ? "enabled" : "disabled");
 599 }
 600 
 601 void Modules::dump_archived_module_info() {
 602   // Write module name into archive
 603   CDS_JAVA_HEAP_ONLY(Modules::dump_main_module_name();)
 604   // Write module names from --add-modules into archive
 605   CDS_JAVA_HEAP_ONLY(Modules::dump_addmods_names();)
 606   // Write native enable-native-access flag into archive
 607   CDS_JAVA_HEAP_ONLY(Modules::dump_native_access_flag());
 608 }
 609 
 610 void Modules::serialize_archived_module_info(SerializeClosure* soc) {
 611   CDS_JAVA_HEAP_ONLY(Modules::serialize(soc);)
 612   CDS_JAVA_HEAP_ONLY(Modules::serialize_addmods_names(soc);)
 613   CDS_JAVA_HEAP_ONLY(Modules::serialize_native_access_flags(soc);)
 614 }
 615 
 616 void Modules::serialize(SerializeClosure* soc) {
 617   soc->do_ptr(&_archived_main_module_name);
 618   if (soc->reading()) {
 619     const char* runtime_main_module = Arguments::get_property("jdk.module.main");
 620     log_info(cds)("_archived_main_module_name %s",
 621       _archived_main_module_name != nullptr ? _archived_main_module_name : "(null)");
 622 
 623     check_archived_flag_consistency(_archived_main_module_name, runtime_main_module, "jdk.module.main");
 624 
 625     // Don't hold onto the pointer, in case we might decide to unmap the archive.
 626     _archived_main_module_name = nullptr;
 627   }
 628 }
 629 
 630 void Modules::dump_native_access_flag() {
 631   const char* native_access_names = get_native_access_flags_as_sorted_string();
 632   if (native_access_names != nullptr) {
 633     _archived_native_access_flags = ArchiveBuilder::current()->ro_strdup(native_access_names);
 634   }
 635 }
 636 
 637 const char* Modules::get_native_access_flags_as_sorted_string() {
 638   return get_numbered_property_as_sorted_string("jdk.module.enable.native.access");
 639 }
 640 
 641 void Modules::serialize_native_access_flags(SerializeClosure* soc) {
 642   soc->do_ptr(&_archived_native_access_flags);
 643   if (soc->reading()) {
 644     check_archived_flag_consistency(_archived_native_access_flags, get_native_access_flags_as_sorted_string(), "jdk.module.enable.native.access");
 645 
 646     // Don't hold onto the pointer, in case we might decide to unmap the archive.
 647     _archived_native_access_flags = nullptr;
 648   }
 649 }
 650 
 651 void Modules::dump_addmods_names() {
 652   const char* addmods_names = get_addmods_names_as_sorted_string();
 653   if (addmods_names != nullptr) {
 654     _archived_addmods_names = ArchiveBuilder::current()->ro_strdup(addmods_names);
 655   }
 656 }
 657 
 658 const char* Modules::get_addmods_names_as_sorted_string() {
 659   return get_numbered_property_as_sorted_string("jdk.module.addmods");
 660 }
 661 
 662 void Modules::serialize_addmods_names(SerializeClosure* soc) {
 663   soc->do_ptr(&_archived_addmods_names);
 664   if (soc->reading()) {
 665     check_archived_flag_consistency(_archived_addmods_names, get_addmods_names_as_sorted_string(), "jdk.module.addmods");
 666 
 667     // Don't hold onto the pointer, in case we might decide to unmap the archive.
 668     _archived_addmods_names = nullptr;
 669   }
 670 }
 671 
 672 const char* Modules::get_numbered_property_as_sorted_string(const char* property) {
 673   ResourceMark rm;
 674   // theoretical string size limit for decimal int, but the following loop will end much sooner due to
 675   // OS command-line size limit.
 676   const int max_digits = 10;
 677   const int extra_symbols_count = 2; // includes '.', '\0'
 678   size_t prop_len = strlen(property) + max_digits + extra_symbols_count;
 679   char* prop_name = resource_allocate_bytes(prop_len);
 680   GrowableArray<const char*> list;
 681   for (unsigned int i = 0;; i++) {
 682     jio_snprintf(prop_name, prop_len, "%s.%d", property, i);
 683     const char* prop_value = Arguments::get_property(prop_name);
 684     if (prop_value == nullptr) {
 685       break;
 686     }
 687     char* p = resource_allocate_bytes(strlen(prop_value) + 1);
 688     strcpy(p, prop_value);
 689     while (*p == ',') p++; // skip leading commas
 690     while (*p) {
 691       char* next = strchr(p, ',');
 692       if (next == nullptr) {
 693         // no more commas, p is the last element
 694         list.append(p);
 695         break;
 696       } else {
 697         *next = 0;
 698         list.append(p);
 699         p = next + 1;
 700       }
 701     }
 702   }
 703 
 704   // Example:
 705   // --add-modules=java.compiler --add-modules=java.base,java.base,,
 706   //
 707   // list[0] = "java.compiler"
 708   // list[1] = "java.base"
 709   // list[2] = "java.base"
 710   // list[3] = ""
 711   // list[4] = ""
 712   list.sort(ClassLoaderExt::compare_module_names);
 713 
 714   const char* prefix = "";
 715   stringStream st;
 716   const char* last_string = ""; // This also filters out all empty strings
 717   for (int i = 0; i < list.length(); i++) {
 718     const char* m = list.at(i);
 719     if (strcmp(m, last_string) != 0) { // filter out duplicates
 720       st.print("%s%s", prefix, m);
 721       last_string = m;
 722       prefix = ",";
 723     }
 724   }
 725 
 726   return (st.size() > 0) ? os::strdup(st.as_string()) : nullptr;  // Example: "java.base,java.compiler"
 727 }
 728 
 729 void Modules::define_archived_modules(Handle h_platform_loader, Handle h_system_loader, TRAPS) {
 730   assert(CDSConfig::is_using_full_module_graph(), "must be");
 731 
 732   // We don't want the classes used by the archived full module graph to be redefined by JVMTI.
 733   // Luckily, such classes are loaded in the JVMTI "early" phase, and CDS is disabled if a JVMTI
 734   // agent wants to redefine classes in this phase.
 735   JVMTI_ONLY(assert(JvmtiExport::is_early_phase(), "must be"));
 736   assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()),
 737          "CDS should be disabled if early class hooks are enabled");
 738 
 739   Handle java_base_module(THREAD, ClassLoaderDataShared::restore_archived_oops_for_null_class_loader_data());
 740   // Patch any previously loaded class's module field with java.base's java.lang.Module.
 741   ModuleEntryTable::patch_javabase_entries(THREAD, java_base_module);
 742 
 743   if (h_platform_loader.is_null()) {
 744     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null platform loader object");
 745   }
 746 
 747   if (h_system_loader.is_null()) {
 748     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null system loader object");
 749   }
 750 
 751   ClassLoaderData* platform_loader_data = SystemDictionary::register_loader(h_platform_loader);
 752   SystemDictionary::set_platform_loader(platform_loader_data);
 753   ClassLoaderDataShared::restore_java_platform_loader_from_archive(platform_loader_data);
 754 
 755   ClassLoaderData* system_loader_data = SystemDictionary::register_loader(h_system_loader);
 756   SystemDictionary::set_system_loader(system_loader_data);
 757   // system_loader_data here is always an instance of jdk.internal.loader.ClassLoader$AppClassLoader.
 758   // However, if -Djava.system.class.loader=xxx is specified, java_platform_loader() would
 759   // be an instance of a user-defined class, so make sure this never happens.
 760   assert(Arguments::get_property("java.system.class.loader") == nullptr,
 761            "archived full module should have been disabled if -Djava.system.class.loader is specified");
 762   ClassLoaderDataShared::restore_java_system_loader_from_archive(system_loader_data);
 763 }
 764 
 765 void Modules::check_cds_restrictions(TRAPS) {
 766   if (CDSConfig::is_dumping_full_module_graph() && Universe::is_module_initialized()) {
 767     THROW_MSG(vmSymbols::java_lang_UnsupportedOperationException(),
 768               "During -Xshare:dump, module system cannot be modified after it's initialized");
 769   }
 770 }
 771 #endif // INCLUDE_CDS_JAVA_HEAP
 772 
 773 void Modules::set_bootloader_unnamed_module(Handle module, TRAPS) {
 774   ResourceMark rm(THREAD);
 775 
 776   if (module.is_null()) {
 777     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
 778   }
 779   if (!java_lang_Module::is_instance(module())) {
 780     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 781               "module is not an instance of type java.lang.Module");
 782   }
 783 
 784   // Ensure that this is an unnamed module
 785   oop name = java_lang_Module::name(module());
 786   if (name != nullptr) {
 787     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 788               "boot loader's unnamed module's java.lang.Module has a name");
 789   }
 790 
 791   // Validate java_base's loader is the boot loader.
 792   oop loader = java_lang_Module::loader(module());
 793   if (loader != nullptr) {
 794     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 795               "Class loader must be the boot class loader");
 796   }
 797 
 798   log_debug(module)("set_bootloader_unnamed_module(): recording unnamed module for boot loader");
 799 
 800   // Set java.lang.Module for the boot loader's unnamed module
 801   ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data();
 802   ModuleEntry* unnamed_module = boot_loader_data->unnamed_module();
 803   assert(unnamed_module != nullptr, "boot loader's unnamed ModuleEntry not defined");
 804   unnamed_module->set_module(boot_loader_data->add_handle(module));
 805   // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object.
 806   java_lang_Module::set_module_entry(module(), unnamed_module);
 807 }
 808 
 809 void Modules::add_module_exports(Handle from_module, jstring package_name, Handle to_module, TRAPS) {
 810   check_cds_restrictions(CHECK);
 811 
 812   if (package_name == nullptr) {
 813     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 814               "package is null");
 815   }
 816   if (from_module.is_null()) {
 817     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 818               "from_module is null");
 819   }
 820   ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
 821   if (from_module_entry == nullptr) {
 822     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 823               "from_module cannot be found");
 824   }
 825 
 826   // All packages in unnamed and open modules are exported by default.
 827   if (!from_module_entry->is_named() || from_module_entry->is_open()) return;
 828 
 829   ModuleEntry* to_module_entry;
 830   if (to_module.is_null()) {
 831     to_module_entry = nullptr;  // It's an unqualified export.
 832   } else {
 833     to_module_entry = get_module_entry(to_module, CHECK);
 834     if (to_module_entry == nullptr) {
 835       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 836                 "to_module is invalid");
 837     }
 838   }
 839 
 840   PackageEntry* package_entry = nullptr;
 841   char buf[128];
 842   int package_len;
 843 
 844   ResourceMark rm(THREAD);
 845   const char* pkg = as_internal_package(JNIHandles::resolve_non_null(package_name), buf, sizeof(buf), package_len);
 846   {
 847     MutexLocker ml(THREAD, Module_lock);
 848     package_entry = get_locked_package_entry(from_module_entry, pkg, package_len);
 849     // Do nothing if modules are the same
 850     // If the package is not found we'll throw an exception later
 851     if (from_module_entry != to_module_entry &&
 852         package_entry != nullptr) {
 853       package_entry->set_exported(to_module_entry);
 854     }
 855   }
 856 
 857   // Handle errors and logging outside locked section
 858   if (package_entry == nullptr) {
 859     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 860               err_msg("Package %s not found in from_module %s",
 861                       pkg != nullptr ? pkg : "",
 862                       from_module_entry->name()->as_C_string()));
 863   }
 864 
 865   if (log_is_enabled(Debug, module)) {
 866     log_debug(module)("add_module_exports(): package %s in module %s is exported to module %s",
 867                       package_entry->name()->as_C_string(),
 868                       from_module_entry->name()->as_C_string(),
 869                       to_module_entry == nullptr ? "null" :
 870                       to_module_entry->is_named() ?
 871                       to_module_entry->name()->as_C_string() : UNNAMED_MODULE);
 872   }
 873 }
 874 
 875 
 876 void Modules::add_module_exports_qualified(Handle from_module, jstring package,
 877                                            Handle to_module, TRAPS) {
 878   check_cds_restrictions(CHECK);
 879   if (to_module.is_null()) {
 880     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 881               "to_module is null");
 882   }
 883   add_module_exports(from_module, package, to_module, CHECK);
 884 }
 885 
 886 void Modules::add_reads_module(Handle from_module, Handle to_module, TRAPS) {
 887   check_cds_restrictions(CHECK);
 888   if (from_module.is_null()) {
 889     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 890               "from_module is null");
 891   }
 892 
 893   ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
 894   if (from_module_entry == nullptr) {
 895     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 896               "from_module is not valid");
 897   }
 898 
 899   ModuleEntry* to_module_entry;
 900   if (!to_module.is_null()) {
 901     to_module_entry = get_module_entry(to_module, CHECK);
 902     if (to_module_entry == nullptr) {
 903       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 904                 "to_module is invalid");
 905     }
 906   } else {
 907     to_module_entry = nullptr;
 908   }
 909 
 910   ResourceMark rm(THREAD);
 911   log_debug(module)("add_reads_module(): Adding read from module %s to module %s",
 912                     from_module_entry->is_named() ?
 913                     from_module_entry->name()->as_C_string() : UNNAMED_MODULE,
 914                     to_module_entry == nullptr ? "all unnamed" :
 915                       (to_module_entry->is_named() ?
 916                        to_module_entry->name()->as_C_string() : UNNAMED_MODULE));
 917 
 918   // if modules are the same or if from_module is unnamed then no need to add the read.
 919   if (from_module_entry != to_module_entry && from_module_entry->is_named()) {
 920     from_module_entry->add_read(to_module_entry);
 921   }
 922 }
 923 
 924 // This method is called by JFR and JNI.
 925 jobject Modules::get_module(jclass clazz, TRAPS) {
 926   assert(ModuleEntryTable::javabase_defined(),
 927          "Attempt to call get_module before " JAVA_BASE_NAME " is defined");
 928 
 929   if (clazz == nullptr) {
 930     THROW_MSG_(vmSymbols::java_lang_NullPointerException(),
 931                "class is null", nullptr);
 932   }
 933   oop mirror = JNIHandles::resolve_non_null(clazz);
 934   if (mirror == nullptr) {
 935     log_debug(module)("get_module(): no mirror, returning nullptr");
 936     return nullptr;
 937   }
 938   if (!java_lang_Class::is_instance(mirror)) {
 939     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 940                "Invalid class", nullptr);
 941   }
 942 
 943   oop module = java_lang_Class::module(mirror);
 944 
 945   assert(module != nullptr, "java.lang.Class module field not set");
 946   assert(java_lang_Module::is_instance(module), "module is not an instance of type java.lang.Module");
 947 
 948   LogTarget(Debug,module) lt;
 949   if (lt.is_enabled()) {
 950     ResourceMark rm(THREAD);
 951     LogStream ls(lt);
 952     Klass* klass = java_lang_Class::as_Klass(mirror);
 953     oop module_name = java_lang_Module::name(module);
 954     if (module_name != nullptr) {
 955       ls.print("get_module(): module ");
 956       java_lang_String::print(module_name, tty);
 957     } else {
 958       ls.print("get_module(): Unnamed Module");
 959     }
 960     if (klass != nullptr) {
 961       ls.print_cr(" for class %s", klass->external_name());
 962     } else {
 963       ls.print_cr(" for primitive class");
 964     }
 965   }
 966 
 967   return JNIHandles::make_local(THREAD, module);
 968 }
 969 
 970 oop Modules::get_named_module(Handle h_loader, const char* package_name) {
 971   assert(ModuleEntryTable::javabase_defined(),
 972          "Attempt to call get_named_module before " JAVA_BASE_NAME " is defined");
 973   assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()),
 974          "Class loader is not a subclass of java.lang.ClassLoader");
 975   assert(package_name != nullptr, "the package_name should not be null");
 976 
 977   if (strlen(package_name) == 0) {
 978     return nullptr;
 979   }
 980   TempNewSymbol package_sym = SymbolTable::new_symbol(package_name);
 981   const PackageEntry* const pkg_entry =
 982     get_package_entry_by_name(package_sym, h_loader);
 983   const ModuleEntry* const module_entry = (pkg_entry != nullptr ? pkg_entry->module() : nullptr);
 984 
 985   if (module_entry != nullptr && module_entry->module() != nullptr && module_entry->is_named()) {
 986     return module_entry->module();
 987   }
 988   return nullptr;
 989 }
 990 
 991 // Export package in module to all unnamed modules.
 992 void Modules::add_module_exports_to_all_unnamed(Handle module, jstring package_name, TRAPS) {
 993   check_cds_restrictions(CHECK);
 994   if (module.is_null()) {
 995     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 996               "module is null");
 997   }
 998   if (package_name == nullptr) {
 999     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
1000               "package is null");
1001   }
1002   ModuleEntry* module_entry = get_module_entry(module, CHECK);
1003   if (module_entry == nullptr) {
1004     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1005               "module is invalid");
1006   }
1007 
1008   // No-op for unnamed module and open modules
1009   if (!module_entry->is_named() || module_entry->is_open())
1010     return;
1011 
1012   ResourceMark rm(THREAD);
1013   char buf[128];
1014   int pkg_len;
1015   const char* pkg = as_internal_package(JNIHandles::resolve_non_null(package_name), buf, sizeof(buf), pkg_len);
1016   PackageEntry* package_entry = nullptr;
1017   {
1018     MutexLocker m1(THREAD, Module_lock);
1019     package_entry = get_locked_package_entry(module_entry, pkg, pkg_len);
1020 
1021     // Mark package as exported to all unnamed modules.
1022     if (package_entry != nullptr) {
1023       package_entry->set_is_exported_allUnnamed();
1024     }
1025   }
1026 
1027   // Handle errors and logging outside locked section
1028   if (package_entry == nullptr) {
1029     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1030               err_msg("Package %s not found in module %s",
1031                       pkg != nullptr ? pkg : "",
1032                       module_entry->name()->as_C_string()));
1033   }
1034 
1035   if (log_is_enabled(Debug, module)) {
1036     log_debug(module)("add_module_exports_to_all_unnamed(): package %s in module"
1037                       " %s is exported to all unnamed modules",
1038                        package_entry->name()->as_C_string(),
1039                        module_entry->name()->as_C_string());
1040   }
1041 }