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