< prev index next >

test/jdk/java/foreign/TestUpcallStructScope.java

Print this page

 20  *  or visit www.oracle.com if you need additional information or have any
 21  *  questions.
 22  *
 23  */
 24 
 25 /*
 26  * @test
 27  * @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
 28  * @modules jdk.incubator.foreign/jdk.internal.foreign
 29  *
 30  * @run testng/othervm/native
 31  *   --enable-native-access=ALL-UNNAMED
 32  *   -Djdk.internal.foreign.ProgrammableInvoker.USE_SPEC=false
 33  *   TestUpcallStructScope
 34  * @run testng/othervm/native
 35  *   --enable-native-access=ALL-UNNAMED
 36  *   -Djdk.internal.foreign.ProgrammableInvoker.USE_SPEC=true
 37  *   TestUpcallStructScope
 38  */
 39 

 40 import jdk.incubator.foreign.CLinker;
 41 import jdk.incubator.foreign.FunctionDescriptor;

 42 import jdk.incubator.foreign.SymbolLookup;
 43 import jdk.incubator.foreign.MemoryAddress;
 44 import jdk.incubator.foreign.MemoryLayout;
 45 import jdk.incubator.foreign.MemorySegment;
 46 import jdk.incubator.foreign.ResourceScope;
 47 import org.testng.annotations.Test;
 48 
 49 import java.lang.invoke.MethodHandle;
 50 import java.lang.invoke.MethodHandles;
 51 import java.lang.invoke.MethodType;
 52 import java.util.concurrent.atomic.AtomicReference;
 53 import java.util.function.Consumer;
 54 
 55 import static jdk.incubator.foreign.CLinker.C_DOUBLE;
 56 import static jdk.incubator.foreign.CLinker.C_INT;
 57 import static jdk.incubator.foreign.CLinker.C_POINTER;
 58 import static org.testng.Assert.assertFalse;
 59 
 60 public class TestUpcallStructScope {
 61     static final MethodHandle MH_do_upcall;
 62     static final CLinker LINKER = CLinker.getInstance();
 63     static final MethodHandle MH_Consumer_accept;
 64 
 65     // struct S_PDI { void* p0; double p1; int p2; };
 66     static final MemoryLayout S_PDI_LAYOUT = MemoryLayout.structLayout(
 67         C_POINTER.withName("p0"),
 68         C_DOUBLE.withName("p1"),
 69         C_INT.withName("p2")
 70     );
 71 
 72     static {
 73         System.loadLibrary("TestUpcallStructScope");
 74         SymbolLookup lookup = SymbolLookup.loaderLookup();
 75         MH_do_upcall = LINKER.downcallHandle(
 76             lookup.lookup("do_upcall").get(),
 77             MethodType.methodType(void.class, MemoryAddress.class, MemorySegment.class),
 78             FunctionDescriptor.ofVoid(C_POINTER, S_PDI_LAYOUT)
 79         );
 80 
 81         try {
 82             MH_Consumer_accept = MethodHandles.publicLookup().findVirtual(Consumer.class, "accept",
 83                     MethodType.methodType(void.class, Object.class));
 84         } catch (NoSuchMethodException | IllegalAccessException e) {
 85             throw new RuntimeException(e);
 86         }
 87     }
 88 
 89     private static MethodHandle methodHandle (Consumer<MemorySegment> callback) {
 90         return MH_Consumer_accept.bindTo(callback).asType(MethodType.methodType(void.class, MemorySegment.class));
 91     }
 92 
 93     @Test
 94     public void testUpcall() throws Throwable {
 95         AtomicReference<MemorySegment> capturedSegment = new AtomicReference<>();
 96         MethodHandle target = methodHandle(capturedSegment::set);
 97         FunctionDescriptor upcallDesc = FunctionDescriptor.ofVoid(S_PDI_LAYOUT);
 98         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 99             MemoryAddress upcallStub = LINKER.upcallStub(target, upcallDesc, scope);
100             MemorySegment argSegment = MemorySegment.allocateNative(S_PDI_LAYOUT, scope);
101             MH_do_upcall.invokeExact(upcallStub.address(), argSegment);
102         }
103 
104         MemorySegment captured = capturedSegment.get();
105         assertFalse(captured.scope().isAlive());
106     }
107 
108 }

 20  *  or visit www.oracle.com if you need additional information or have any
 21  *  questions.
 22  *
 23  */
 24 
 25 /*
 26  * @test
 27  * @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
 28  * @modules jdk.incubator.foreign/jdk.internal.foreign
 29  *
 30  * @run testng/othervm/native
 31  *   --enable-native-access=ALL-UNNAMED
 32  *   -Djdk.internal.foreign.ProgrammableInvoker.USE_SPEC=false
 33  *   TestUpcallStructScope
 34  * @run testng/othervm/native
 35  *   --enable-native-access=ALL-UNNAMED
 36  *   -Djdk.internal.foreign.ProgrammableInvoker.USE_SPEC=true
 37  *   TestUpcallStructScope
 38  */
 39 
 40 import jdk.incubator.foreign.Addressable;
 41 import jdk.incubator.foreign.CLinker;
 42 import jdk.incubator.foreign.FunctionDescriptor;
 43 import jdk.incubator.foreign.NativeSymbol;
 44 import jdk.incubator.foreign.SymbolLookup;
 45 import jdk.incubator.foreign.MemoryAddress;
 46 import jdk.incubator.foreign.MemoryLayout;
 47 import jdk.incubator.foreign.MemorySegment;
 48 import jdk.incubator.foreign.ResourceScope;
 49 import org.testng.annotations.Test;
 50 
 51 import java.lang.invoke.MethodHandle;
 52 import java.lang.invoke.MethodHandles;
 53 import java.lang.invoke.MethodType;
 54 import java.util.concurrent.atomic.AtomicReference;
 55 import java.util.function.Consumer;
 56 



 57 import static org.testng.Assert.assertFalse;
 58 
 59 public class TestUpcallStructScope extends NativeTestHelper {
 60     static final MethodHandle MH_do_upcall;
 61     static final CLinker LINKER = CLinker.systemCLinker();
 62     static final MethodHandle MH_Consumer_accept;
 63 
 64     // struct S_PDI { void* p0; double p1; int p2; };
 65     static final MemoryLayout S_PDI_LAYOUT = MemoryLayout.structLayout(
 66         C_POINTER.withName("p0"),
 67         C_DOUBLE.withName("p1"),
 68         C_INT.withName("p2")
 69     );
 70 
 71     static {
 72         System.loadLibrary("TestUpcallStructScope");
 73         SymbolLookup lookup = SymbolLookup.loaderLookup();
 74         MH_do_upcall = LINKER.downcallHandle(
 75             lookup.lookup("do_upcall").get(),
 76                 FunctionDescriptor.ofVoid(C_POINTER, S_PDI_LAYOUT)

 77         );
 78 
 79         try {
 80             MH_Consumer_accept = MethodHandles.publicLookup().findVirtual(Consumer.class, "accept",
 81                     MethodType.methodType(void.class, Object.class));
 82         } catch (NoSuchMethodException | IllegalAccessException e) {
 83             throw new RuntimeException(e);
 84         }
 85     }
 86 
 87     private static MethodHandle methodHandle (Consumer<MemorySegment> callback) {
 88         return MH_Consumer_accept.bindTo(callback).asType(MethodType.methodType(void.class, MemorySegment.class));
 89     }
 90 
 91     @Test
 92     public void testUpcall() throws Throwable {
 93         AtomicReference<MemorySegment> capturedSegment = new AtomicReference<>();
 94         MethodHandle target = methodHandle(capturedSegment::set);
 95         FunctionDescriptor upcallDesc = FunctionDescriptor.ofVoid(S_PDI_LAYOUT);
 96         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 97             NativeSymbol upcallStub = LINKER.upcallStub(target, upcallDesc, scope);
 98             MemorySegment argSegment = MemorySegment.allocateNative(S_PDI_LAYOUT, scope);
 99             MH_do_upcall.invoke(upcallStub, argSegment);
100         }
101 
102         MemorySegment captured = capturedSegment.get();
103         assertFalse(captured.scope().isAlive());
104     }
105 
106 }
< prev index next >