diff --git a/src/java.base/share/native/libjava/VMFunctions.c b/src/java.base/share/native/libjava/VMFunctions.c new file mode 100644 index 00000000000..80501802fe6 --- /dev/null +++ b/src/java.base/share/native/libjava/VMFunctions.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +#include + +#include "jni.h" + +JNIEXPORT void JNICALL +Java_jdk_internal_foreign_abi_VMFunctions_initVMFunctions(JNIEnv *env, + jclass cls, + jlong address) +{ + size_t* addresses = (size_t*)(void*)address; + // The order in which the function pointers are stored has to match the order of constants + // in the VMFunctions.FunctionName enum. + addresses[0] = (size_t)&malloc; + addresses[1] = (size_t)&free; +} diff --git a/src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/SharedUtils.java b/src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/SharedUtils.java index b7858a903c8..31208b419e6 100644 --- a/src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/SharedUtils.java +++ b/src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/SharedUtils.java @@ -50,7 +50,6 @@ import java.lang.invoke.VarHandle; import java.lang.ref.Reference; import java.nio.charset.Charset; -import java.nio.file.Path; import java.util.List; import java.util.Map; import java.util.function.Consumer; @@ -412,31 +411,9 @@ static MethodHandle wrapWithAllocator(MethodHandle specializedHandle, return specializedHandle; } - // lazy init MH_ALLOC and MH_FREE handles - private static class AllocHolder { - - private static final CLinker linker = getSystemLinker(); - - static { - // FIXME: This should go away. This is temporary hack to get testing on Windows going. - // After fix for 8266627, this whole section will be removed. - if (linker instanceof Windowsx64Linker) { - System.load(Path.of(System.getenv("SystemRoot"), "System32", "msvcrt.dll").toAbsolutePath().toString()); - } - } - - static final MethodHandle MH_MALLOC = linker.downcallHandle(CLinker.findNative("malloc").get(), - MethodType.methodType(MemoryAddress.class, long.class), - FunctionDescriptor.of(C_POINTER, C_LONG_LONG)); - - static final MethodHandle MH_FREE = linker.downcallHandle(CLinker.findNative("free").get(), - MethodType.methodType(void.class, MemoryAddress.class), - FunctionDescriptor.ofVoid(C_POINTER)); - } - public static MemoryAddress allocateMemoryInternal(long size) { try { - return (MemoryAddress) AllocHolder.MH_MALLOC.invokeExact(size); + return (MemoryAddress) VMFunctions.MH_MALLOC.invokeExact(size); } catch (Throwable th) { throw new RuntimeException(th); } @@ -444,7 +421,7 @@ public static MemoryAddress allocateMemoryInternal(long size) { public static void freeMemoryInternal(MemoryAddress addr) { try { - AllocHolder.MH_FREE.invokeExact(addr); + VMFunctions.MH_FREE.invokeExact(addr); } catch (Throwable th) { throw new RuntimeException(th); } diff --git a/src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/VMFunctions.java b/src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/VMFunctions.java new file mode 100644 index 00000000000..08ea65d417c --- /dev/null +++ b/src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/VMFunctions.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.internal.foreign.abi; + +import jdk.incubator.foreign.CLinker; +import jdk.incubator.foreign.FunctionDescriptor; +import jdk.incubator.foreign.MemoryAccess; +import jdk.incubator.foreign.MemoryAddress; +import jdk.incubator.foreign.MemoryLayouts; +import jdk.incubator.foreign.MemorySegment; +import jdk.incubator.foreign.ResourceScope; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodType; + +import static jdk.incubator.foreign.CLinker.C_LONG_LONG; +import static jdk.incubator.foreign.CLinker.C_POINTER; + +/** + * This class is used to setup downcall method handles which refer to commonly used functions within the JVM. + * A memory segment is allocated, with enough room to contain as many pointers as the number of constants + * defined in {@link FunctionName}. This segment is then filled by the JVM with function pointers which target + * the desired functions. + */ +class VMFunctions { + + /** + * The order of these constants has to match that in which the VM will fill the {@code vmFunctions} pointer array. + */ + enum FunctionName { + MALLOC, + FREE; + + MemoryAddress get() { + return MemoryAccess.getAddressAtIndex(vmFunctions, ordinal()); + } + } + + private static final CLinker linker = SharedUtils.getSystemLinker(); + private static final MemorySegment vmFunctions; + + static { + vmFunctions = MemorySegment.allocateNative( + MemoryLayouts.ADDRESS.byteSize() * FunctionName.values().length, + ResourceScope.newImplicitScope()); + initVMFunctions(vmFunctions.address().toRawLongValue()); + } + + static final MethodHandle MH_MALLOC = linker.downcallHandle(FunctionName.MALLOC.get(), + MethodType.methodType(MemoryAddress.class, long.class), + FunctionDescriptor.of(C_POINTER, C_LONG_LONG)); + + static final MethodHandle MH_FREE = linker.downcallHandle(FunctionName.FREE.get(), + MethodType.methodType(void.class, MemoryAddress.class), + FunctionDescriptor.ofVoid(C_POINTER)); + + static native void initVMFunctions(long address); +}