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