1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import org.testng.annotations.Test;
import java.lang.foreign.*;
import java.lang.foreign.Arena;
import java.lang.invoke.MethodHandle;
import java.nio.file.Path;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static org.testng.Assert.*;
/*
* @test
* @enablePreview
* @requires jdk.foreign.linker != "UNSUPPORTED"
* @run testng/othervm --enable-native-access=ALL-UNNAMED LibraryLookupTest
*/
public class LibraryLookupTest {
static final Path JAVA_LIBRARY_PATH = Path.of(System.getProperty("java.library.path"));
static final MethodHandle INC = Linker.nativeLinker().downcallHandle(FunctionDescriptor.ofVoid());
static final Path LIB_PATH = JAVA_LIBRARY_PATH.resolve(System.mapLibraryName("LibraryLookup"));
@Test
void testLoadLibraryConfined() {
try (Arena arena0 = Arena.ofConfined()) {
callFunc(loadLibrary(arena0));
try (Arena arena1 = Arena.ofConfined()) {
callFunc(loadLibrary(arena1));
try (Arena arena2 = Arena.ofConfined()) {
callFunc(loadLibrary(arena2));
}
}
}
}
@Test(expectedExceptions = IllegalStateException.class)
void testLoadLibraryConfinedClosed() {
MemorySegment addr;
try (Arena arena = Arena.ofConfined()) {
addr = loadLibrary(arena);
}
callFunc(addr);
}
@Test(expectedExceptions = IllegalArgumentException.class)
void testLoadLibraryBadName() {
try (Arena arena = Arena.ofConfined()) {
SymbolLookup.libraryLookup(LIB_PATH.toString() + "\u0000", arena);
}
}
@Test
void testLoadLibraryBadLookupName() {
try (Arena arena = Arena.ofConfined()) {
SymbolLookup lookup = SymbolLookup.libraryLookup(LIB_PATH, arena);
assertTrue(lookup.find("inc\u0000foobar").isEmpty());
}
}
private static MemorySegment loadLibrary(Arena session) {
SymbolLookup lib = SymbolLookup.libraryLookup(LIB_PATH, session);
MemorySegment addr = lib.find("inc").get();
assertEquals(addr.scope(), session.scope());
return addr;
}
private static void callFunc(MemorySegment addr) {
try {
INC.invokeExact(addr);
} catch (IllegalStateException ex) {
throw ex;
} catch (Throwable ex) {
throw new AssertionError(ex);
}
}
static final int ITERATIONS = 100;
static final int MAX_EXECUTOR_WAIT_SECONDS = 20;
static final int NUM_ACCESSORS = Math.min(10, Runtime.getRuntime().availableProcessors());
@Test(expectedExceptions = IllegalArgumentException.class)
void testBadLibraryLookupName() {
SymbolLookup.libraryLookup("nonExistent", Arena.global());
}
@Test(expectedExceptions = IllegalArgumentException.class)
void testBadLibraryLookupPath() {
SymbolLookup.libraryLookup(Path.of("nonExistent"), Arena.global());
}
@Test
void testLoadLibraryShared() throws Throwable {
ExecutorService accessExecutor = Executors.newCachedThreadPool();
for (int i = 0; i < NUM_ACCESSORS ; i++) {
accessExecutor.execute(new LibraryLoadAndAccess());
}
accessExecutor.shutdown();
assertTrue(accessExecutor.awaitTermination(MAX_EXECUTOR_WAIT_SECONDS, TimeUnit.SECONDS));
}
static class LibraryLoadAndAccess implements Runnable {
@Override
public void run() {
for (int i = 0 ; i < ITERATIONS ; i++) {
try (Arena arena = Arena.ofConfined()) {
callFunc(loadLibrary(arena));
}
}
}
}
@Test
void testLoadLibrarySharedClosed() throws Throwable {
Arena arena = Arena.ofShared();
MemorySegment addr = loadLibrary(arena);
ExecutorService accessExecutor = Executors.newCachedThreadPool();
for (int i = 0; i < NUM_ACCESSORS ; i++) {
accessExecutor.execute(new LibraryAccess(addr));
}
while (true) {
try {
arena.close();
break;
} catch (IllegalStateException ex) {
// wait for addressable parameter to be released
Thread.onSpinWait();
}
}
accessExecutor.shutdown();
assertTrue(accessExecutor.awaitTermination(MAX_EXECUTOR_WAIT_SECONDS, TimeUnit.SECONDS));
}
static class LibraryAccess implements Runnable {
final MemorySegment addr;
LibraryAccess(MemorySegment addr) {
this.addr = addr;
}
@Override
public void run() {
for (int i = 0 ; i < ITERATIONS ; i++) {
try {
callFunc(addr);
} catch (IllegalStateException ex) {
// library closed
break;
}
}
}
}
}
|