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.nodes.JumpToExceptionHandlerInCallerNode.jumpToExceptionHandlerInCaller; 26 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.registerAsWord; 27 import static org.graalvm.compiler.hotspot.stubs.ExceptionHandlerStub.checkExceptionNotNull; 28 import static org.graalvm.compiler.hotspot.stubs.ExceptionHandlerStub.checkNoExceptionInThread; 29 import static org.graalvm.compiler.hotspot.stubs.StubUtil.cAssertionsEnabled; 30 import static org.graalvm.compiler.hotspot.stubs.StubUtil.decipher; 31 import static org.graalvm.compiler.hotspot.stubs.StubUtil.newDescriptor; 32 import static org.graalvm.compiler.hotspot.stubs.StubUtil.printf; 33 34 import org.graalvm.compiler.api.replacements.Fold; 35 import org.graalvm.compiler.api.replacements.Fold.InjectedParameter; 36 import org.graalvm.compiler.api.replacements.Snippet; 37 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter; 38 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor; 39 import org.graalvm.compiler.graph.Node.ConstantNodeParameter; 40 import org.graalvm.compiler.graph.Node.NodeIntrinsic; 41 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 42 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage; 43 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 44 import org.graalvm.compiler.hotspot.nodes.StubForeignCallNode; 45 import org.graalvm.compiler.nodes.UnwindNode; 46 import org.graalvm.compiler.word.Pointer; 47 import org.graalvm.compiler.word.Word; 48 49 import jdk.vm.ci.code.Register; 50 51 /** 52 * Stub called by an {@link UnwindNode}. This stub executes in the frame of the method throwing an 53 * exception and completes by jumping to the exception handler in the calling frame. 54 */ 55 public class UnwindExceptionToCallerStub extends SnippetStub { 56 57 public UnwindExceptionToCallerStub(HotSpotProviders providers, HotSpotForeignCallLinkage linkage) { 58 super("unwindExceptionToCaller", providers, linkage); 59 } 60 61 /** 62 * The current frame is unwound by this stub. Therefore, it does not need to save any registers 63 * as HotSpot uses a caller save convention. 64 */ 65 @Override 66 public boolean preservesRegisters() { 67 return false; 68 } 69 70 @Override 71 protected Object getConstantParameterValue(int index, String name) { 72 assert index == 2; 73 return providers.getRegisters().getThreadRegister(); 74 } 75 76 @Snippet 77 private static void unwindExceptionToCaller(Object exception, Word returnAddress, @ConstantParameter Register threadRegister) { 78 Pointer exceptionOop = Word.objectToTrackedPointer(exception); 79 if (logging()) { 80 printf("unwinding exception %p (", exceptionOop.rawValue()); 81 decipher(exceptionOop.rawValue()); 82 printf(") at %p (", returnAddress.rawValue()); 83 decipher(returnAddress.rawValue()); 84 printf(")\n"); 85 } 86 Word thread = registerAsWord(threadRegister); 87 checkNoExceptionInThread(thread, assertionsEnabled(null)); 88 checkExceptionNotNull(assertionsEnabled(null), exception); 89 90 Word handlerInCallerPc = exceptionHandlerForReturnAddress(EXCEPTION_HANDLER_FOR_RETURN_ADDRESS, thread, returnAddress); 91 92 if (logging()) { 93 printf("handler for exception %p at return address %p is at %p (", exceptionOop.rawValue(), returnAddress.rawValue(), handlerInCallerPc.rawValue()); 94 decipher(handlerInCallerPc.rawValue()); 95 printf(")\n"); 96 } 97 98 jumpToExceptionHandlerInCaller(handlerInCallerPc, exception, returnAddress); 99 } 100 101 @Fold 102 static boolean logging() { 103 return StubOptions.TraceUnwindStub.getValue(); 104 } 105 106 /** 107 * Determines if either Java assertions are enabled for {@link UnwindExceptionToCallerStub} or 108 * if this is a HotSpot build where the ASSERT mechanism is enabled. 109 * <p> 110 * This first check relies on the per-class assertion status which is why this method must be in 111 * this class. 112 */ 113 @Fold 114 @SuppressWarnings("all") 115 static boolean assertionsEnabled(@InjectedParameter GraalHotSpotVMConfig config) { 116 boolean enabled = false; 117 assert enabled = true; 118 return enabled || cAssertionsEnabled(config); 119 } 120 121 public static final ForeignCallDescriptor EXCEPTION_HANDLER_FOR_RETURN_ADDRESS = newDescriptor(UnwindExceptionToCallerStub.class, "exceptionHandlerForReturnAddress", Word.class, Word.class, 122 Word.class); 123 124 @NodeIntrinsic(value = StubForeignCallNode.class, setStampFromReturnType = true) 125 public static native Word exceptionHandlerForReturnAddress(@ConstantNodeParameter ForeignCallDescriptor exceptionHandlerForReturnAddress, Word thread, Word returnAddress); 126 }