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.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 }