|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, 2026, 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. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package com.oracle.svm.core.foreign; |
| 27 | + |
| 28 | +/* |
| 29 | + * Copyright (c) 2026, 2026, Oracle and/or its affiliates. All rights reserved. |
| 30 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 31 | + * |
| 32 | + * This code is free software; you can redistribute it and/or modify it |
| 33 | + * under the terms of the GNU General Public License version 2 only, as |
| 34 | + * published by the Free Software Foundation. Oracle designates this |
| 35 | + * particular file as subject to the "Classpath" exception as provided |
| 36 | + * by Oracle in the LICENSE file that accompanied this code. |
| 37 | + * |
| 38 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 39 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 40 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 41 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 42 | + * accompanied this code). |
| 43 | + * |
| 44 | + * You should have received a copy of the GNU General Public License version |
| 45 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 46 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 47 | + * |
| 48 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 49 | + * or visit www.oracle.com if you need additional information or have any |
| 50 | + * questions. |
| 51 | + */ |
| 52 | + |
| 53 | +import java.lang.invoke.MethodType; |
| 54 | +import java.util.Arrays; |
| 55 | +import java.util.Objects; |
| 56 | + |
| 57 | +import org.graalvm.nativeimage.Platform; |
| 58 | +import org.graalvm.nativeimage.Platforms; |
| 59 | + |
| 60 | +import com.oracle.svm.shared.util.BasedOnJDKClass; |
| 61 | +import com.oracle.svm.shared.util.VMError; |
| 62 | +import com.oracle.svm.util.GuestAccess; |
| 63 | +import com.oracle.svm.util.GuestElements; |
| 64 | +import com.oracle.svm.util.JVMCIReflectionUtil; |
| 65 | + |
| 66 | +import jdk.internal.foreign.abi.NativeEntryPoint; |
| 67 | +import jdk.internal.foreign.abi.VMStorage; |
| 68 | +import jdk.vm.ci.meta.JavaConstant; |
| 69 | + |
| 70 | +@BasedOnJDKClass(NativeEntryPoint.class) |
| 71 | +@BasedOnJDKClass(className = "jdk.internal.foreign.abi.SoftReferenceCache") |
| 72 | +@BasedOnJDKClass(className = "jdk.internal.foreign.abi.NativeEntryPoint$CacheKey") |
| 73 | +@Platforms(Platform.HOSTED_ONLY.class) |
| 74 | +public final class NativeEntryPointHelper { |
| 75 | + |
| 76 | + /** |
| 77 | + * Extracts the information (i.e. {@link NativeEntryPointInfo}) required to create the given |
| 78 | + * {@link NativeEntryPoint}. This leverages the NativeEntryPoint cache (static field |
| 79 | + * {@code NEP_CACHE}) which stores this information as key. Unfortunately, since the cache is |
| 80 | + * private, it can only be extracted via guest metadata and constant access. |
| 81 | + * |
| 82 | + * @param nativeEntryPointConstant The {@link NativeEntryPoint} to extract the information for. |
| 83 | + * @return A {@link NativeEntryPointInfo} object encapsulating all information required to |
| 84 | + * create the {@link NativeEntryPoint}. |
| 85 | + */ |
| 86 | + public static NativeEntryPointInfo extractNativeEntryPointInfo(JavaConstant nativeEntryPointConstant) { |
| 87 | + GuestAccess access = GuestAccess.get(); |
| 88 | + GuestElements elements = access.elements; |
| 89 | + JavaConstant nepCacheConstant = JVMCIReflectionUtil.readStaticField(elements.jdk_internal_foreign_abi_NativeEntryPoint, "NEP_CACHE"); |
| 90 | + JavaConstant cacheConstant = JVMCIReflectionUtil.readInstanceField(nepCacheConstant, elements.jdk_internal_foreign_abi_SoftReferenceCache, "cache"); |
| 91 | + JavaConstant entrySetConstant = access.invoke(elements.java_util_Map_entrySet, cacheConstant); |
| 92 | + JavaConstant entriesConstant = access.invoke(elements.java_util_Collection_toArray, entrySetConstant); |
| 93 | + Object[] entries = access.asHostObject(Object[].class, entriesConstant); |
| 94 | + |
| 95 | + // We have the value; search the key |
| 96 | + JavaConstant cacheKeyConstant = null; |
| 97 | + for (Object entry : entries) { |
| 98 | + JavaConstant entryConstant = access.getSnippetReflection().forObject(entry); |
| 99 | + JavaConstant nodeConstant = access.invoke(elements.java_util_Map_Entry_getValue, entryConstant); |
| 100 | + JavaConstant refConstant = JVMCIReflectionUtil.readInstanceField(nodeConstant, elements.jdk_internal_foreign_abi_SoftReferenceCache_Node, "ref"); |
| 101 | + if (!refConstant.isNull() && access.invoke(elements.java_lang_ref_Reference_refersTo, refConstant, nativeEntryPointConstant).asBoolean()) { |
| 102 | + cacheKeyConstant = access.invoke(elements.java_util_Map_Entry_getKey, entryConstant); |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /* |
| 108 | + * Any instance of NativeEntryPoint is created via 'NativeEntryPoint.make' and will |
| 109 | + * therefore be added to NEP_CACHE. Further, the keys of NEP_CACHE are strongly referenced. |
| 110 | + * This means that there must be a cache key for any NativeEntryPoint that exists. |
| 111 | + */ |
| 112 | + VMError.guarantee(cacheKeyConstant != null, "Cannot extract info for NativeEntryPoint because it is not in NEP_CACHE"); |
| 113 | + |
| 114 | + /* |
| 115 | + * Field 'CacheKey.abi' is ignored because the ABIDescriptor is JDK's equivalent of our |
| 116 | + * Substrate*RegisterConfig. |
| 117 | + */ |
| 118 | + JavaConstant argMovesListConstant = JVMCIReflectionUtil.readInstanceField(cacheKeyConstant, elements.jdk_internal_foreign_abi_NativeEntryPoint_CacheKey, "argMoves"); |
| 119 | + JavaConstant returnMovesListConstant = JVMCIReflectionUtil.readInstanceField(cacheKeyConstant, elements.jdk_internal_foreign_abi_NativeEntryPoint_CacheKey, "retMoves"); |
| 120 | + JavaConstant emptyVmStorageArray = access.asArrayConstant(elements.jdk_internal_foreign_abi_VMStorage); |
| 121 | + VMStorage[] argMoves = access.asHostObject(VMStorage[].class, |
| 122 | + access.invoke(elements.java_util_Collection_toArray_withArray, argMovesListConstant, emptyVmStorageArray)); |
| 123 | + VMStorage[] returnMoves = access.asHostObject(VMStorage[].class, |
| 124 | + access.invoke(elements.java_util_Collection_toArray_withArray, returnMovesListConstant, emptyVmStorageArray)); |
| 125 | + MethodType methodType = access.asHostObject(MethodType.class, |
| 126 | + access.invoke(elements.jdk_internal_foreign_abi_NativeEntryPoint_type, nativeEntryPointConstant)); |
| 127 | + boolean needsReturnBuffer = JVMCIReflectionUtil.readInstanceField(cacheKeyConstant, elements.jdk_internal_foreign_abi_NativeEntryPoint_CacheKey, "needsReturnBuffer").asBoolean(); |
| 128 | + int capturedStateMask = JVMCIReflectionUtil.readInstanceField(cacheKeyConstant, elements.jdk_internal_foreign_abi_NativeEntryPoint_CacheKey, "capturedStateMask").asInt(); |
| 129 | + boolean needsTransition = JVMCIReflectionUtil.readInstanceField(cacheKeyConstant, elements.jdk_internal_foreign_abi_NativeEntryPoint_CacheKey, "needsTransition").asBoolean(); |
| 130 | + boolean allowHeapAccess = Arrays.stream(argMoves).anyMatch(Objects::isNull); |
| 131 | + |
| 132 | + return NativeEntryPointInfo.make(argMoves, returnMoves, methodType, needsReturnBuffer, capturedStateMask, needsTransition, allowHeapAccess); |
| 133 | + } |
| 134 | +} |
0 commit comments