1 /*
   2  * Copyright (c) 2014, 2020, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.module;
  27 
  28 import java.io.File;
  29 import java.io.PrintStream;
  30 import java.lang.module.Configuration;
  31 import java.lang.module.ModuleDescriptor;
  32 import java.lang.module.ModuleFinder;
  33 import java.lang.module.ModuleReference;
  34 import java.lang.module.ResolvedModule;
  35 import java.net.URI;
  36 import java.nio.file.Path;
  37 import java.util.ArrayList;
  38 import java.util.Collections;
  39 import java.util.HashMap;
  40 import java.util.HashSet;
  41 import java.util.Iterator;
  42 import java.util.LinkedHashMap;
  43 import java.util.List;
  44 import java.util.Map;
  45 import java.util.NoSuchElementException;
  46 import java.util.Objects;
  47 import java.util.Optional;
  48 import java.util.Set;
  49 import java.util.function.Function;
  50 import java.util.stream.Collectors;
  51 
  52 import jdk.internal.loader.BootLoader;
  53 import jdk.internal.loader.BuiltinClassLoader;
  54 import jdk.internal.loader.ClassLoaders;
  55 import jdk.internal.misc.VM;
  56 import jdk.internal.access.JavaLangAccess;
  57 import jdk.internal.access.JavaLangModuleAccess;
  58 import jdk.internal.access.SharedSecrets;
  59 import jdk.internal.perf.PerfCounter;
  60 
  61 /**
  62  * Initializes/boots the module system.
  63  *
  64  * The {@link #boot() boot} method is called early in the startup to initialize
  65  * the module system. In summary, the boot method creates a Configuration by
  66  * resolving a set of module names specified via the launcher (or equivalent)
  67  * -m and --add-modules options. The modules are located on a module path that
  68  * is constructed from the upgrade module path, system modules, and application
  69  * module path. The Configuration is instantiated as the boot layer with each
  70  * module in the configuration defined to a class loader.
  71  */
  72 
  73 public final class ModuleBootstrap {
  74     private ModuleBootstrap() { }
  75 
  76     private static final String JAVA_BASE = "java.base";
  77 
  78     // the token for "all default modules"
  79     private static final String ALL_DEFAULT = "ALL-DEFAULT";
  80 
  81     // the token for "all unnamed modules"
  82     private static final String ALL_UNNAMED = "ALL-UNNAMED";
  83 
  84     // the token for "all system modules"
  85     private static final String ALL_SYSTEM = "ALL-SYSTEM";
  86 
  87     // the token for "all modules on the module path"
  88     private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
  89 
  90     // access to java.lang/module
  91     private static final JavaLangModuleAccess JLMA
  92         = SharedSecrets.getJavaLangModuleAccess();
  93 
  94     // The ModulePatcher for the initial configuration
  95     private static final ModulePatcher patcher = initModulePatcher();
  96 
  97     /**
  98      * Returns the ModulePatcher for the initial configuration.
  99      */
 100     public static ModulePatcher patcher() {
 101         return patcher;
 102     }
 103 
 104     // ModuleFinders for the initial configuration
 105     private static volatile ModuleFinder unlimitedFinder;
 106     private static volatile ModuleFinder limitedFinder;
 107 
 108     /**
 109      * Returns the ModuleFinder for the initial configuration before
 110      * observability is limited by the --limit-modules command line option.
 111      *
 112      * @apiNote Used to support locating modules {@code java.instrument} and
 113      * {@code jdk.management.agent} modules when they are loaded dynamically.
 114      */
 115     public static ModuleFinder unlimitedFinder() {
 116         ModuleFinder finder = unlimitedFinder;
 117         if (finder == null) {
 118             return ModuleFinder.ofSystem();
 119         } else {
 120             return finder;
 121         }
 122     }
 123 
 124     /**
 125      * Returns the ModuleFinder for the initial configuration.
 126      *
 127      * @apiNote Used to support "{@code java --list-modules}".
 128      */
 129     public static ModuleFinder limitedFinder() {
 130         ModuleFinder finder = limitedFinder;
 131         if (finder == null) {
 132             return unlimitedFinder();
 133         } else {
 134             return finder;
 135         }
 136     }
 137 
 138     private static class ArchivedBootLayer {
 139         private static ArchivedBootLayer archivedBootLayer;
 140 
 141         private final ModuleLayer bootLayer;
 142         private final ModuleFinder limitedFinder;
 143         private final IllegalAccessLogger.Builder builder;
 144         private final ModuleFinder unlimitedFinder;
 145         private final ServicesCatalog platformCatalog;
 146         private final ServicesCatalog appCatalog;
 147 
 148         public ArchivedBootLayer(ModuleLayer bootLayer,
 149                                  ModuleFinder limitedFinder,
 150                                  ModuleFinder unlimitedFinder,
 151                                  IllegalAccessLogger.Builder builder) {
 152             this.bootLayer = bootLayer;
 153             this.limitedFinder = limitedFinder;
 154             this.unlimitedFinder = unlimitedFinder;
 155             this.builder = builder;
 156 
 157             this.platformCatalog = ServicesCatalog.getServicesCatalog(ClassLoaders.platformClassLoader());
 158             this.appCatalog = ServicesCatalog.getServicesCatalog(ClassLoaders.appClassLoader());
 159         }
 160 
 161         static ArchivedBootLayer get() {
 162             // The VM will initialize archivedBootLayer only if MetaspaceShared::use_full_module_graph() is true.
 163             return archivedBootLayer;
 164         }
 165 
 166         static void archive(ArchivedBootLayer layer) {
 167             archivedBootLayer = layer;
 168         }
 169 
 170         static {
 171             VM.initializeFromArchive(ArchivedBootLayer.class);
 172         }
 173     }
 174 
 175     private static boolean hasProperty(String key) {
 176         return System.getProperty(key) != null;
 177     }
 178 
 179     private static boolean mayUseArchivedBootLayer() {
 180         // If these properties are set, we cannot use the archived boot layer.
 181         // The VM should have already checked this before initializing
 182         // ArchivedBootLayer::archivedBootLayer.
 183         if (hasProperty("jdk.module.upgrade.path") ||
 184             hasProperty("jdk.module.main") ||
 185             hasProperty("jdk.module.limitmods") ||
 186             hasProperty("jdk.module.validation") ||
 187             hasProperty("jdk.module.showModuleResolution") ||
 188             hasProperty("jdk.module.illegalAccess") ||
 189             hasProperty("java.system.class.loader") ||
 190             hasProperty("jdk.module.addexports.0") || 
 191             hasProperty("jdk.module.addopens.0") ||
 192             hasProperty("jdk.module.addreads.0") ||
 193             hasProperty("jdk.module.patch.0") ||
 194             hasProperty("jdk.module.addmods.0")) {
 195             return false;
 196         } else {
 197             return true;
 198         }
 199     }
 200 
 201     private static ModuleLayer getArchivedBootLayer() {
 202         ArchivedBootLayer archivedBootLayer = ArchivedBootLayer.get();
 203         if (archivedBootLayer != null) {
 204             assert mayUseArchivedBootLayer();
 205             Counters.add("jdk.module.boot.0.archivedBootLayer");
 206             limitedFinder = archivedBootLayer.limitedFinder;
 207             unlimitedFinder = archivedBootLayer.unlimitedFinder;
 208             ModuleLayer bootLayer = archivedBootLayer.bootLayer;
 209 
 210             // Trigger BootLoader.<clinit> 
 211             BootLoader.getUnnamedModule();
 212 
 213             // BootLoader.SERVICES_CATALOG is saved/restored separately in BootLoader.java
 214             ServicesCatalog.setServicesCatalog(ClassLoaders.platformClassLoader(),
 215                                                archivedBootLayer.platformCatalog);
 216             ServicesCatalog.setServicesCatalog(ClassLoaders.appClassLoader(),
 217                                                archivedBootLayer.appCatalog);
 218 
 219             JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
 220             jla.bindToLoader(bootLayer, ClassLoaders.appClassLoader());
 221 
 222             IllegalAccessLogger.Builder builder = archivedBootLayer.builder;
 223             if (builder != null) {
 224                 builder.complete();
 225             }
 226 
 227             Counters.publish("jdk.module.boot.totalTime");
 228             return bootLayer;
 229         }
 230 
 231         return null;
 232     }
 233 
 234 
 235     /**
 236      * Initialize the module system, returning the boot layer.
 237      *
 238      * @see java.lang.System#initPhase2(boolean, boolean)
 239      */
 240     public static ModuleLayer boot() throws Exception {
 241 
 242         Counters.start();
 243         ModuleLayer bootLayer = getArchivedBootLayer();
 244         if (bootLayer != null) {
 245             return bootLayer;
 246         }
 247 
 248         // Step 0: Command line options
 249 
 250         ModuleFinder upgradeModulePath = finderFor("jdk.module.upgrade.path");
 251         ModuleFinder appModulePath = finderFor("jdk.module.path");
 252         boolean isPatched = patcher.hasPatches();
 253 
 254         String mainModule = System.getProperty("jdk.module.main");
 255         Set<String> addModules = addModules();
 256         Set<String> limitModules = limitModules();
 257 
 258         PrintStream traceOutput = null;
 259         String trace = getAndRemoveProperty("jdk.module.showModuleResolution");
 260         if (trace != null && Boolean.parseBoolean(trace))
 261             traceOutput = System.out;
 262 
 263         Counters.add("jdk.module.boot.0.commandLineTime");
 264 
 265         // Step 1: The observable system modules, either all system modules
 266         // or the system modules pre-generated for the initial module (the
 267         // initial module may be the unnamed module). If the system modules
 268         // are pre-generated for the initial module then resolution can be
 269         // skipped.
 270 
 271         SystemModules systemModules = null;
 272         ModuleFinder systemModuleFinder;
 273 
 274         boolean haveModulePath = (appModulePath != null || upgradeModulePath != null);
 275         boolean needResolution = true;
 276         boolean canArchive = false;
 277         boolean hasSplitPackages;
 278         boolean hasIncubatorModules;
 279 
 280         // If the java heap was archived at CDS dump time and the environment
 281         // at dump time matches the current environment then use the archived
 282         // system modules and finder.
 283         ArchivedModuleGraph archivedModuleGraph = ArchivedModuleGraph.get(mainModule);
 284         if (archivedModuleGraph != null
 285                 && !haveModulePath
 286                 && addModules.isEmpty()
 287                 && limitModules.isEmpty()
 288                 && !isPatched) {
 289             systemModuleFinder = archivedModuleGraph.finder();
 290             hasSplitPackages = archivedModuleGraph.hasSplitPackages();
 291             hasIncubatorModules = archivedModuleGraph.hasIncubatorModules();
 292             needResolution = (traceOutput != null);
 293         } else {
 294             if (!haveModulePath && addModules.isEmpty() && limitModules.isEmpty()) {
 295                 systemModules = SystemModuleFinders.systemModules(mainModule);
 296                 if (systemModules != null && !isPatched) {
 297                     needResolution = (traceOutput != null);
 298                     canArchive = true;
 299                 }
 300             }
 301             if (systemModules == null) {
 302                 // all system modules are observable
 303                 systemModules = SystemModuleFinders.allSystemModules();
 304             }
 305             if (systemModules != null) {
 306                 // images build
 307                 systemModuleFinder = SystemModuleFinders.of(systemModules);
 308             } else {
 309                 // exploded build or testing
 310                 systemModules = new ExplodedSystemModules();
 311                 systemModuleFinder = SystemModuleFinders.ofSystem();
 312             }
 313 
 314             hasSplitPackages = systemModules.hasSplitPackages();
 315             hasIncubatorModules = systemModules.hasIncubatorModules();
 316             // not using the archived module graph - avoid accidental use
 317             archivedModuleGraph = null;
 318         }
 319 
 320         Counters.add("jdk.module.boot.1.systemModulesTime");
 321 
 322         // Step 2: Define and load java.base. This patches all classes loaded
 323         // to date so that they are members of java.base. Once java.base is
 324         // loaded then resources in java.base are available for error messages
 325         // needed from here on.
 326 
 327         ModuleReference base = systemModuleFinder.find(JAVA_BASE).orElse(null);
 328         if (base == null)
 329             throw new InternalError(JAVA_BASE + " not found");
 330         URI baseUri = base.location().orElse(null);
 331         if (baseUri == null)
 332             throw new InternalError(JAVA_BASE + " does not have a location");
 333         BootLoader.loadModule(base);
 334         Modules.defineModule(null, base.descriptor(), baseUri);
 335 
 336         // Step 2a: Scan all modules when --validate-modules specified
 337 
 338         if (getAndRemoveProperty("jdk.module.validation") != null) {
 339             int errors = ModulePathValidator.scanAllModules(System.out);
 340             if (errors > 0) {
 341                 fail("Validation of module path failed");
 342             }
 343         }
 344 
 345         Counters.add("jdk.module.boot.2.defineBaseTime");
 346 
 347         // Step 3: If resolution is needed then create the module finder and
 348         // the set of root modules to resolve.
 349 
 350         ModuleFinder savedModuleFinder = null;
 351         ModuleFinder finder;
 352         Set<String> roots;
 353         if (needResolution) {
 354 
 355             // upgraded modules override the modules in the run-time image
 356             if (upgradeModulePath != null)
 357                 systemModuleFinder = ModuleFinder.compose(upgradeModulePath,
 358                                                           systemModuleFinder);
 359 
 360             // The module finder: [--upgrade-module-path] system [--module-path]
 361             if (appModulePath != null) {
 362                 finder = ModuleFinder.compose(systemModuleFinder, appModulePath);
 363             } else {
 364                 finder = systemModuleFinder;
 365             }
 366 
 367             // The root modules to resolve
 368             roots = new HashSet<>();
 369 
 370             // launcher -m option to specify the main/initial module
 371             if (mainModule != null)
 372                 roots.add(mainModule);
 373 
 374             // additional module(s) specified by --add-modules
 375             boolean addAllDefaultModules = false;
 376             boolean addAllSystemModules = false;
 377             boolean addAllApplicationModules = false;
 378             for (String mod : addModules) {
 379                 switch (mod) {
 380                     case ALL_DEFAULT:
 381                         addAllDefaultModules = true;
 382                         break;
 383                     case ALL_SYSTEM:
 384                         addAllSystemModules = true;
 385                         break;
 386                     case ALL_MODULE_PATH:
 387                         addAllApplicationModules = true;
 388                         break;
 389                     default:
 390                         roots.add(mod);
 391                 }
 392             }
 393 
 394             // --limit-modules
 395             savedModuleFinder = finder;
 396             if (!limitModules.isEmpty()) {
 397                 finder = limitFinder(finder, limitModules, roots);
 398             }
 399 
 400             // If there is no initial module specified then assume that the initial
 401             // module is the unnamed module of the application class loader. This
 402             // is implemented by resolving all observable modules that export an
 403             // API. Modules that have the DO_NOT_RESOLVE_BY_DEFAULT bit set in
 404             // their ModuleResolution attribute flags are excluded from the
 405             // default set of roots.
 406             if (mainModule == null || addAllDefaultModules) {
 407                 roots.addAll(DefaultRoots.compute(systemModuleFinder, finder));
 408             }
 409 
 410             // If `--add-modules ALL-SYSTEM` is specified then all observable system
 411             // modules will be resolved.
 412             if (addAllSystemModules) {
 413                 ModuleFinder f = finder;  // observable modules
 414                 systemModuleFinder.findAll()
 415                     .stream()
 416                     .map(ModuleReference::descriptor)
 417                     .map(ModuleDescriptor::name)
 418                     .filter(mn -> f.find(mn).isPresent())  // observable
 419                     .forEach(mn -> roots.add(mn));
 420             }
 421 
 422             // If `--add-modules ALL-MODULE-PATH` is specified then all observable
 423             // modules on the application module path will be resolved.
 424             if (appModulePath != null && addAllApplicationModules) {
 425                 ModuleFinder f = finder;  // observable modules
 426                 appModulePath.findAll()
 427                     .stream()
 428                     .map(ModuleReference::descriptor)
 429                     .map(ModuleDescriptor::name)
 430                     .filter(mn -> f.find(mn).isPresent())  // observable
 431                     .forEach(mn -> roots.add(mn));
 432             }
 433         } else {
 434             // no resolution case
 435             finder = systemModuleFinder;
 436             roots = null;
 437         }
 438 
 439         Counters.add("jdk.module.boot.3.optionsAndRootsTime");
 440 
 441         // Step 4: Resolve the root modules, with service binding, to create
 442         // the configuration for the boot layer. If resolution is not needed
 443         // then create the configuration for the boot layer from the
 444         // readability graph created at link time.
 445 
 446         Configuration cf;
 447         if (needResolution) {
 448             cf = Modules.newBootLayerConfiguration(finder, roots, traceOutput);
 449         } else {
 450             if (archivedModuleGraph != null) {
 451                 cf = archivedModuleGraph.configuration();
 452             } else {
 453                 Map<String, Set<String>> map = systemModules.moduleReads();
 454                 cf = JLMA.newConfiguration(systemModuleFinder, map);
 455             }
 456         }
 457 
 458         // check that modules specified to --patch-module are resolved
 459         if (isPatched) {
 460             patcher.patchedModules()
 461                     .stream()
 462                     .filter(mn -> !cf.findModule(mn).isPresent())
 463                     .forEach(mn -> warnUnknownModule(PATCH_MODULE, mn));
 464         }
 465 
 466         Counters.add("jdk.module.boot.4.resolveTime");
 467 
 468         // Step 5: Map the modules in the configuration to class loaders.
 469         // The static configuration provides the mapping of standard and JDK
 470         // modules to the boot and platform loaders. All other modules (JDK
 471         // tool modules, and both explicit and automatic modules on the
 472         // application module path) are defined to the application class
 473         // loader.
 474 
 475         // mapping of modules to class loaders
 476         Function<String, ClassLoader> clf;
 477         if (archivedModuleGraph != null) {
 478             clf = archivedModuleGraph.classLoaderFunction();
 479         } else {
 480             clf = ModuleLoaderMap.mappingFunction(cf);
 481         }
 482 
 483         // check that all modules to be mapped to the boot loader will be
 484         // loaded from the runtime image
 485         if (haveModulePath) {
 486             for (ResolvedModule resolvedModule : cf.modules()) {
 487                 ModuleReference mref = resolvedModule.reference();
 488                 String name = mref.descriptor().name();
 489                 ClassLoader cl = clf.apply(name);
 490                 if (cl == null) {
 491                     if (upgradeModulePath != null
 492                             && upgradeModulePath.find(name).isPresent())
 493                         fail(name + ": cannot be loaded from upgrade module path");
 494                     if (!systemModuleFinder.find(name).isPresent())
 495                         fail(name + ": cannot be loaded from application module path");
 496                 }
 497             }
 498         }
 499 
 500         // check for split packages in the modules mapped to the built-in loaders
 501         if (hasSplitPackages || isPatched || haveModulePath) {
 502             checkSplitPackages(cf, clf);
 503         }
 504 
 505         // load/register the modules with the built-in class loaders
 506         loadModules(cf, clf);
 507         Counters.add("jdk.module.boot.5.loadModulesTime");
 508 
 509         // Step 6: Define all modules to the VM
 510 
 511         bootLayer = ModuleLayer.empty().defineModules(cf, clf);
 512         Counters.add("jdk.module.boot.6.layerCreateTime");
 513 
 514         // Step 7: Miscellaneous
 515 
 516         // check incubating status
 517         if (hasIncubatorModules || haveModulePath) {
 518             checkIncubatingStatus(cf);
 519         }
 520 
 521         // --add-reads, --add-exports/--add-opens, and --illegal-access
 522         addExtraReads(bootLayer);
 523         boolean extraExportsOrOpens = addExtraExportsAndOpens(bootLayer);
 524 
 525         Map<String, Set<String>> concealedPackagesToOpen;
 526         Map<String, Set<String>> exportedPackagesToOpen;
 527         if (archivedModuleGraph != null) {
 528             concealedPackagesToOpen = archivedModuleGraph.concealedPackagesToOpen();
 529             exportedPackagesToOpen = archivedModuleGraph.exportedPackagesToOpen();
 530         } else {
 531             concealedPackagesToOpen = systemModules.concealedPackagesToOpen();
 532             exportedPackagesToOpen = systemModules.exportedPackagesToOpen();
 533         }
 534         IllegalAccessLogger.Builder builder =
 535             addIllegalAccess(upgradeModulePath,
 536                              concealedPackagesToOpen,
 537                              exportedPackagesToOpen,
 538                              bootLayer,
 539                              extraExportsOrOpens);
 540         Counters.add("jdk.module.boot.7.adjustModulesTime");
 541 
 542         // save module finders for later use
 543         if (savedModuleFinder != null) {
 544             unlimitedFinder = new SafeModuleFinder(savedModuleFinder);
 545             if (savedModuleFinder != finder)
 546                 limitedFinder = new SafeModuleFinder(finder);
 547         }
 548 
 549         // Module graph can be archived at CDS dump time. Only allow the
 550         // unnamed module case for now.
 551         if (canArchive && (mainModule == null)) {
 552             ArchivedModuleGraph.archive(
 553                     new ArchivedModuleGraph(hasSplitPackages,
 554                                             hasIncubatorModules,
 555                                             systemModuleFinder,
 556                                             cf,
 557                                             clf,
 558                                             concealedPackagesToOpen,
 559                                             exportedPackagesToOpen));
 560             ArchivedBootLayer.archive(
 561                     new ArchivedBootLayer(bootLayer,
 562                                           limitedFinder,
 563                                           unlimitedFinder,
 564                                           builder));
 565         }
 566 
 567         // total time to initialize
 568         Counters.publish("jdk.module.boot.totalTime");
 569 
 570         return bootLayer;
 571     }
 572 
 573     /**
 574      * Load/register the modules to the built-in class loaders.
 575      */
 576     private static void loadModules(Configuration cf,
 577                                     Function<String, ClassLoader> clf) {
 578         for (ResolvedModule resolvedModule : cf.modules()) {
 579             ModuleReference mref = resolvedModule.reference();
 580             String name = resolvedModule.name();
 581             ClassLoader loader = clf.apply(name);
 582             if (loader == null) {
 583                 // skip java.base as it is already loaded
 584                 if (!name.equals(JAVA_BASE)) {
 585                     BootLoader.loadModule(mref);
 586                 }
 587             } else if (loader instanceof BuiltinClassLoader) {
 588                 ((BuiltinClassLoader) loader).loadModule(mref);
 589             }
 590         }
 591     }
 592 
 593     /**
 594      * Checks for split packages between modules defined to the built-in class
 595      * loaders.
 596      */
 597     private static void checkSplitPackages(Configuration cf,
 598                                            Function<String, ClassLoader> clf) {
 599         Map<String, String> packageToModule = new HashMap<>();
 600         for (ResolvedModule resolvedModule : cf.modules()) {
 601             ModuleDescriptor descriptor = resolvedModule.reference().descriptor();
 602             String name = descriptor.name();
 603             ClassLoader loader = clf.apply(name);
 604             if (loader == null || loader instanceof BuiltinClassLoader) {
 605                 for (String p : descriptor.packages()) {
 606                     String other = packageToModule.putIfAbsent(p, name);
 607                     if (other != null) {
 608                         String msg = "Package " + p + " in both module "
 609                                      + name + " and module " + other;
 610                         throw new LayerInstantiationException(msg);
 611                     }
 612                 }
 613             }
 614         }
 615     }
 616 
 617     /**
 618      * Returns a ModuleFinder that limits observability to the given root
 619      * modules, their transitive dependences, plus a set of other modules.
 620      */
 621     private static ModuleFinder limitFinder(ModuleFinder finder,
 622                                             Set<String> roots,
 623                                             Set<String> otherMods)
 624     {
 625         // resolve all root modules
 626         Configuration cf = Configuration.empty().resolve(finder,
 627                                                          ModuleFinder.of(),
 628                                                          roots);
 629 
 630         // module name -> reference
 631         Map<String, ModuleReference> map = new HashMap<>();
 632 
 633         // root modules and their transitive dependences
 634         cf.modules().stream()
 635             .map(ResolvedModule::reference)
 636             .forEach(mref -> map.put(mref.descriptor().name(), mref));
 637 
 638         // additional modules
 639         otherMods.stream()
 640             .map(finder::find)
 641             .flatMap(Optional::stream)
 642             .forEach(mref -> map.putIfAbsent(mref.descriptor().name(), mref));
 643 
 644         // set of modules that are observable
 645         Set<ModuleReference> mrefs = new HashSet<>(map.values());
 646 
 647         return new ModuleFinder() {
 648             @Override
 649             public Optional<ModuleReference> find(String name) {
 650                 return Optional.ofNullable(map.get(name));
 651             }
 652             @Override
 653             public Set<ModuleReference> findAll() {
 654                 return mrefs;
 655             }
 656         };
 657     }
 658 
 659     /**
 660      * Creates a finder from the module path that is the value of the given
 661      * system property and optionally patched by --patch-module
 662      */
 663     private static ModuleFinder finderFor(String prop) {
 664         String s = System.getProperty(prop);
 665         if (s == null) {
 666             return null;
 667         } else {
 668             String[] dirs = s.split(File.pathSeparator);
 669             Path[] paths = new Path[dirs.length];
 670             int i = 0;
 671             for (String dir: dirs) {
 672                 paths[i++] = Path.of(dir);
 673             }
 674             return ModulePath.of(patcher, paths);
 675         }
 676     }
 677 
 678     /**
 679      * Initialize the module patcher for the initial configuration passed on the
 680      * value of the --patch-module options.
 681      */
 682     private static ModulePatcher initModulePatcher() {
 683         Map<String, List<String>> map = decode("jdk.module.patch.",
 684                                                File.pathSeparator,
 685                                                false);
 686         return new ModulePatcher(map);
 687     }
 688 
 689     /**
 690      * Returns the set of module names specified by --add-module options.
 691      */
 692     private static Set<String> addModules() {
 693         String prefix = "jdk.module.addmods.";
 694         int index = 0;
 695         // the system property is removed after decoding
 696         String value = getAndRemoveProperty(prefix + index);
 697         if (value == null) {
 698             return Set.of();
 699         } else {
 700             Set<String> modules = new HashSet<>();
 701             while (value != null) {
 702                 for (String s : value.split(",")) {
 703                     if (!s.isEmpty())
 704                         modules.add(s);
 705                 }
 706                 index++;
 707                 value = getAndRemoveProperty(prefix + index);
 708             }
 709             return modules;
 710         }
 711     }
 712 
 713     /**
 714      * Returns the set of module names specified by --limit-modules.
 715      */
 716     private static Set<String> limitModules() {
 717         String value = getAndRemoveProperty("jdk.module.limitmods");
 718         if (value == null) {
 719             return Set.of();
 720         } else {
 721             Set<String> names = new HashSet<>();
 722             for (String name : value.split(",")) {
 723                 if (name.length() > 0) names.add(name);
 724             }
 725             return names;
 726         }
 727     }
 728 
 729     /**
 730      * Process the --add-reads options to add any additional read edges that
 731      * are specified on the command-line.
 732      */
 733     private static void addExtraReads(ModuleLayer bootLayer) {
 734 
 735         // decode the command line options
 736         Map<String, List<String>> map = decode("jdk.module.addreads.");
 737         if (map.isEmpty())
 738             return;
 739 
 740         for (Map.Entry<String, List<String>> e : map.entrySet()) {
 741 
 742             // the key is $MODULE
 743             String mn = e.getKey();
 744             Optional<Module> om = bootLayer.findModule(mn);
 745             if (!om.isPresent()) {
 746                 warnUnknownModule(ADD_READS, mn);
 747                 continue;
 748             }
 749             Module m = om.get();
 750 
 751             // the value is the set of other modules (by name)
 752             for (String name : e.getValue()) {
 753                 if (ALL_UNNAMED.equals(name)) {
 754                     Modules.addReadsAllUnnamed(m);
 755                 } else {
 756                     om = bootLayer.findModule(name);
 757                     if (om.isPresent()) {
 758                         Modules.addReads(m, om.get());
 759                     } else {
 760                         warnUnknownModule(ADD_READS, name);
 761                     }
 762                 }
 763             }
 764         }
 765     }
 766 
 767     /**
 768      * Process the --add-exports and --add-opens options to export/open
 769      * additional packages specified on the command-line.
 770      */
 771     private static boolean addExtraExportsAndOpens(ModuleLayer bootLayer) {
 772         boolean extraExportsOrOpens = false;
 773 
 774         // --add-exports
 775         String prefix = "jdk.module.addexports.";
 776         Map<String, List<String>> extraExports = decode(prefix);
 777         if (!extraExports.isEmpty()) {
 778             addExtraExportsOrOpens(bootLayer, extraExports, false);
 779             extraExportsOrOpens = true;
 780         }
 781 
 782 
 783         // --add-opens
 784         prefix = "jdk.module.addopens.";
 785         Map<String, List<String>> extraOpens = decode(prefix);
 786         if (!extraOpens.isEmpty()) {
 787             addExtraExportsOrOpens(bootLayer, extraOpens, true);
 788             extraExportsOrOpens = true;
 789         }
 790 
 791         return extraExportsOrOpens;
 792     }
 793 
 794     private static void addExtraExportsOrOpens(ModuleLayer bootLayer,
 795                                                Map<String, List<String>> map,
 796                                                boolean opens)
 797     {
 798         String option = opens ? ADD_OPENS : ADD_EXPORTS;
 799         for (Map.Entry<String, List<String>> e : map.entrySet()) {
 800 
 801             // the key is $MODULE/$PACKAGE
 802             String key = e.getKey();
 803             String[] s = key.split("/");
 804             if (s.length != 2)
 805                 fail(unableToParse(option, "<module>/<package>", key));
 806 
 807             String mn = s[0];
 808             String pn = s[1];
 809             if (mn.isEmpty() || pn.isEmpty())
 810                 fail(unableToParse(option, "<module>/<package>", key));
 811 
 812             // The exporting module is in the boot layer
 813             Module m;
 814             Optional<Module> om = bootLayer.findModule(mn);
 815             if (!om.isPresent()) {
 816                 warnUnknownModule(option, mn);
 817                 continue;
 818             }
 819 
 820             m = om.get();
 821 
 822             if (!m.getDescriptor().packages().contains(pn)) {
 823                 warn("package " + pn + " not in " + mn);
 824                 continue;
 825             }
 826 
 827             // the value is the set of modules to export to (by name)
 828             for (String name : e.getValue()) {
 829                 boolean allUnnamed = false;
 830                 Module other = null;
 831                 if (ALL_UNNAMED.equals(name)) {
 832                     allUnnamed = true;
 833                 } else {
 834                     om = bootLayer.findModule(name);
 835                     if (om.isPresent()) {
 836                         other = om.get();
 837                     } else {
 838                         warnUnknownModule(option, name);
 839                         continue;
 840                     }
 841                 }
 842                 if (allUnnamed) {
 843                     if (opens) {
 844                         Modules.addOpensToAllUnnamed(m, pn);
 845                     } else {
 846                         Modules.addExportsToAllUnnamed(m, pn);
 847                     }
 848                 } else {
 849                     if (opens) {
 850                         Modules.addOpens(m, pn, other);
 851                     } else {
 852                         Modules.addExports(m, pn, other);
 853                     }
 854                 }
 855             }
 856         }
 857     }
 858 
 859     /**
 860      * Process the --illegal-access option (and its default) to open packages
 861      * of system modules in the boot layer to code in unnamed modules.
 862      */
 863     private static IllegalAccessLogger.Builder
 864         addIllegalAccess(ModuleFinder upgradeModulePath,
 865                          Map<String, Set<String>> concealedPackagesToOpen,
 866                          Map<String, Set<String>> exportedPackagesToOpen,
 867                          ModuleLayer bootLayer,
 868                          boolean extraExportsOrOpens) {
 869         String value = getAndRemoveProperty("jdk.module.illegalAccess");
 870         IllegalAccessLogger.Mode mode = IllegalAccessLogger.Mode.ONESHOT;
 871         if (value != null) {
 872             switch (value) {
 873                 case "deny":
 874                     return null;
 875                 case "permit":
 876                     break;
 877                 case "warn":
 878                     mode = IllegalAccessLogger.Mode.WARN;
 879                     break;
 880                 case "debug":
 881                     mode = IllegalAccessLogger.Mode.DEBUG;
 882                     break;
 883                 default:
 884                     fail("Value specified to --illegal-access not recognized:"
 885                             + " '" + value + "'");
 886                     return null;
 887             }
 888         }
 889         IllegalAccessLogger.Builder builder
 890             = new IllegalAccessLogger.Builder(mode, System.err);
 891 
 892         if (concealedPackagesToOpen.isEmpty() && exportedPackagesToOpen.isEmpty()) {
 893             // need to generate (exploded build)
 894             IllegalAccessMaps maps = IllegalAccessMaps.generate(limitedFinder());
 895             concealedPackagesToOpen = maps.concealedPackagesToOpen();
 896             exportedPackagesToOpen = maps.exportedPackagesToOpen();
 897         }
 898 
 899         // open specific packages in the system modules
 900         Set<String> emptySet = Set.of();
 901         for (Module m : bootLayer.modules()) {
 902             ModuleDescriptor descriptor = m.getDescriptor();
 903             String name = m.getName();
 904 
 905             // skip open modules
 906             if (descriptor.isOpen()) {
 907                 continue;
 908             }
 909 
 910             // skip modules loaded from the upgrade module path
 911             if (upgradeModulePath != null
 912                 && upgradeModulePath.find(name).isPresent()) {
 913                 continue;
 914             }
 915 
 916             Set<String> concealedPackages = concealedPackagesToOpen.getOrDefault(name, emptySet);
 917             Set<String> exportedPackages = exportedPackagesToOpen.getOrDefault(name, emptySet);
 918 
 919             // refresh the set of concealed and exported packages if needed
 920             if (extraExportsOrOpens) {
 921                 concealedPackages = new HashSet<>(concealedPackages);
 922                 exportedPackages = new HashSet<>(exportedPackages);
 923                 Iterator<String> iterator = concealedPackages.iterator();
 924                 while (iterator.hasNext()) {
 925                     String pn = iterator.next();
 926                     if (m.isExported(pn, BootLoader.getUnnamedModule())) {
 927                         // concealed package is exported to ALL-UNNAMED
 928                         iterator.remove();
 929                         exportedPackages.add(pn);
 930                     }
 931                 }
 932                 iterator = exportedPackages.iterator();
 933                 while (iterator.hasNext()) {
 934                     String pn = iterator.next();
 935                     if (m.isOpen(pn, BootLoader.getUnnamedModule())) {
 936                         // exported package is opened to ALL-UNNAMED
 937                         iterator.remove();
 938                     }
 939                 }
 940             }
 941 
 942             // log reflective access to all types in concealed packages
 943             builder.logAccessToConcealedPackages(m, concealedPackages);
 944 
 945             // log reflective access to non-public members/types in exported packages
 946             builder.logAccessToExportedPackages(m, exportedPackages);
 947 
 948             // open the packages to unnamed modules
 949             JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
 950             jla.addOpensToAllUnnamed(m, concealedPackages, exportedPackages);
 951         }
 952 
 953         builder.complete();
 954         return builder;
 955     }
 956 
 957     /**
 958      * Decodes the values of --add-reads, -add-exports, --add-opens or
 959      * --patch-modules options that are encoded in system properties.
 960      *
 961      * @param prefix the system property prefix
 962      * @praam regex the regex for splitting the RHS of the option value
 963      */
 964     private static Map<String, List<String>> decode(String prefix,
 965                                                     String regex,
 966                                                     boolean allowDuplicates) {
 967         int index = 0;
 968         // the system property is removed after decoding
 969         String value = getAndRemoveProperty(prefix + index);
 970         if (value == null)
 971             return Map.of();
 972 
 973         Map<String, List<String>> map = new HashMap<>();
 974 
 975         while (value != null) {
 976 
 977             int pos = value.indexOf('=');
 978             if (pos == -1)
 979                 fail(unableToParse(option(prefix), "<module>=<value>", value));
 980             if (pos == 0)
 981                 fail(unableToParse(option(prefix), "<module>=<value>", value));
 982 
 983             // key is <module> or <module>/<package>
 984             String key = value.substring(0, pos);
 985 
 986             String rhs = value.substring(pos+1);
 987             if (rhs.isEmpty())
 988                 fail(unableToParse(option(prefix), "<module>=<value>", value));
 989 
 990             // value is <module>(,<module>)* or <file>(<pathsep><file>)*
 991             if (!allowDuplicates && map.containsKey(key))
 992                 fail(key + " specified more than once to " + option(prefix));
 993             List<String> values = map.computeIfAbsent(key, k -> new ArrayList<>());
 994             int ntargets = 0;
 995             for (String s : rhs.split(regex)) {
 996                 if (!s.isEmpty()) {
 997                     values.add(s);
 998                     ntargets++;
 999                 }
1000             }
1001             if (ntargets == 0)
1002                 fail("Target must be specified: " + option(prefix) + " " + value);
1003 
1004             index++;
1005             value = getAndRemoveProperty(prefix + index);
1006         }
1007 
1008         return map;
1009     }
1010 
1011     /**
1012      * Decodes the values of --add-reads, -add-exports or --add-opens
1013      * which use the "," to separate the RHS of the option value.
1014      */
1015     private static Map<String, List<String>> decode(String prefix) {
1016         return decode(prefix, ",", true);
1017     }
1018 
1019     /**
1020      * Gets and remove the named system property
1021      */
1022     private static String getAndRemoveProperty(String key) {
1023         return (String)System.getProperties().remove(key);
1024     }
1025 
1026     /**
1027      * Checks incubating status of modules in the configuration
1028      */
1029     private static void checkIncubatingStatus(Configuration cf) {
1030         String incubating = null;
1031         for (ResolvedModule resolvedModule : cf.modules()) {
1032             ModuleReference mref = resolvedModule.reference();
1033 
1034             // emit warning if the WARN_INCUBATING module resolution bit set
1035             if (ModuleResolution.hasIncubatingWarning(mref)) {
1036                 String mn = mref.descriptor().name();
1037                 if (incubating == null) {
1038                     incubating = mn;
1039                 } else {
1040                     incubating += ", " + mn;
1041                 }
1042             }
1043         }
1044         if (incubating != null)
1045             warn("Using incubator modules: " + incubating);
1046     }
1047 
1048     /**
1049      * Throws a RuntimeException with the given message
1050      */
1051     static void fail(String m) {
1052         throw new RuntimeException(m);
1053     }
1054 
1055     static void warn(String m) {
1056         System.err.println("WARNING: " + m);
1057     }
1058 
1059     static void warnUnknownModule(String option, String mn) {
1060         warn("Unknown module: " + mn + " specified to " + option);
1061     }
1062 
1063     static String unableToParse(String option, String text, String value) {
1064         return "Unable to parse " +  option + " " + text + ": " + value;
1065     }
1066 
1067     private static final String ADD_MODULES  = "--add-modules";
1068     private static final String ADD_EXPORTS  = "--add-exports";
1069     private static final String ADD_OPENS    = "--add-opens";
1070     private static final String ADD_READS    = "--add-reads";
1071     private static final String PATCH_MODULE = "--patch-module";
1072 
1073 
1074     /*
1075      * Returns the command-line option name corresponds to the specified
1076      * system property prefix.
1077      */
1078     static String option(String prefix) {
1079         switch (prefix) {
1080             case "jdk.module.addexports.":
1081                 return ADD_EXPORTS;
1082             case "jdk.module.addopens.":
1083                 return ADD_OPENS;
1084             case "jdk.module.addreads.":
1085                 return ADD_READS;
1086             case "jdk.module.patch.":
1087                 return PATCH_MODULE;
1088             case "jdk.module.addmods.":
1089                 return ADD_MODULES;
1090             default:
1091                 throw new IllegalArgumentException(prefix);
1092         }
1093     }
1094 
1095     /**
1096      * Wraps a (potentially not thread safe) ModuleFinder created during startup
1097      * for use after startup.
1098      */
1099     static class SafeModuleFinder implements ModuleFinder {
1100         private final Set<ModuleReference> mrefs;
1101         private volatile Map<String, ModuleReference> nameToModule;
1102 
1103         SafeModuleFinder(ModuleFinder finder) {
1104             this.mrefs = Collections.unmodifiableSet(finder.findAll());
1105         }
1106         @Override
1107         public Optional<ModuleReference> find(String name) {
1108             Objects.requireNonNull(name);
1109             Map<String, ModuleReference> nameToModule = this.nameToModule;
1110             if (nameToModule == null) {
1111                 this.nameToModule = nameToModule = mrefs.stream()
1112                         .collect(Collectors.toMap(m -> m.descriptor().name(),
1113                                                   Function.identity()));
1114             }
1115             return Optional.ofNullable(nameToModule.get(name));
1116         }
1117         @Override
1118         public Set<ModuleReference> findAll() {
1119             return mrefs;
1120         }
1121     }
1122 
1123     /**
1124      * Counters for startup performance analysis.
1125      */
1126     static class Counters {
1127         private static final boolean PUBLISH_COUNTERS;
1128         private static final boolean PRINT_COUNTERS;
1129         private static Map<String, Long> counters;
1130         private static long startTime;
1131         private static long previousTime;
1132 
1133         static {
1134             String s = System.getProperty("jdk.module.boot.usePerfData");
1135             if (s == null) {
1136                 PUBLISH_COUNTERS = false;
1137                 PRINT_COUNTERS = false;
1138             } else {
1139                 PUBLISH_COUNTERS = true;
1140                 PRINT_COUNTERS = s.equals("debug");
1141                 counters = new LinkedHashMap<>();  // preserve insert order
1142             }
1143         }
1144 
1145         /**
1146          * Start counting time.
1147          */
1148         static void start() {
1149             if (PUBLISH_COUNTERS) {
1150                 startTime = previousTime = System.nanoTime();
1151             }
1152         }
1153 
1154         /**
1155          * Add a counter - storing the time difference between now and the
1156          * previous add or the start.
1157          */
1158         static void add(String name) {
1159             if (PUBLISH_COUNTERS) {
1160                 long current = System.nanoTime();
1161                 long elapsed = current - previousTime;
1162                 previousTime = current;
1163                 counters.put(name, elapsed);
1164             }
1165         }
1166 
1167         /**
1168          * Publish the counters to the instrumentation buffer or stdout.
1169          */
1170         static void publish(String totalTimeName) {
1171             if (PUBLISH_COUNTERS) {
1172                 long currentTime = System.nanoTime();
1173                 for (Map.Entry<String, Long> e : counters.entrySet()) {
1174                     String name = e.getKey();
1175                     long value = e.getValue();
1176                     PerfCounter.newPerfCounter(name).set(value);
1177                     if (PRINT_COUNTERS)
1178                         System.out.println(name + " = " + value);
1179                 }
1180                 long elapsedTotal = currentTime - startTime;
1181                 PerfCounter.newPerfCounter(totalTimeName).set(elapsedTotal);
1182                 if (PRINT_COUNTERS)
1183                     System.out.println(totalTimeName + " = " + elapsedTotal);
1184             }
1185         }
1186     }
1187 }