1 /*
   2  * Copyright (c) 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 /**
  26  * @test
  27  * @bug 8136421
  28  * @requires (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.simpleArch == "aarch64")
  29  * @library /testlibrary /test/lib /
  30  * @library ../common/patches
  31  * @modules java.base/jdk.internal.misc
  32  * @modules java.base/jdk.internal.org.objectweb.asm
  33  *          java.base/jdk.internal.org.objectweb.asm.tree
  34  *          jdk.vm.ci/jdk.vm.ci.hotspot
  35  *          jdk.vm.ci/jdk.vm.ci.code
  36  * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
  37  * @build compiler.jvmci.compilerToVM.GetExceptionTableTest
  38  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  39  *                   compiler.jvmci.compilerToVM.GetExceptionTableTest
  40  */
  41 
  42 package compiler.jvmci.compilerToVM;
  43 
  44 import compiler.jvmci.common.CTVMUtilities;
  45 import jdk.test.lib.Asserts;
  46 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  47 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  48 
  49 import java.io.IOException;
  50 import java.lang.reflect.Executable;
  51 import java.net.Socket;
  52 import java.util.HashMap;
  53 import java.util.Map;
  54 
  55 public class GetExceptionTableTest {
  56 
  57     public static final int TRY_CATCH_COUNT = 3;
  58     public static final int TRY_CATCH_FINALLY_COUNT = 8;
  59     public static final int TRY_WITH_RESOURCES_COUNT = 6;
  60     public static final int EMPTY_COUNT = 0;
  61 
  62     public static void main(String[] args) {
  63         Map<Executable, Integer> testCases = createTestCases();
  64         testCases.forEach(GetExceptionTableTest::runSanityTest);
  65     }
  66 
  67     private static Map<Executable, Integer> createTestCases() {
  68         HashMap<Executable, Integer> methods = new HashMap<>();
  69         try {
  70             Class<?> aClass = GetExceptionTableTest.DummyClass.class;
  71             methods.put(aClass.getMethod("tryCatchDummy"), TRY_CATCH_COUNT);
  72             methods.put(aClass.getMethod("tryCatchFinallyDummy"),
  73                     TRY_CATCH_FINALLY_COUNT);
  74             methods.put(aClass.getMethod("tryWithResourcesDummy"),
  75                     TRY_WITH_RESOURCES_COUNT);
  76             methods.put(aClass.getMethod("emptyFunction"), EMPTY_COUNT);
  77         } catch (NoSuchMethodException e) {
  78             throw new Error("TEST BUG", e);
  79         }
  80         return methods;
  81     }
  82 
  83     private static void runSanityTest(Executable aMethod,
  84                                       Integer expectedTableLength) {
  85         HotSpotResolvedJavaMethod method = CTVMUtilities
  86                 .getResolvedMethod(aMethod);
  87         int tableLength = CompilerToVMHelper.getExceptionTableLength(method);
  88         Asserts.assertEQ(tableLength, expectedTableLength, aMethod
  89                 + " incorrect exception table length.");
  90 
  91         long tableStart = CompilerToVMHelper.getExceptionTableStart(method);
  92         if (tableLength > 0) {
  93             Asserts.assertNE(tableStart, 0L, aMethod + " exception table starts "
  94                     + "at 0.");
  95         }
  96     }
  97 
  98     private static class DummyClass {
  99         public static void emptyFunction() {}
 100         public static void tryCatchDummy() throws Throwable {
 101             try {
 102                 throw new Exception("Dummy exception");
 103             } catch (ArithmeticException ex) {
 104                 throw new IOException(ex.getMessage());
 105             } catch (IOException ex) {
 106                 throw new Exception(ex);
 107             } catch (Exception ex) {
 108                 throw new Exception(ex);
 109             }
 110         }
 111 
 112         public int tryCatchFinallyDummy() {
 113             // 4 times catch/finally = 8 catch-blocks and finally-blocks
 114             try {
 115                 throw new Exception("Dummy exception");
 116             } catch (IndexOutOfBoundsException ex) {
 117                 return 1;
 118             } catch (ArithmeticException ex) {
 119                 return 2;
 120             } catch (IOException ex) {
 121                 return 3;
 122             } catch (Exception ex) {
 123                 return 4;
 124             } finally {
 125                 return 0;
 126             }
 127         }
 128 
 129         public static int tryWithResourcesDummy() throws Throwable {
 130             try (Socket socket = new Socket()) {
 131                 throw new Exception("Dummy exception");
 132             } catch (ArithmeticException ex) {
 133                 return 1;
 134             } catch (IOException ex) {
 135                 return 2;
 136             } catch (Exception ex) {
 137                 return 3;
 138             }
 139         }
 140     }
 141 }
 142