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         testNoUseAES();
  52         testNoUseAESUseSSE2();
  53         testNoUseAESIntrinsic();
  54     }
  55 
  56     /**
  57      * Check if value of TieredStopAtLevel flag is greater than specified level.
  58      *
  59      * @param level tiered compilation level to compare with
  60      */
  61     private boolean isTieredLevelGreaterThan(int level) {
  62         Long val = WhiteBox.getWhiteBox().getIntxVMFlag("TieredStopAtLevel");
  63         return (val != null && val > level);
  64     }
  65 
  66     /**
  67      * Test checks following situation: <br/>
  68      * UseAES flag is set to true, TestAESMain is executed <br/>
  69      * Expected result: UseAESIntrinsics flag is set to true <br/>
  70      * If vm type is server then output should contain intrinsics usage <br/>
  71      *
  72      * @throws Throwable
  73      */
  74     private void testUseAES() throws Throwable {
  75         OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
  76                 prepareArguments(prepareBooleanFlag(AESIntrinsicsBase
  77                         .USE_AES, true)));
  78         final String errorMessage = "Case testUseAES failed";
  79         if (Platform.isServer() && !Platform.isEmulatedClient() && isTieredLevelGreaterThan(3)) {
  80             verifyOutput(new String[]{AESIntrinsicsBase.CIPHER_INTRINSIC,
  81                     AESIntrinsicsBase.AES_INTRINSIC}, null, errorMessage,
  82                     outputAnalyzer);
  83         } else {
  84             verifyOutput(null, new String[]{AESIntrinsicsBase.CIPHER_INTRINSIC,
  85                     AESIntrinsicsBase.AES_INTRINSIC}, errorMessage,
  86                     outputAnalyzer);
  87         }
  88         verifyOptionValue(AESIntrinsicsBase.USE_AES, "true", errorMessage,
  89                 outputAnalyzer);
  90         verifyOptionValue(AESIntrinsicsBase.USE_AES_INTRINSICS, "true",
  91                 errorMessage, outputAnalyzer);
  92     }
  93 
  94     /**
  95      * Test checks following situation: <br/>
  96      * UseAES flag is set to true, UseSSE flag is set to 2,
  97      * Platform should support UseSSE (x86 or x64) <br/>
  98      * TestAESMain is executed <br/>
  99      * Expected result: UseAESIntrinsics flag is set to false <br/>
 100      * Output shouldn't contain intrinsics usage <br/>
 101      *
 102      * @throws Throwable
 103      */
 104     private void testUseAESUseSSE2() throws Throwable {
 105         if (Platform.isX86() || Platform.isX64()) {
 106             OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
 107                     prepareArguments(prepareBooleanFlag(AESIntrinsicsBase
 108                                     .USE_AES_INTRINSICS, true),
 109                             prepareNumericFlag(AESIntrinsicsBase.USE_SSE, 2)));
 110             final String errorMessage = "Case testUseAESUseSSE2 failed";
 111             verifyOutput(null, new String[]{AESIntrinsicsBase.CIPHER_INTRINSIC,
 112                             AESIntrinsicsBase.AES_INTRINSIC},
 113                     errorMessage, outputAnalyzer);
 114             verifyOptionValue(AESIntrinsicsBase.USE_AES, "true", errorMessage,
 115                     outputAnalyzer);
 116             verifyOptionValue(AESIntrinsicsBase.USE_AES_INTRINSICS, "false",
 117                     errorMessage, outputAnalyzer);
 118             verifyOptionValue(AESIntrinsicsBase.USE_SSE, "2", errorMessage,
 119                     outputAnalyzer);
 120         }
 121     }
 122 
 123     /**
 124      * Test checks following situation: <br/>
 125      * UseAES flag is set to false, UseSSE flag is set to 2,
 126      * Platform should support UseSSE (x86 or x64) <br/>
 127      * TestAESMain is executed <br/>
 128      * Expected result: UseAESIntrinsics flag is set to false <br/>
 129      * Output shouldn't contain intrinsics usage <br/>
 130      *
 131      * @throws Throwable
 132      */
 133     private void testNoUseAESUseSSE2() throws Throwable {
 134         if (Platform.isX86() || Platform.isX64()) {
 135             OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
 136                     prepareArguments(prepareBooleanFlag(AESIntrinsicsBase
 137                                     .USE_AES, false),
 138                             prepareNumericFlag(AESIntrinsicsBase.USE_SSE, 2)));
 139             final String errorMessage = "Case testNoUseAESUseSSE2 failed";
 140             verifyOutput(null, new String[]{AESIntrinsicsBase.CIPHER_INTRINSIC,
 141                             AESIntrinsicsBase.AES_INTRINSIC},
 142                     errorMessage, outputAnalyzer);
 143             verifyOptionValue(AESIntrinsicsBase.USE_AES, "false", errorMessage,
 144                     outputAnalyzer);
 145             verifyOptionValue(AESIntrinsicsBase.USE_AES_INTRINSICS, "false",
 146                     errorMessage, outputAnalyzer);
 147             verifyOptionValue(AESIntrinsicsBase.USE_SSE, "2", errorMessage,
 148                     outputAnalyzer);
 149         }
 150     }
 151 
 152     /**
 153      * Test checks following situation: <br/>
 154      * UseAES flag is set to false, TestAESMain is executed <br/>
 155      * Expected result: UseAESIntrinsics flag is set to false <br/>
 156      * Output shouldn't contain intrinsics usage <br/>
 157      *
 158      * @throws Throwable
 159      */
 160     private void testNoUseAES() throws Throwable {
 161         OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
 162                 prepareArguments(prepareBooleanFlag(AESIntrinsicsBase
 163                         .USE_AES, false)));
 164         final String errorMessage = "Case testNoUseAES failed";
 165         verifyOutput(null, new String[]{AESIntrinsicsBase.CIPHER_INTRINSIC,
 166                         AESIntrinsicsBase.AES_INTRINSIC},
 167                 errorMessage, outputAnalyzer);
 168         verifyOptionValue(AESIntrinsicsBase.USE_AES, "false", errorMessage,
 169                 outputAnalyzer);
 170         verifyOptionValue(AESIntrinsicsBase.USE_AES_INTRINSICS, "false",
 171                 errorMessage, outputAnalyzer);
 172     }
 173 
 174     /**
 175      * Test checks following situation: <br/>
 176      * UseAESIntrinsics flag is set to false, TestAESMain is executed <br/>
 177      * Expected result: UseAES flag is set to true <br/>
 178      * Output shouldn't contain intrinsics usage <br/>
 179      *
 180      * @throws Throwable
 181      */
 182     private void testNoUseAESIntrinsic() throws Throwable {
 183         OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
 184                 prepareArguments(prepareBooleanFlag(AESIntrinsicsBase
 185                         .USE_AES_INTRINSICS, false)));
 186         final String errorMessage = "Case testNoUseAESIntrinsic failed";
 187         verifyOutput(null, new String[]{AESIntrinsicsBase.CIPHER_INTRINSIC,
 188                         AESIntrinsicsBase.AES_INTRINSIC}, errorMessage,
 189                 outputAnalyzer);
 190         verifyOptionValue(AESIntrinsicsBase.USE_AES, "true", errorMessage,
 191                 outputAnalyzer);
 192         verifyOptionValue(AESIntrinsicsBase.USE_AES_INTRINSICS, "false",
 193                 errorMessage, outputAnalyzer);
 194     }
 195 
 196     public static void main(String args[]) throws Throwable {
 197         new TestAESIntrinsicsOnSupportedConfig().runTestCases();
 198     }
 199 }