/* * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @bug 8138651 * @library /testlibrary /../../test/lib * @build IntrinsicDisabledTest * @run main ClassFileInstaller sun.hotspot.WhiteBox * sun.hotspot.WhiteBox$WhiteBoxPermission * @run main/othervm -Xbootclasspath/a:. * -XX:+UnlockDiagnosticVMOptions * -XX:+WhiteBoxAPI * -XX:DisableIntrinsic=_putCharVolatile * -XX:CompileCommand=option,sun.misc.Unsafe::putChar,ccstr,DisableIntrinsic,_getCharVolatile * IntrinsicDisabledTest */ import java.lang.reflect.Executable; import java.util.Objects; import sun.hotspot.WhiteBox; public class IntrinsicDisabledTest { private static final WhiteBox wb = WhiteBox.getWhiteBox(); /* Compilation level corresponding to C1. */ private static final int COMP_LEVEL_SIMPLE = 1; /* Compilation level corresponding to C2. */ private static final int COMP_LEVEL_FULL_OPTIMIZATION = 4; /* Determine if the VM the test is running on is a server or a * client VM. */ private static boolean isServerVM() { return System.getProperty("java.vm.name").toLowerCase().contains("server"); } /* Determine if tiered compilation is enabled. */ private static boolean isTieredCompilationEnabled() { return Boolean.valueOf(Objects.toString(wb.getVMFlag("TieredCompilation"))); } /* Four methods from sun.misc.Unsafe are used in this test: * putChar, putCharVolatile, getChar, and getCharVolatile. These * methods were selected because they can be intrinsified by both * the C1 and the C2 compiler. * * The method getMethod() returns the Executable for the method * corresponding to the combination of parameters. (There are four * possible combinations, one for each previously mentioned * method.) */ static Executable getMethod(boolean isPut, boolean isVolatile) throws RuntimeException { Executable aMethod; try { Class aClass = Class.forName("sun.misc.Unsafe"); if (isPut) { aMethod = aClass.getDeclaredMethod(isVolatile ? "putCharVolatile" : "putChar", Object.class, long.class, char.class); } else { aMethod = aClass.getDeclaredMethod(isVolatile ? "getCharVolatile" : "getChar", Object.class, long.class); } } catch (NoSuchMethodException e) { throw new RuntimeException("Test bug, method is unavailable. " + e); } catch (ClassNotFoundException e) { throw new RuntimeException("Test bug, class is unavailable. " + e); } return aMethod; } public static void test(int compLevel) { Executable putChar = getMethod(/*isPut =*/ true, /*isVolatile = */ false); Executable getChar = getMethod(/*isPut =*/ false, /*isVolatile = */ false); Executable putCharVolatile = getMethod(/*isPut =*/ true, /*isVolatile = */ true); Executable getCharVolatile = getMethod(/*isPut =*/ false, /*isVolatile = */ true); /* Test if globally disabling intrinsics works. The intrinsics * tested are putChar and putCharVolatile. */ if (!wb.isIntrinsicAvailable(putChar, compLevel)) { throw new RuntimeException("Intrinsic for sun.misc.putChar is " + "not available globally " + "although it should be."); } if (wb.isIntrinsicAvailable(putCharVolatile, compLevel)) { throw new RuntimeException("Intrinsic for sun.misc.putCharVolatile is " + "available globally " + "although it should not be."); } /* Test if disabling intrinsics on a per-method level * works. The intrinsics tested are getChar and * getCharVolatile. The method for which the intrinsic is * disabled (the compilation context) is putChar. */ if (!wb.isIntrinsicAvailable(getChar, putChar, compLevel)) { throw new RuntimeException("Intrinsic for sun.misc.getChar is " + "not available for intrinsification in " + "sun.misc.putChar although it should be."); } if (wb.isIntrinsicAvailable(getCharVolatile, putChar, compLevel)) { throw new RuntimeException("Intrinsic for sun.misc.getCharVolatile is " + "available for intrinsification in " + "sun.misc.putChar although it should not be."); } /* Test if disabling an intrinsic globally disables it on a * per-method level as well. The intrinsics tested are putChar * and putCharVolatile, the compilation context is getChar. */ if (!wb.isIntrinsicAvailable(putChar, getChar, compLevel)) { throw new RuntimeException("Intrinsic for sun.misc.putChar is " + "not available for intrinsification in " + "sun.misc.getChar although it should be."); } if (wb.isIntrinsicAvailable(putCharVolatile, getChar, compLevel)) { throw new RuntimeException("Intrinsic for sun.mis.putCharVolatile is " + "available for intrinsification in" + "sun.misc.getChar although it should not be."); } } public static void main(String args[]) { if (isServerVM()) { if (isTieredCompilationEnabled()) { test(COMP_LEVEL_SIMPLE); } test(COMP_LEVEL_FULL_OPTIMIZATION); } else { test(COMP_LEVEL_SIMPLE); } } }