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