1 /*
   2  * Copyright (c) 2012, 2018, 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     /**
  75      * This stub is called when returning to a method to handle an exception thrown by a callee. It
  76      * is not used for routing implicit exceptions. Therefore, it does not need to save any
  77      * registers as HotSpot uses a caller save convention.
  78      */
  79     @Override
  80     public boolean preservesRegisters() {
  81         return false;
  82     }
  83 
  84     @Override
  85     protected Object getConstantParameterValue(int index, String name) {
  86         if (index == 2) {
  87             return providers.getRegisters().getThreadRegister();
  88         }
  89         assert index == 3;
  90         return StubOptions.TraceExceptionHandlerStub.getValue(options);
  91     }
  92 
  93     @Snippet
  94     private static void exceptionHandler(Object exception, Word exceptionPc, @ConstantParameter Register threadRegister, @ConstantParameter boolean logging) {
  95         Word thread = registerAsWord(threadRegister);
  96         checkNoExceptionInThread(thread, assertionsEnabled(INJECTED_VMCONFIG));
  97         checkExceptionNotNull(assertionsEnabled(INJECTED_VMCONFIG), exception);
  98         writeExceptionOop(thread, exception);
  99         writeExceptionPc(thread, exceptionPc);
 100         if (logging) {
 101             printf("handling exception %p (", Word.objectToTrackedPointer(exception).rawValue());
 102             decipher(Word.objectToTrackedPointer(exception).rawValue());
 103             printf(") at %p (", exceptionPc.rawValue());
 104             decipher(exceptionPc.rawValue());
 105             printf(")\n");
 106         }
 107 
 108         // patch throwing pc into return address so that deoptimization finds the right debug info
 109         patchReturnAddress(exceptionPc);
 110 
 111         Word handlerPc = exceptionHandlerForPc(EXCEPTION_HANDLER_FOR_PC, thread);
 112 
 113         if (logging) {
 114             printf("handler for exception %p at %p is at %p (", Word.objectToTrackedPointer(exception).rawValue(), exceptionPc.rawValue(), handlerPc.rawValue());
 115             decipher(handlerPc.rawValue());
 116             printf(")\n");
 117         }
 118 
 119         // patch the return address so that this stub returns to the exception handler
 120         jumpToExceptionHandler(handlerPc);
 121     }
 122 
 123     static void checkNoExceptionInThread(Word thread, boolean enabled) {
 124         if (enabled) {
 125             Object currentException = readExceptionOop(thread);
 126             if (currentException != null) {
 127                 fatal("exception object in thread must be null, not %p", Word.objectToTrackedPointer(currentException).rawValue());
 128             }
 129             if (cAssertionsEnabled(INJECTED_VMCONFIG)) {
 130                 // This thread-local is only cleared in DEBUG builds of the VM
 131                 // (see OptoRuntime::generate_exception_blob)
 132                 Word currentExceptionPc = readExceptionPc(thread);
 133                 if (currentExceptionPc.notEqual(WordFactory.zero())) {
 134                     fatal("exception PC in thread must be zero, not %p", currentExceptionPc.rawValue());
 135                 }
 136             }
 137         }
 138     }
 139 
 140     static void checkExceptionNotNull(boolean enabled, Object exception) {
 141         if (enabled && exception == null) {
 142             fatal("exception must not be null");
 143         }
 144     }
 145 
 146     /**
 147      * Determines if either Java assertions are enabled for Graal or if this is a HotSpot build
 148      * where the ASSERT mechanism is enabled.
 149      */
 150     @Fold
 151     @SuppressWarnings("all")
 152     static boolean assertionsEnabled(@InjectedParameter GraalHotSpotVMConfig config) {
 153         return Assertions.assertionsEnabled() || cAssertionsEnabled(config);
 154     }
 155 
 156     public static final ForeignCallDescriptor EXCEPTION_HANDLER_FOR_PC = newDescriptor(ExceptionHandlerStub.class, "exceptionHandlerForPc", Word.class, Word.class);
 157 
 158     @NodeIntrinsic(value = StubForeignCallNode.class)
 159     public static native Word exceptionHandlerForPc(@ConstantNodeParameter ForeignCallDescriptor exceptionHandlerForPc, Word thread);
 160 }