Skip to content

Commit

Permalink
8321159: SymbolLookup.libraryLookup(Path, Arena) Assumes default File…
Browse files Browse the repository at this point in the history
…system

Reviewed-by: mcimadamore
  • Loading branch information
minborg committed Dec 6, 2023
1 parent 9d77677 commit a0920aa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import jdk.internal.reflect.Reflection;

import java.lang.invoke.MethodHandles;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -284,6 +285,7 @@ static SymbolLookup libraryLookup(String name, Arena arena) {
* @throws WrongThreadException if {@code arena} is a confined arena, and this method
* is called from a thread {@code T}, other than the arena's owner thread
* @throws IllegalArgumentException if {@code path} does not point to a valid library
* in the default file system
* @throws IllegalCallerException If the caller is in a module that does not have
* native access enabled
*/
Expand All @@ -292,6 +294,9 @@ static SymbolLookup libraryLookup(String name, Arena arena) {
static SymbolLookup libraryLookup(Path path, Arena arena) {
Reflection.ensureNativeAccess(Reflection.getCallerClass(),
SymbolLookup.class, "libraryLookup");
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new IllegalArgumentException("Path not in default file system: " + path);
}
return libraryLookup(path, RawNativeLibraries::load, arena);
}

Expand Down
37 changes: 37 additions & 0 deletions test/jdk/java/foreign/LibraryLookupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@

import org.testng.annotations.Test;

import java.io.IOException;
import java.lang.foreign.*;
import java.lang.foreign.Arena;
import java.lang.invoke.MethodHandle;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -91,6 +97,37 @@ void testLoadLibraryBadLookupName() {
}
}

@Test
void testLoadLibraryNonDefaultFileSystem() throws URISyntaxException, IOException {
try (FileSystem customFs = fsFromJarOfClass(org.testng.annotations.Test.class)) {
try (Arena arena = Arena.ofConfined()) {
Path p = customFs.getPath(".");
try {
SymbolLookup.libraryLookup(p, arena);
fail("Did not throw IAE");
} catch (IllegalArgumentException iae) {
assertTrue(iae.getMessage().contains("not in default file system"));
}
}
}
}

private static FileSystem fsFromJarOfClass(Class<?> clazz) throws URISyntaxException, IOException {
String name = clazz.getName();
final int lastDot = name.lastIndexOf('.');
if (lastDot != -1) {
name = name.substring(lastDot + 1);
}
URI uri = clazz.getResource(name + ".class").toURI();
if (uri.getScheme().equals("jar")) {
final String[] parts = uri.toString().split("!");
if (parts.length == 2) {
return FileSystems.newFileSystem(URI.create(parts[0]), new HashMap<>());
}
}
throw new AssertionError("Unable to create file system from " + clazz);
}

private static MemorySegment loadLibrary(Arena session) {
SymbolLookup lib = SymbolLookup.libraryLookup(LIB_PATH, session);
MemorySegment addr = lib.find("inc").get();
Expand Down

1 comment on commit a0920aa

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.