Skip to content

Commit 11af434

Browse files
committed
JDK 24 remove java.security.AccessController.doPrivileged part 1
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]>
1 parent 7a12bae commit 11af434

31 files changed

+377
-62
lines changed

jcl/src/java.base/share/classes/com/ibm/oti/util/Msg.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
* 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
2525
*/
2626

27+
/*[IF JAVA_SPEC_VERSION < 24]*/
2728
import java.security.AccessController;
29+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
2830
import java.util.*;
2931
import com.ibm.oti.vm.*;
3032

@@ -61,9 +63,18 @@ public class Msg {
6163
static private Hashtable messages;
6264

6365
static {
66+
String resourceName = "com/ibm/oti/util/ExternalMessages"; //$NON-NLS-1$
6467
// Attempt to load the messages.
68+
/*[IF JAVA_SPEC_VERSION >= 24]*/
69+
try {
70+
messages = MsgHelp.loadMessages(resourceName);
71+
} catch (java.io.IOException e) {
72+
// ignore: continue without messages
73+
}
74+
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
6575
messages = (Hashtable) AccessController.doPrivileged(
66-
PriviAction.loadMessages("com/ibm/oti/util/ExternalMessages")); //$NON-NLS-1$
76+
PriviAction.loadMessages(resourceName));
77+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
6778
}
6879

6980
/**

jcl/src/java.base/share/classes/com/ibm/oti/util/PriviAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*[INCLUDE-IF Sidecar16]*/
1+
/*[INCLUDE-IF JAVA_SPEC_VERSION < 24]*/
22
package com.ibm.oti.util;
33

44
/*

jcl/src/java.base/share/classes/com/ibm/oti/util/RuntimePermissions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*[INCLUDE-IF Sidecar18-SE]*/
1+
/*[INCLUDE-IF JAVA_SPEC_VERSION < 24]*/
22
package com.ibm.oti.util;
33

