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  *
28  * @run testng/othervm/native
29  *   --enable-native-access=ALL-UNNAMED
30  *   -Djdk.internal.foreign.DowncallLinker.USE_SPEC=false
31  *   TestUpcallStructScope
32  * @run testng/othervm/native
33  *   --enable-native-access=ALL-UNNAMED
34  *   -Djdk.internal.foreign.DowncallLinker.USE_SPEC=true
35  *   TestUpcallStructScope
36  */
37 
38 import java.lang.foreign.*;
39 
40 import org.testng.annotations.Test;
41 
42 import java.lang.invoke.MethodHandle;
43 import java.lang.invoke.MethodHandles;
44 import java.lang.invoke.MethodType;
45 import java.util.concurrent.atomic.AtomicReference;
46 import java.util.function.Consumer;
47 
48 import static org.testng.Assert.assertFalse;
49 
50 public class TestUpcallStructScope extends NativeTestHelper {
51     static final MethodHandle MH_do_upcall;
52     static final MethodHandle MH_Consumer_accept;
53 
54     static {
55         System.loadLibrary("TestUpcallStructScope");
56         MH_do_upcall = LINKER.downcallHandle(
57                 findNativeOrThrow("do_upcall"),
58                 FunctionDescriptor.ofVoid(C_POINTER, S_PDI_LAYOUT)
59         );
60 
61         try {
62             MH_Consumer_accept = MethodHandles.publicLookup().findVirtual(Consumer.class, "accept",
63                     MethodType.methodType(void.class, Object.class));
64         } catch (NoSuchMethodException | IllegalAccessException e) {
65             throw new RuntimeException(e);
66         }
67     }
68 
69     private static MethodHandle methodHandle (Consumer<MemorySegment> callback) {
70         return MH_Consumer_accept.bindTo(callback).asType(MethodType.methodType(void.class, MemorySegment.class));
71     }
72 
73     @Test
74     public void testUpcall() throws Throwable {
75         AtomicReference<MemorySegment> capturedSegment = new AtomicReference<>();
76         MethodHandle target = methodHandle(capturedSegment::set);
77         FunctionDescriptor upcallDesc = FunctionDescriptor.ofVoid(S_PDI_LAYOUT);
78         try (Arena arena = Arena.ofConfined()) {
79             MemorySegment upcallStub = LINKER.upcallStub(target, upcallDesc, arena);
80             MemorySegment argSegment = arena.allocate(S_PDI_LAYOUT);
81             MH_do_upcall.invoke(upcallStub, argSegment);
82         }
83 
84         MemorySegment captured = capturedSegment.get();
85         assertFalse(captured.scope().isAlive());
86     }
87 
88 }