mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8327218: Add an ability to specify modules which should have native access enabled
Co-authored-by: Maurizio Cimadamore <mcimadamore@openjdk.org> Reviewed-by: mcimadamore, erikj, alanb, ihse
This commit is contained in:
parent
d0d4912c3b
commit
27a03e0dc3
9 changed files with 123 additions and 26 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2014, 2024, 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
|
||||
|
@ -143,10 +143,6 @@ public final class Module implements AnnotatedElement {
|
|||
String loc = Objects.toString(uri, null);
|
||||
Object[] packages = descriptor.packages().toArray();
|
||||
defineModule0(this, isOpen, vs, loc, packages);
|
||||
if (loader == null || loader == ClassLoaders.platformClassLoader()) {
|
||||
// boot/builtin modules are always native
|
||||
implAddEnableNativeAccess();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2014, 2024, 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
|
||||
|
@ -881,6 +881,24 @@ public final class ModuleLayer {
|
|||
.findAny();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the module with the given {@code name} in this layer
|
||||
* to allow access to restricted methods.
|
||||
*
|
||||
* @param name the name of the module for which the native access
|
||||
* should be enabled
|
||||
* @return {@code true} iff the module is present in this layer,
|
||||
* {@code false} otherwise
|
||||
*/
|
||||
boolean addEnableNativeAccess(String name) {
|
||||
Module m = nameToModule.get(name);
|
||||
if (m != null) {
|
||||
m.implAddEnableNativeAccess();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@code ClassLoader} for the module with the given name. If
|
||||
|
|
|
@ -2459,6 +2459,9 @@ public final class System {
|
|||
public Module addEnableNativeAccess(Module m) {
|
||||
return m.implAddEnableNativeAccess();
|
||||
}
|
||||
public boolean addEnableNativeAccess(ModuleLayer layer, String name) {
|
||||
return layer.addEnableNativeAccess(name);
|
||||
}
|
||||
public void addEnableNativeAccessToAllUnnamed() {
|
||||
Module.implAddEnableNativeAccessToAllUnnamed();
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2003, 2024, 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
|
||||
|
@ -269,6 +269,12 @@ public interface JavaLangAccess {
|
|||
*/
|
||||
Module addEnableNativeAccess(Module m);
|
||||
|
||||
/**
|
||||
* Updates module named {@code name} in layer {@code layer} to allow access to restricted methods.
|
||||
* Returns true iff the given module exists in the given layer.
|
||||
*/
|
||||
boolean addEnableNativeAccess(ModuleLayer layer, String name);
|
||||
|
||||
/**
|
||||
* Updates all unnamed modules to allow access to restricted methods.
|
||||
*/
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2014, 2024, 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
|
||||
|
@ -788,31 +788,38 @@ public final class ModuleBootstrap {
|
|||
}
|
||||
|
||||
private static final boolean HAS_ENABLE_NATIVE_ACCESS_FLAG;
|
||||
private static final Set<String> NATIVE_ACCESS_MODULES;
|
||||
private static final Set<String> USER_NATIVE_ACCESS_MODULES;
|
||||
private static final Set<String> JDK_NATIVE_ACCESS_MODULES;
|
||||
|
||||
public static boolean hasEnableNativeAccessFlag() {
|
||||
return HAS_ENABLE_NATIVE_ACCESS_FLAG;
|
||||
}
|
||||
|
||||
static {
|
||||
NATIVE_ACCESS_MODULES = decodeEnableNativeAccess();
|
||||
HAS_ENABLE_NATIVE_ACCESS_FLAG = !NATIVE_ACCESS_MODULES.isEmpty();
|
||||
USER_NATIVE_ACCESS_MODULES = decodeEnableNativeAccess();
|
||||
HAS_ENABLE_NATIVE_ACCESS_FLAG = !USER_NATIVE_ACCESS_MODULES.isEmpty();
|
||||
JDK_NATIVE_ACCESS_MODULES = ModuleLoaderMap.nativeAccessModules();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the --enable-native-access option to grant access to restricted methods to selected modules.
|
||||
* Grants native access to modules selected using the --enable-native-access
|
||||
* command line option, and also to JDK modules that need the access.
|
||||
*/
|
||||
private static void addEnableNativeAccess(ModuleLayer layer) {
|
||||
for (String name : NATIVE_ACCESS_MODULES) {
|
||||
addEnableNativeAccess(layer, USER_NATIVE_ACCESS_MODULES, true);
|
||||
addEnableNativeAccess(layer, JDK_NATIVE_ACCESS_MODULES, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Grants native access for the given modules in the given layer.
|
||||
* Warns optionally about modules that were specified, but not present in the layer.
|
||||
*/
|
||||
private static void addEnableNativeAccess(ModuleLayer layer, Set<String> moduleNames, boolean shouldWarn) {
|
||||
for (String name : moduleNames) {
|
||||
if (name.equals("ALL-UNNAMED")) {
|
||||
JLA.addEnableNativeAccessToAllUnnamed();
|
||||
} else {
|
||||
Optional<Module> module = layer.findModule(name);
|
||||
if (module.isPresent()) {
|
||||
JLA.addEnableNativeAccess(module.get());
|
||||
} else {
|
||||
warnUnknownModule(ENABLE_NATIVE_ACCESS, name);
|
||||
}
|
||||
} else if (!JLA.addEnableNativeAccess(layer, name) && shouldWarn) {
|
||||
warnUnknownModule(ENABLE_NATIVE_ACCESS, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2015, 2024, 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
|
||||
|
@ -110,6 +110,13 @@ public final class ModuleLoaderMap {
|
|||
return Modules.platformModules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the names of the modules defined to the application loader which perform native access.
|
||||
*/
|
||||
public static Set<String> nativeAccessModules() {
|
||||
return Modules.nativeAccessModules;
|
||||
}
|
||||
|
||||
private static class Modules {
|
||||
// list of boot modules is generated at build time.
|
||||
private static final Set<String> bootModules =
|
||||
|
@ -118,6 +125,10 @@ public final class ModuleLoaderMap {
|
|||
// list of platform modules is generated at build time.
|
||||
private static final Set<String> platformModules =
|
||||
Set.of(new String[] { "@@PLATFORM_MODULE_NAMES@@" });
|
||||
|
||||
// list of jdk modules is generated at build time.
|
||||
private static final Set<String> nativeAccessModules =
|
||||
Set.of(new String[] { "@@NATIVE_ACCESS_MODULE_NAMES@@" });
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue