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 {
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      */
patcher()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      */
unlimitedFinder()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      */
limitedFinder()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      */
boot()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      */
loadModules(Configuration cf, Function<String, ClassLoader> clf)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      */
checkSplitPackages(Configuration cf, Function<String, ClassLoader> clf)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      */
limitFinder(ModuleFinder finder, Set<String> roots, Set<String> otherMods)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 Set.of();
577         } else {
578             Set<String> modules = new HashSet<>();
579             while (value != null) {
580                 for (String s : value.split(",")) {
581                     if (!s.isEmpty())
582                         modules.add(s);
583                 }
584                 index++;
585                 value = getAndRemoveProperty(prefix + index);
586             }
587             return modules;
588         }
589     }
590 
591     /**
592      * Returns the set of module names specified by --limit-modules.
593      */
594     private static Set<String> limitModules() {
595         String value = getAndRemoveProperty("jdk.module.limitmods");
596         if (value == null) {
597             return Set.of();
598         } else {
599             Set<String> names = new HashSet<>();
600             for (String name : value.split(",")) {
601                 if (name.length() > 0) names.add(name);
602             }
603             return names;
604         }
605     }
606 
607     /**
608      * Process the --add-reads options to add any additional read edges that
609      * are specified on the command-line.
610      */
611     private static void addExtraReads(ModuleLayer bootLayer) {
612 
613         // decode the command line options
614         Map<String, List<String>> map = decode("jdk.module.addreads.");
615         if (map.isEmpty())
616             return;
617 
618         for (Map.Entry<String, List<String>> e : map.entrySet()) {
619 
620             // the key is $MODULE
621             String mn = e.getKey();
622             Optional<Module> om = bootLayer.findModule(mn);
623             if (!om.isPresent()) {
624                 warnUnknownModule(ADD_READS, mn);
625                 continue;
626             }
627             Module m = om.get();
628 
629             // the value is the set of other modules (by name)
630             for (String name : e.getValue()) {
631                 if (ALL_UNNAMED.equals(name)) {
632                     Modules.addReadsAllUnnamed(m);
633                 } else {
634                     om = bootLayer.findModule(name);
635                     if (om.isPresent()) {
636                         Modules.addReads(m, om.get());
637                     } else {
638                         warnUnknownModule(ADD_READS, name);
639                     }
640                 }
641             }
642         }
643     }
644 
645     /**
646      * Process the --add-exports and --add-opens options to export/open
647      * additional packages specified on the command-line.
648      */
649     private static boolean addExtraExportsAndOpens(ModuleLayer bootLayer) {
650         boolean extraExportsOrOpens = false;
651 
652         // --add-exports
653         String prefix = "jdk.module.addexports.";
654         Map<String, List<String>> extraExports = decode(prefix);
655         if (!extraExports.isEmpty()) {
656             addExtraExportsOrOpens(bootLayer, extraExports, false);
657             extraExportsOrOpens = true;
658         }
659 
660 
661         // --add-opens
662         prefix = "jdk.module.addopens.";
663         Map<String, List<String>> extraOpens = decode(prefix);
664         if (!extraOpens.isEmpty()) {
665             addExtraExportsOrOpens(bootLayer, extraOpens, true);
666             extraExportsOrOpens = true;
667         }
668 
669         return extraExportsOrOpens;
670     }
671 
672     private static void addExtraExportsOrOpens(ModuleLayer bootLayer,
673                                                Map<String, List<String>> map,
674                                                boolean opens)
675     {
676         String option = opens ? ADD_OPENS : ADD_EXPORTS;
677         for (Map.Entry<String, List<String>> e : map.entrySet()) {
678 
679             // the key is $MODULE/$PACKAGE
680             String key = e.getKey();
681             String[] s = key.split("/");
682             if (s.length != 2)
683                 fail(unableToParse(option, "<module>/<package>", key));
684 
685             String mn = s[0];
686             String pn = s[1];
687             if (mn.isEmpty() || pn.isEmpty())
688                 fail(unableToParse(option, "<module>/<package>", key));
689 
690             // The exporting module is in the boot layer
691             Module m;
692             Optional<Module> om = bootLayer.findModule(mn);
693             if (!om.isPresent()) {
694                 warnUnknownModule(option, mn);
695                 continue;
696             }
697 
698             m = om.get();
699 
700             if (!m.getDescriptor().packages().contains(pn)) {
701                 warn("package " + pn + " not in " + mn);
702                 continue;
703             }
704 
705             // the value is the set of modules to export to (by name)
706             for (String name : e.getValue()) {
707                 boolean allUnnamed = false;
708                 Module other = null;
709                 if (ALL_UNNAMED.equals(name)) {
710                     allUnnamed = true;
711                 } else {
712                     om = bootLayer.findModule(name);
713                     if (om.isPresent()) {
714                         other = om.get();
715                     } else {
716                         warnUnknownModule(option, name);
717                         continue;
718                     }
719                 }
720                 if (allUnnamed) {
721                     if (opens) {
722                         Modules.addOpensToAllUnnamed(m, pn);
723                     } else {
724                         Modules.addExportsToAllUnnamed(m, pn);
725                     }
726                 } else {
727                     if (opens) {
728                         Modules.addOpens(m, pn, other);
729                     } else {
730                         Modules.addExports(m, pn, other);
731                     }
732                 }
733 
734             }
735         }
736     }
737 
738     /**
739      * Process the --illegal-access option (and its default) to open packages
740      * of system modules in the boot layer to code in unnamed modules.
741      */
742     private static void addIllegalAccess(ModuleFinder upgradeModulePath,
743                                          SystemModules systemModules,
744                                          ModuleLayer bootLayer,
745                                          boolean extraExportsOrOpens) {
746         String value = getAndRemoveProperty("jdk.module.illegalAccess");
747         IllegalAccessLogger.Mode mode = IllegalAccessLogger.Mode.ONESHOT;
748         if (value != null) {
749             switch (value) {
750                 case "deny":
751                     return;
752                 case "permit":
753                     break;
754                 case "warn":
755                     mode = IllegalAccessLogger.Mode.WARN;
756                     break;
757                 case "debug":
758                     mode = IllegalAccessLogger.Mode.DEBUG;
759                     break;
760                 default:
761                     fail("Value specified to --illegal-access not recognized:"
762                             + " '" + value + "'");
763                     return;
764             }
765         }
766         IllegalAccessLogger.Builder builder
767             = new IllegalAccessLogger.Builder(mode, System.err);
768 
769         Map<String, Set<String>> map1 = systemModules.concealedPackagesToOpen();
770         Map<String, Set<String>> map2 = systemModules.exportedPackagesToOpen();
771         if (map1.isEmpty() && map2.isEmpty()) {
772             // need to generate (exploded build)
773             IllegalAccessMaps maps = IllegalAccessMaps.generate(limitedFinder());
774             map1 = maps.concealedPackagesToOpen();
775             map2 = maps.exportedPackagesToOpen();
776         }
777 
778         // open specific packages in the system modules
779         for (Module m : bootLayer.modules()) {
780             ModuleDescriptor descriptor = m.getDescriptor();
781             String name = m.getName();
782 
783             // skip open modules
784             if (descriptor.isOpen()) {
785                 continue;
786             }
787 
788             // skip modules loaded from the upgrade module path
789             if (upgradeModulePath != null
790                 && upgradeModulePath.find(name).isPresent()) {
791                 continue;
792             }
793 
794             Set<String> concealedPackages = map1.getOrDefault(name, Set.of());
795             Set<String> exportedPackages = map2.getOrDefault(name, Set.of());
796 
797             // refresh the set of concealed and exported packages if needed
798             if (extraExportsOrOpens) {
799                 concealedPackages = new HashSet<>(concealedPackages);
800                 exportedPackages = new HashSet<>(exportedPackages);
801                 Iterator<String> iterator = concealedPackages.iterator();
802                 while (iterator.hasNext()) {
803                     String pn = iterator.next();
804                     if (m.isExported(pn, BootLoader.getUnnamedModule())) {
805                         // concealed package is exported to ALL-UNNAMED
806                         iterator.remove();
807                         exportedPackages.add(pn);
808                     }
809                 }
810                 iterator = exportedPackages.iterator();
811                 while (iterator.hasNext()) {
812                     String pn = iterator.next();
813                     if (m.isOpen(pn, BootLoader.getUnnamedModule())) {
814                         // exported package is opened to ALL-UNNAMED
815                         iterator.remove();
816                     }
817                 }
818             }
819 
820             // log reflective access to all types in concealed packages
821             builder.logAccessToConcealedPackages(m, concealedPackages);
822 
823             // log reflective access to non-public members/types in exported packages
824             builder.logAccessToExportedPackages(m, exportedPackages);
825 
826             // open the packages to unnamed modules
827             JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
828             jla.addOpensToAllUnnamed(m, concat(concealedPackages.iterator(),
829                                                exportedPackages.iterator()));
830         }
831 
832         builder.complete();
833     }
834 
835     /**
836      * Decodes the values of --add-reads, -add-exports, --add-opens or
837      * --patch-modules options that are encoded in system properties.
838      *
839      * @param prefix the system property prefix
840      * @praam regex the regex for splitting the RHS of the option value
841      */
842     private static Map<String, List<String>> decode(String prefix,
843                                                     String regex,
844                                                     boolean allowDuplicates) {
845         int index = 0;
846         // the system property is removed after decoding
847         String value = getAndRemoveProperty(prefix + index);
848         if (value == null)
849             return Map.of();
850 
851         Map<String, List<String>> map = new HashMap<>();
852 
853         while (value != null) {
854 
855             int pos = value.indexOf('=');
856             if (pos == -1)
857                 fail(unableToParse(option(prefix), "<module>=<value>", value));
858             if (pos == 0)
859                 fail(unableToParse(option(prefix), "<module>=<value>", value));
860 
861             // key is <module> or <module>/<package>
862             String key = value.substring(0, pos);
863 
864             String rhs = value.substring(pos+1);
865             if (rhs.isEmpty())
866                 fail(unableToParse(option(prefix), "<module>=<value>", value));
867 
868             // value is <module>(,<module>)* or <file>(<pathsep><file>)*
869             if (!allowDuplicates && map.containsKey(key))
870                 fail(key + " specified more than once to " + option(prefix));
871             List<String> values = map.computeIfAbsent(key, k -> new ArrayList<>());
872             int ntargets = 0;
873             for (String s : rhs.split(regex)) {
874                 if (!s.isEmpty()) {
875                     values.add(s);
876                     ntargets++;
877                 }
878             }
879             if (ntargets == 0)
880                 fail("Target must be specified: " + option(prefix) + " " + value);
881 
882             index++;
883             value = getAndRemoveProperty(prefix + index);
884         }
885 
886         return map;
887     }
888 
889     /**
890      * Decodes the values of --add-reads, -add-exports or --add-opens
891      * which use the "," to separate the RHS of the option value.
892      */
893     private static Map<String, List<String>> decode(String prefix) {
894         return decode(prefix, ",", true);
895     }
896 
897     /**
898      * Gets and remove the named system property
899      */
900     private static String getAndRemoveProperty(String key) {
901         return (String)System.getProperties().remove(key);
902     }
903 
904     /**
905      * Checks incubating status of modules in the configuration
906      */
907     private static void checkIncubatingStatus(Configuration cf) {
908         String incubating = null;
909         for (ResolvedModule resolvedModule : cf.modules()) {
910             ModuleReference mref = resolvedModule.reference();
911 
912             // emit warning if the WARN_INCUBATING module resolution bit set
913             if (ModuleResolution.hasIncubatingWarning(mref)) {
914                 String mn = mref.descriptor().name();
915                 if (incubating == null) {
916                     incubating = mn;
917                 } else {
918                     incubating += ", " + mn;
919                 }
920             }
921         }
922         if (incubating != null)
923             warn("Using incubator modules: " + incubating);
924     }
925 
926     /**
927      * Throws a RuntimeException with the given message
928      */
929     static void fail(String m) {
930         throw new RuntimeException(m);
931     }
932 
933     static void warn(String m) {
934         System.err.println("WARNING: " + m);
935     }
936 
937     static void warnUnknownModule(String option, String mn) {
938         warn("Unknown module: " + mn + " specified to " + option);
939     }
940 
941     static String unableToParse(String option, String text, String value) {
942         return "Unable to parse " +  option + " " + text + ": " + value;
943     }
944 
945     private static final String ADD_MODULES  = "--add-modules";
946     private static final String ADD_EXPORTS  = "--add-exports";
947     private static final String ADD_OPENS    = "--add-opens";
948     private static final String ADD_READS    = "--add-reads";
949     private static final String PATCH_MODULE = "--patch-module";
950 
951 
952     /*
953      * Returns the command-line option name corresponds to the specified
954      * system property prefix.
955      */
956     static String option(String prefix) {
957         switch (prefix) {
958             case "jdk.module.addexports.":
959                 return ADD_EXPORTS;
960             case "jdk.module.addopens.":
961                 return ADD_OPENS;
962             case "jdk.module.addreads.":
963                 return ADD_READS;
964             case "jdk.module.patch.":
965                 return PATCH_MODULE;
966             case "jdk.module.addmods.":
967                 return ADD_MODULES;
968             default:
969                 throw new IllegalArgumentException(prefix);
970         }
971     }
972 
973     /**
974      * Returns an iterator that yields all elements of the first iterator
975      * followed by all the elements of the second iterator.
976      */
977     static <T> Iterator<T> concat(Iterator<T> iterator1, Iterator<T> iterator2) {
978         return new Iterator<T>() {
979             @Override
980             public boolean hasNext() {
981                 return iterator1.hasNext() || iterator2.hasNext();
982             }
983             @Override
984             public T next() {
985                 if (iterator1.hasNext()) return iterator1.next();
986                 if (iterator2.hasNext()) return iterator2.next();
987                 throw new NoSuchElementException();
988             }
989         };
990     }
991 
992     /**
993      * Wraps a (potentially not thread safe) ModuleFinder created during startup
994      * for use after startup.
995      */
996     static class SafeModuleFinder implements ModuleFinder {
997         private final Set<ModuleReference> mrefs;
998         private volatile Map<String, ModuleReference> nameToModule;
999 
1000         SafeModuleFinder(ModuleFinder finder) {
1001             this.mrefs = Collections.unmodifiableSet(finder.findAll());
1002         }
1003         @Override
1004         public Optional<ModuleReference> find(String name) {
1005             Objects.requireNonNull(name);
1006             Map<String, ModuleReference> nameToModule = this.nameToModule;
1007             if (nameToModule == null) {
1008                 this.nameToModule = nameToModule = mrefs.stream()
1009                         .collect(Collectors.toMap(m -> m.descriptor().name(),
1010                                                   Function.identity()));
1011             }
1012             return Optional.ofNullable(nameToModule.get(name));
1013         }
1014         @Override
1015         public Set<ModuleReference> findAll() {
1016             return mrefs;
1017         }
1018     }
1019 
1020     /**
1021      * Counters for startup performance analysis.
1022      */
1023     static class Counters {
1024         private static final boolean PUBLISH_COUNTERS;
1025         private static final boolean PRINT_COUNTERS;
1026         private static Map<String, Long> counters;
1027         static {
1028             String s = System.getProperty("jdk.module.boot.usePerfData");
1029             if (s == null) {
1030                 PUBLISH_COUNTERS = false;
1031                 PRINT_COUNTERS = false;
1032             } else {
1033                 PUBLISH_COUNTERS = true;
1034                 PRINT_COUNTERS = s.equals("debug");
1035                 counters = new LinkedHashMap<>();  // preserve insert order
1036             }
1037         }
1038 
1039         /**
1040          * Add a counter
1041          */
1042         static void add(String name, long start) {
1043             if (PUBLISH_COUNTERS || PRINT_COUNTERS) {
1044                 counters.put(name, (System.nanoTime() - start));
1045             }
1046         }
1047 
1048         /**
1049          * Publish the counters to the instrumentation buffer or stdout.
1050          */
1051         static void publish() {
1052             if (PUBLISH_COUNTERS || PRINT_COUNTERS) {
1053                 for (Map.Entry<String, Long> e : counters.entrySet()) {
1054                     String name = e.getKey();
1055                     long value = e.getValue();
1056                     if (PUBLISH_COUNTERS)
1057                         PerfCounter.newPerfCounter(name).set(value);
1058                     if (PRINT_COUNTERS)
1059                         System.out.println(name + " = " + value);
1060                 }
1061             }
1062         }
1063     }
1064 }
1065