Skip to content

Commit

Permalink
JDK 24 remove java.security.AccessController.doPrivileged part 1
Browse files Browse the repository at this point in the history
This pull requests is the first 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 Jan 30, 2025
1 parent 64284dd commit 2430b36
Show file tree
Hide file tree
Showing 31 changed files with 357 additions and 49 deletions.
13 changes: 12 additions & 1 deletion jcl/src/java.base/share/classes/com/ibm/oti/util/Msg.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*/

/*[IF JAVA_SPEC_VERSION < 24]*/
import java.security.AccessController;
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
import java.util.*;
import com.ibm.oti.vm.*;

Expand Down Expand Up @@ -62,8 +64,17 @@ public class Msg {

static {
// Attempt to load the messages.
String resourceName = "com/ibm/oti/util/ExternalMessages"; //$NON-NLS-1$
/*[IF JAVA_SPEC_VERSION >= 24]*/
try {
messages = MsgHelp.loadMessages(resourceName);
} catch (java.io.IOException e) {
e.printStackTrace();
}
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
messages = (Hashtable) AccessController.doPrivileged(
PriviAction.loadMessages("com/ibm/oti/util/ExternalMessages")); //$NON-NLS-1$
PriviAction.loadMessages(resourceName));
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*[INCLUDE-IF Sidecar16]*/
/*[INCLUDE-IF Sidecar16 & (JAVA_SPEC_VERSION < 24)]*/
package com.ibm.oti.util;

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*[INCLUDE-IF Sidecar18-SE]*/
/*[INCLUDE-IF Sidecar18-SE & (JAVA_SPEC_VERSION < 24)]*/
package com.ibm.oti.util;

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

import java.util.*;

import com.ibm.oti.util.PriviAction;

import java.io.FilePermission;
import java.lang.reflect.Method;
import java.security.AccessController;
Expand Down
2 changes: 2 additions & 0 deletions jcl/src/java.base/share/classes/java/lang/Access.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
/*[IF JAVA_SPEC_VERSION < 24]*/
import java.security.AccessControlContext;
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
import java.util.Map;

import com.ibm.oti.reflect.AnnotationParser;
Expand Down
81 changes: 62 additions & 19 deletions jcl/src/java.base/share/classes/java/lang/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
package java.lang;

import java.io.InputStream;
import java.security.AccessControlContext;
import java.security.ProtectionDomain;
import java.security.Permissions;
/*[IF JAVA_SPEC_VERSION >= 12]*/
import java.lang.constant.ClassDesc;
/*[ENDIF] JAVA_SPEC_VERSION >= 12*/
Expand All @@ -35,6 +32,15 @@
import java.lang.reflect.*;
import java.net.URL;
import java.lang.annotation.*;
/*[IF JAVA_SPEC_VERSION < 24]*/
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
import java.security.Permissions;
import java.security.ProtectionDomain;
import java.util.Collection;
import java.util.HashMap;
/*[IF JAVA_SPEC_VERSION >= 16]*/
Expand All @@ -49,9 +55,6 @@
/*[ENDIF] JAVA_SPEC_VERSION >= 12 */
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.security.PrivilegedAction;
import java.lang.ref.*;
/*[IF JAVA_SPEC_VERSION >= 12]*/
import java.lang.constant.ClassDesc;
Expand Down Expand Up @@ -91,7 +94,6 @@
import java.lang.annotation.Repeatable;
import java.lang.invoke.*;
import com.ibm.oti.reflect.TypeAnnotationParser;
import java.security.PrivilegedActionException;
import sun.security.util.SecurityConstants;

/*[IF JAVA_SPEC_VERSION >= 18]*/
Expand Down Expand Up @@ -484,7 +486,15 @@ boolean casAnnotationType(AnnotationType oldType, AnnotationType newType) {
AnnotationVars localAnnotationVars = getAnnotationVars();
long localTypeOffset = AnnotationVars.annotationTypeOffset;
if (-1 == localTypeOffset) {
Field field = AccessController.doPrivileged(new PrivilegedAction<Field>() {
Field field = null;
/*[IF JAVA_SPEC_VERSION >= 24]*/
try {
field = AnnotationVars.class.getDeclaredField("annotationType"); //$NON-NLS-1$
} catch (Exception e) {
throw newInternalError(e);
}
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
field = AccessController.doPrivileged(new PrivilegedAction<Field>() {
public Field run() {
try {
return AnnotationVars.class.getDeclaredField("annotationType"); //$NON-NLS-1$
Expand All @@ -493,6 +503,7 @@ public Field run() {
}
}
});
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
localTypeOffset = getUnsafe().objectFieldOffset(field);
AnnotationVars.annotationTypeOffset = localTypeOffset;
}
Expand All @@ -509,7 +520,9 @@ public Field run() {
* of a class as described in the class definition of
* java.lang.Class, however Classes representing base
* types can not be found using this method.
/*[IF JAVA_SPEC_VERSION < 24]
* Security rules will be obeyed.
/*[ENDIF] JAVA_SPEC_VERSION < 24
*
* @param className The name of the non-base type class to find
* @param initializeBoolean A boolean indicating whether the class should be
Expand Down Expand Up @@ -614,7 +627,9 @@ private static Class<?> forNameHelper(
* It does not invoke the class initializer.
* Note that this method does not check whether the
* requested class is accessible to its caller.
/*[IF JAVA_SPEC_VERSION < 24]
* Security rules will be obeyed.
/*[ENDIF] JAVA_SPEC_VERSION < 24
*
* @param module The name of the module
* @param name The name of the non-base type class to find
Expand All @@ -629,14 +644,15 @@ public static Class<?> forName(Module module, String name)
/*[IF JAVA_SPEC_VERSION >= 18]*/
return forNameHelper(module, name, null, false);
/*[ELSE] JAVA_SPEC_VERSION >= 18 */
@SuppressWarnings("removal")
SecurityManager sm = null;
ClassLoader classLoader;
Class<?> c;

if ((null == module) || (null == name)) {
throw new NullPointerException();
}
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager sm = null;
if (J9VMInternals.initialized) {
sm = System.getSecurityManager();
}
Expand All @@ -651,7 +667,9 @@ public ClassLoader run() {
return module.getClassLoader();
}
});
} else {
} else
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
{
classLoader = module.getClassLoader();
}

Expand Down Expand Up @@ -694,7 +712,7 @@ private static Class<?> forNameHelper(Module module, String name, Class<?> calle
if ((null == module) || (null == name)) {
throw new NullPointerException();
}
/*[IF JAVA_SPEC_VERSION < 24]*/
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager sm = null;
if (J9VMInternals.initialized) {
Expand All @@ -714,7 +732,7 @@ public ClassLoader run() {
}
});
} else
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
{
classLoader = module.getClassLoader();
}
Expand Down Expand Up @@ -840,7 +858,9 @@ public ClassLoader getClassLoader() {
/**
* Returns the classloader used to load the receiver's class.
* Returns null if the class was loaded by the bootstrap (system) class loader.
/*[IF JAVA_SPEC_VERSION < 24]
* This skips security checks.
/*[ENDIF] JAVA_SPEC_VERSION < 24
* @return the receiver's class loader or null
* @see java.lang.ClassLoader
*/
Expand Down Expand Up @@ -1848,7 +1868,9 @@ private Method throwExceptionOrReturnNull(boolean throwException, String name, C
* public Method getMethod(String name, Class<?>... parameterTypes)
* List<Method> getDeclaredPublicMethods(String name, Class<?>... parameterTypes)
* Method findMethod(boolean publicOnly, String methodName, Class<?>... parameterTypes)
/*[IF JAVA_SPEC_VERSION < 24]
* without going thorough security checking
/*[ENDIF] JAVA_SPEC_VERSION < 24
*
* @param throwException boolean
* true - throw exception in this helper;
Expand Down Expand Up @@ -2428,8 +2450,10 @@ public String getName() {
* Note: In order to conserve space in embedded targets, we allow this
* method to answer null for classes in the system protection domain
* (i.e. for system classes). System classes are always given full
* permissions (i.e. AllPermission). This is not changeable via the
* java.security.Policy.
* permissions (i.e. AllPermission).
/*[IF JAVA_SPEC_VERSION < 24]
* This is not changeable via the java.security.Policy.
/*[ENDIF] JAVA_SPEC_VERSION < 24
*
* @return ProtectionDomain
* the receiver's ProtectionDomain.
Expand Down Expand Up @@ -3573,9 +3597,12 @@ private MethodHandle getValueMethod(final Class<? extends Annotation> containedT
MethodHandle valueMethod = localAnnotationVars.valueMethod;
if (valueMethod == null) {
final MethodType methodType = MethodType.methodType(Array.newInstance(containedType, 0).getClass());
/*[IF JAVA_SPEC_VERSION < 24]*/
valueMethod = AccessController.doPrivileged(new PrivilegedAction<MethodHandle>() {
@Override
public MethodHandle run() {
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
MethodHandle handle = null;
try {
MethodHandles.Lookup localImplLookup = implLookup;
if (localImplLookup == null) {
Expand All @@ -3592,7 +3619,7 @@ public MethodHandle run() {
getUnsafe().putOrderedObject(Class.class, implLookupOffset, localImplLookup);
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */
}
MethodHandle handle = localImplLookup.findVirtual(Class.this, "value", methodType); //$NON-NLS-1$
handle = localImplLookup.findVirtual(Class.this, "value", methodType); //$NON-NLS-1$
if (AnnotationVars.valueMethodOffset == -1) {
Field valueMethodField = AnnotationVars.class.getDeclaredField("valueMethod"); //$NON-NLS-1$
AnnotationVars.valueMethodOffset = getUnsafe().objectFieldOffset(valueMethodField);
Expand All @@ -3604,14 +3631,18 @@ public MethodHandle run() {
/*[ELSE] JAVA_SPEC_VERSION >= 9 */
getUnsafe().putOrderedObject(localAnnotationVars, AnnotationVars.valueMethodOffset, handle);
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */
return handle;
} catch (NoSuchMethodException e) {
return null;
handle = null;
} catch (IllegalAccessException | NoSuchFieldException e) {
throw newInternalError(e);
}
/*[IF JAVA_SPEC_VERSION >= 24]*/
valueMethod = handle;
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
return handle;
}
});
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
}
return valueMethod;
}
Expand Down Expand Up @@ -3981,6 +4012,10 @@ T[] getEnumConstantsShared() {
T[] enums = localEnumVars.cachedEnumConstants;
if (null == enums && isEnum()) {
try {
/*[IF JAVA_SPEC_VERSION >= 24]*/
Method values = getMethod("values"); //$NON-NLS-1$
values.setAccessible(true);
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
final PrivilegedExceptionAction<Method> privilegedAction = new PrivilegedExceptionAction<Method>() {
@Override
public Method run() throws Exception {
Expand All @@ -3993,6 +4028,7 @@ public Method run() throws Exception {
};

Method values = AccessController.doPrivileged(privilegedAction);
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
Object rawEnums = values.invoke(this);
if ((rawEnums == null) || !rawEnums.getClass().isArray()) {
return null;
Expand All @@ -4016,7 +4052,14 @@ public Method run() throws Exception {
/*[ELSE] JAVA_SPEC_VERSION >= 9 */
getUnsafe().putOrderedObject(localEnumVars, localEnumConstantsOffset, enums);
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | PrivilegedActionException e) {
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
/*[IF JAVA_SPEC_VERSION >= 24]*/
| NoSuchMethodException
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
| PrivilegedActionException
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
e
) {
enums = null;
}
}
Expand Down
18 changes: 15 additions & 3 deletions jcl/src/java.base/share/classes/java/lang/J9VMInternals.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@
import com.ibm.oti.vm.J9UnmodifiableClass;
import java.lang.ref.SoftReference;
import java.lang.reflect.*;
/*[IF JAVA_SPEC_VERSION < 24]*/
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.WeakHashMap;
import java.security.AccessControlContext;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.io.FileDescriptor;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -271,8 +273,10 @@ private static void ensureError(Throwable err) {
private static native Throwable newInstance(Class exceptionClass, Class constructorClass);

private static Throwable cloneThrowable(final Throwable throwable, final HashMap hashMapThrowable) {
/*[IF JAVA_SPEC_VERSION < 24]*/
return (Throwable)AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
Throwable clone;
try {
Class cls = throwable.getClass();
Expand Down Expand Up @@ -305,8 +309,10 @@ public Object run() {
clone = new Throwable(Msg.getString("K05c3", e, throwable.toString())); //$NON-NLS-1$
}
return clone;
/*[IF JAVA_SPEC_VERSION < 24]*/
}
});
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
}

/**
Expand Down Expand Up @@ -612,9 +618,11 @@ private static String[] getClassInfoStrings(final Class<?> clazz, String classPa
ClassLoader classLoader = clazz.getClassLoader();
if (classLoader != null) {
classLoaderStr = classLoader.toString();
/*[IF JAVA_SPEC_VERSION < 24]*/
classPath = AccessController.doPrivileged(new PrivilegedAction<String>() {
@Override
public String run() {
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
String path = null;
try {
ProtectionDomain pd = clazz.getProtectionDomain();
Expand All @@ -629,9 +637,13 @@ public String run() {
}
} catch (Exception e) {
}
/*[IF JAVA_SPEC_VERSION >= 24]*/
classPath = path;
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
return path;
}
});
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
}
}
if (classPath != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*[INCLUDE-IF Sidecar16]*/
/*[INCLUDE-IF Sidecar16 & (JAVA_SPEC_VERSION < 24)]*/
package java.lang;

/*
Expand Down
Loading

0 comments on commit 2430b36

Please sign in to comment.