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