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

Conversation

calvinccheung
Copy link
Member

@calvinccheung calvinccheung commented Apr 16, 2025

This RFE allows --add-opens to be specified for AOT cache creation. AOT cache can be used during production run with --add-opens option as long as the same set of options is used during assembly phase.

Passed tiers 1 - 4 testing.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8352003: Support --add-opens with -XX:+AOTClassLinking (Enhancement - P4)

Reviewers

Contributors

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/24695/head:pull/24695
$ git checkout pull/24695

Update a local copy of the PR:
$ git checkout pull/24695
$ git pull https://git.openjdk.org/jdk.git pull/24695/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24695

View PR using the GUI difftool:
$ git pr show -t 24695

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/24695.diff

Using Webrev

Link to Webrev Comment

@calvinccheung calvinccheung marked this pull request as ready for review April 16, 2025 18:21
@bridgekeeper
Copy link

bridgekeeper bot commented Apr 16, 2025

👋 Welcome back ccheung! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Apr 16, 2025

@calvinccheung This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

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

Co-authored-by: Alan Bateman <[email protected]>
Reviewed-by: iklam, alanb, matsaave

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 245 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk
Copy link

openjdk bot commented Apr 16, 2025

@calvinccheung The following labels will be automatically applied to this pull request:

  • core-libs
  • hotspot-runtime

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the rfr Pull request is ready for review label Apr 16, 2025
@mlbridge
Copy link

mlbridge bot commented Apr 16, 2025

Copy link
Contributor

@matias9927 matias9927 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Apr 17, 2025
@iklam
Copy link
Member

iklam commented Apr 17, 2025

Do we have a test case where --add-opens is used both at dump time and run time? That will verify that --add-opens at dump time is correctly processed.

If we don't have a test yet, I think we can modify the existing test to iterate this block twice: once with --add-opens at dump time and once without. Both iterations should allow the setAccessible call.

OutputAnalyzer output = TestCommon.createArchive(
destJar.toString(), appClasses,
"--module-path", moduleDir.toString(),
"-m", TEST_MODULE1);
TestCommon.checkDump(output);
// run with the archive using the same command line as in dump time
// plus the "--add-opens java.base/java.lang=com.simple" option.
// The main class should be loaded from the archive.
// The setaccessible(true) on the ClassLoader.defineClass method should
// be successful.
TestCommon.run( "-Xlog:class+load=trace",
"-cp", destJar.toString(),
"--add-opens", "java.base/java.lang=" + TEST_MODULE1,
"--module-path", moduleDir.toString(),
"-m", TEST_MODULE1, "with_add_opens")
.assertNormalExit(
"[class,load] com.simple.Main source: shared objects file",
"method.setAccessible succeeded!");
}

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Apr 17, 2025
@calvinccheung
Copy link
Member Author

Do we have a test case where --add-opens is used both at dump time and run time? That will verify that --add-opens at dump time is correctly processed.

Yes, the new test AddopensOption.java uses the --add-opens both at dump time and run time.
I have done some minor cleanup on the test, please review the new version.

@iklam
Copy link
Member

iklam commented Apr 18, 2025

Do we have a test case where --add-opens is used both at dump time and run time? That will verify that --add-opens at dump time is correctly processed.

Yes, the new test AddopensOption.java uses the --add-opens both at dump time and run time. I have done some minor cleanup on the test, please review the new version.

AddopensOption.java only checks if the --add-opens is accepted. It doesn't check if the option has any effect. I think it's better to run TEST_MODULE1 to make sure that setAccessible can be called without throwing an exception. That will verify that the option has achieved the intended effect.

@openjdk openjdk bot removed the rfr Pull request is ready for review label Apr 21, 2025
@calvinccheung
Copy link
Member Author

AddopensOption.java only checks if the --add-opens is accepted. It doesn't check if the option has any effect. I think it's better to run TEST_MODULE1 to make sure that setAccessible can be called without throwing an exception. That will verify that the option has achieved the intended effect.

