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