1 /*
2  * Copyright (c) 2017, 2018, 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 package org.graalvm.compiler.replacements.test;
26 
27 import static org.graalvm.compiler.core.common.GraalOptions.RemoveNeverExecutedCode;
28 import static org.graalvm.compiler.core.common.GraalOptions.UseExceptionProbability;
29 import static org.graalvm.compiler.core.common.GraalOptions.UseTypeCheckHints;
30 
31 import org.graalvm.compiler.api.directives.GraalDirectives;
32 import org.graalvm.compiler.core.phases.HighTier;
33 import org.graalvm.compiler.core.test.GraalCompilerTest;
34 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
35 import org.graalvm.compiler.options.OptionValues;
36 import org.junit.Ignore;
37 import org.junit.Test;
38 
39 import jdk.vm.ci.meta.ResolvedJavaMethod;
40 
41 public class NestedExceptionHandlerTest extends GraalCompilerTest {
42 
43     @BytecodeParserNeverInline(invokeWithException = true)
foo()44     public static void foo() {
45     }
46 
47     @BytecodeParserNeverInline(invokeWithException = true)
bar()48     public static void bar() {
49         throw new NegativeArraySizeException();
50     }
51 
nestedExceptionHandler()52     public static int nestedExceptionHandler() {
53         int flag = 0;
54         try {
55             try {
56                 try {
57                     foo();
58                 } catch (NegativeArraySizeException e) {
59                     flag = -1;
60                 }
61                 bar();
62             } catch (NullPointerException e) {
63                 flag = -2;
64             }
65         } catch (Throwable e) {
66             GraalDirectives.deoptimize();
67         }
68         return flag;
69     }
70 
71     @Test
testNestedExceptionHandler()72     public void testNestedExceptionHandler() {
73         test(new OptionValues(getInitialOptions(), HighTier.Options.Inline, false), "nestedExceptionHandler");
74     }
75 
76     @SuppressWarnings("try")
snippet1()77     public static String snippet1() {
78         try {
79             synchronized (String.class) {
80                 try (AutoCloseable scope = null) {
81                     return "RETURN";
82                 } catch (Throwable t) {
83                     return t.toString();
84                 }
85             }
86         } finally {
87             raise();
88         }
89     }
90 
raise()91     public static void raise() {
92         throw new RuntimeException();
93     }
94 
95     @SuppressWarnings("try")
snippet2()96     public static String snippet2() {
97         try {
98             synchronized (String.class) {
99                 try (AutoCloseable scope = null) {
100                     return performCompilation();
101                 } catch (Throwable t) {
102                     return t.toString();
103                 }
104             }
105         } finally {
106             synchronized (String.class) {
107                 String.class.toString();
108             }
109         }
110     }
111 
performCompilation()112     private static String performCompilation() {
113         return "passed";
114     }
115 
116     @Ignore("https://bugs.eclipse.org/bugs/show_bug.cgi?id=533187")
117     @Test
testSnippet1()118     public void testSnippet1() {
119         OptionValues options = parseAllCodeWithoutInlining();
120         ResolvedJavaMethod method = getResolvedJavaMethod("snippet1");
121         parseEager(method, AllowAssumptions.YES, options);
122     }
123 
124     @Ignore("https://bugs.eclipse.org/bugs/show_bug.cgi?id=533187")
125     @Test
testSnippet2()126     public void testSnippet2() {
127         OptionValues options = parseAllCodeWithoutInlining();
128         ResolvedJavaMethod method = getResolvedJavaMethod("snippet2");
129         parseEager(method, AllowAssumptions.YES, options);
130     }
131 
parseAllCodeWithoutInlining()132     private static OptionValues parseAllCodeWithoutInlining() {
133         OptionValues options = new OptionValues(getInitialOptions(),
134                         UseTypeCheckHints, false,
135                         UseExceptionProbability, false,
136                         RemoveNeverExecutedCode, false);
137         return options;
138     }
139 }
140