Skip to content

Commit

Permalink
JDK 24 remove java.security.AccessController.doPrivileged part 2
Browse files Browse the repository at this point in the history
This pull requests is the second of two prs to remove
the use of java.security.AccessController.doPrivileged
in JDK 24. It also removes a few cases of System.getSystemProperty
and SecurityException mentions that were previously missed.

Signed-off-by: Theresa Mammarella <[email protected]>
  • Loading branch information
theresa-m committed Feb 4, 2025
1 parent 0ab22ee commit a1a3712
Show file tree
Hide file tree
Showing 20 changed files with 243 additions and 20 deletions.
4 changes: 3 additions & 1 deletion jcl/src/java.base/share/classes/java/lang/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -5655,11 +5655,11 @@ public boolean isNestmateOf(Class<?> that) {
/**
* Answers the nest member classes of the receiver's nest host.
*
* @throws SecurityException if a SecurityManager is present and package access is not allowed
/*[IF JAVA_SPEC_VERSION < 15]
* @throws LinkageError if there is any problem loading or validating a nest member or the nest host
/*[ENDIF]
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException if a SecurityManager is present and package access is not allowed
* @throws SecurityException if a returned class is not the current class, a security manager is enabled,
* the caller's class loader is not the same or an ancestor of that returned class, and the
* checkPackageAccess() denies access
Expand Down Expand Up @@ -5924,7 +5924,9 @@ Object getClassData() {
*
* @return array of Class objects if permitted subclasses exist or null if not a sealed class.
*
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException if access to any of the classes returned in the array is denied
/*[ENDIF] JAVA_SPEC_VERSION < 24
*
* @since 16
*/
Expand Down
2 changes: 2 additions & 0 deletions jcl/src/java.base/share/classes/java/lang/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -900,9 +900,11 @@ private static void initializeSystemProperties(Map<String, String> mapProperties
*
* @param code the return code.
*
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException if the running thread is not allowed to cause the vm to exit.
*
* @see SecurityManager#checkExit
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public static void exit(int code) {
RUNTIME.exit(code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,9 @@ public MethodHandle run() {
* Wrap a ThreadInfoBase object in a ThreadInfo object.
* @param base container for the ThreadInfo data
* @return ThreadInfo object, or null if base is null
/*[IF JAVA_SPEC_VERSION < 24]
* @note this must be wrapped in a doPrivileged().
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
private static ThreadInfo makeThreadInfo(ThreadInfoBase base) {
ThreadInfo newThreadInfo = null;
Expand Down
16 changes: 14 additions & 2 deletions jcl/src/openj9.cuda/share/classes/com/ibm/cuda/Cuda.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/*[IF JAVA_SPEC_VERSION < 24]*/
import java.security.AccessController;
import java.security.PrivilegedAction;
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* The {@code Cuda} class provides general CUDA utilities.
*/
/*[IF JAVA_SPEC_VERSION >= 24]*/
@SuppressWarnings({"removal", "restricted"})
@SuppressWarnings("restricted")
/*[ELSEIF JAVA_SPEC_VERSION >= 17]*/
@SuppressWarnings("removal")
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
Expand All @@ -56,11 +58,17 @@ private static final class Cleaner implements Runnable {
static {
instance = new Cleaner();

Thread daemon;
/*[IF JAVA_SPEC_VERSION >= 24]*/
daemon = VM.getVMLangAccess().createThread(instance,
"CUDA pinned buffer cleaner", true, false, true, null); //$NON-NLS-1$
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
PrivilegedAction<Thread> createThread = () -> VM.getVMLangAccess().createThread(instance,
"CUDA pinned buffer cleaner", true, false, true, null); //$NON-NLS-1$

// we assert privilege to create the Thread
Thread daemon = AccessController.doPrivileged(createThread);
daemon = AccessController.doPrivileged(createThread);
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */

daemon.start();
}
Expand Down Expand Up @@ -102,10 +110,14 @@ public void run() {
}

static {
/*[IF JAVA_SPEC_VERSION >= 24]*/
System.loadLibrary("cuda4j29"); //$NON-NLS-1$
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
System.loadLibrary("cuda4j29"); //$NON-NLS-1$
return null;
});
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */

Method runMethod;

Expand Down
20 changes: 20 additions & 0 deletions jcl/src/openj9.cuda/share/classes/com/ibm/cuda/CudaDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -520,17 +520,21 @@ public boolean canAccessPeer(CudaDevice peerDevice) throws CudaException {
* the peer device
* @throws CudaException
* if a CUDA exception occurs
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException
* if a security manager exists and the calling thread
* does not have permission to disable peer access
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public void disablePeerAccess(CudaDevice peerDevice) throws CudaException {
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();

if (security != null) {
security.checkPermission(CudaPermission.DisablePeerAccess);
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

disablePeerAccess(deviceId, peerDevice.deviceId);
}
Expand All @@ -542,17 +546,21 @@ public void disablePeerAccess(CudaDevice peerDevice) throws CudaException {
* the peer device
* @throws CudaException
* if a CUDA exception occurs
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException
* if a security manager exists and the calling thread
* does not have permission to enable peer access
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public void enablePeerAccess(CudaDevice peerDevice) throws CudaException {
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();

if (security != null) {
security.checkPermission(CudaPermission.EnablePeerAccess);
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

enablePeerAccess(deviceId, peerDevice.deviceId);
}
Expand Down Expand Up @@ -733,17 +741,21 @@ public int hashCode() {
* the desired cache configuration
* @throws CudaException
* if a CUDA exception occurs
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException
* if a security manager exists and the calling thread
* does not have permission to set device cache configurations
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public void setCacheConfig(CacheConfig config) throws CudaException {
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();

if (security != null) {
security.checkPermission(CudaPermission.SetCacheConfig);
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

setCacheConfig(deviceId, config.nativeValue);
}
Expand All @@ -757,17 +769,21 @@ public void setCacheConfig(CacheConfig config) throws CudaException {
* the desired limit value
* @throws CudaException
* if a CUDA exception occurs
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException
* if a security manager exists and the calling thread
* does not have permission to set device limits
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public void setLimit(Limit limit, long value) throws CudaException {
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();

if (security != null) {
security.checkPermission(CudaPermission.SetLimit);
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

setLimit(deviceId, limit.nativeValue, value);
}
Expand All @@ -779,17 +795,21 @@ public void setLimit(Limit limit, long value) throws CudaException {
* the desired shared memory configuration
* @throws CudaException
* if a CUDA exception occurs
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException
* if a security manager exists and the calling thread does
* not have permission to set device shared memory configurations
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public void setSharedMemConfig(SharedMemConfig config) throws CudaException {
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();

if (security != null) {
security.checkPermission(CudaPermission.SetSharedMemConfig);
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

setSharedMemConfig(deviceId, config.nativeValue);
}
Expand Down
10 changes: 10 additions & 0 deletions jcl/src/openj9.cuda/share/classes/com/ibm/cuda/CudaModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ private static native void unload(int deviceId, long moduleHandle)
* the module image
* @throws CudaException
* if a CUDA exception occurs
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException
* if a security manager exists and the calling thread
* does not have permission to load GPU modules
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public CudaModule(CudaDevice device, byte[] image) throws CudaException {
this(device, image, null);
Expand All @@ -161,20 +163,24 @@ public CudaModule(CudaDevice device, byte[] image) throws CudaException {
* the desired options
* @throws CudaException
* if a CUDA exception occurs
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException
* if a security manager exists and the calling thread
* does not have permission to load GPU modules
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public CudaModule(CudaDevice device, byte[] image, CudaJitOptions options)
throws CudaException {
super();

/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();

if (security != null) {
security.checkPermission(CudaPermission.LoadModule);
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

if (image == null) {
throw new NullPointerException();
Expand Down Expand Up @@ -210,9 +216,11 @@ public CudaModule(CudaDevice device, byte[] image, CudaJitOptions options)
* if a CUDA exception occurs
* @throws IOException
* if an I/O error occurs reading {@code input}
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException
* if a security manager exists and the calling thread
* does not have permission to load GPU modules
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public CudaModule(CudaDevice device, InputStream input)
throws CudaException, IOException {
Expand All @@ -233,9 +241,11 @@ public CudaModule(CudaDevice device, InputStream input)
* if a CUDA exception occurs
* @throws IOException
* if an I/O error occurs reading {@code input}
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException
* if a security manager exists and the calling thread
* does not have permission to load GPU modules
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public CudaModule(CudaDevice device, InputStream input,
CudaJitOptions options) throws CudaException, IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*[INCLUDE-IF Sidecar18-SE]*/
/*[INCLUDE-IF JAVA_SPEC_VERSION >= 8]*/
/*
* Copyright IBM Corp. and others 2014
*
Expand Down Expand Up @@ -68,7 +68,14 @@
* See {@link CudaDevice#setLimit(Limit, long)}.</td>
* </tr>
* </table>
*
/*[IF JAVA_SPEC_VERSION >= 24]
* @deprecated Checking permissions is not supported.
/*[ENDIF] JAVA_SPEC_VERSION >= 24
*/
/*[IF JAVA_SPEC_VERSION >= 24]*/
@Deprecated(since = "24", forRemoval = true)
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
public final class CudaPermission extends BasicPermission {

private static final long serialVersionUID = 5769765985242116236L;
Expand Down
10 changes: 10 additions & 0 deletions jcl/src/openj9.gpu/share/classes/com/ibm/gpu/CUDAManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

import java.io.IOException;
import java.io.InputStream;
/*[IF JAVA_SPEC_VERSION < 24]*/
import java.security.AccessController;
import java.security.PrivilegedAction;
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
Expand Down Expand Up @@ -60,9 +62,13 @@ private static final class Configuration {
private static final int DEFAULT_THRESHOLD = 30000;

private static void loadProperties(Properties properties, String resourceName) throws IOException {
/*[IF JAVA_SPEC_VERSION >= 24]*/
try (InputStream input = CUDAManager.class.getResourceAsStream(resourceName)) {
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
PrivilegedAction<InputStream> action = () -> CUDAManager.class.getResourceAsStream(resourceName);

try (InputStream input = AccessController.doPrivileged(action)) {
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
if (input != null) {
properties.load(input);
}
Expand Down Expand Up @@ -311,9 +317,13 @@ public static String getOutputHeader() {
}

static String getProperty(String name) {
/*[IF JAVA_SPEC_VERSION >= 24]*/
return System.getProperty(name);
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
PrivilegedAction<String> action = () -> System.getProperty(name);

return AccessController.doPrivileged(action);
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*[INCLUDE-IF Sidecar17]*/
/*[INCLUDE-IF JAVA_SPEC_VERSION >= 8]*/
/*
* Copyright IBM Corp. and others 2016
*
Expand Down Expand Up @@ -40,7 +40,14 @@
* See {@link CUDAManager#instance()}.</td>
* </tr>
* </table>
*
/*[IF JAVA_SPEC_VERSION >= 24]
* @deprecated Checking permissions is not supported.
/*[ENDIF] JAVA_SPEC_VERSION >= 24
*/
/*[IF JAVA_SPEC_VERSION >= 24]*/
@Deprecated(since = "24", forRemoval = true)
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
public final class GPUPermission extends BasicPermission {

static final Permission Access = new GPUPermission("access"); //$NON-NLS-1$
Expand Down
Loading

0 comments on commit a1a3712

Please sign in to comment.