TestUnstableIfTrap.java 11.0 KB
Newer Older
1
/*
2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 * 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.
 */
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Properties;

import com.oracle.java.testlibrary.ByteCodeLoader;
import com.oracle.java.testlibrary.Platform;
import jdk.internal.org.objectweb.asm.ClassVisitor;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Label;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import static jdk.internal.org.objectweb.asm.Opcodes.*;

import sun.hotspot.WhiteBox;
import uncommontrap.Verifier;

/*
 * @test
 * @bug 8030976 8059226
 * @library /testlibrary /compiler/testlibrary /../../test/lib
44 45 46 47 48
 * @modules java.base/jdk.internal.org.objectweb.asm
 *          java.base/sun.misc
 *          java.compiler
 *          java.management
 *          jdk.jvmstat/sun.jvmstat.monitor
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
 * @build TestUnstableIfTrap com.oracle.java.testlibrary.* uncommontrap.Verifier
 * @run main ClassFileInstaller sun.hotspot.WhiteBox
 *                              sun.hotspot.WhiteBox$WhiteBoxPermission
 * @run main/othervm -Xbatch -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
 *                   -XX:+WhiteBoxAPI -XX:+LogCompilation
 *                   -XX:CompileCommand=compileonly,UnstableIfExecutable.test
 *                   -XX:LogFile=always_taken_not_fired.xml
 *                   TestUnstableIfTrap ALWAYS_TAKEN false
 * @run main/othervm -Xbatch -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
 *                   -XX:+WhiteBoxAPI -XX:+LogCompilation
 *                   -XX:CompileCommand=compileonly,UnstableIfExecutable.test
 *                   -XX:LogFile=always_taken_fired.xml
 *                   TestUnstableIfTrap ALWAYS_TAKEN true
 * @run main/othervm -Xbatch -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
 *                   -XX:+WhiteBoxAPI -XX:+LogCompilation
 *                   -XX:CompileCommand=compileonly,UnstableIfExecutable.test
 *                   -XX:LogFile=never_taken_not_fired.xml
 *                   TestUnstableIfTrap NEVER_TAKEN false
 * @run main/othervm -Xbatch -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
 *                   -XX:+WhiteBoxAPI -XX:+LogCompilation
 *                   -XX:CompileCommand=compileonly,UnstableIfExecutable.test
 *                   -XX:LogFile=never_taken_fired.xml
 *                   TestUnstableIfTrap NEVER_TAKEN true
 * @run main uncommontrap.Verifier always_taken_not_fired.xml
 *                                 always_taken_fired.xml
 *                                 never_taken_not_fired.xml
 *                                 never_taken_fired.xml
 */
public class TestUnstableIfTrap {
    private static final WhiteBox WB = WhiteBox.getWhiteBox();
    private static final String CLASS_NAME = "UnstableIfExecutable";
    private static final String METHOD_NAME = "test";
    private static final String FIELD_NAME = "field";
    private static final int ITERATIONS = 1_000_000;
    // There is no dependency on particular class file version, so it could be
    // set to any version (if you're updating this test for Java 42).
    private static final int CLASS_FILE_VERSION = 49;
    private static final int MAX_TIER = 4;
    // This test aimed to verify that uncommon trap with reason "unstable_if"
    // is emitted when method that contain control-flow divergence such that
    // one of two branches is never taken (and other one is taken always).
    // C2 will made a decision whether or not the branch was ever taken
    // depending on method's profile.
    // If profile was collected for a few method's invocations, then C2 will not
    // trust in branches' probabilities and the tested trap won't be emitted.
    // In fact, a method has to be invoked at least 40 time at the day when this
    // comment was written (see Parse::dynamic_branch_prediction for an actual
    // value). It would be to implementation dependent to use "40" as
    // a threshold value in the test, so in order to improve test's robustness
    // the threshold value is 1000: if the tested method was compiled by C2
    // before it was invoked 1000 times, then we won't verify that trap was
    // emitted and fired.
    private static final int MIN_INVOCATIONS_BEFORE_C2_COMPILATION = 1000;
    /**
     * Description of test case parameters and uncommon trap that will
     * be emitted during tested method compilation.
     */
    private static enum TestCaseName {
        ALWAYS_TAKEN(false, "taken always"),
        NEVER_TAKEN(true, "taken never");
        TestCaseName(boolean predicate, String comment) {
            this.predicate = predicate;
            this.comment = comment;
        }

        public final boolean predicate;
        public final String name = "unstable_if";
        public final String comment;
    }

    public static void main(String args[]) {
        if (args.length != 2) {
            throw new Error("Expected two arguments: test case name and a "
                    + "boolean determining if uncommon trap should be fired.");
        }
        test(TestCaseName.valueOf(args[0]), Boolean.valueOf(args[1]));
    }

