1 /*
  2  * Copyright (c) 2025, 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          * Unfortunately -XX:-UseCRC32 does not clear the feature bit in VM_Version::_features!
 72          * Disable this test until it is fixed
 73         } else if (Platform.isAArch64()) {
 74             if (isCRC32Supported(cpuFeatures)) {
 75                 testIncompatibleFeature("-XX:-UseCRC32", "crc32");
 76             }
 77         */
 78         }
 79     }
 80 
 81     // vmOption = command line option to disable CPU feature
 82     // featureName = name of the CPU feature used by the JVM in the log messages
 83     public static void testIncompatibleFeature(String vmOption, String featureName) throws Exception {
 84         new CDSAppTester("AOTCodeCPUFeatureIncompatibilityTest") {
 85             @Override
 86             public String[] vmArgs(RunMode runMode) {
 87                 if (runMode == RunMode.PRODUCTION) {
 88                     return new String[] {vmOption, "-Xlog:aot+codecache+init=debug"};
 89                 }
 90                 return new String[] {};
 91             }
 92             @Override
 93             public void checkExecution(OutputAnalyzer out, RunMode runMode) throws Exception {
 94                 if (runMode == RunMode.ASSEMBLY) {
 95                     out.shouldMatch("CPU features recorded in AOTCodeCache:.*" + featureName + ".*");
 96                 } else if (runMode == RunMode.PRODUCTION) {
 97                     out.shouldMatch("AOT Code Cache disabled: required cpu features are missing:.*" + featureName + ".*");
 98                     out.shouldContain("Unable to use AOT Code Cache");
 99                 }
100             }
101             @Override
102             public String classpath(RunMode runMode) {
103                 return "app.jar";
104             }
105             @Override
106             public String[] appCommandLine(RunMode runMode) {
107                 return new String[] {
108                     "JavacBenchApp", "10"
109                 };
110             }
111         }.runAOTWorkflow("--two-step-training");
112     }
113 
114     // Only used on x86-64 platform
115     static boolean isSSE3Supported(List<String> cpuFeatures) {
116         return cpuFeatures.contains("sse3");
117     }
118 
119     // Only used on x86-64 platform
120     static boolean isAVXSupported(List<String> cpuFeatures) {
121         return cpuFeatures.contains("avx");
122     }
123 
124     // Only used on aarch64 platofrm
125     static boolean isCRC32Supported(List<String> cpuFeatures) {
126         return cpuFeatures.contains("crc32");
127     }
128 }