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 (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
  29  * @library /testlibrary /../../test/lib /
  30  * @compile ../common/CompilerToVMHelper.java
  31  * @run main ClassFileInstaller jdk.vm.ci.hotspot.CompilerToVMHelper
  32  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  33  *      -Xbootclasspath/a:.
  34  *      compiler.jvmci.compilerToVM.GetExceptionTableTest
  35  */
  36 
  37 package compiler.jvmci.compilerToVM;
  38 
  39 import compiler.jvmci.common.CTVMUtilities;
  40 import java.io.IOException;
  41 import java.lang.reflect.Executable;
  42 import java.net.Socket;
  43 import java.util.HashMap;
  44 import java.util.Map;
  45 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl;
  46 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  47 import jdk.test.lib.Asserts;
  48 
  49 public class GetExceptionTableTest {
  50 
  51     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         HotSpotResolvedJavaMethodImpl 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