1 /*
   2  * Copyright (c) 2014, 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 CorrectnessTest
  26  * @bug 8038418
  27  * @summary Tests correctness of type usage with type profiling and speculations
  28  * @requires vm.flavor == "server"
  29  * @library /test/lib /
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  *
  33  * @build sun.hotspot.WhiteBox
  34  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  35  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  36  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions
  37  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  38  *                   -XX:TypeProfileLevel=222 -XX:+UseTypeSpeculation
  39  *                   -XX:CompileCommand=exclude,compiler.types.correctness.execution.*::methodNotToCompile
  40  *                   -XX:CompileCommand=dontinline,compiler.types.correctness.scenarios.Scenario::collectReturnType
  41  *                   compiler.types.correctness.CorrectnessTest RETURN
  42  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions
  43  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  44  *                   -XX:TypeProfileLevel=222 -XX:+UseTypeSpeculation
  45  *                   -XX:CompileCommand=exclude,compiler.types.correctness.execution.*::methodNotToCompile
  46  *                   -XX:CompileCommand=dontinline,compiler.types.correctness.scenarios.Scenario::collectReturnType
  47  *                   compiler.types.correctness.CorrectnessTest PARAMETERS
  48  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions
  49  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  50  *                   -XX:TypeProfileLevel=222 -XX:+UseTypeSpeculation
  51  *                   -XX:CompileCommand=exclude,compiler.types.correctness.execution.*::methodNotToCompile
  52  *                   -XX:CompileCommand=dontinline,compiler.types.correctness.scenarios.Scenario::collectReturnType
  53  *                   compiler.types.correctness.CorrectnessTest ARGUMENTS
  54  */
  55 
  56 package compiler.types.correctness;
  57 
  58 import compiler.types.correctness.execution.Execution;
  59 import compiler.types.correctness.execution.MethodHandleDelegate;
  60 import compiler.types.correctness.execution.TypeConflict;
  61 import compiler.types.correctness.execution.TypeProfile;
  62 import compiler.types.correctness.hierarchies.DefaultMethodInterface;
  63 import compiler.types.correctness.hierarchies.DefaultMethodInterface2;
  64 import compiler.types.correctness.hierarchies.Linear;
  65 import compiler.types.correctness.hierarchies.Linear2;
  66 import compiler.types.correctness.hierarchies.NullableType;
  67 import compiler.types.correctness.hierarchies.OneRank;
  68 import compiler.types.correctness.hierarchies.TypeHierarchy;
  69 import compiler.types.correctness.scenarios.ArrayCopy;
  70 import compiler.types.correctness.scenarios.ArrayReferenceStore;
  71 import compiler.types.correctness.scenarios.CheckCast;
  72 import compiler.types.correctness.scenarios.ClassIdentity;
  73 import compiler.types.correctness.scenarios.ClassInstanceOf;
  74 import compiler.types.correctness.scenarios.ClassIsInstance;
  75 import compiler.types.correctness.scenarios.ProfilingType;
  76 import compiler.types.correctness.scenarios.ReceiverAtInvokes;
  77 import compiler.types.correctness.scenarios.Scenario;
  78 import jdk.test.lib.Asserts;
  79 import jdk.test.lib.Platform;
  80 import sun.hotspot.WhiteBox;
  81 
  82 import java.lang.reflect.Method;
  83 import java.util.ArrayList;
  84 import java.util.List;
  85 import java.util.function.BiFunction;
  86 
  87 public class CorrectnessTest {
  88     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  89 
  90     public static void main(String[] args) {
  91         if (!Platform.isServer()) {
  92             throw new Error("TESTBUG: Not server VM");
  93         }
  94         Asserts.assertGTE(args.length, 1);
  95         ProfilingType profilingType = ProfilingType.valueOf(args[0]);
  96         if (runTests(profilingType)) {
  97             System.out.println("ALL TESTS PASSED");
  98         } else {
  99             throw new RuntimeException("SOME TESTS FAILED");
 100         }
 101     }
 102 
 103     @SuppressWarnings("unchecked")
 104     public static boolean runTests(ProfilingType profilingType) {
 105         boolean result = true;
 106 
 107         List<Execution> executionList = new ArrayList<>();
 108         executionList.add(new TypeConflict());
 109         executionList.add(new TypeProfile());
 110         for (int i = 0, n = executionList.size(); i < n; i++) {
 111             executionList.add(new MethodHandleDelegate(executionList.get(i)));
 112         }
 113 
 114         List<TypeHierarchy> hierarchyList = new ArrayList<>();
 115         hierarchyList.add(new DefaultMethodInterface.Hierarchy());
 116         hierarchyList.add(new DefaultMethodInterface2.Hierarchy());
 117         hierarchyList.add(new Linear.Hierarchy());
 118         hierarchyList.add(new Linear2.Hierarchy());
 119         hierarchyList.add(new OneRank.Hierarchy());
 120         for (int i = 0, n = hierarchyList.size(); i < n; i++) {
 121             hierarchyList.add(new NullableType(hierarchyList.get(i)));
 122         }
 123 
 124         List<BiFunction<ProfilingType, TypeHierarchy, Scenario<?, ?>>> testCasesConstructors
 125                 = new ArrayList<>();
 126         testCasesConstructors.add(ArrayCopy::new);
 127         testCasesConstructors.add(ArrayReferenceStore::new);
 128         testCasesConstructors.add(ClassIdentity::new);
 129         testCasesConstructors.add(ClassInstanceOf::new);
 130         testCasesConstructors.add(ClassIsInstance::new);
 131         testCasesConstructors.add(ReceiverAtInvokes::new);
 132         testCasesConstructors.add(CheckCast::new);
 133 
 134         for (TypeHierarchy hierarchy : hierarchyList) {
 135             for (BiFunction<ProfilingType, TypeHierarchy, Scenario<?, ?>> constructor : testCasesConstructors) {
 136                 for (Execution execution : executionList) {
 137                     Scenario<?, ?> scenario = constructor.apply(profilingType, hierarchy);
 138                     if (scenario.isApplicable()) {
 139                         result &= executeTest(hierarchy, execution, scenario);
 140                     }
 141                 }
 142             }
 143         }
 144         return result;
 145     }
 146 
 147     /**
 148      * Executes test case
 149      *
 150      * @param hierarchy type hierarchy for the test
 151      * @param execution execution scenario
 152      * @param scenario  test scenario executed with given Execution
 153      */
 154     private static boolean executeTest(TypeHierarchy hierarchy, Execution execution, Scenario<?, ?> scenario) {
 155         boolean testCaseResult = false;
 156         String testName = hierarchy.getClass().getName() + " :: " + scenario.getName() + " @ " + execution.getName();
 157         clearAllMethodsState(scenario.getClass());
 158         try {
 159             execution.execute(scenario);
 160             testCaseResult = true;
 161         } catch (Exception e) {
 162             System.err.println(testName + " failed with exception " + e);
 163             e.printStackTrace();
 164         }
 165         System.out.println((testCaseResult ? "PASSED: " : "FAILED: ") + testName);
 166         return testCaseResult;
 167     }
 168 
 169     private static void clearAllMethodsState(Class aClass) {
 170         while (aClass != null) {
 171             for (Method m : aClass.getDeclaredMethods()) {
 172                 WHITE_BOX.clearMethodState(m);
 173             }
 174             aClass = aClass.getSuperclass();
 175         }
 176     }
 177 }