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  * @test
 27  * @summary CPU feature compatibility test for AOT Code Cache
 28  * @requires vm.cds.supports.aot.code.caching
 29  * @requires vm.compMode != "Xcomp" & vm.compMode != "Xint"
 30  * @requires os.simpleArch == "x64" | os.simpleArch == "aarch64"
 31  * @comment The test verifies AOT checks during VM startup and not code generation.
 32  *          No need to run it with -Xcomp.
 33  * @library /test/lib /test/setup_aot
 34  * @build AOTCodeCPUFeatureIncompatibilityTest JavacBenchApp
 35  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 36  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar
 37  *             JavacBenchApp
 38  *             JavacBenchApp$ClassFile
 39  *             JavacBenchApp$FileManager
 40  *             JavacBenchApp$SourceFile
 41  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI AOTCodeCPUFeatureIncompatibilityTest
 42  */
 43 
 44 import java.util.ArrayList;
 45 import java.util.List;
 46 import java.util.regex.Matcher;
 47 import java.util.regex.Pattern;
 48 
 49 import jdk.test.lib.Platform;
 50 import jdk.test.lib.cds.CDSAppTester;
 51 import jdk.test.lib.process.ProcessTools;
 52 import jdk.test.lib.process.OutputAnalyzer;
 53 
 54 import jdk.test.whitebox.WhiteBox;
 55 import jdk.test.whitebox.cpuinfo.CPUInfo;
 56 
 57 public class AOTCodeCPUFeatureIncompatibilityTest {
 58     public static void main(String... args) throws Exception {
 59         List<String> cpuFeatures = CPUInfo.getFeatures();
 60         if (Platform.isX64()) {
 61             // Minimum value of UseSSE required by JVM is 2. So the production run has to be executed with UseSSE=2.
 62             // To simulate the case of incmpatible SSE feature, we can run this test only on system with higher SSE level (sse3 or above).
 63             if (isSSE3Supported(cpuFeatures)) {
 64                 testIncompatibleFeature("-XX:UseSSE=2", "sse3");
 65             }
 66             if (isAVXSupported(cpuFeatures)) {
 67                 testIncompatibleFeature("-XX:UseAVX=0", "avx");
 68             }
 69         }
 70     }
 71 
 72     // vmOption = command line option to disable CPU feature
 73     // featureName = name of the CPU feature used by the JVM in the log messages
 74     public static void testIncompatibleFeature(String vmOption, String featureName) throws Exception {
 75         new CDSAppTester("AOTCodeCPUFeatureIncompatibilityTest") {
 76             @Override
 77             public String[] vmArgs(RunMode runMode) {
 78                 if (runMode == RunMode.PRODUCTION) {
 79                     return new String[] {vmOption, "-Xlog:aot+codecache+init=debug"};
 80                 }
 81                 return new String[] {};
 82             }
 83             @Override
 84             public void checkExecution(OutputAnalyzer out, RunMode runMode) throws Exception {
 85                 if (runMode == RunMode.ASSEMBLY) {
 86                     out.shouldMatch("CPU features recorded in AOTCodeCache:.*" + featureName + ".*");
 87                 } else if (runMode == RunMode.PRODUCTION) {
 88                     out.shouldMatch("AOT Code Cache disabled: required cpu features are missing:.*" + featureName + ".*");
 89                     out.shouldContain("Unable to use AOT Code Cache");
 90                 }
 91             }
 92             @Override
 93             public String classpath(RunMode runMode) {
 94                 return "app.jar";
 95             }
 96             @Override
 97             public String[] appCommandLine(RunMode runMode) {
 98                 return new String[] {
 99                     "JavacBenchApp", "10"
100                 };
101             }
102         }.runAOTWorkflow("--two-step-training");
103     }
104 
105     // Only used on x86-64 platform
106     static boolean isSSE3Supported(List<String> cpuFeatures) {
107         return cpuFeatures.contains("sse3");
108     }
109 
110     // Only used on x86-64 platform
111     static boolean isAVXSupported(List<String> cpuFeatures) {
112         return cpuFeatures.contains("avx");
113     }
114 }