1 /*
   2  * Copyright (c) 2015, 2020, 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  * @test
  26  * @library /test/lib /
  27  * @modules java.base/jdk.internal.misc
  28  *          java.management
  29  * @requires vm.cpu.features ~= ".*aes.*" & !vm.graal.enabled
  30  * @build sun.hotspot.WhiteBox
  31  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  32  * @run main/othervm/timeout=600 -Xbootclasspath/a:.
  33  *                   -XX:+UnlockDiagnosticVMOptions
  34  *                   -XX:+WhiteBoxAPI -Xbatch
  35  *                   compiler.cpuflags.TestAESIntrinsicsOnSupportedConfig
  36  */
  37 
  38 package compiler.cpuflags;
  39 
  40 import jdk.test.lib.process.OutputAnalyzer;
  41 import jdk.test.lib.Platform;
  42 import jdk.test.lib.process.ProcessTools;
  43 import sun.hotspot.WhiteBox;
  44 import static jdk.test.lib.cli.CommandLineOptionTest.*;
  45 
  46 public class TestAESIntrinsicsOnSupportedConfig extends AESIntrinsicsBase {
  47 
  48     protected void runTestCases() throws Throwable {
  49         testUseAES();
  50         testUseAESUseSSE2();
  51         testUseAESUseVIS2();
  52         testNoUseAES();
  53         testNoUseAESUseSSE2();
  54         testNoUseAESUseVIS2();
  55         testNoUseAESIntrinsic();
  56     }
  57 
  58     /**
  59      * Check if value of TieredStopAtLevel flag is greater than specified level.
  60      *
  61      * @param level tiered compilation level to compare with
  62      */
  63     private boolean isTieredLevelGreaterThan(int level) {
  64         Long val = WhiteBox.getWhiteBox().getIntxVMFlag("TieredStopAtLevel");
  65         return (val != null && val > level);
  66     }
  67 
  68     /**
  69      * Test checks following situation: <br/>
  70      * UseAES flag is set to true, TestAESMain is executed <br/>
  71      * Expected result: UseAESIntrinsics flag is set to true <br/>
  72      * If vm type is server then output should contain intrinsics usage <br/>
  73      *
  74      * @throws Throwable
  75      */
  76     private void testUseAES() throws Throwable {
  77         OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
  78                 prepareArguments(prepareBooleanFlag(AESIntrinsicsBase
  79                         .USE_AES, true)));
  80         final String errorMessage = "Case testUseAES failed";
  81         if (Platform.isServer() && !Platform.isEmulatedClient() && isTieredLevelGreaterThan(3)) {
  82             verifyOutput(new String[]{AESIntrinsicsBase.CIPHER_INTRINSIC,
  83                     AESIntrinsicsBase.AES_INTRINSIC}, null, errorMessage,
  84                     outputAnalyzer);
  85         } else {
  86             verifyOutput(null, new String[]{AESIntrinsicsBase.CIPHER_INTRINSIC,
  87                     AESIntrinsicsBase.AES_INTRINSIC}, errorMessage,
  88                     outputAnalyzer);
  89         }
  90         verifyOptionValue(AESIntrinsicsBase.USE_AES, "true", errorMessage,
  91                 outputAnalyzer);
  92         verifyOptionValue(AESIntrinsicsBase.USE_AES_INTRINSICS, "true",
  93                 errorMessage, outputAnalyzer);
  94     }
  95 
  96     /**
  97      * Test checks following situation: <br/>
  98      * UseAES flag is set to true, UseSSE flag is set to 2,
  99      * Platform should support UseSSE (x86 or x64) <br/>
 100      * TestAESMain is executed <br/>
 101      * Expected result: UseAESIntrinsics flag is set to false <br/>
 102      * Output shouldn't contain intrinsics usage <br/>
 103      *
 104      * @throws Throwable
 105      */
 106     private void testUseAESUseSSE2() throws Throwable {
 107         if (Platform.isX86() || Platform.isX64()) {
 108             OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
 109                     prepareArguments(prepareBooleanFlag(AESIntrinsicsBase
 110                                     .USE_AES_INTRINSICS, true),
 111                             prepareNumericFlag(AESIntrinsicsBase.USE_SSE, 2)));
 112             final String errorMessage = "Case testUseAESUseSSE2 failed";
 113             verifyOutput(null, new String[]{AESIntrinsicsBase.CIPHER_INTRINSIC,
 114                             AESIntrinsicsBase.AES_INTRINSIC},
 115                     errorMessage, outputAnalyzer);
 116             verifyOptionValue(AESIntrinsicsBase.USE_AES, "true", errorMessage,
 117                     outputAnalyzer);
 118             verifyOptionValue(AESIntrinsicsBase.USE_AES_INTRINSICS, "false",
 119                     errorMessage, outputAnalyzer);
 120             verifyOptionValue(AESIntrinsicsBase.USE_SSE, "2", errorMessage,
 121                     outputAnalyzer);
 122         }
 123     }
 124 
 125     /**
 126      * Test checks following situation: <br/>
 127      * UseAES flag is set to false, UseSSE flag is set to 2,
 128      * Platform should support UseSSE (x86 or x64) <br/>
 129      * TestAESMain is executed <br/>
 130      * Expected result: UseAESIntrinsics flag is set to false <br/>
 131      * Output shouldn't contain intrinsics usage <br/>
 132      *
 133      * @throws Throwable
 134      */
 135     private void testNoUseAESUseSSE2() throws Throwable {
 136         if (Platform.isX86() || Platform.isX64()) {
 137             OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
 138                     prepareArguments(prepareBooleanFlag(AESIntrinsicsBase
 139                                     .USE_AES, false),
 140                             prepareNumericFlag(AESIntrinsicsBase.USE_SSE, 2)));
 141             final String errorMessage = "Case testNoUseAESUseSSE2 failed";
 142             verifyOutput(null, new String[]{AESIntrinsicsBase.CIPHER_INTRINSIC,
 143                             AESIntrinsicsBase.AES_INTRINSIC},
 144                     errorMessage, outputAnalyzer);
 145             verifyOptionValue(AESIntrinsicsBase.USE_AES, "false", errorMessage,
 146                     outputAnalyzer);
 147             verifyOptionValue(AESIntrinsicsBase.USE_AES_INTRINSICS, "false",
 148                     errorMessage, outputAnalyzer);
 149             verifyOptionValue(AESIntrinsicsBase.USE_SSE, "2", errorMessage,
 150                     outputAnalyzer);
 151         }
 152     }
 153 
 154     /**
 155      * Test checks following situation: <br/>
 156      * UseAES flag is set to true, UseVIS flag is set to 2,
 157      * Platform should support UseVIS (sparc) <br/>
 158      * TestAESMain is executed <br/>
 159      * Expected result: UseAESIntrinsics flag is set to false <br/>
 160      * Output shouldn't contain intrinsics usage <br/>
 161      *
 162      * @throws Throwable
 163      */
 164     private void testUseAESUseVIS2() throws Throwable {
 165         if (Platform.isSparc()) {
 166             OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
 167                     prepareArguments(prepareBooleanFlag(AESIntrinsicsBase
 168                                     .USE_AES_INTRINSICS, true),
 169                             prepareNumericFlag(AESIntrinsicsBase.USE_VIS, 2)));
 170             final String errorMessage = "Case testUseAESUseVIS2 failed";
 171             verifyOutput(null, new String[]{AESIntrinsicsBase.CIPHER_INTRINSIC,
 172                             AESIntrinsicsBase.AES_INTRINSIC},
 173                     errorMessage, outputAnalyzer);
 174             verifyOptionValue(AESIntrinsicsBase.USE_AES, "true", errorMessage,
 175                     outputAnalyzer);
 176             verifyOptionValue(AESIntrinsicsBase.USE_AES_INTRINSICS, "false",
 177                     errorMessage, outputAnalyzer);
 178             verifyOptionValue(AESIntrinsicsBase.USE_VIS, "2", errorMessage,
 179                     outputAnalyzer);
 180         }
 181     }
 182 
 183 
 184     /**
 185      * Test checks following situation: <br/>
 186      * UseAES flag is set to false, UseVIS flag is set to 2,
 187      * Platform should support UseVIS (sparc) <br/>
 188      * TestAESMain is executed <br/>
 189      * Expected result: UseAESIntrinsics flag is set to false <br/>
 190      * Output shouldn't contain intrinsics usage <br/>
 191      *
 192      * @throws Throwable
 193      */
 194     private void testNoUseAESUseVIS2() throws Throwable {
 195         if (Platform.isSparc()) {
 196             OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
 197                     prepareArguments(prepareBooleanFlag(AESIntrinsicsBase
 198                                     .USE_AES, false),
 199                             prepareNumericFlag(AESIntrinsicsBase.USE_VIS, 2)));
 200             final String errorMessage = "Case testNoUseAESUseVIS2 failed";
 201             verifyOutput(null, new String[]{AESIntrinsicsBase.CIPHER_INTRINSIC,
 202                             AESIntrinsicsBase.AES_INTRINSIC},
 203                     errorMessage, outputAnalyzer);
 204             verifyOptionValue(AESIntrinsicsBase.USE_AES, "false", errorMessage,
 205                     outputAnalyzer);
 206             verifyOptionValue(AESIntrinsicsBase.USE_AES_INTRINSICS, "false",
 207                     errorMessage, outputAnalyzer);
 208             verifyOptionValue(AESIntrinsicsBase.USE_VIS, "2", errorMessage,
 209                     outputAnalyzer);
 210         }
 211     }
 212 
 213     /**
 214      * Test checks following situation: <br/>
 215      * UseAES flag is set to false, TestAESMain is executed <br/>
 216      * Expected result: UseAESIntrinsics flag is set to false <br/>
 217      * Output shouldn't contain intrinsics usage <br/>
 218      *
 219      * @throws Throwable
 220      */
 221     private void testNoUseAES() throws Throwable {
 222         OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
 223                 prepareArguments(prepareBooleanFlag(AESIntrinsicsBase
 224                         .USE_AES, false)));
 225         final String errorMessage = "Case testNoUseAES failed";
 226         verifyOutput(null, new String[]{AESIntrinsicsBase.CIPHER_INTRINSIC,
 227                         AESIntrinsicsBase.AES_INTRINSIC},
 228                 errorMessage, outputAnalyzer);
 229         verifyOptionValue(AESIntrinsicsBase.USE_AES, "false", errorMessage,
 230                 outputAnalyzer);
 231         verifyOptionValue(AESIntrinsicsBase.USE_AES_INTRINSICS, "false",
 232                 errorMessage, outputAnalyzer);
 233     }
 234 
 235     /**
 236      * Test checks following situation: <br/>
 237      * UseAESIntrinsics flag is set to false, TestAESMain is executed <br/>
 238      * Expected result: UseAES flag is set to true <br/>
 239      * Output shouldn't contain intrinsics usage <br/>
 240      *
 241      * @throws Throwable
 242      */
 243     private void testNoUseAESIntrinsic() throws Throwable {
 244         OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
 245                 prepareArguments(prepareBooleanFlag(AESIntrinsicsBase
 246                         .USE_AES_INTRINSICS, false)));
 247         final String errorMessage = "Case testNoUseAESIntrinsic failed";
 248         verifyOutput(null, new String[]{AESIntrinsicsBase.CIPHER_INTRINSIC,
 249                         AESIntrinsicsBase.AES_INTRINSIC}, errorMessage,
 250                 outputAnalyzer);
 251         verifyOptionValue(AESIntrinsicsBase.USE_AES, "true", errorMessage,
 252                 outputAnalyzer);
 253         verifyOptionValue(AESIntrinsicsBase.USE_AES_INTRINSICS, "false",
 254                 errorMessage, outputAnalyzer);
 255     }
 256 
 257     public static void main(String args[]) throws Throwable {
 258         new TestAESIntrinsicsOnSupportedConfig().runTestCases();
 259     }
 260 }