Search in sources :

Example 56 with MemoryAddress

use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.

the class PrimitiveTypeTests1 method test_memoryAllocFreeFromDefaultLib_1.

@Test
public void test_memoryAllocFreeFromDefaultLib_1() throws Throwable {
    /* Temporarily disable the default library loading on AIX till we figure out a way
		 * around to handle the case as the official implementation in OpenJDK17 doesn't
		 * help to load the static libray (libc.a).
		 */
    if (!isAixOS) {
        NativeSymbol allocSymbol = clinker.lookup("malloc").get();
        FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(ADDRESS, JAVA_LONG);
        MethodHandle allocHandle = clinker.downcallHandle(allocSymbol, allocFuncDesc);
        MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(10L);
        allocMemAddr.set(JAVA_INT, 0, 15);
        Assert.assertEquals(allocMemAddr.get(JAVA_INT, 0), 15);
        NativeSymbol freeSymbol = clinker.lookup("free").get();
        FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(ADDRESS);
        MethodHandle freeHandle = clinker.downcallHandle(freeSymbol, freeFuncDesc);
        freeHandle.invoke(allocMemAddr);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 57 with MemoryAddress

use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.

the class StructTests1 method test_addIntFromPointerAndIntsFromStruct_returnIntPointer_1.

@Test
public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"));
    VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct_returnIntPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment intSegmt = allocator.allocate(JAVA_INT, 1122333);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        intHandle1.set(structSegmt, 4455666);
        intHandle2.set(structSegmt, 7788999);
        MemoryAddress resultAddr = (MemoryAddress) mh.invoke(intSegmt, structSegmt);
        Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 13366998);
        Assert.assertEquals(resultAddr.toRawLongValue(), intSegmt.address().toRawLongValue());
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 58 with MemoryAddress

use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.

the class StructTests2 method test_addLongFromPointerAndLongsFromStruct_returnLongPointer_2.

@Test
public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer_2() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(longLayout.withName("elem1"), longLayout.withName("elem2"));
    VarHandle longHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1"));
    VarHandle longHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointer").get();
    MethodHandle mh = clinker.downcallHandle(mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment longSegmt = allocator.allocate(longLayout);
        MemoryAccess.setLong(longSegmt, 1122334455L);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        longHandle1.set(structSegmt, 6677889900L);
        longHandle2.set(structSegmt, 1234567890L);
        MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(functionSymbol, longSegmt.address(), structSegmt);
        MemorySegment resultSegmt = resultAddr.asSegment(longLayout.byteSize(), scope);
        VarHandle longHandle = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder());
        long result = (long) longHandle.get(resultSegmt, 0);
        Assert.assertEquals(result, 9034792245L);
        Assert.assertEquals(resultSegmt.address().toRawLongValue(), longSegmt.address().toRawLongValue());
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 59 with MemoryAddress

use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.

the class StructTests2 method test_addShortFromPointerAndShortsFromStruct_returnShortPointer_2.

@Test
public void test_addShortFromPointerAndShortsFromStruct_returnShortPointer_2() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
    VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1"));
    VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStruct_returnShortPointer").get();
    MethodHandle mh = clinker.downcallHandle(mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment shortSegmt = allocator.allocate(C_SHORT);
        MemoryAccess.setShort(shortSegmt, (short) 12);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        shortHandle1.set(structSegmt, (short) 18);
        shortHandle2.set(structSegmt, (short) 19);
        MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(functionSymbol, shortSegmt.address(), structSegmt);
        MemorySegment resultSegmt = resultAddr.asSegment(C_SHORT.byteSize(), scope);
        VarHandle shortHandle = MemoryHandles.varHandle(short.class, ByteOrder.nativeOrder());
        short result = (short) shortHandle.get(resultSegmt, 0);
        Assert.assertEquals(result, 49);
        Assert.assertEquals(resultSegmt.address().toRawLongValue(), shortSegmt.address().toRawLongValue());
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 60 with MemoryAddress

use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.

the class StructTests1 method test_addLongFromPointerAndLongsFromStruct_returnLongPointer_1.

@Test
public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"));
    VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1122334455L);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        longHandle1.set(structSegmt, 6677889900L);
        longHandle2.set(structSegmt, 1234567890L);
        MemoryAddress resultAddr = (MemoryAddress) mh.invoke(longSegmt, structSegmt);
        Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 9034792245L);
        Assert.assertEquals(resultAddr.toRawLongValue(), longSegmt.address().toRawLongValue());
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

MemoryAddress (jdk.incubator.foreign.MemoryAddress)136 Test (org.testng.annotations.Test)131 MethodHandle (java.lang.invoke.MethodHandle)127 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)127 MemorySegment (jdk.incubator.foreign.MemorySegment)102 MethodType (java.lang.invoke.MethodType)93 GroupLayout (jdk.incubator.foreign.GroupLayout)80 VarHandle (java.lang.invoke.VarHandle)78 ResourceScope (jdk.incubator.foreign.ResourceScope)65 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)64 Addressable (jdk.incubator.foreign.Addressable)63 NativeSymbol (jdk.incubator.foreign.NativeSymbol)34 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)30 AbstractEndpoint (org.apache.tomcat.util.net.AbstractEndpoint)4 X509Certificate (java.security.cert.X509Certificate)3 CertificateException (java.security.cert.CertificateException)2 ArrayList (java.util.ArrayList)2 SSLException (javax.net.ssl.SSLException)2 PrivateKey (java.security.PrivateKey)1 X509KeyManager (javax.net.ssl.X509KeyManager)1