|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 2025, 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.test.foreign; |
| 27 | + |
| 28 | +import static org.junit.Assert.assertEquals; |
| 29 | + |
| 30 | +import java.lang.foreign.Arena; |
| 31 | +import java.lang.foreign.FunctionDescriptor; |
| 32 | +import java.lang.foreign.Linker; |
| 33 | +import java.lang.foreign.MemorySegment; |
| 34 | +import java.lang.foreign.SymbolLookup; |
| 35 | +import java.lang.foreign.ValueLayout; |
| 36 | +import java.lang.invoke.MethodHandle; |
| 37 | +import java.lang.invoke.MethodHandles; |
| 38 | +import java.lang.invoke.MethodType; |
| 39 | +import java.util.Arrays; |
| 40 | + |
| 41 | +import org.graalvm.nativeimage.hosted.Feature; |
| 42 | +import org.graalvm.nativeimage.hosted.RuntimeForeignAccess; |
| 43 | +import org.junit.Assert; |
| 44 | +import org.junit.Assume; |
| 45 | +import org.junit.Test; |
| 46 | + |
| 47 | +import com.oracle.svm.core.SubstrateOptions; |
| 48 | + |
| 49 | +@SuppressWarnings("restricted") |
| 50 | +public class ForeignTests { |
| 51 | + public static final FunctionDescriptor QSORT_COMPARE_DESC = FunctionDescriptor.of( |
| 52 | + ValueLayout.JAVA_INT, |
| 53 | + ValueLayout.ADDRESS.withTargetLayout(ValueLayout.JAVA_INT), |
| 54 | + ValueLayout.ADDRESS.withTargetLayout(ValueLayout.JAVA_INT)); |
| 55 | + private static final String STRING = "Hello, World!"; |
| 56 | + private static final FunctionDescriptor STRLEN_SIG = FunctionDescriptor.of(ValueLayout.JAVA_LONG, ValueLayout.ADDRESS); |
| 57 | + private static final FunctionDescriptor QSORT_SIG = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.JAVA_LONG, ValueLayout.JAVA_LONG, ValueLayout.ADDRESS); |
| 58 | + private static final MethodHandle COMPARE_HANDLE = initCompareHandle(); |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testInvokeStrlen() throws Throwable { |
| 62 | + Assume.assumeFalse("Linker.nativeLinker().defaultLookup() is not supported in static executables", |
| 63 | + SubstrateOptions.StaticExecutable.getValue()); |
| 64 | + |
| 65 | + try (Arena arena = Arena.ofConfined()) { |
| 66 | + MemorySegment nativeString = arena.allocateFrom(STRING); |
| 67 | + assertEquals(STRING.length(), (long) strlenMH().invokeExact(nativeString)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testUpcall() throws Throwable { |
| 73 | + Assume.assumeFalse("Linker.nativeLinker().defaultLookup() is not supported in static executables", |
| 74 | + SubstrateOptions.StaticExecutable.getValue()); |
| 75 | + |
| 76 | + /* |
| 77 | + * Due to native memory tracking (NMT) tests, we use 'Arena.global()' to ensure that the |
| 78 | + * upcall stub won't be deallocated during NMT tests which would lead to unexpected memory |
| 79 | + * usage values. |
| 80 | + */ |
| 81 | + MemorySegment compareFunc = Linker.nativeLinker().upcallStub(COMPARE_HANDLE, QSORT_COMPARE_DESC, Arena.global()); |
| 82 | + |
| 83 | + int[] unsortedArray = new int[]{0, 9, 3, 4, 6, 5, 1, 8, 2, 7}; |
| 84 | + |
| 85 | + int[] sorted; |
| 86 | + |
| 87 | + try (Arena arena = Arena.ofConfined()) { |
| 88 | + MemorySegment array = arena.allocateFrom(ValueLayout.JAVA_INT, unsortedArray); |
| 89 | + |
| 90 | + qsortMH().invoke(array, |
| 91 | + (long) unsortedArray.length, |
| 92 | + ValueLayout.JAVA_INT.byteSize(), |
| 93 | + compareFunc); |
| 94 | + |
| 95 | + sorted = array.toArray(ValueLayout.JAVA_INT); |
| 96 | + } |
| 97 | + |
| 98 | + int[] jSortedArray = Arrays.copyOf(unsortedArray, unsortedArray.length); |
| 99 | + Arrays.sort(jSortedArray); |
| 100 | + |
| 101 | + Assert.assertArrayEquals(jSortedArray, sorted); |
| 102 | + } |
| 103 | + |
| 104 | + private static MethodHandle strlenMH() { |
| 105 | + SymbolLookup stdLib = Linker.nativeLinker().defaultLookup(); |
| 106 | + MemorySegment strlenAddr = stdLib.find("strlen").orElseThrow(); |
| 107 | + return Linker.nativeLinker().downcallHandle(strlenAddr, STRLEN_SIG); |
| 108 | + } |
| 109 | + |
| 110 | + private static MethodHandle qsortMH() { |
| 111 | + MemorySegment qsortAddr = Linker.nativeLinker().defaultLookup().find("qsort").orElseThrow(); |
| 112 | + return Linker.nativeLinker().downcallHandle(qsortAddr, QSORT_SIG); |
| 113 | + } |
| 114 | + |
| 115 | + private static MethodHandle initCompareHandle() { |
| 116 | + try { |
| 117 | + return MethodHandles.lookup().findStatic(Qsort.class, "qsortCompare", |
| 118 | + MethodType.methodType(int.class, MemorySegment.class, MemorySegment.class)); |
| 119 | + } catch (NoSuchMethodException | IllegalAccessException e) { |
| 120 | + throw new RuntimeException(e); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + static class Qsort { |
| 125 | + static int qsortCompare(MemorySegment elem1, MemorySegment elem2) { |
| 126 | + return Integer.compare(elem1.get(ValueLayout.JAVA_INT, 0), elem2.get(ValueLayout.JAVA_INT, 0)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public static class TestFeature implements Feature { |
| 131 | + |
| 132 | + @Override |
| 133 | + public void duringSetup(DuringSetupAccess access) { |
| 134 | + RuntimeForeignAccess.registerForDowncall(STRLEN_SIG); |
| 135 | + RuntimeForeignAccess.registerForDowncall(QSORT_SIG); |
| 136 | + RuntimeForeignAccess.registerForDirectUpcall(COMPARE_HANDLE, QSORT_COMPARE_DESC); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments