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