Path: blob/master/test/jdk/java/foreign/SafeFunctionAccessTest.java
41144 views
/*1* Copyright (c) 2021, 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*/2223/*24* @test25* @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"26* @run testng/othervm --enable-native-access=ALL-UNNAMED SafeFunctionAccessTest27*/2829import jdk.incubator.foreign.CLinker;30import jdk.incubator.foreign.FunctionDescriptor;31import jdk.incubator.foreign.SymbolLookup;32import jdk.incubator.foreign.MemoryAddress;33import jdk.incubator.foreign.MemoryLayout;34import jdk.incubator.foreign.MemorySegment;35import jdk.incubator.foreign.ResourceScope;3637import java.lang.invoke.MethodHandle;38import java.lang.invoke.MethodType;3940import org.testng.annotations.*;41import static org.testng.Assert.*;4243public class SafeFunctionAccessTest {44static {45System.loadLibrary("SafeAccess");46}4748static MemoryLayout POINT = MemoryLayout.structLayout(49CLinker.C_INT, CLinker.C_INT50);5152static final SymbolLookup LOOKUP = SymbolLookup.loaderLookup();5354@Test(expectedExceptions = IllegalStateException.class)55public void testClosedStruct() throws Throwable {56MemorySegment segment;57try (ResourceScope scope = ResourceScope.newConfinedScope()) {58segment = MemorySegment.allocateNative(POINT, scope);59}60assertFalse(segment.scope().isAlive());61MethodHandle handle = CLinker.getInstance().downcallHandle(62LOOKUP.lookup("struct_func").get(),63MethodType.methodType(void.class, MemorySegment.class),64FunctionDescriptor.ofVoid(POINT));6566handle.invokeExact(segment);67}6869@Test(expectedExceptions = IllegalStateException.class)70public void testClosedPointer() throws Throwable {71MemoryAddress address;72try (ResourceScope scope = ResourceScope.newConfinedScope()) {73address = MemorySegment.allocateNative(POINT, scope).address();74}75assertFalse(address.scope().isAlive());76MethodHandle handle = CLinker.getInstance().downcallHandle(77LOOKUP.lookup("addr_func").get(),78MethodType.methodType(void.class, MemoryAddress.class),79FunctionDescriptor.ofVoid(CLinker.C_POINTER));8081handle.invokeExact(address);82}83}848586