Skip to content

8352003: Support --add-opens with -XX:+AOTClassLinking #24695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/hotspot/share/classfile/modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,13 +580,14 @@ class Modules::ArchivedProperty {
};

Modules::ArchivedProperty Modules::_archived_props[] = {
// numbered
// non-numbered
{"jdk.module.main", false},

// non-numbered
// numbered
{"jdk.module.addexports", true}, // --add-exports
{"jdk.module.addmods", true}, // --add-modules
{"jdk.module.enable.native.access", true}, // --enable-native-access
{"jdk.module.addopens", true}, // --add-opens
};

constexpr size_t Modules::num_archived_props() {
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/runtime/arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ bool Arguments::internal_module_property_helper(const char* property, bool check
if (strncmp(property, MODULE_PROPERTY_PREFIX, MODULE_PROPERTY_PREFIX_LEN) == 0) {
const char* property_suffix = property + MODULE_PROPERTY_PREFIX_LEN;
if (matches_property_suffix(property_suffix, ADDREADS, ADDREADS_LEN) ||
matches_property_suffix(property_suffix, ADDOPENS, ADDOPENS_LEN) ||
matches_property_suffix(property_suffix, PATCH, PATCH_LEN) ||
matches_property_suffix(property_suffix, LIMITMODS, LIMITMODS_LEN) ||
matches_property_suffix(property_suffix, UPGRADE_PATH, UPGRADE_PATH_LEN) ||
Expand All @@ -343,6 +342,7 @@ bool Arguments::internal_module_property_helper(const char* property, bool check
if (!check_for_cds) {
// CDS notes: these properties are supported by CDS archived full module graph.
if (matches_property_suffix(property_suffix, ADDEXPORTS, ADDEXPORTS_LEN) ||
matches_property_suffix(property_suffix, ADDOPENS, ADDOPENS_LEN) ||
matches_property_suffix(property_suffix, PATH, PATH_LEN) ||
matches_property_suffix(property_suffix, ADDMODS, ADDMODS_LEN) ||
matches_property_suffix(property_suffix, ENABLE_NATIVE_ACCESS, ENABLE_NATIVE_ACCESS_LEN)) {
Expand Down
90 changes: 38 additions & 52 deletions src/java.base/share/classes/java/lang/Module.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -40,7 +40,6 @@
import java.net.URI;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -688,7 +687,6 @@ public boolean isOpen(String pn) {
return implIsExportedOrOpen(pn, EVERYONE_MODULE, /*open*/true);
}


/**
* Returns {@code true} if this module exports or opens the given package
* to the given module. If the other module is {@code EVERYONE_MODULE} then
Expand All @@ -707,12 +705,12 @@ private boolean implIsExportedOrOpen(String pn, Module other, boolean open) {
if (descriptor.isOpen() || descriptor.isAutomatic())
return descriptor.packages().contains(pn);

// exported/opened via module declaration/descriptor
if (isStaticallyExportedOrOpen(pn, other, open))
// exported/opened via module declaration/descriptor or CLI options
if (isExplicitlyExportedOrOpened(pn, other, open))
return true;

// exported via addExports/addOpens
if (isReflectivelyExportedOrOpen(pn, other, open))
if (isReflectivelyExportedOrOpened(pn, other, open))
return true;

// not exported or open to other
Expand All @@ -723,7 +721,7 @@ private boolean implIsExportedOrOpen(String pn, Module other, boolean open) {
* Returns {@code true} if this module exports or opens a package to
* the given module via its module declaration or CLI options.
*/
private boolean isStaticallyExportedOrOpen(String pn, Module other, boolean open) {
private boolean isExplicitlyExportedOrOpened(String pn, Module other, boolean open) {
// test if package is open to everyone or <other>
Map<String, Set<Module>> openPackages = this.openPackages;
if (openPackages != null && allows(openPackages.get(pn), other)) {
Expand Down Expand Up @@ -764,7 +762,7 @@ private boolean allows(Set<Module> targets, Module module) {
* Returns {@code true} if this module reflectively exports or opens the
* given package to the given module.
*/
private boolean isReflectivelyExportedOrOpen(String pn, Module other, boolean open) {
private boolean isReflectivelyExportedOrOpened(String pn, Module other, boolean open) {
// exported or open to all modules
Map<String, Boolean> exports = ReflectionData.exports.get(this, EVERYONE_MODULE);
if (exports != null) {
Expand Down Expand Up @@ -809,15 +807,15 @@ private boolean isReflectivelyExportedOrOpen(String pn, Module other, boolean op
* given package to the given module.
*/
boolean isReflectivelyExported(String pn, Module other) {
return isReflectivelyExportedOrOpen(pn, other, false);
return isReflectivelyExportedOrOpened(pn, other, false);
}

/**
* Returns {@code true} if this module reflectively opens the
* given package to the given module.
*/
boolean isReflectivelyOpened(String pn, Module other) {
return isReflectivelyExportedOrOpen(pn, other, true);
return isReflectivelyExportedOrOpened(pn, other, true);
}


Expand Down Expand Up @@ -1033,50 +1031,38 @@ private void implAddExportsOrOpens(String pn,
}
}

// add package name to exports if absent
Map<String, Boolean> map = ReflectionData.exports
.computeIfAbsent(this, other,
(m1, m2) -> new ConcurrentHashMap<>());
if (open) {
map.put(pn, Boolean.TRUE); // may need to promote from FALSE to TRUE
} else {
map.putIfAbsent(pn, Boolean.FALSE);
}
}

/**
* Updates a module to open all packages in the given sets to all unnamed
* modules.
*
* @apiNote Used during startup to open packages for illegal access.
*/
void implAddOpensToAllUnnamed(Set<String> concealedPkgs, Set<String> exportedPkgs) {
if (jdk.internal.misc.VM.isModuleSystemInited()) {
throw new IllegalStateException("Module system already initialized");
}

// replace this module's openPackages map with a new map that opens
// the packages to all unnamed modules.
Map<String, Set<Module>> openPackages = this.openPackages;
if (openPackages == null) {
openPackages = HashMap.newHashMap(concealedPkgs.size() + exportedPkgs.size());
if (VM.isBooted()) {
// add package name to ReflectionData.exports if absent
Map<String, Boolean> map = ReflectionData.exports
.computeIfAbsent(this, other,
(m1, m2) -> new ConcurrentHashMap<>());
if (open) {
map.put(pn, Boolean.TRUE); // may need to promote from FALSE to TRUE
} else {
map.putIfAbsent(pn, Boolean.FALSE);
}
} else {
openPackages = new HashMap<>(openPackages);
}
implAddOpensToAllUnnamed(concealedPkgs, openPackages);
implAddOpensToAllUnnamed(exportedPkgs, openPackages);
this.openPackages = openPackages;
}

private void implAddOpensToAllUnnamed(Set<String> pkgs, Map<String, Set<Module>> openPackages) {
for (String pn : pkgs) {
Set<Module> prev = openPackages.putIfAbsent(pn, ALL_UNNAMED_MODULE_SET);
if (prev != null) {
prev.add(ALL_UNNAMED_MODULE);
// export/open packages during startup (--add-exports and --add-opens)
Map<String, Set<Module>> packageToTargets = (open) ? openPackages : exportedPackages;
if (packageToTargets != null) {
// copy existing map
packageToTargets = new HashMap<>(packageToTargets);
packageToTargets.compute(pn, (_, values) -> {
var targets = new HashSet<Module>();
if (values != null) {
targets.addAll(values);
}
targets.add(other);
return targets;
});
} else {
packageToTargets = Map.of(pn, Set.of(other));
}
if (open) {
this.openPackages = packageToTargets;
} else {
this.exportedPackages = packageToTargets;
}

// update VM to export the package
addExportsToAllUnnamed0(this, pn);
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/java.base/share/classes/java/lang/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -2059,9 +2059,6 @@ public void addOpens(Module m, String pn, Module other) {
public void addOpensToAllUnnamed(Module m, String pn) {
m.implAddOpensToAllUnnamed(pn);
}
public void addOpensToAllUnnamed(Module m, Set<String> concealedPackages, Set<String> exportedPackages) {
m.implAddOpensToAllUnnamed(concealedPackages, exportedPackages);
}
public void addUses(Module m, Class<?> service) {
m.implAddUses(service);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,6 @@ public interface JavaLangAccess {
*/
void addOpensToAllUnnamed(Module m, String pkg);

/**
* Updates module m to open all packages in the given sets.
*/
void addOpensToAllUnnamed(Module m, Set<String> concealedPkgs, Set<String> exportedPkgs);

/**
* Updates module m to use a service.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ private static boolean canUseArchivedBootLayer() {
return getProperty("jdk.module.upgrade.path") == null &&
getProperty("jdk.module.patch.0") == null && // --patch-module
getProperty("jdk.module.limitmods") == null && // --limit-modules
getProperty("jdk.module.addreads.0") == null && // --add-reads
getProperty("jdk.module.addopens.0") == null; // --add-opens
getProperty("jdk.module.addreads.0") == null; // --add-reads
}

/**
Expand Down Expand Up @@ -452,7 +451,7 @@ private static ModuleLayer boot2() {

// --add-reads, --add-exports/--add-opens
addExtraReads(bootLayer);
boolean extraExportsOrOpens = addExtraExportsAndOpens(bootLayer);
addExtraExportsAndOpens(bootLayer);

// add enable native access
addEnableNativeAccess(bootLayer);
Expand Down Expand Up @@ -722,15 +721,13 @@ private static void addExtraReads(ModuleLayer bootLayer) {
* Process the --add-exports and --add-opens options to export/open
* additional packages specified on the command-line.
*/
private static boolean addExtraExportsAndOpens(ModuleLayer bootLayer) {
boolean extraExportsOrOpens = false;
private static void addExtraExportsAndOpens(ModuleLayer bootLayer) {

// --add-exports
String prefix = "jdk.module.addexports.";
Map<String, List<String>> extraExports = decode(prefix);
if (!extraExports.isEmpty()) {
addExtraExportsOrOpens(bootLayer, extraExports, false);
extraExportsOrOpens = true;
}


Expand All @@ -739,10 +736,8 @@ private static boolean addExtraExportsAndOpens(ModuleLayer bootLayer) {
Map<String, List<String>> extraOpens = decode(prefix);
if (!extraOpens.isEmpty()) {
addExtraExportsOrOpens(bootLayer, extraOpens, true);
extraExportsOrOpens = true;
}

return extraExportsOrOpens;
}

private static void addExtraExportsOrOpens(ModuleLayer bootLayer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public class ExactOptionMatch {
record Option(String cmdLine, String property, String valueA, String valueB) {}

static Option[] allOptions = new Option[] {
new Option("--add-opens",
"jdk.module.addopens",
"java.base/java.util.concurrent.regex=ALL-UNNAMED",
"java.base/sun.security.x509=ALL-UNNAMED"),
new Option("--add-exports",
"jdk.module.addexports",
"java.base/jdk.internal.misc=ALL-UNNAMED",
Expand Down
Loading