44
/*

jcl/src/java.base/share/classes/com/ibm/oti/vm/BootstrapClassLoader.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
import java.util.*;
2626

27-
import com.ibm.oti.util.PriviAction;
28-
2927
import java.io.FilePermission;
3028
import java.lang.reflect.Method;
3129
import java.security.AccessController;

jcl/src/java.base/share/classes/java/lang/Access.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.lang.reflect.Constructor;
3636
import java.lang.reflect.Executable;
3737
import java.lang.reflect.Method;
38-
import java.security.AccessControlContext;
3938
import java.util.Map;
4039

4140
import com.ibm.oti.reflect.AnnotationParser;
@@ -173,7 +172,7 @@ public java.lang.StackTraceElement getStackTraceElement(java.lang.Throwable arg0
173172
/*[IF JAVA_SPEC_VERSION < 24]*/
174173
/*[PR CMVC 199693] Prevent trusted method chain attack. */
175174
@SuppressWarnings("removal")
176-
public Thread newThreadWithAcc(Runnable runnable, AccessControlContext acc) {
175+
public Thread newThreadWithAcc(Runnable runnable, java.security.AccessControlContext acc) {
177176
return new Thread(runnable, acc);
178177
}
179178
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

jcl/src/java.base/share/classes/java/lang/Class.java

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
package java.lang;
2424

2525
import java.io.InputStream;
26-
import java.security.AccessControlContext;
27-
import java.security.ProtectionDomain;
28-
import java.security.Permissions;
2926
/*[IF JAVA_SPEC_VERSION >= 12]*/
3027
import java.lang.constant.ClassDesc;
3128
/*[ENDIF] JAVA_SPEC_VERSION >= 12*/
@@ -35,6 +32,15 @@
3532
import java.lang.reflect.*;
3633
import java.net.URL;
3734
import java.lang.annotation.*;
35+
/*[IF JAVA_SPEC_VERSION < 24]*/
36+
import java.security.AccessControlContext;
37+
import java.security.AccessController;
38+
import java.security.PrivilegedAction;
39+
import java.security.PrivilegedActionException;
40+
import java.security.PrivilegedExceptionAction;
41+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
42+
import java.security.Permissions;
43+
import java.security.ProtectionDomain;
3844
import java.util.Collection;
3945
import java.util.HashMap;
4046
/*[IF JAVA_SPEC_VERSION >= 16]*/
@@ -49,9 +55,6 @@
4955
/*[ENDIF] JAVA_SPEC_VERSION >= 12 */
5056
import java.util.concurrent.ConcurrentHashMap;
5157
import java.util.concurrent.atomic.AtomicInteger;
52-
import java.security.AccessController;
53-
import java.security.PrivilegedExceptionAction;
54-
import java.security.PrivilegedAction;
5558
import java.lang.ref.*;
5659
/*[IF JAVA_SPEC_VERSION >= 12]*/
5760
import java.lang.constant.ClassDesc;
@@ -91,7 +94,6 @@
9194
import java.lang.annotation.Repeatable;
9295
import java.lang.invoke.*;
9396
import com.ibm.oti.reflect.TypeAnnotationParser;
94-
import java.security.PrivilegedActionException;
9597
import sun.security.util.SecurityConstants;
9698

9799
/*[IF JAVA_SPEC_VERSION >= 18]*/
@@ -484,7 +486,16 @@ boolean casAnnotationType(AnnotationType oldType, AnnotationType newType) {
484486
AnnotationVars localAnnotationVars = getAnnotationVars();
485487
long localTypeOffset = AnnotationVars.annotationTypeOffset;
486488
if (-1 == localTypeOffset) {
487-
Field field = AccessController.doPrivileged(new PrivilegedAction<Field>() {
489+
Field field;
490+
/*[IF JAVA_SPEC_VERSION >= 24]*/
491+
try {
492+
field = AnnotationVars.class.getDeclaredField("annotationType"); //$NON-NLS-1$
493+
} catch (Exception e) {
494+
throw newInternalError(e);
495+
}
496+
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
497+
field = AccessController.doPrivileged(new PrivilegedAction<Field>() {
498+
@Override
488499
public Field run() {
489500
try {
490501
return AnnotationVars.class.getDeclaredField("annotationType"); //$NON-NLS-1$
@@ -493,6 +504,7 @@ public Field run() {
493504
}
494505
}
495506
});
507+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
496508
localTypeOffset = getUnsafe().objectFieldOffset(field);
497509
AnnotationVars.annotationTypeOffset = localTypeOffset;
498510
}
@@ -509,7 +521,9 @@ public Field run() {
509521
* of a class as described in the class definition of
510522
* java.lang.Class, however Classes representing base
511523
* types can not be found using this method.
524+
/*[IF JAVA_SPEC_VERSION < 24]
512525
* Security rules will be obeyed.
526+
/*[ENDIF] JAVA_SPEC_VERSION < 24
513527
*
514528
* @param className The name of the non-base type class to find
515529
* @param initializeBoolean A boolean indicating whether the class should be
@@ -614,7 +628,9 @@ private static Class<?> forNameHelper(
614628
* It does not invoke the class initializer.
615629
* Note that this method does not check whether the
616630
* requested class is accessible to its caller.
631+
/*[IF JAVA_SPEC_VERSION < 24]
617632
* Security rules will be obeyed.
633+
/*[ENDIF] JAVA_SPEC_VERSION < 24
618634
*
619635
* @param module The name of the module
620636
* @param name The name of the non-base type class to find
@@ -629,14 +645,15 @@ public static Class<?> forName(Module module, String name)
629645
/*[IF JAVA_SPEC_VERSION >= 18]*/
630646
return forNameHelper(module, name, null, false);
631647
/*[ELSE] JAVA_SPEC_VERSION >= 18 */
632-
@SuppressWarnings("removal")
633-
SecurityManager sm = null;
634648
ClassLoader classLoader;
635649
Class<?> c;
636650

637651
if ((null == module) || (null == name)) {
638652
throw new NullPointerException();
639653
}
654+
/*[IF JAVA_SPEC_VERSION < 24]*/
655+
@SuppressWarnings("removal")
656+
SecurityManager sm = null;
640657
if (J9VMInternals.initialized) {
641658
sm = System.getSecurityManager();
642659
}
@@ -651,7 +668,9 @@ public ClassLoader run() {
651668
return module.getClassLoader();
652669
}
653670
});
654-
} else {
671+
} else
672+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
673+
{
655674
classLoader = module.getClassLoader();
656675
}
657676

@@ -694,7 +713,7 @@ private static Class<?> forNameHelper(Module module, String name, Class<?> calle
694713
if ((null == module) || (null == name)) {
695714
throw new NullPointerException();
696715
}
697-
/*[IF JAVA_SPEC_VERSION < 24]*/
716+
/*[IF JAVA_SPEC_VERSION < 24]*/
698717
@SuppressWarnings("removal")
699718
SecurityManager sm = null;
700719
if (J9VMInternals.initialized) {
@@ -714,7 +733,7 @@ public ClassLoader run() {
714733
}
715734
});
716735
} else
717-
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
736+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
718737
{
719738
classLoader = module.getClassLoader();
720739
}
@@ -840,7 +859,9 @@ public ClassLoader getClassLoader() {
840859
/**
841860
* Returns the classloader used to load the receiver's class.
842861
* Returns null if the class was loaded by the bootstrap (system) class loader.
862+
/*[IF JAVA_SPEC_VERSION < 24]
843863
* This skips security checks.
864+
/*[ENDIF] JAVA_SPEC_VERSION < 24
844865
* @return the receiver's class loader or null
845866
* @see java.lang.ClassLoader
846867
*/
@@ -1848,7 +1869,9 @@ private Method throwExceptionOrReturnNull(boolean throwException, String name, C
18481869
* public Method getMethod(String name, Class<?>... parameterTypes)
18491870
* List<Method> getDeclaredPublicMethods(String name, Class<?>... parameterTypes)
18501871
* Method findMethod(boolean publicOnly, String methodName, Class<?>... parameterTypes)
1872+
/*[IF JAVA_SPEC_VERSION < 24]
18511873
* without going thorough security checking
1874+
/*[ENDIF] JAVA_SPEC_VERSION < 24
18521875
*
18531876
* @param throwException boolean
18541877
* true - throw exception in this helper;
@@ -2428,8 +2451,10 @@ public String getName() {
24282451
* Note: In order to conserve space in embedded targets, we allow this
24292452
* method to answer null for classes in the system protection domain
24302453
* (i.e. for system classes). System classes are always given full
2431-
* permissions (i.e. AllPermission). This is not changeable via the
2432-
* java.security.Policy.
2454+
* permissions (i.e. AllPermission).
2455+
/*[IF JAVA_SPEC_VERSION < 24]
2456+
* This is not changeable via the java.security.Policy.
2457+
/*[ENDIF] JAVA_SPEC_VERSION < 24
24332458
*
24342459
* @return ProtectionDomain
24352460
* the receiver's ProtectionDomain.
@@ -3573,9 +3598,12 @@ private MethodHandle getValueMethod(final Class<? extends Annotation> containedT
35733598
MethodHandle valueMethod = localAnnotationVars.valueMethod;
35743599
if (valueMethod == null) {
35753600
final MethodType methodType = MethodType.methodType(Array.newInstance(containedType, 0).getClass());
3601+
/*[IF JAVA_SPEC_VERSION < 24]*/
35763602
valueMethod = AccessController.doPrivileged(new PrivilegedAction<MethodHandle>() {
35773603
@Override
35783604
public MethodHandle run() {
3605+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
3606+
MethodHandle handle;
35793607
try {
35803608
MethodHandles.Lookup localImplLookup = implLookup;
35813609
if (localImplLookup == null) {
@@ -3592,7 +3620,7 @@ public MethodHandle run() {
35923620
getUnsafe().putOrderedObject(Class.class, implLookupOffset, localImplLookup);
35933621
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */
35943622
}
3595-
MethodHandle handle = localImplLookup.findVirtual(Class.this, "value", methodType); //$NON-NLS-1$
3623+
handle = localImplLookup.findVirtual(Class.this, "value", methodType); //$NON-NLS-1$
35963624
if (AnnotationVars.valueMethodOffset == -1) {
35973625
Field valueMethodField = AnnotationVars.class.getDeclaredField("valueMethod"); //$NON-NLS-1$
35983626
AnnotationVars.valueMethodOffset = getUnsafe().objectFieldOffset(valueMethodField);
@@ -3604,14 +3632,18 @@ public MethodHandle run() {
36043632
/*[ELSE] JAVA_SPEC_VERSION >= 9 */
36053633
getUnsafe().putOrderedObject(localAnnotationVars, AnnotationVars.valueMethodOffset, handle);
36063634
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */
3607-
return handle;
36083635
} catch (NoSuchMethodException e) {
3609-
return null;
3636+
handle = null;
36103637
} catch (IllegalAccessException | NoSuchFieldException e) {
36113638
throw newInternalError(e);
36123639
}
3640+
/*[IF JAVA_SPEC_VERSION >= 24]*/
3641+
valueMethod = handle;
3642+
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
3643+
return handle;
36133644
}
36143645
});
3646+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
36153647
}
36163648
return valueMethod;
36173649
}
@@ -3981,6 +4013,10 @@ T[] getEnumConstantsShared() {
39814013
T[] enums = localEnumVars.cachedEnumConstants;
39824014
if (null == enums && isEnum()) {
39834015
try {
4016+
/*[IF JAVA_SPEC_VERSION >= 24]*/
4017+
Method values = getMethod("values"); //$NON-NLS-1$
4018+
values.setAccessible(true);
4019+
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
39844020
final PrivilegedExceptionAction<Method> privilegedAction = new PrivilegedExceptionAction<Method>() {
39854021
@Override
39864022
public Method run() throws Exception {
@@ -3993,6 +4029,7 @@ public Method run() throws Exception {
39934029
};
39944030

39954031
Method values = AccessController.doPrivileged(privilegedAction);
4032+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
39964033
Object rawEnums = values.invoke(this);
39974034
if ((rawEnums == null) || !rawEnums.getClass().isArray()) {
39984035
return null;
@@ -4016,7 +4053,14 @@ public Method run() throws Exception {
40164053
/*[ELSE] JAVA_SPEC_VERSION >= 9 */
40174054
getUnsafe().putOrderedObject(localEnumVars, localEnumConstantsOffset, enums);
40184055
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */
4019-
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | PrivilegedActionException e) {
4056+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
4057+
/*[IF JAVA_SPEC_VERSION >= 24]*/
4058+
| NoSuchMethodException
4059+
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
4060+
| PrivilegedActionException
4061+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
4062+
e
4063+
) {
40204064
enums = null;
40214065
}
40224066
}

jcl/src/java.base/share/classes/java/lang/J9VMInternals.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@
2525
import com.ibm.oti.vm.J9UnmodifiableClass;
2626
import java.lang.ref.SoftReference;
2727
import java.lang.reflect.*;
28+
/*[IF JAVA_SPEC_VERSION < 24]*/
29+
import java.security.AccessControlContext;
2830
import java.security.AccessController;
2931
import java.security.PrivilegedAction;
32+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
33+
import java.security.CodeSource;
34+
import java.security.ProtectionDomain;
3035
import java.util.HashMap;
3136
import java.util.Iterator;
3237
import java.util.Map;
3338
import java.util.Properties;
3439
import java.util.WeakHashMap;
35-
import java.security.AccessControlContext;
36-
import java.security.CodeSource;
37-
import java.security.ProtectionDomain;
3840
import java.io.FileDescriptor;
3941
import java.io.InputStream;
4042
import java.io.OutputStream;
@@ -271,8 +273,11 @@ private static void ensureError(Throwable err) {
271273
private static native Throwable newInstance(Class exceptionClass, Class constructorClass);
272274

273275
private static Throwable cloneThrowable(final Throwable throwable, final HashMap hashMapThrowable) {
276+
/*[IF JAVA_SPEC_VERSION < 24]*/
274277
return (Throwable)AccessController.doPrivileged(new PrivilegedAction() {
278+
@Override
275279
public Object run() {
280+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
276281
Throwable clone;
277282
try {
278283
Class cls = throwable.getClass();
@@ -305,8 +310,10 @@ public Object run() {
305310
clone = new Throwable(Msg.getString("K05c3", e, throwable.toString())); //$NON-NLS-1$
306311
}
307312
return clone;
313+
/*[IF JAVA_SPEC_VERSION < 24]*/
308314
}
309315
});
316+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
310317
}
311318

312319
/**
@@ -612,9 +619,11 @@ private static String[] getClassInfoStrings(final Class<?> clazz, String classPa
612619
ClassLoader classLoader = clazz.getClassLoader();
613620
if (classLoader != null) {
614621
classLoaderStr = classLoader.toString();
622+
/*[IF JAVA_SPEC_VERSION < 24]*/
615623
classPath = AccessController.doPrivileged(new PrivilegedAction<String>() {
616624
@Override
617625
public String run() {
626+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
618627
String path = null;
619628
try {
620629
ProtectionDomain pd = clazz.getProtectionDomain();
@@ -629,9 +638,13 @@ public String run() {
629638
}
630639
} catch (Exception e) {
631640
}
641+
/*[IF JAVA_SPEC_VERSION >= 24]*/
642+
classPath = path;
643+
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
632644
return path;
633645
}
634646
});
647+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
635648
}
636649
}
637650
if (classPath != null) {

jcl/src/java.base/share/classes/java/lang/RuntimePermission.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*[INCLUDE-IF Sidecar16]*/
1+
/*[INCLUDE-IF JAVA_SPEC_VERSION < 24]*/
22
package java.lang;
33

44
/*

0 commit comments

Comments
 (0)