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