1 /* 2 * Copyright (c) 2011, 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 package org.graalvm.compiler.hotspot.test; 24 25 import org.graalvm.compiler.core.test.GraalCompilerTest; 26 import org.graalvm.compiler.nodes.StructuredGraph; 27 import org.graalvm.compiler.nodes.extended.ForeignCallNode; 28 import org.graalvm.compiler.options.OptionValues; 29 import org.junit.Assume; 30 import org.junit.Test; 31 32 import jdk.vm.ci.code.InstalledCode; 33 import jdk.vm.ci.meta.ProfilingInfo; 34 import jdk.vm.ci.meta.ResolvedJavaMethod; 35 import jdk.vm.ci.meta.TriState; 36 37 public class ExplicitExceptionTest extends GraalCompilerTest { 38 39 private int expectedForeignCallCount; 40 41 /** 42 * Determines if profiling info for {@code method} indicates an exception was thrown somewhere 43 * in the method. In the case of the {@code -Xcomp} VM option, interpreter execution can be 44 * skipped altogether and other execution engines (e.g., C1) may not record seen exceptions in a 45 * method profile. 46 */ 47 private static boolean exceptionWasSeen(ResolvedJavaMethod method) { 48 ProfilingInfo profilingInfo = method.getProfilingInfo(); 49 if (profilingInfo != null) { 50 for (int i = 0; i < profilingInfo.getCodeSize(); i++) { 51 if (profilingInfo.getExceptionSeen(i) == TriState.TRUE) { 52 return true; 53 } 54 } 55 } 56 return false; 57 } 58 59 @Override 60 protected InstalledCode getCode(ResolvedJavaMethod method, StructuredGraph graph, boolean forceCompile, boolean installAsDefault, OptionValues options) { 61 InstalledCode installedCode = super.getCode(method, graph, forceCompile, installAsDefault, options); 62 Assume.assumeTrue(exceptionWasSeen(method)); 63 assertDeepEquals(expectedForeignCallCount, lastCompiledGraph.getNodes().filter(ForeignCallNode.class).count()); 64 return installedCode; 65 } 66 67 public static int testAIOOBESnippet(int[] array) { 68 return array[10]; 69 } 70 71 @Test 72 public void testAIOOBE() { 73 int[] array = new int[4]; 74 for (int i = 0; i < 10000; i++) { 75 try { 76 testAIOOBESnippet(array); 77 } catch (ArrayIndexOutOfBoundsException e) { 78 // nothing to do 79 } 80 } 81 expectedForeignCallCount = 2; 82 test("testAIOOBESnippet", array); 83 } 84 85 public static int testNPEArraySnippet(int[] array) { 86 return array[10]; 87 } 88 89 @Test 90 public void testNPEArray() { 91 int[] array = null; 92 for (int i = 0; i < 10000; i++) { 93 try { 94 testNPEArraySnippet(array); 95 } catch (NullPointerException e) { 96 // nothing to do 97 } 98 } 99 expectedForeignCallCount = 2; 100 test("testNPEArraySnippet", array); 101 } 102 103 private static class TestClass { 104 int field; 105 } 106 107 public static int testNPESnippet(TestClass obj) { 108 return obj.field; 109 } 110 111 @SuppressWarnings("unused") 112 @Test 113 public void testNPE() { 114 new TestClass(); 115 TestClass obj = null; 116 for (int i = 0; i < 10000; i++) { 117 try { 118 testNPESnippet(obj); 119 } catch (NullPointerException e) { 120 // nothing to do 121 } 122 } 123 expectedForeignCallCount = 1; 124 test("testNPESnippet", obj); 125 } 126 127 }