I've modified the AppOpens.java test to include --add-opens during dump time.
Also needed to change ModuleBootStrap.java slightly for the test to run correctly. The change is to add the call to addExtraExportsAndOpens(bootLayer) in the case where the bootLayer is obtained from CDS archive.

@@ -162,6 +162,7 @@ public static ModuleLayer boot() {
bootLayer = archivedBootLayer.bootLayer();
BootLoader.getUnnamedModule(); // trigger <clinit> of BootLoader.
CDS.defineArchivedModules(ClassLoaders.platformClassLoader(), ClassLoaders.appClassLoader());
boolean extraExportsOrOpens = addExtraExportsAndOpens(bootLayer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(1) The returned value of addExtraExportsAndOpens() is not used. So I think this function can be changed to void, and all occurrences of the local variable extraExportsOrOpens can be removed.

(2) I traced the code paths that depend on the effects of --add-opens and --add-exports. It Looks like some of the effects are recorded in the java.lang.Module$ReflectionData::export table:

https://github.com/openjdk/jdk/blob/684d3b336e9cb31707d35e75f9b785e04e1fdbee/src/java.base/share/classes/java/lang/Module.java#L398C2-L412

        /**
         * A module (1st key) exports or opens a package to another module
         * (2nd key). The map value is a map of package name to a boolean
         * that indicates if the package is opened.
         */
        static final WeakPairMap<Module, Module, Map<String, Boolean>> exports =
            new WeakPairMap<>();

This table is not stored as part of the ArchivedBootLayer, so we must re-initialize this table in the production run. @AlanBateman could you confirm that this is correct.

Eventually, we should enhance the ArchivedBootLayer to also include the tables in Module$ReflectionData. That will obviate the call to addExtraExportsAndOpens() and save a few bytecodes during start-up (but the overall impact would be small, so it's not critical in the current PR). Because these tables use WeakReference, we need to wait for JDK-8354897.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed (1) above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(2) I traced the code paths that depend on the effects of --add-opens and --add-exports. It Looks like some of the effects are recorded in the java.lang.Module$ReflectionData::export table:

https://github.com/openjdk/jdk/blob/684d3b336e9cb31707d35e75f9b785e04e1fdbee/src/java.base/share/classes/java/lang/Module.java#L398C2-L412

        /**
         * A module (1st key) exports or opens a package to another module
         * (2nd key). The map value is a map of package name to a boolean
         * that indicates if the package is opened.
         */
        static final WeakPairMap<Module, Module, Map<String, Boolean>> exports =
            new WeakPairMap<>();

This table is not stored as part of the ArchivedBootLayer, so we must re-initialize this table in the production run. @AlanBateman could you confirm that this is correct.

In the changes for "JEP draft: Prepare final means final", this is changed significantly so that reflectively exporting or opening a package during startup will add to openPackages/exportedPackages. Once the VM is fully initialized then reflective change via the addExports/addOpens API make use of ReflectionData. So no ReflectionData or weak refs setup for --add-exports/--add-opens. We could potential separate this out in advance so that ReflectionData doesn't need to be re-initialized.

@openjdk openjdk bot added the rfr Pull request is ready for review label Apr 22, 2025
Copy link
Member

@iklam iklam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Apr 22, 2025
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Apr 22, 2025
@calvinccheung
Copy link
Member Author

/contributor add @AlanBateman

@openjdk
Copy link

openjdk bot commented Apr 25, 2025

@calvinccheung
Contributor Alan Bateman <[email protected]> successfully added.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Apr 25, 2025
@calvinccheung
Copy link
Member Author

Thanks @iklam @AlanBateman @matias9927 for the review.

/integrate

@openjdk
Copy link

openjdk bot commented Apr 25, 2025

Going to push as commit 597bcc6.
Since your change was applied there have been 254 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Apr 25, 2025
@openjdk openjdk bot closed this Apr 25, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Apr 25, 2025
@openjdk
Copy link

openjdk bot commented Apr 25, 2025

@calvinccheung Pushed as commit 597bcc6.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@calvinccheung calvinccheung deleted the 8352003-add-opens branch April 25, 2025 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants