Search in sources :

Example 51 with MemoryAddress

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

the class PrimitiveTypeTests3 method test_memoryAllocFreeFromDefaultLib_3.

@Test
public void test_memoryAllocFreeFromDefaultLib_3() 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) {
        Addressable allocSymbol = defaultLibLookup.lookup("malloc").get();
        MethodType allocMethodType = MethodType.methodType(MemoryAddress.class, long.class);
        FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(C_POINTER, longLayout);
        MethodHandle allocHandle = clinker.downcallHandle(allocMethodType, allocFuncDesc);
        MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(allocSymbol, 10L);
        MemorySegment memSeg = allocMemAddr.asSegment(10L, resourceScope);
        MemoryAccess.setIntAtOffset(memSeg, 0, 15);
        Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, 0), 15);
        Addressable freeSymbol = defaultLibLookup.lookup("free").get();
        MethodType freeMethodType = MethodType.methodType(void.class, MemoryAddress.class);
        FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(C_POINTER);
        MethodHandle freeHandle = clinker.downcallHandle(freeMethodType, freeFuncDesc);
        freeHandle.invokeExact(freeSymbol, allocMemAddr);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) 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 52 with MemoryAddress

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

the class PrimitiveTypeTests3 method test_memoryAllocFreeFromDefaultLib_fromMemAddr_3.

@Test
public void test_memoryAllocFreeFromDefaultLib_fromMemAddr_3() 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) {
        Addressable allocSymbol = defaultLibLookup.lookup("malloc").get();
        MemoryAddress allocMemAddrFromSymbol = allocSymbol.address();
        MethodType allocMethodType = MethodType.methodType(MemoryAddress.class, long.class);
        FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(C_POINTER, longLayout);
        MethodHandle allocHandle = clinker.downcallHandle(allocMethodType, allocFuncDesc);
        MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(allocSymbol, 10L);
        MemorySegment memSeg = allocMemAddr.asSegment(10L, resourceScope);
        MemoryAccess.setIntAtOffset(memSeg, 0, 15);
        Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, 0), 15);
        Addressable freeSymbol = defaultLibLookup.lookup("free").get();
        MemoryAddress freeMemAddr = freeSymbol.address();
        MethodType freeMethodType = MethodType.methodType(void.class, MemoryAddress.class);
        FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(C_POINTER);
        MethodHandle freeHandle = clinker.downcallHandle(freeMethodType, freeFuncDesc);
        freeHandle.invokeExact(freeSymbol, allocMemAddr);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) 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 53 with MemoryAddress

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

the class VaListTests method test_vprintfFromDefaultLibWithVaList_fromMemAddr.

@Test
public void test_vprintfFromDefaultLibWithVaList_fromMemAddr() throws Throwable {
    /* Disable the test on Windows given a misaligned access exception coming from
		 * java.base/java.lang.invoke.MemoryAccessVarHandleBase triggered by CLinker.toCString()
		 * is also captured on OpenJDK/Hotspot.
		 */
    if (!isWinOS) {
        Addressable functionSymbol = defaultLibLookup.lookup("vprintf").get();
        MemoryAddress memAddr = functionSymbol.address();
        MethodType mt = MethodType.methodType(int.class, MemoryAddress.class, VaList.class);
        FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, C_VA_LIST);
        MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
        try (ResourceScope scope = ResourceScope.newConfinedScope()) {
            MemorySegment formatMemSegment = CLinker.toCString("%d * %d = %d\n", scope);
            VaList vaList = CLinker.VaList.make(vaListBuilder -> vaListBuilder.vargFromInt(C_INT, 7).vargFromInt(C_INT, 8).vargFromInt(C_INT, 56), scope);
            mh.invoke(formatMemSegment.address(), vaList);
        }
    }
}
Also used : MethodType(java.lang.invoke.MethodType) ResourceScope(jdk.incubator.foreign.ResourceScope) 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 54 with MemoryAddress

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

the class StructTests1 method test_add2FloatStructs_returnStructPointer_1.

@Test
public void test_add2FloatStructs_returnStructPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"));
    VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        floatHandle1.set(structSegmt1, 25.12F);
        floatHandle2.set(structSegmt1, 11.23F);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        floatHandle1.set(structSegmt2, 24.34F);
        floatHandle2.set(structSegmt2, 13.45F);
        MemoryAddress resultAddr = (MemoryAddress) mh.invoke(structSegmt1, structSegmt2);
        Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 49.46F, 0.01F);
        Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 4), 24.68F, 0.01F);
    }
}
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 55 with MemoryAddress

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

the class StructTests1 method test_add2BoolStructsWithXor_returnStructPointer_1.

@Test
public void test_add2BoolStructsWithXor_returnStructPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"));
    VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        boolHandle1.set(structSegmt1, true);
        boolHandle2.set(structSegmt1, false);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        boolHandle1.set(structSegmt2, true);
        boolHandle2.set(structSegmt2, true);
        MemoryAddress resultAddr = (MemoryAddress) mh.invoke(structSegmt1, structSegmt2);
        Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), false);
        Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 1), true);
    }
}
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