|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +/* |
| 26 | + * @test |
| 27 | + * @bug 8295102 |
| 28 | + * @summary Always load the lambda-form-invoker lines from default classlist |
| 29 | + * @requires vm.cds |
| 30 | + * @library /test/jdk/lib/testlibrary /test/lib /test/hotspot/jtreg/runtime/cds/appcds |
| 31 | + * @build DefaultClassListLFInvokers |
| 32 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar |
| 33 | + * DefaultClassListLFInvokersApp DefaultClassListLFInvokersApp$CompMethods |
| 34 | + * @run driver DefaultClassListLFInvokers |
| 35 | + */ |
| 36 | + |
| 37 | +import java.io.BufferedWriter; |
| 38 | +import java.io.File; |
| 39 | +import java.io.FileWriter; |
| 40 | +import java.lang.reflect.Method; |
| 41 | +import java.nio.file.Path; |
| 42 | +import java.util.Arrays; |
| 43 | +import java.util.Comparator; |
| 44 | + |
| 45 | +import jdk.test.lib.cds.CDSOptions; |
| 46 | +import jdk.test.lib.cds.CDSTestUtils; |
| 47 | +import jdk.test.lib.helpers.ClassFileInstaller; |
| 48 | + |
| 49 | +public class DefaultClassListLFInvokers { |
| 50 | + static final String appClass = DefaultClassListLFInvokersApp.class.getName(); |
| 51 | + static final String appJar = ClassFileInstaller.getJarPath("app.jar"); |
| 52 | + |
| 53 | + static final String[] classlist = { |
| 54 | + appClass, |
| 55 | + // If we have at least one line of @lambda-form-invoker in the classlist, it triggers |
| 56 | + // the regeneration of the 4 XXX$Holder during -Xshare:dump. |
| 57 | + "@lambda-form-invoker [LF_RESOLVE] java.lang.invoke.DirectMethodHandle$Holder invokeStatic L_V" |
| 58 | + }; |
| 59 | + |
| 60 | + public static void main(String[] args) throws Exception { |
| 61 | + File classListFile = CDSTestUtils.makeClassList(classlist); |
| 62 | + CDSTestUtils.createArchiveAndCheck("-XX:SharedClassListFile=" + classListFile.getPath(), |
| 63 | + "-cp", appJar); |
| 64 | + |
| 65 | + // Make sure we still have all the LF invoker methods as when CDS is disabled, |
| 66 | + // in which case the XXX$Holder classes are loaded from $JAVA_HOME/lib/modules |
| 67 | + Path no_cds_logfile = run(Mode.no_cds); |
| 68 | + Path custom_cds_logfile = run(Mode.custom_cds); |
| 69 | + System.out.println("\n\n============================== Checking output: custom_cds vs no_cds"); |
| 70 | + TestCommon.filesMustMatch(custom_cds_logfile, no_cds_logfile); |
| 71 | + |
| 72 | + // We should also have all the LF invoker methods as when the default CDS archive is used |
| 73 | + // in which case the XXX$Holder classes are loaded from the default archive, |
| 74 | + // e.g., $JAVA_HOME/lib/server/classes.jsa |
| 75 | + Path default_cds_logfile = run(Mode.default_cds); |
| 76 | + System.out.println("\n\n============================== Checking output: custom_cds vs default_cds"); |
| 77 | + TestCommon.filesMustMatch(custom_cds_logfile, default_cds_logfile); |
| 78 | + } |
| 79 | + |
| 80 | + enum Mode { |
| 81 | + no_cds, |
| 82 | + default_cds, |
| 83 | + custom_cds |
| 84 | + }; |
| 85 | + |
| 86 | + static Path run(Mode mode) throws Exception { |
| 87 | + File f = new File("log_" + mode.name() + ".txt"); |
| 88 | + CDSOptions opts = (new CDSOptions()) |
| 89 | + .addSuffix("-showversion", "-cp", appJar, appClass, f.toString()) |
| 90 | + .setUseVersion(false); |
| 91 | + |
| 92 | + switch (mode) { |
| 93 | + case no_cds: |
| 94 | + opts.setXShareMode("off"); |
| 95 | + break; |
| 96 | + case custom_cds: |
| 97 | + // We will use the archive created by the last CDSTestUtils.createArchiveAndCheck() call |
| 98 | + opts.setUseSystemArchive(false); |
| 99 | + opts.setXShareMode("auto"); |
| 100 | + break; |
| 101 | + case default_cds: |
| 102 | + default: |
| 103 | + // We will use the default archive. |
| 104 | + opts.setUseSystemArchive(true); |
| 105 | + opts.setXShareMode("auto"); |
| 106 | + break; |
| 107 | + } |
| 108 | + CDSTestUtils.run(opts).assertNormalExit(DefaultClassListLFInvokersApp.FLAG); |
| 109 | + return f.toPath(); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +class DefaultClassListLFInvokersApp { |
| 114 | + public static final String FLAG = "Test Success!"; |
| 115 | + static class CompMethods implements Comparator<Method> { |
| 116 | + public int compare(Method a, Method b) { |
| 117 | + return a.toString().compareTo(b.toString()); |
| 118 | + } |
| 119 | + } |
| 120 | + static final CompMethods compMethods = new CompMethods(); |
| 121 | + |
| 122 | + public static void main(String[] args) throws Exception { |
| 123 | + try (BufferedWriter w = new BufferedWriter(new FileWriter(args[0]))) { |
| 124 | + test(w, "java.lang.invoke.Invokers$Holder"); |
| 125 | + test(w, "java.lang.invoke.DirectMethodHandle$Holder"); |
| 126 | + test(w, "java.lang.invoke.DelegatingMethodHandle$Holder"); |
| 127 | + test(w, "java.lang.invoke.LambdaForm$Holder"); |
| 128 | + System.out.println(FLAG); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + static void test(BufferedWriter w, String className) throws Exception { |
| 133 | + Class c = Class.forName(className); |
| 134 | + Method[] methods = c.getDeclaredMethods(); |
| 135 | + w.write("Dumping all methods in " + c + "\n"); |
| 136 | + Arrays.sort(methods, 0, methods.length, compMethods); |
| 137 | + for (Method m : methods) { |
| 138 | + w.write(m + "\n"); |
| 139 | + } |
| 140 | + w.write("Found " + methods.length + " methods\n\n\n"); |
| 141 | + } |
| 142 | +} |
0 commit comments