    private static void test(TestCaseName testCase, boolean shouldBeFired) {
        Method testMethod;
        Label unstableIfLocation = new Label();
        boolean shouldBeEmitted;
        boolean compiledToEarly = false;

        try {
            Class testClass = ByteCodeLoader.load(CLASS_NAME,
                    generateTest(unstableIfLocation));
            testMethod = testClass.getDeclaredMethod(METHOD_NAME,
                    boolean.class);
            for (int i = 0; i < ITERATIONS; i++) {
                testMethod.invoke(null, testCase.predicate);
                if (i < MIN_INVOCATIONS_BEFORE_C2_COMPILATION
                        && isMethodCompiledByC2(testMethod)) {
                    compiledToEarly = true;
                    // There is no sense in further invocations: we already
                    // decided to avoid verification.
                    break;
                }
            }
            // We're checking that trap should be emitted (i.e. it was compiled
            // by C2) before the trap is fired, because otherwise the nmethod
            // will be deoptimized and isMethodCompiledByC2 will return false.
            shouldBeEmitted = isMethodCompiledByC2(testMethod)
                    && !compiledToEarly;
            if (shouldBeFired) {
                testMethod.invoke(null, !testCase.predicate);
            }
        } catch (ReflectiveOperationException e) {
            throw new Error("Test case should be generated, loaded and executed"
                    + " without any issues.", e);
        }

        shouldBeFired &= shouldBeEmitted;

        Properties properties = new Properties();
        properties.setProperty(Verifier.VERIFICATION_SHOULD_BE_SKIPPED,
                Boolean.toString(compiledToEarly));
        properties.setProperty(Verifier.UNCOMMON_TRAP_SHOULD_EMITTED,
                Boolean.toString(shouldBeEmitted));
        properties.setProperty(Verifier.UNCOMMON_TRAP_SHOULD_FIRED,
                Boolean.toString(shouldBeFired));
        properties.setProperty(Verifier.UNCOMMON_TRAP_NAME, testCase.name);
        properties.setProperty(Verifier.UNCOMMON_TRAP_COMMENT,
                testCase.comment);
        properties.setProperty(Verifier.UNCOMMON_TRAP_BCI,
                Integer.toString(unstableIfLocation.getOffset()));

        properties.list(System.out);

        File f = new File(WB.getStringVMFlag("LogFile") +
                Verifier.PROPERTIES_FILE_SUFFIX);
        try (FileWriter wr = new FileWriter(f)) {
            properties.store(wr, "");
        } catch (IOException e) {
            throw new Error("Unable to store test properties.", e);
        }
    }

    private static boolean isMethodCompiledByC2(Method m) {
        boolean isTiered = WB.getBooleanVMFlag("TieredCompilation");
        boolean isMethodCompiled = WB.isMethodCompiled(m);
        boolean isMethodCompiledAtMaxTier
                = WB.getMethodCompilationLevel(m) == MAX_TIER;

        return Platform.isServer() && isMethodCompiled
                && (!isTiered || isMethodCompiledAtMaxTier);
    }

    /**
     * Generates class with name {@code CLASS_NAME}, which will contain a
     * static method {@code METHOD_NAME}:
     *
     * <pre>{@code
     * public abstract class UnstableIfExecutable {
     *   private static int field = 0;
     *
     *   public static void test(boolean alwaysTrue) {
     *     if (alwaysTrue) {
     *       field++;
     *     } else {
     *       field--;
     *     }
     *   }
     * }
     * }</pre>
     *
     * @return generated bytecode.
     */
    private static byte[] generateTest(Label unstableIfLocation) {
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

        cw.visit(CLASS_FILE_VERSION, ACC_PUBLIC | ACC_ABSTRACT, CLASS_NAME,
                null, "java/lang/Object", null);

        cw.visitField(ACC_PUBLIC | ACC_STATIC | ACC_VOLATILE, FIELD_NAME,
                "I", null, Integer.valueOf(0));

        generateTestMethod(cw, unstableIfLocation);

        return cw.toByteArray();
    }

    private static void generateTestMethod(ClassVisitor cv,
            Label unstableIfLocation) {
        MethodVisitor mv = cv.visitMethod(ACC_PUBLIC | ACC_STATIC, METHOD_NAME,
                "(Z)V", null, null);
        mv.visitCode();

        Label end = new Label();
        Label falseBranch = new Label();

        // push "field" field's value and 1 to stack
        mv.visitFieldInsn(GETSTATIC, CLASS_NAME, FIELD_NAME, "I");
        mv.visitInsn(ICONST_1);
        // load argument's value
        mv.visitVarInsn(ILOAD, 0); // alwaysTrue
        // here is our unstable if
        mv.visitLabel(unstableIfLocation);
        mv.visitJumpInsn(IFEQ, falseBranch);
        // increment on "true"
        mv.visitInsn(IADD);
        mv.visitJumpInsn(GOTO, end);
        // decrement on "false"
        mv.visitLabel(falseBranch);
        mv.visitInsn(ISUB);
        mv.visitLabel(end);
        // bye bye
        mv.visitInsn(RETURN);

        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
}