1 /*
2  * Copyright (c) 2011, 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 org.graalvm.compiler.core.phases.HighTier;
28 import org.graalvm.compiler.core.test.GraalCompilerTest;
29 import org.graalvm.compiler.nodes.StructuredGraph;
30 import org.graalvm.compiler.nodes.StructuredGraph.Builder;
31 import org.graalvm.compiler.nodes.ValueNode;
32 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
33 import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin.InlineInfo;
34 import org.graalvm.compiler.nodes.java.ExceptionObjectNode;
35 import org.graalvm.compiler.options.OptionValues;
36 import org.graalvm.compiler.phases.PhaseSuite;
37 import org.graalvm.compiler.phases.tiers.HighTierContext;
38 import org.graalvm.compiler.phases.tiers.Suites;
39 import org.junit.Assert;
40 import org.junit.Test;
41 
42 import jdk.vm.ci.meta.ResolvedJavaMethod;
43 
44 /**
45  * Tests compilation of a hot exception handler.
46  */
47 public class CompiledExceptionHandlerTest extends GraalCompilerTest {
48 
49     @Override
50     @SuppressWarnings("try")
createSuites(OptionValues options)51     protected Suites createSuites(OptionValues options) {
52         return super.createSuites(new OptionValues(options, HighTier.Options.Inline, false));
53     }
54 
55     @Override
bytecodeParserShouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args)56     protected InlineInfo bytecodeParserShouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
57         /*
58          * We don't care whether other invokes are inlined or not, but we definitely don't want
59          * another explicit exception handler in the graph.
60          */
61         return InlineInfo.DO_NOT_INLINE_NO_EXCEPTION;
62     }
63 
64     @Override
parse(Builder builder, PhaseSuite<HighTierContext> graphBuilderSuite)65     protected StructuredGraph parse(Builder builder, PhaseSuite<HighTierContext> graphBuilderSuite) {
66         StructuredGraph graph = super.parse(builder, graphBuilderSuite);
67         int handlers = graph.getNodes().filter(ExceptionObjectNode.class).count();
68         Assert.assertEquals(1, handlers);
69         return graph;
70     }
71 
72     @BytecodeParserNeverInline(invokeWithException = true)
raiseExceptionSimple(String s)73     private static void raiseExceptionSimple(String s) {
74         throw new RuntimeException("Raising exception with message \"" + s + "\"");
75     }
76 
77     @Test
test1()78     public void test1() {
79         test("test1Snippet", "a string");
80         test("test1Snippet", (String) null);
81     }
82 
test1Snippet(String message)83     public static String test1Snippet(String message) {
84         if (message != null) {
85             try {
86                 raiseExceptionSimple(message);
87             } catch (Exception e) {
88                 return message + e.getMessage();
89             }
90         }
91         return null;
92     }
93 
94     @BytecodeParserNeverInline(invokeWithException = true)
raiseException(String m1, String m2, String m3, String m4, String m5)95     private static void raiseException(String m1, String m2, String m3, String m4, String m5) {
96         throw new RuntimeException(m1 + m2 + m3 + m4 + m5);
97     }
98 
99     @Test
test2()100     public void test2() {
101         test("test2Snippet", "m1", "m2", "m3", "m4", "m5");
102         test("test2Snippet", null, "m2", "m3", "m4", "m5");
103     }
104 
test2Snippet(String m1, String m2, String m3, String m4, String m5)105     public static String test2Snippet(String m1, String m2, String m3, String m4, String m5) {
106         if (m1 != null) {
107             try {
108                 raiseException(m1, m2, m3, m4, m5);
109             } catch (Exception e) {
110                 return m5 + m4 + m3 + m2 + m1;
111             }
112         }
113         return m4 + m3;
114     }
115 }
116