1 /*
 2  * Copyright (c) 2020, 2023, 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.
 8  *
 9  *  This code is distributed in the hope that it will be useful, but WITHOUT
10  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  *  version 2 for more details (a copy is included in the LICENSE file that
13  *  accompanied this code).
14  *
15  *  You should have received a copy of the GNU General Public License version
16  *  2 along with this work; if not, write to the Free Software Foundation,
17  *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  *  Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  *  or visit www.oracle.com if you need additional information or have any
21  *  questions.
22  *
23  */
24 
25 /*
26  * @test
27  * @enablePreview
28  * @requires jdk.foreign.linker != "UNSUPPORTED"
29  *
30  * @run testng/othervm/native
31  *   --enable-native-access=ALL-UNNAMED
32  *   -Djdk.internal.foreign.DowncallLinker.USE_SPEC=false
33  *   TestUpcallStructScope
34  * @run testng/othervm/native
35  *   --enable-native-access=ALL-UNNAMED
36  *   -Djdk.internal.foreign.DowncallLinker.USE_SPEC=true
37  *   TestUpcallStructScope
38  */
39 
40 import java.lang.foreign.*;
41 
42 import org.testng.annotations.Test;
43 
44 import java.lang.invoke.MethodHandle;
45 import java.lang.invoke.MethodHandles;
46 import java.lang.invoke.MethodType;
47 import java.util.concurrent.atomic.AtomicReference;
48 import java.util.function.Consumer;
49 
50 import static org.testng.Assert.assertFalse;
51 
52 public class TestUpcallStructScope extends NativeTestHelper {
53     static final MethodHandle MH_do_upcall;
54     static final Linker LINKER = Linker.nativeLinker();
55     static final MethodHandle MH_Consumer_accept;
56 
57     // struct S_PDI { void* p0; double p1; int p2; };
58     static final MemoryLayout S_PDI_LAYOUT = MemoryLayout.structLayout(
59         C_POINTER.withName("p0"),
60         C_DOUBLE.withName("p1"),
61         C_INT.withName("p2"),
62         MemoryLayout.paddingLayout(4)
63     );
64 
65     static {
66         System.loadLibrary("TestUpcallStructScope");
67         MH_do_upcall = LINKER.downcallHandle(
68                 findNativeOrThrow("do_upcall"),
69                 FunctionDescriptor.ofVoid(C_POINTER, S_PDI_LAYOUT)
70         );
71 
72         try {
73             MH_Consumer_accept = MethodHandles.publicLookup().findVirtual(Consumer.class, "accept",
74                     MethodType.methodType(void.class, Object.class));
75         } catch (NoSuchMethodException | IllegalAccessException e) {
76             throw new RuntimeException(e);
77         }
78     }
79 
80     private static MethodHandle methodHandle (Consumer<MemorySegment> callback) {
81         return MH_Consumer_accept.bindTo(callback).asType(MethodType.methodType(void.class, MemorySegment.class));
82     }
83 
84     @Test
85     public void testUpcall() throws Throwable {
86         AtomicReference<MemorySegment> capturedSegment = new AtomicReference<>();
87         MethodHandle target = methodHandle(capturedSegment::set);
88         FunctionDescriptor upcallDesc = FunctionDescriptor.ofVoid(S_PDI_LAYOUT);
89         try (Arena arena = Arena.ofConfined()) {
90             MemorySegment upcallStub = LINKER.upcallStub(target, upcallDesc, arena);
91             MemorySegment argSegment = arena.allocate(S_PDI_LAYOUT);
92             MH_do_upcall.invoke(upcallStub, argSegment);
93         }
94 
95         MemorySegment captured = capturedSegment.get();
96         assertFalse(captured.scope().isAlive());
97     }
98 
99 }