|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package org.graalvm.nativebridge; |
| 42 | + |
| 43 | +/** |
| 44 | + * Provides utility methods for merging stack traces for process isolate. |
| 45 | + */ |
| 46 | +final class ProcessIsolateStack { |
| 47 | + |
| 48 | + private static final String THREAD_CHANNEL_CLASS = ProcessIsolateThreadSupport.ThreadChannel.class.getName(); |
| 49 | + private static final String DISPATCH_RUNNABLE = ProcessIsolateThreadSupport.DispatchRunnable.class.getName(); |
| 50 | + private static final String FOREIGN_EXCEPTION_CLASS = ForeignException.class.getName(); |
| 51 | + private static final String SEND_RECEIVE_METHOD = "sendAndReceive"; |
| 52 | + private static final String CREATE_METHOD = "create"; |
| 53 | + private static final String DISPATCH_METHOD = "dispatch"; |
| 54 | + private static final String RUN_METHOD = "run"; |
| 55 | + |
| 56 | + private ProcessIsolateStack() { |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Merges {@code host} and {@code isolate} stack traces respecting process isolate transition |
| 61 | + * boundaries. |
| 62 | + */ |
| 63 | + static StackTraceElement[] mergeStackTraces(StackTraceElement[] host, StackTraceElement[] isolate, boolean originatedInHost) { |
| 64 | + int hostStackEndIndex; |
| 65 | + int isolateStackEndIndex; |
| 66 | + if (originatedInHost) { |
| 67 | + MirrorThreadInfo mirrorThreadInfo = getMirrorThreadEntryMethodInfo(host); |
| 68 | + hostStackEndIndex = mirrorThreadInfo.runnableIndex; |
| 69 | + if (hostStackEndIndex == host.length && mirrorThreadInfo.dispatchLoopIndex != -1) { |
| 70 | + // Already merged |
| 71 | + return host; |
| 72 | + } |
| 73 | + isolateStackEndIndex = getMirrorThreadEntryMethodInfo(isolate).runnableIndex; |
| 74 | + } else { |
| 75 | + MirrorThreadInfo mirrorThreadInfo = getMirrorThreadEntryMethodInfo(isolate); |
| 76 | + isolateStackEndIndex = mirrorThreadInfo.runnableIndex; |
| 77 | + if (isolateStackEndIndex == isolate.length && mirrorThreadInfo.dispatchLoopIndex != -1) { |
| 78 | + // Already merged |
| 79 | + return isolate; |
| 80 | + } |
| 81 | + hostStackEndIndex = getMirrorThreadEntryMethodInfo(host).runnableIndex; |
| 82 | + } |
| 83 | + StackTraceElement[] merged = mergeStackTraces(host, isolate, getTransitionIndex(host), hostStackEndIndex, getTransitionIndex(isolate), isolateStackEndIndex, originatedInHost); |
| 84 | + return merged; |
| 85 | + } |
| 86 | + |
| 87 | + private static StackTraceElement[] mergeStackTraces( |
| 88 | + StackTraceElement[] hostStackTrace, |
| 89 | + StackTraceElement[] isolateStackTrace, |
| 90 | + int hostStackStartIndex, |
| 91 | + int hostStackEndIndex, |
| 92 | + int isolateStackStartIndex, |
| 93 | + int isolateStackEndIndex, |
| 94 | + boolean originatedInHotSpot) { |
| 95 | + int targetIndex = 0; |
| 96 | + StackTraceElement[] merged = new StackTraceElement[hostStackEndIndex - hostStackStartIndex + isolateStackEndIndex - isolateStackStartIndex]; |
| 97 | + boolean startingHostFrame = true; |
| 98 | + boolean startingIsolateFrame = true; |
| 99 | + boolean useHostStack = originatedInHotSpot; |
| 100 | + int hostStackIndex = hostStackStartIndex; |
| 101 | + int isolateStackIndex = isolateStackStartIndex; |
| 102 | + while (hostStackIndex < hostStackEndIndex || isolateStackIndex < isolateStackEndIndex) { |
| 103 | + if (useHostStack) { |
| 104 | + while (hostStackIndex < hostStackEndIndex && (startingHostFrame || !isBoundary(hostStackTrace[hostStackIndex]))) { |
| 105 | + startingHostFrame = false; |
| 106 | + merged[targetIndex++] = hostStackTrace[hostStackIndex++]; |
| 107 | + } |
| 108 | + startingHostFrame = true; |
| 109 | + } else { |
| 110 | + useHostStack = true; |
| 111 | + } |
| 112 | + while (isolateStackIndex < isolateStackEndIndex && (startingIsolateFrame || !isBoundary(isolateStackTrace[isolateStackIndex]))) { |
| 113 | + startingIsolateFrame = false; |
| 114 | + merged[targetIndex++] = isolateStackTrace[isolateStackIndex++]; |
| 115 | + } |
| 116 | + startingIsolateFrame = true; |
| 117 | + } |
| 118 | + return merged; |
| 119 | + } |
| 120 | + |
| 121 | + private static boolean isBoundary(StackTraceElement frame) { |
| 122 | + return THREAD_CHANNEL_CLASS.equals(frame.getClassName()) && SEND_RECEIVE_METHOD.equals(frame.getMethodName()); |
| 123 | + } |
| 124 | + |
| 125 | + private static int getTransitionIndex(StackTraceElement[] stack) { |
| 126 | + return FOREIGN_EXCEPTION_CLASS.equals(stack[0].getClassName()) && CREATE_METHOD.equals(stack[0].getMethodName()) ? 1 : 0; |
| 127 | + } |
| 128 | + |
| 129 | + private static MirrorThreadInfo getMirrorThreadEntryMethodInfo(StackTraceElement[] stack) { |
| 130 | + int dispatchIndex = -1; |
| 131 | + int index = 0; |
| 132 | + for (; index < stack.length; index++) { |
| 133 | + StackTraceElement frame = stack[index]; |
| 134 | + if (THREAD_CHANNEL_CLASS.equals(frame.getClassName()) && DISPATCH_METHOD.equals(frame.getMethodName())) { |
| 135 | + dispatchIndex = index; |
| 136 | + } |
| 137 | + if (DISPATCH_RUNNABLE.equals(frame.getClassName()) && RUN_METHOD.equals(frame.getMethodName())) { |
| 138 | + break; |
| 139 | + } |
| 140 | + } |
| 141 | + return new MirrorThreadInfo(index, dispatchIndex); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Holds the positions of key method frames in a mirror thread stack trace. |
| 146 | + * |
| 147 | + * @param runnableIndex the index of the mirror thread's {@code run()} method; this frame and |
| 148 | + * all frames below it are excluded during stack trace merging |
| 149 | + * @param dispatchLoopIndex the index of the dispatch loop method; this is the first frame from |
| 150 | + * the mirror thread included in a merged stack trace. Its presence may also indicate |
| 151 | + * that the stack trace has already been merged. |
| 152 | + */ |
| 153 | + private record MirrorThreadInfo(int runnableIndex, int dispatchLoopIndex) { |
| 154 | + } |
| 155 | +} |
0 commit comments