|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, 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 | +/* |
| 27 | + * @test |
| 28 | + * @summary AOT training run should fail if critical classes have been redefined by JVMTI |
| 29 | + * @bug 8380409 |
| 30 | + * @requires vm.cds.supports.aot.class.linking |
| 31 | + * @library /test/lib |
| 32 | + * @run driver RedefineClassHelper |
| 33 | + * @build RedefineCriticalClasses |
| 34 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar RedefineCriticalClassesApp |
| 35 | + * @run driver RedefineCriticalClasses |
| 36 | + */ |
| 37 | + |
| 38 | +import java.io.InputStream; |
| 39 | +import java.net.URL; |
| 40 | +import java.util.ArrayList; |
| 41 | +import jdk.test.lib.cds.CDSTestUtils; |
| 42 | +import jdk.test.lib.helpers.ClassFileInstaller; |
| 43 | +import jdk.test.lib.process.ProcessTools; |
| 44 | + |
| 45 | +public class RedefineCriticalClasses { |
| 46 | + public static void main(String... args) throws Exception { |
| 47 | + ArrayList<String> processArgs = new ArrayList<>(); |
| 48 | + |
| 49 | + // redefineagent.jar is created by "@run driver RedefineClassHelper" |
| 50 | + processArgs.add("-javaagent:redefineagent.jar"); |
| 51 | + |
| 52 | + processArgs.add("-XX:AOTMode=record"); |
| 53 | + processArgs.add("-XX:AOTConfiguration=app.aotconfig"); |
| 54 | + processArgs.add("-Xlog:aot,cds"); |
| 55 | + processArgs.add("-cp"); |
| 56 | + processArgs.add("app.jar"); |
| 57 | + processArgs.add("RedefineCriticalClassesApp"); |
| 58 | + |
| 59 | + ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(processArgs); |
| 60 | + CDSTestUtils.executeAndLog(pb, "train") |
| 61 | + .shouldContain("Redefined: class java.lang.Class") |
| 62 | + .shouldContain("Skipping java/lang/Class: Has been redefined") |
| 63 | + .shouldContain("Critical class java.lang.Class has been excluded. AOT configuration file cannot be written") |
| 64 | + .shouldHaveExitValue(1); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +class RedefineCriticalClassesApp { |
| 69 | + public static void main(String[] args) throws Exception { |
| 70 | + // Use RedefineClassHelper (loaded from redefineagent.jar into the boot class loader) |
| 71 | + // to redefine java/lang/Class, using the exact same bytecodes as from the JDK. |
| 72 | + // The JVM will mark it as having been redefined by JVMTI and will exclude it from the |
| 73 | + // AOT configuration file. |
| 74 | + |
| 75 | + URL url = new URL("jrt:/java.base/java/lang/Class.class"); |
| 76 | + try (InputStream in = url.openConnection().getInputStream()) { |
| 77 | + byte[] b = in.readAllBytes(); |
| 78 | + System.out.println("Length = " + b.length); |
| 79 | + RedefineClassHelper.redefineClass(Class.class, b); |
| 80 | + System.out.println("Redefined: " + Class.class); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments