1 /*
  2  * Copyright (c) 2020, 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  * @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 }