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