GetExceptionTableTest.java revision 9287:40bd4478a362
1145519Sdarrenr/*
2145510Sdarrenr * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3170268Sdarrenr * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4170268Sdarrenr *
5170268Sdarrenr * This code is free software; you can redistribute it and/or modify it
6170268Sdarrenr * under the terms of the GNU General Public License version 2 only, as
7170268Sdarrenr * published by the Free Software Foundation.
8170268Sdarrenr *
9170268Sdarrenr * This code is distributed in the hope that it will be useful, but WITHOUT
10170268Sdarrenr * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11145510Sdarrenr * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12145510Sdarrenr * version 2 for more details (a copy is included in the LICENSE file that
13145510Sdarrenr * accompanied this code).
14145510Sdarrenr *
15145510Sdarrenr * You should have received a copy of the GNU General Public License version
16145510Sdarrenr * 2 along with this work; if not, write to the Free Software Foundation,
17145510Sdarrenr * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18145510Sdarrenr *
19145510Sdarrenr * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20145510Sdarrenr * or visit www.oracle.com if you need additional information or have any
21145510Sdarrenr * questions.
22145510Sdarrenr *
23145510Sdarrenr */
24145510Sdarrenr
25145510Sdarrenr/**
26145510Sdarrenr * @test
27145510Sdarrenr * @bug 8136421
28145510Sdarrenr * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
29145510Sdarrenr * @library /testlibrary /../../test/lib /
30145510Sdarrenr * @compile ../common/CompilerToVMHelper.java
31145510Sdarrenr * @run main ClassFileInstaller jdk.vm.ci.hotspot.CompilerToVMHelper
32145510Sdarrenr * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
33145510Sdarrenr *      -Xbootclasspath/a:.
34145510Sdarrenr *      compiler.jvmci.compilerToVM.GetExceptionTableTest
35145510Sdarrenr */
36145510Sdarrenr
37145510Sdarrenrpackage compiler.jvmci.compilerToVM;
38145510Sdarrenr
39145510Sdarrenrimport compiler.jvmci.common.CTVMUtilities;
40145510Sdarrenrimport java.io.IOException;
41145510Sdarrenrimport java.lang.reflect.Executable;
42145510Sdarrenrimport java.net.Socket;
43145510Sdarrenrimport java.util.HashMap;
44145510Sdarrenrimport java.util.Map;
45145510Sdarrenrimport jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
46145510Sdarrenrimport jdk.vm.ci.hotspot.CompilerToVMHelper;
47145510Sdarrenrimport jdk.test.lib.Asserts;
48145510Sdarrenr
49145510Sdarrenrpublic class GetExceptionTableTest {
50145510Sdarrenr
51145510Sdarrenr    public static final int TRY_CATCH_COUNT = 3;
52    public static final int TRY_CATCH_FINALLY_COUNT = 8;
53    public static final int TRY_WITH_RESOURCES_COUNT = 6;
54    public static final int EMPTY_COUNT = 0;
55
56    public static void main(String[] args) {
57        Map<Executable, Integer> testCases = createTestCases();
58        testCases.forEach(GetExceptionTableTest::runSanityTest);
59    }
60
61    private static Map<Executable, Integer> createTestCases() {
62        HashMap<Executable, Integer> methods = new HashMap<>();
63        try {
64            Class<?> aClass = GetExceptionTableTest.DummyClass.class;
65            methods.put(aClass.getMethod("tryCatchDummy"), TRY_CATCH_COUNT);
66            methods.put(aClass.getMethod("tryCatchFinallyDummy"),
67                    TRY_CATCH_FINALLY_COUNT);
68            methods.put(aClass.getMethod("tryWithResourcesDummy"),
69                    TRY_WITH_RESOURCES_COUNT);
70            methods.put(aClass.getMethod("emptyFunction"), EMPTY_COUNT);
71        } catch (NoSuchMethodException e) {
72            throw new Error("TEST BUG", e);
73        }
74        return methods;
75    }
76
77    private static void runSanityTest(Executable aMethod,
78                                      Integer expectedTableLength) {
79        HotSpotResolvedJavaMethod method = CTVMUtilities
80                .getResolvedMethod(aMethod);
81        int tableLength = CompilerToVMHelper.getExceptionTableLength(method);
82        Asserts.assertEQ(tableLength, expectedTableLength, aMethod
83                + " incorrect exception table length.");
84
85        long tableStart = CompilerToVMHelper.getExceptionTableStart(method);
86        if (tableLength > 0) {
87            Asserts.assertNE(tableStart, 0L, aMethod + " exception table starts "
88                    + "at 0.");
89        }
90    }
91
92    private static class DummyClass {
93        public static void emptyFunction() {}
94        public static void tryCatchDummy() throws Throwable {
95            try {
96                throw new Exception("Dummy exception");
97            } catch (ArithmeticException ex) {
98                throw new IOException(ex.getMessage());
99            } catch (IOException ex) {
100                throw new Exception(ex);
101            } catch (Exception ex) {
102                throw new Exception(ex);
103            }
104        }
105
106        public int tryCatchFinallyDummy() {
107            // 4 times catch/finally = 8 catch-blocks and finally-blocks
108            try {
109                throw new Exception("Dummy exception");
110            } catch (IndexOutOfBoundsException ex) {
111                return 1;
112            } catch (ArithmeticException ex) {
113                return 2;
114            } catch (IOException ex) {
115                return 3;
116            } catch (Exception ex) {
117                return 4;
118            } finally {
119                return 0;
120            }
121        }
122
123        public static int tryWithResourcesDummy() throws Throwable {
124            try (Socket socket = new Socket()) {
125                throw new Exception("Dummy exception");
126            } catch (ArithmeticException ex) {
127                return 1;
128            } catch (IOException ex) {
129                return 2;
130            } catch (Exception ex) {
131                return 3;
132            }
133        }
134    }
135}
136
137