1 /*
2  * Copyright (c) 2015, 2019, 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
26  * @bug 8136421
27  * @requires vm.jvmci
28  * @library /test/lib /
29  * @library ../common/patches
30  * @modules java.base/jdk.internal.misc
31  * @modules java.base/jdk.internal.org.objectweb.asm
32  *          java.base/jdk.internal.org.objectweb.asm.tree
33  *          jdk.internal.vm.ci/jdk.vm.ci.hotspot
34  *          jdk.internal.vm.ci/jdk.vm.ci.code
35  *          jdk.internal.vm.ci/jdk.vm.ci.meta
36  *          jdk.internal.vm.ci/jdk.vm.ci.runtime
37  *
38  * @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
39  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
40  *                   -XX:-UseJVMCICompiler
41  *                   compiler.jvmci.compilerToVM.GetStackTraceElementTest
42  */
43 
44 package compiler.jvmci.compilerToVM;
45 
46 import compiler.jvmci.common.CTVMUtilities;
47 import compiler.jvmci.common.testcases.TestCase;
48 import jdk.test.lib.Asserts;
49 import jdk.vm.ci.hotspot.CompilerToVMHelper;
50 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
51 
52 import java.lang.reflect.Executable;
53 import java.lang.reflect.Method;
54 import java.lang.reflect.Modifier;
55 import java.util.HashMap;
56 import java.util.Map;
57 
58 public class GetStackTraceElementTest {
59 
main(String[] args)60     public static void main(String[] args) {
61         Map<Executable, int[]> testCases = createTestCases();
62         testCases.forEach(GetStackTraceElementTest::runSanityTest);
63     }
64 
runSanityTest(Executable aMethod, int[] bcis)65     private static void runSanityTest(Executable aMethod, int[] bcis) {
66         HotSpotResolvedJavaMethod method = CTVMUtilities
67                 .getResolvedMethod(aMethod);
68         String className = aMethod.getDeclaringClass().getName();
69         String methodName = aMethod.getName().equals(className)
70                 ? "<init>"
71                 : aMethod.getName();
72         String fileName = getFileName(className);
73         Map<Integer, Integer> bciWithLineNumber = CTVMUtilities
74                 .getBciToLineNumber(aMethod);
75         boolean isNative = Modifier.isNative(aMethod.getModifiers());
76         int lineNumber = -1;
77         for (int bci : bcis) {
78             StackTraceElement ste = CompilerToVMHelper
79                     .getStackTraceElement(method, bci);
80             Asserts.assertNotNull(ste, aMethod + " : got null StackTraceElement"
81                     + " at bci " + bci);
82             Asserts.assertEQ(className, ste.getClassName(), aMethod
83                     + " : unexpected class name");
84             Asserts.assertEQ(fileName, ste.getFileName(), aMethod
85                     + " : unexpected filename");
86             Asserts.assertEQ(methodName, ste.getMethodName(), aMethod
87                     + " : unexpected method name");
88             Asserts.assertEQ(isNative, ste.isNativeMethod(), aMethod
89                     + " : unexpected 'isNative' value");
90             if (bciWithLineNumber.size() > 0) {
91                 if (bciWithLineNumber.containsKey(bci)) {
92                     lineNumber = bciWithLineNumber.get(bci);
93                 }
94                 Asserts.assertEQ(lineNumber, ste.getLineNumber(), aMethod
95                         + " : unexpected line number");
96             } else {
97                 // native and abstract function
98                 Asserts.assertGT(0, ste.getLineNumber(),
99                         aMethod + " : unexpected line number for abstract "
100                                 + "or native method");
101             }
102         }
103 
104     }
105 
getFileName(String className)106     private static String getFileName(String className) {
107         int lastDot = className.lastIndexOf('.');
108         int firstDol = className.contains("$")
109                 ? className.indexOf('$')
110                 : className.length();
111         return className.substring(lastDot + 1, firstDol) + ".java";
112     }
113 
createTestCases()114     private static Map<Executable, int[]> createTestCases() {
115         Map<Executable, int[]> testCases = new HashMap<>();
116 
117         try {
118             Class<?> aClass = DummyClass.class;
119             Method aMethod = aClass.getDeclaredMethod("dummyInstanceFunction");
120             int[] bci = new int[] {0, 2, 3, 6, 7, 8, 11, 13, 15, 16, 17, 18};
121             testCases.put(aMethod, bci);
122 
123             aMethod = aClass.getDeclaredMethod("dummyEmptyFunction");
124             bci = new int[] {0};
125             testCases.put(aMethod, bci);
126 
127             aMethod = aClass.getDeclaredMethod("nativeFunction");
128             bci = new int[] {0};
129             testCases.put(aMethod, bci);
130 
131             TestCase.getAllExecutables()
132                     .forEach(c -> testCases.put(c, new int[] {0}));
133         } catch (NoSuchMethodException e) {
134             throw new Error("TEST BUG : test method not found", e);
135         }
136         return testCases;
137     }
138 
139     private class DummyClass {
dummyInstanceFunction()140         public int dummyInstanceFunction() {
141             String str1 = "123123123";
142             double x = 3.14;
143             int y = Integer.parseInt(str1);
144 
145             return y / (int)x;
146         }
147 
dummyEmptyFunction()148         public void dummyEmptyFunction() {}
149 
nativeFunction()150         public native void nativeFunction();
151     }
152 }
153