1 /*
   2  * Copyright (c) 2015, 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  * @bug 8138651
  27  * @library /testlibrary /../../test/lib
  28  * @build IntrinsicDisabledTest
  29  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  30  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  31  * @run main/othervm -Xbootclasspath/a:.
  32  *                   -XX:+UnlockDiagnosticVMOptions
  33  *                   -XX:+WhiteBoxAPI
  34  *                   -XX:DisableIntrinsic=_putCharVolatile,_putInt
  35  *                   -XX:DisableIntrinsic=_putIntVolatile
  36  *                   -XX:CompileCommand=option,sun.misc.Unsafe::putChar,ccstrlist,DisableIntrinsic,_getCharVolatile,_getInt
  37  *                   -XX:CompileCommand=option,sun.misc.Unsafe::putCharVolatile,ccstrlist,DisableIntrinsic,_getIntVolatile
  38  *                   IntrinsicDisabledTest
  39  */
  40 
  41 import java.lang.reflect.Executable;
  42 import java.util.Objects;
  43 
  44 import sun.hotspot.WhiteBox;
  45 
  46 public class IntrinsicDisabledTest {
  47 
  48     private static final WhiteBox wb = WhiteBox.getWhiteBox();
  49 
  50     /* Compilation level corresponding to C1. */
  51     private static final int COMP_LEVEL_SIMPLE = 1;
  52 
  53     /* Compilation level corresponding to C2. */
  54     private static final int COMP_LEVEL_FULL_OPTIMIZATION = 4;
  55 
  56     /* Determine if the VM the test is running on is a server or a
  57      * client VM. */
  58     private static boolean isServerVM() {
  59         return System.getProperty("java.vm.name").toLowerCase().contains("server");
  60     }
  61 
  62     /* Determine if tiered compilation is enabled. */
  63     private static boolean isTieredCompilationEnabled() {
  64         return Boolean.valueOf(Objects.toString(wb.getVMFlag("TieredCompilation")));
  65     }
  66 
  67     /* This test uses several methods from sun.misc.Unsafe. The method
  68      * getMethod() returns a different Executable for each different
  69      * combination of its input parameters. There are eight possible
  70      * combinations, getMethod can return an Executable representing
  71      * the following methods: putChar, putCharVolatile, getChar,
  72      * getCharVolatile, putInt, putIntVolatile, getInt,
  73      * getIntVolatile. These methods were selected because they can
  74      * be intrinsified by both the C1 and the C2 compiler.
  75      */
  76     static Executable getMethod(boolean isChar, boolean isPut, boolean isVolatile) throws RuntimeException {
  77         Executable aMethod;
  78         String methodTypeName = isChar ? "Char" : "Int";
  79 
  80         try {
  81             Class aClass = Class.forName("sun.misc.Unsafe");
  82             if (isPut) {
  83                 aMethod = aClass.getDeclaredMethod("put" + methodTypeName + (isVolatile ? "Volatile" : ""),
  84                                                    Object.class,
  85                                                    long.class,
  86                                                    isChar ? char.class : int.class);
  87             } else {
  88                 aMethod = aClass.getDeclaredMethod("get" + methodTypeName + (isVolatile ? "Volatile" : ""),
  89                                                    Object.class,
  90                                                    long.class);
  91             }
  92         } catch (NoSuchMethodException e) {
  93             throw new RuntimeException("Test bug, method is unavailable. " + e);
  94         } catch (ClassNotFoundException e) {
  95             throw new RuntimeException("Test bug, class is unavailable. " + e);
  96         }
  97 
  98         return aMethod;
  99     }
 100 
 101     public static void test(int compLevel) {
 102 
 103         Executable putChar = getMethod(true, /*isPut =*/ true, /*isVolatile = */ false);
 104         Executable getChar = getMethod(true, /*isPut =*/ false, /*isVolatile = */ false);
 105         Executable putCharVolatile = getMethod(true, /*isPut =*/ true, /*isVolatile = */ true);
 106         Executable getCharVolatile = getMethod(true, /*isPut =*/ false, /*isVolatile = */ true);
 107         Executable putInt = getMethod(false, /*isPut =*/ true, /*isVolatile = */ false);
 108         Executable getInt = getMethod(false, /*isPut =*/ false, /*isVolatile = */ false);
 109         Executable putIntVolatile = getMethod(false, /*isPut =*/ true, /*isVolatile = */ true);
 110         Executable getIntVolatile = getMethod(false, /*isPut =*/ false, /*isVolatile = */ true);
 111 
 112         /* Test if globally disabling intrinsics works. */
 113         if (!wb.isIntrinsicAvailable(putChar, compLevel)) {
 114             throw new RuntimeException("Intrinsic for [" + putChar.toGenericString() +
 115                                        "] is not available globally although it should be.");
 116         }
 117 
 118         if (wb.isIntrinsicAvailable(putCharVolatile, compLevel)) {
 119             throw new RuntimeException("Intrinsic for [" + putCharVolatile.toGenericString() +
 120                                        "] is available globally although it should not be.");
 121         }
 122 
 123         if (wb.isIntrinsicAvailable(putInt, compLevel)) {
 124             throw new RuntimeException("Intrinsic for [" + putInt.toGenericString() +
 125                                        "] is available globally although it should not be.");
 126         }
 127 
 128         if (wb.isIntrinsicAvailable(putIntVolatile, compLevel)) {
 129             throw new RuntimeException("Intrinsic for [" + putIntVolatile.toGenericString() +
 130                                        "] is available globally although it should not be.");
 131         }
 132 
 133         /* Test if disabling intrinsics on a per-method level
 134          * works. The method for which intrinsics are
 135          * disabled (the compilation context) is putChar. */
 136         if (!wb.isIntrinsicAvailable(getChar, putChar, compLevel)) {
 137             throw new RuntimeException("Intrinsic for [" + getChar.toGenericString() +
 138                                        "] is not available for intrinsification in [" +
 139                                        putChar.toGenericString() + "] although it should be.");
 140         }
 141 
 142         if (wb.isIntrinsicAvailable(getCharVolatile, putChar, compLevel)) {
 143             throw new RuntimeException("Intrinsic for [" + getCharVolatile.toGenericString() +
 144                                        "] is available for intrinsification in [" +
 145                                        putChar.toGenericString() + "] although it should not be.");
 146         }
 147 
 148         if (wb.isIntrinsicAvailable(getInt, putChar, compLevel)) {
 149             throw new RuntimeException("Intrinsic for [" + getInt.toGenericString() +
 150                                        "] is available for intrinsification in [" +
 151                                        putChar.toGenericString() + "] although it should not be.");
 152         }
 153 
 154         if (wb.isIntrinsicAvailable(getIntVolatile, putCharVolatile, compLevel)) {
 155             throw new RuntimeException("Intrinsic for [" + getIntVolatile.toGenericString() +
 156                                        "] is available for intrinsification in [" +
 157                                        putCharVolatile.toGenericString() + "] although it should not be.");
 158         }
 159 
 160         /* Test if disabling intrinsics on a per-method level
 161          * leaves those intrinsics enabled globally. */
 162         if (!wb.isIntrinsicAvailable(getCharVolatile, compLevel)) {
 163             throw new RuntimeException("Intrinsic for [" + getCharVolatile.toGenericString() +
 164                                        "] is not available globally although it should be.");
 165         }
 166 
 167         if (!wb.isIntrinsicAvailable(getInt, compLevel)) {
 168             throw new RuntimeException("Intrinsic for [" + getInt.toGenericString() +
 169                                        "] is not available globally although it should be.");
 170         }
 171 
 172 
 173         if (!wb.isIntrinsicAvailable(getIntVolatile, compLevel)) {
 174             throw new RuntimeException("Intrinsic for [" + getIntVolatile.toGenericString() +
 175                                        "] is not available globally although it should be.");
 176         }
 177 
 178         /* Test if disabling an intrinsic globally disables it on a
 179          * per-method level as well. */
 180         if (!wb.isIntrinsicAvailable(putChar, getChar, compLevel)) {
 181             throw new RuntimeException("Intrinsic for [" + putChar.toGenericString() +
 182                                        "] is not available for intrinsification in [" +
 183                                        getChar.toGenericString() + "] although it should be.");
 184         }
 185 
 186         if (wb.isIntrinsicAvailable(putCharVolatile, getChar, compLevel)) {
 187             throw new RuntimeException("Intrinsic for [" + putCharVolatile.toGenericString() +
 188                                        "] is available for intrinsification in [" +
 189                                        getChar.toGenericString() + "] although it should not be.");
 190         }
 191 
 192         if (wb.isIntrinsicAvailable(putInt, getChar, compLevel)) {
 193             throw new RuntimeException("Intrinsic for [" + putInt.toGenericString() +
 194                                        "] is available for intrinsification in [" +
 195                                        getChar.toGenericString() + "] although it should not be.");
 196         }
 197 
 198         if (wb.isIntrinsicAvailable(putIntVolatile, getChar, compLevel)) {
 199             throw new RuntimeException("Intrinsic for [" + putIntVolatile.toGenericString() +
 200                                        "] is available for intrinsification in [" +
 201                                        getChar.toGenericString() + "] although it should not be.");
 202         }
 203     }
 204 
 205     public static void main(String args[]) {
 206         if (isServerVM()) {
 207             if (isTieredCompilationEnabled()) {
 208                 test(COMP_LEVEL_SIMPLE);
 209             }
 210             test(COMP_LEVEL_FULL_OPTIMIZATION);
 211         } else {
 212             test(COMP_LEVEL_SIMPLE);
 213         }
 214     }
 215 }