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