1 /*
  2  * Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 /**
 27  * @test
 28  * @summary CPU feature compatibility test for AOT Code Cache
 29  * @requires vm.cds.supports.aot.code.caching
 30  * @requires vm.compMode != "Xcomp" & vm.compMode != "Xint"
 31  * @requires os.simpleArch == "x64" | os.simpleArch == "aarch64"
 32  * @comment The test verifies AOT checks during VM startup and not code generation.
 33  *          No need to run it with -Xcomp.
 34  * @library /test/lib /test/setup_aot
 35  * @build AOTCodeCPUFeatureIncompatibilityTest JavacBenchApp
 36  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 37  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar
 38  *             JavacBenchApp
 39  *             JavacBenchApp$ClassFile
 40  *             JavacBenchApp$FileManager
 41  *             JavacBenchApp$SourceFile
 42  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI AOTCodeCPUFeatureIncompatibilityTest
 43  */
 44 
 45 import java.util.ArrayList;
 46 import java.util.List;
 47 import java.util.regex.Matcher;
 48 import java.util.regex.Pattern;
 49 
 50 import jdk.test.lib.Platform;
 51 import jdk.test.lib.cds.CDSAppTester;
 52 import jdk.test.lib.process.ProcessTools;
 53 import jdk.test.lib.process.OutputAnalyzer;
 54 
 55 import jdk.test.whitebox.WhiteBox;
 56 import jdk.test.whitebox.cpuinfo.CPUInfo;
 57 
 58 public class AOTCodeCPUFeatureIncompatibilityTest {
 59     public static void main(String... args) throws Exception {
 60         List<String> cpuFeatures = CPUInfo.getFeatures();
 61         if (Platform.isX64()) {
 62             // Minimum value of UseSSE required by JVM is 2. So the production run has to be executed with UseSSE=2.
 63             // To simulate the case of incmpatible SSE feature, we can run this test only on system with higher SSE level (sse3 or above).
 64             if (isSSE3Supported(cpuFeatures)) {
 65                 testIncompatibleFeature("-XX:UseSSE=2", "sse3");
 66             }
 67             if (isAVXSupported(cpuFeatures)) {
 68                 testIncompatibleFeature("-XX:UseAVX=0", "avx");
 69             }
 70 
 71         /*
 72          * Unfortunately -XX:-UseCRC32 does not clear the feature bit in VM_Version::_features!
 73          * Disable this test until it is fixed
 74         } else if (Platform.isAArch64()) {
 75             if (isCRC32Supported(cpuFeatures)) {
 76                 testIncompatibleFeature("-XX:-UseCRC32", "crc32");
 77             }
 78         */
 79         }
 80     }
 81 
 82     // vmOption = command line option to disable CPU feature
 83     // featureName = name of the CPU feature used by the JVM in the log messages
 84     public static void testIncompatibleFeature(String vmOption, String featureName) throws Exception {
 85         new CDSAppTester("AOTCodeCPUFeatureIncompatibilityTest") {
 86             @Override
 87             public String[] vmArgs(RunMode runMode) {
 88                 if (runMode == RunMode.PRODUCTION) {
 89                     return new String[] {vmOption, "-Xlog:aot+codecache+init=debug"};
 90                 }
 91                 return new String[] {};
 92             }
 93             @Override
 94             public void checkExecution(OutputAnalyzer out, RunMode runMode) throws Exception {
 95                 if (runMode == RunMode.ASSEMBLY) {
 96                     out.shouldMatch("CPU features recorded in AOTCodeCache:.*" + featureName + ".*");
 97                 } else if (runMode == RunMode.PRODUCTION) {
 98                     out.shouldMatch("AOT Code Cache disabled: required cpu features are missing:.*" + featureName + ".*");
 99                     out.shouldContain("Unable to use AOT Code Cache");
100                 }
101             }
102             @Override
103             public String classpath(RunMode runMode) {
104                 return "app.jar";
105             }
106             @Override
107             public String[] appCommandLine(RunMode runMode) {
108                 return new String[] {
109                     "JavacBenchApp", "10"
110                 };
111             }
112         }.runAOTWorkflow("--two-step-training");
113     }
114 
115     // Only used on x86-64 platform
116     static boolean isSSE3Supported(List<String> cpuFeatures) {
117         return cpuFeatures.contains("sse3");
118     }
119 
120     // Only used on x86-64 platform
121     static boolean isAVXSupported(List<String> cpuFeatures) {
122         return cpuFeatures.contains("avx");
123     }
124 
125     // Only used on aarch64 platofrm
126     static boolean isCRC32Supported(List<String> cpuFeatures) {
127         return cpuFeatures.contains("crc32");
128     }
129 }