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 /**
  26  * @test
  27  * @bug 8136421
  28  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
  29  * @library /testlibrary /test/lib /
  30  * @library ../common/patches
  31  * @modules java.base/jdk.internal.org.objectweb.asm
  32  *          java.base/jdk.internal.org.objectweb.asm.tree

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