1 /* 2 * Copyright (c) 2012, 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 package org.graalvm.compiler.hotspot.stubs; 24 25 import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfig.INJECTED_VMCONFIG; 26 import static org.graalvm.compiler.hotspot.nodes.JumpToExceptionHandlerNode.jumpToExceptionHandler; 27 import static org.graalvm.compiler.hotspot.nodes.PatchReturnAddressNode.patchReturnAddress; 28 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.readExceptionOop; 29 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.readExceptionPc; 30 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.registerAsWord; 31 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.writeExceptionOop; 32 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.writeExceptionPc; 33 import static org.graalvm.compiler.hotspot.stubs.StubUtil.cAssertionsEnabled; 34 import static org.graalvm.compiler.hotspot.stubs.StubUtil.decipher; 35 import static org.graalvm.compiler.hotspot.stubs.StubUtil.fatal; 36 import static org.graalvm.compiler.hotspot.stubs.StubUtil.newDescriptor; 37 import static org.graalvm.compiler.hotspot.stubs.StubUtil.printf; 38 39 import org.graalvm.compiler.api.replacements.Fold; 40 import org.graalvm.compiler.api.replacements.Fold.InjectedParameter; 41 import org.graalvm.compiler.api.replacements.Snippet; 42 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter; 43 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor; 44 import org.graalvm.compiler.debug.Assertions; 45 import org.graalvm.compiler.graph.Node.ConstantNodeParameter; 46 import org.graalvm.compiler.graph.Node.NodeIntrinsic; 47 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 48 import org.graalvm.compiler.hotspot.HotSpotBackend; 49 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage; 50 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 51 import org.graalvm.compiler.hotspot.nodes.StubForeignCallNode; 52 import org.graalvm.compiler.options.OptionValues; 53 import org.graalvm.compiler.word.Word; 54 import org.graalvm.word.WordFactory; 55 56 import jdk.vm.ci.code.Register; 57 58 /** 59 * Stub called by the {@linkplain GraalHotSpotVMConfig#MARKID_EXCEPTION_HANDLER_ENTRY exception 60 * handler entry point} in a compiled method. This entry point is used when returning to a method to 61 * handle an exception thrown by a callee. It is not used for routing implicit exceptions. 62 * Therefore, it does not need to save any registers as HotSpot uses a caller save convention. 63 * <p> 64 * The descriptor for a call to this stub is {@link HotSpotBackend#EXCEPTION_HANDLER}. 65 */ 66 public class ExceptionHandlerStub extends SnippetStub { 67 68 public ExceptionHandlerStub(OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) { 69 super("exceptionHandler", options, providers, linkage); 70 } 71 72 /** 73 * This stub is called when returning to a method to handle an exception thrown by a callee. It 74 * is not used for routing implicit exceptions. Therefore, it does not need to save any 75 * registers as HotSpot uses a caller save convention. 76 */ 77 @Override 78 public boolean preservesRegisters() { 79 return false; 80 } 81 82 @Override 83 protected Object getConstantParameterValue(int index, String name) { 84 if (index == 2) { 85 return providers.getRegisters().getThreadRegister(); 86 } 87 assert index == 3; 88 return options; 89 } 90 91 @Snippet 92 private static void exceptionHandler(Object exception, Word exceptionPc, @ConstantParameter Register threadRegister, @ConstantParameter OptionValues options) { 93 Word thread = registerAsWord(threadRegister); 94 checkNoExceptionInThread(thread, assertionsEnabled(INJECTED_VMCONFIG)); 95 checkExceptionNotNull(assertionsEnabled(INJECTED_VMCONFIG), exception); 96 writeExceptionOop(thread, exception); 97 writeExceptionPc(thread, exceptionPc); 98 if (logging(options)) { 99 printf("handling exception %p (", Word.objectToTrackedPointer(exception).rawValue()); 100 decipher(Word.objectToTrackedPointer(exception).rawValue()); 101 printf(") at %p (", exceptionPc.rawValue()); 102 decipher(exceptionPc.rawValue()); 103 printf(")\n"); 104 } 105 106 // patch throwing pc into return address so that deoptimization finds the right debug info 107 patchReturnAddress(exceptionPc); 108 109 Word handlerPc = exceptionHandlerForPc(EXCEPTION_HANDLER_FOR_PC, thread); 110 111 if (logging(options)) { 112 printf("handler for exception %p at %p is at %p (", Word.objectToTrackedPointer(exception).rawValue(), exceptionPc.rawValue(), handlerPc.rawValue()); 113 decipher(handlerPc.rawValue()); 114 printf(")\n"); 115 } 116 117 // patch the return address so that this stub returns to the exception handler 118 jumpToExceptionHandler(handlerPc); 119 } 120 121 static void checkNoExceptionInThread(Word thread, boolean enabled) { 122 if (enabled) { 123 Object currentException = readExceptionOop(thread); 124 if (currentException != null) { 125 fatal("exception object in thread must be null, not %p", Word.objectToTrackedPointer(currentException).rawValue()); 126 } 127 if (cAssertionsEnabled(INJECTED_VMCONFIG)) { 128 // This thread-local is only cleared in DEBUG builds of the VM 129 // (see OptoRuntime::generate_exception_blob) 130 Word currentExceptionPc = readExceptionPc(thread); 131 if (currentExceptionPc.notEqual(WordFactory.zero())) { 132 fatal("exception PC in thread must be zero, not %p", currentExceptionPc.rawValue()); 133 } 134 } 135 } 136 } 137 138 static void checkExceptionNotNull(boolean enabled, Object exception) { 139 if (enabled && exception == null) { 140 fatal("exception must not be null"); 141 } 142 } 143 144 @Fold 145 static boolean logging(OptionValues options) { 146 return StubOptions.TraceExceptionHandlerStub.getValue(options); 147 } 148 149 /** 150 * Determines if either Java assertions are enabled for Graal or if this is a HotSpot build 151 * where the ASSERT mechanism is enabled. 152 */ 153 @Fold 154 @SuppressWarnings("all") 155 static boolean assertionsEnabled(@InjectedParameter GraalHotSpotVMConfig config) { 156 return Assertions.ENABLED || cAssertionsEnabled(config); 157 } 158 159 public static final ForeignCallDescriptor EXCEPTION_HANDLER_FOR_PC = newDescriptor(ExceptionHandlerStub.class, "exceptionHandlerForPc", Word.class, Word.class); 160 161 @NodeIntrinsic(value = StubForeignCallNode.class) 162 public static native Word exceptionHandlerForPc(@ConstantNodeParameter ForeignCallDescriptor exceptionHandlerForPc, Word thread); 163 }