Path: blob/master/test/jdk/java/foreign/TestUpcallStructScope.java
41145 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/*25* @test26* @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"27* @modules jdk.incubator.foreign/jdk.internal.foreign28*29* @run testng/othervm/native30* --enable-native-access=ALL-UNNAMED31* -Djdk.internal.foreign.ProgrammableInvoker.USE_SPEC=false32* TestUpcallStructScope33* @run testng/othervm/native34* --enable-native-access=ALL-UNNAMED35* -Djdk.internal.foreign.ProgrammableInvoker.USE_SPEC=true36* TestUpcallStructScope37*/3839import jdk.incubator.foreign.CLinker;40import jdk.incubator.foreign.FunctionDescriptor;41import jdk.incubator.foreign.SymbolLookup;42import jdk.incubator.foreign.MemoryAddress;43import jdk.incubator.foreign.MemoryLayout;44import jdk.incubator.foreign.MemorySegment;45import jdk.incubator.foreign.ResourceScope;46import org.testng.annotations.Test;4748import java.lang.invoke.MethodHandle;49import java.lang.invoke.MethodHandles;50import java.lang.invoke.MethodType;51import java.util.concurrent.atomic.AtomicReference;52import java.util.function.Consumer;5354import static jdk.incubator.foreign.CLinker.C_DOUBLE;55import static jdk.incubator.foreign.CLinker.C_INT;56import static jdk.incubator.foreign.CLinker.C_POINTER;57import static org.testng.Assert.assertFalse;5859public class TestUpcallStructScope {60static final MethodHandle MH_do_upcall;61static final CLinker LINKER = CLinker.getInstance();62static final MethodHandle MH_Consumer_accept;6364// struct S_PDI { void* p0; double p1; int p2; };65static final MemoryLayout S_PDI_LAYOUT = MemoryLayout.structLayout(66C_POINTER.withName("p0"),67C_DOUBLE.withName("p1"),68C_INT.withName("p2")69);7071static {72System.loadLibrary("TestUpcallStructScope");73SymbolLookup lookup = SymbolLookup.loaderLookup();74MH_do_upcall = LINKER.downcallHandle(75lookup.lookup("do_upcall").get(),76MethodType.methodType(void.class, MemoryAddress.class, MemorySegment.class),77FunctionDescriptor.ofVoid(C_POINTER, S_PDI_LAYOUT)78);7980try {81MH_Consumer_accept = MethodHandles.publicLookup().findVirtual(Consumer.class, "accept",82MethodType.methodType(void.class, Object.class));83} catch (NoSuchMethodException | IllegalAccessException e) {84throw new RuntimeException(e);85}86}8788private static MethodHandle methodHandle (Consumer<MemorySegment> callback) {89return MH_Consumer_accept.bindTo(callback).asType(MethodType.methodType(void.class, MemorySegment.class));90}9192@Test93public void testUpcall() throws Throwable {94AtomicReference<MemorySegment> capturedSegment = new AtomicReference<>();95MethodHandle target = methodHandle(capturedSegment::set);96FunctionDescriptor upcallDesc = FunctionDescriptor.ofVoid(S_PDI_LAYOUT);97try (ResourceScope scope = ResourceScope.newConfinedScope()) {98MemoryAddress upcallStub = LINKER.upcallStub(target, upcallDesc, scope);99MemorySegment argSegment = MemorySegment.allocateNative(S_PDI_LAYOUT, scope);100MH_do_upcall.invokeExact(upcallStub.address(), argSegment);101}102103MemorySegment captured = capturedSegment.get();104assertFalse(captured.scope().isAlive());105}106107}108109110