8321159: SymbolLookup.libraryLookup(Path, Arena) Assumes default Filesystem

Reviewed-by: mcimadamore
This commit is contained in:
Per Minborg 2023-12-06 08:02:52 +00:00
parent 9d776777c5
commit a0920aa436
2 changed files with 42 additions and 0 deletions

View File

@ -37,6 +37,7 @@ import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.Reflection; import jdk.internal.reflect.Reflection;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.nio.file.FileSystems;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
@ -284,6 +285,7 @@ public interface SymbolLookup {
* @throws WrongThreadException if {@code arena} is a confined arena, and this method * @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 * 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 * @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 * @throws IllegalCallerException If the caller is in a module that does not have
* native access enabled * native access enabled
*/ */
@ -292,6 +294,9 @@ public interface SymbolLookup {
static SymbolLookup libraryLookup(Path path, Arena arena) { static SymbolLookup libraryLookup(Path path, Arena arena) {
Reflection.ensureNativeAccess(Reflection.getCallerClass(), Reflection.ensureNativeAccess(Reflection.getCallerClass(),
SymbolLookup.class, "libraryLookup"); SymbolLookup.class, "libraryLookup");
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new IllegalArgumentException("Path not in default file system: " + path);
}
return libraryLookup(path, RawNativeLibraries::load, arena); return libraryLookup(path, RawNativeLibraries::load, arena);
} }

View File

@ -23,10 +23,16 @@
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.io.IOException;
import java.lang.foreign.*; import java.lang.foreign.*;
import java.lang.foreign.Arena; import java.lang.foreign.Arena;
import java.lang.invoke.MethodHandle; 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.nio.file.Path;
import java.util.HashMap;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -91,6 +97,37 @@ public class LibraryLookupTest {
} }
} }
@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) { private static MemorySegment loadLibrary(Arena session) {
SymbolLookup lib = SymbolLookup.libraryLookup(LIB_PATH, session); SymbolLookup lib = SymbolLookup.libraryLookup(LIB_PATH, session);
MemorySegment addr = lib.find("inc").get(); MemorySegment addr = lib.find("inc").get();