8314260: Unable to load system libraries on Windows when using a SecurityManager

Co-authored-by: Jorn Vernee <jvernee@openjdk.org>
Reviewed-by: jvernee
This commit is contained in:
Per Minborg 2023-09-07 11:52:14 +00:00
parent 726c9c977d
commit b408a82f9b
3 changed files with 26 additions and 2 deletions

View File

@ -29,6 +29,8 @@ import java.lang.foreign.*;
import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
@ -72,11 +74,24 @@ public final class SystemLookup implements SymbolLookup {
}
private static SymbolLookup makeWindowsLookup() {
Path system32 = Path.of(System.getenv("SystemRoot"), "System32");
@SuppressWarnings("removal")
String systemRoot = AccessController.doPrivileged(new PrivilegedAction<String>() {
@Override
public String run() {
return System.getenv("SystemRoot");
}
});
Path system32 = Path.of(systemRoot, "System32");
Path ucrtbase = system32.resolve("ucrtbase.dll");
Path msvcrt = system32.resolve("msvcrt.dll");
boolean useUCRT = Files.exists(ucrtbase);
@SuppressWarnings("removal")
boolean useUCRT = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
return Files.exists(ucrtbase);
}
});
Path stdLib = useUCRT ? ucrtbase : msvcrt;
SymbolLookup lookup = libLookup(libs -> libs.load(stdLib));

View File

@ -27,6 +27,8 @@
* @requires jdk.foreign.linker != "UNSUPPORTED"
* @modules java.base/jdk.internal.foreign
* @run testng TestLinker
* @run testng/othervm/policy=security.policy
* -Djava.security.manager=default TestLinker
*/
import jdk.internal.foreign.CABI;

View File

@ -0,0 +1,7 @@
grant codeBase "file:${test.classes}/*" {
// Permissions needed to run the test
permission java.util.PropertyPermission "os.name", "read";
permission java.util.PropertyPermission "NativeTestHelper.DEFAULT_RANDOM.seed", "read";
permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.foreign";
};