Search in sources :

Example 1 with MemoryAddress

use of jdk.incubator.foreign.MemoryAddress in project tomcat by apache.

the class OpenSSLContext method getCiphers.

private static String[] getCiphers(MemoryAddress sslCtx) {
    MemoryAddress sk = SSL_CTX_get_ciphers(sslCtx);
    int len = OPENSSL_sk_num(sk);
    if (len <= 0) {
        return null;
    }
    ArrayList<String> ciphers = new ArrayList<>(len);
    for (int i = 0; i < len; i++) {
        MemoryAddress cipher = OPENSSL_sk_value(sk, i);
        MemoryAddress cipherName = SSL_CIPHER_get_name(cipher);
        ciphers.add(new String(CLinker.toJavaString(cipherName)));
    }
    return ciphers.toArray(new String[0]);
}
Also used : ArrayList(java.util.ArrayList) MemoryAddress(jdk.incubator.foreign.MemoryAddress) AbstractEndpoint(org.apache.tomcat.util.net.AbstractEndpoint)

Example 2 with MemoryAddress

use of jdk.incubator.foreign.MemoryAddress in project tomcat by apache.

the class OpenSSLLifecycleListener method freeDHParameters.

private static void freeDHParameters() {
    for (int i = 0; i < dhParameters.length; i++) {
        if (dhParameters[i] != null) {
            MemoryAddress dh = dhParameters[i].dh;
            if (dh != null && !MemoryAddress.NULL.equals(dh)) {
                DH_free(dh);
                dhParameters[i] = null;
            }
        }
    }
}
Also used : MemoryAddress(jdk.incubator.foreign.MemoryAddress)

Example 3 with MemoryAddress

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

the class StructTests method test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer.

@Test
public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
    VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1"));
    VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addFloatFromPointerAndFloatsFromStruct_returnFloatPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment floatSegmt = MemorySegment.allocateNative(C_FLOAT);
    MemoryAccess.setFloat(floatSegmt, 12.12F);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    floatHandle1.set(structSegmt, 18.23F);
    floatHandle2.set(structSegmt, 19.34F);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(floatSegmt.address(), structSegmt);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_FLOAT.byteSize());
    VarHandle floatHandle = MemoryHandles.varHandle(float.class, ByteOrder.nativeOrder());
    float result = (float) floatHandle.get(resultSegmt, 0);
    Assert.assertEquals(result, 49.69F, 0.01F);
    Assert.assertEquals(resultSegmt.address().toRawLongValue(), floatSegmt.address().toRawLongValue());
    floatSegmt.close();
    structSegmt.close();
    resultSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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 4 with MemoryAddress

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

the class StructTests method test_add2BoolStructsWithXor_returnStructPointer.

@Test
public void test_add2BoolStructsWithXor_returnStructPointer() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"));
    VarHandle boolHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
    VarHandle boolHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add2BoolStructsWithXor_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    boolHandle1.set(structSegmt1, 1);
    boolHandle2.set(structSegmt1, 0);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    boolHandle1.set(structSegmt2, 1);
    boolHandle2.set(structSegmt2, 1);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
    Assert.assertEquals(boolHandle1.get(resultSegmt), 0);
    Assert.assertEquals(boolHandle2.get(resultSegmt), 1);
    structSegmt1.close();
    structSegmt2.close();
    resultSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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 5 with MemoryAddress

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

the class StructTests method test_add2ByteStructs_returnStructPointer.

@Test
public void test_add2ByteStructs_returnStructPointer() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
    VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add2ByteStructs_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    byteHandle1.set(structSegmt1, (byte) 25);
    byteHandle2.set(structSegmt1, (byte) 11);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    byteHandle1.set(structSegmt2, (byte) 24);
    byteHandle2.set(structSegmt2, (byte) 13);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
    Assert.assertEquals((byte) byteHandle1.get(resultSegmt), (byte) 49);
    Assert.assertEquals((byte) byteHandle2.get(resultSegmt), (byte) 24);
    structSegmt1.close();
    structSegmt2.close();
    resultSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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