Skip to content

Commit 2430b36

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 64284dd commit 2430b36

31 files changed

+357
-49
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

@@ -62,8 +64,17 @@ public class Msg {
6264

6365
static {
6466
// Attempt to load the messages.
67+
String resourceName = "com/ibm/oti/util/ExternalMessages"; //$NON-NLS-1$
68+
/*[IF JAVA_SPEC_VERSION >= 24]*/
69+
try {
70+
messages = MsgHelp.loadMessages(resourceName);
71+
} catch (java.io.IOException e) {
72+
e.printStackTrace();
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 Sidecar16 & (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 Sidecar18-SE & (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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
import java.lang.reflect.Constructor;
3636
import java.lang.reflect.Executable;
3737
import java.lang.reflect.Method;
38+
/*[IF JAVA_SPEC_VERSION < 24]*/
3839
import java.security.AccessControlContext;
40+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
3941
import java.util.Map;
4042

4143
import com.ibm.oti.reflect.AnnotationParser;

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

Lines changed: 62 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,15 @@ 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 = null;
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>() {
488498
public Field run() {
489499
try {
490500
return AnnotationVars.class.getDeclaredField("annotationType"); //$NON-NLS-1$
@@ -493,6 +503,7 @@ public Field run() {
493503
}
494504
}
495505
});
506+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
496507
localTypeOffset = getUnsafe().objectFieldOffset(field);
497508
AnnotationVars.annotationTypeOffset = localTypeOffset;
498509
}
@@ -509,7 +520,9 @@ public Field run() {
509520
* of a class as described in the class definition of
510521
* java.lang.Class, however Classes representing base
511522
* types can not be found using this method.
523+
/*[IF JAVA_SPEC_VERSION < 24]
512524
* Security rules will be obeyed.
525+
/*[ENDIF] JAVA_SPEC_VERSION < 24
513526
*
514527
* @param className The name of the non-base type class to find
515528
* @param initializeBoolean A boolean indicating whether the class should be
@@ -614,7 +627,9 @@ private static Class<?> forNameHelper(
614627
* It does not invoke the class initializer.
615628
* Note that this method does not check whether the
616629
* requested class is accessible to its caller.
630+
/*[IF JAVA_SPEC_VERSION < 24]
617631
* Security rules will be obeyed.
632+
/*[ENDIF] JAVA_SPEC_VERSION < 24
618633
*
619634
* @param module The name of the module
620635
* @param name The name of the non-base type class to find
@@ -629,14 +644,15 @@ public static Class<?> forName(Module module, String name)
629644
/*[IF JAVA_SPEC_VERSION >= 18]*/
630645
return forNameHelper(module, name, null, false);
631646
/*[ELSE] JAVA_SPEC_VERSION >= 18 */
632-
@SuppressWarnings("removal")
633-
SecurityManager sm = null;
634647
ClassLoader classLoader;
635648
Class<?> c;
636649

637650
if ((null == module) || (null == name)) {
638651
throw new NullPointerException();
639652
}
653+
/*[IF JAVA_SPEC_VERSION < 24]*/
654+
@SuppressWarnings("removal")
655+
SecurityManager sm = null;
640656
if (J9VMInternals.initialized) {
641657
sm = System.getSecurityManager();
642658
}
@@ -651,7 +667,9 @@ public ClassLoader run() {
651667
return module.getClassLoader();
652668
}
653669
});
654-
} else {
670+
} else
671+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
672+
{
655673
classLoader = module.getClassLoader();
656674
}
657675

@@ -694,7 +712,7 @@ private static Class<?> forNameHelper(Module module, String name, Class<?> calle
694712
if ((null == module) || (null == name)) {
695713
throw new NullPointerException();
696714
}
697-
/*[IF JAVA_SPEC_VERSION < 24]*/
715+
/*[IF JAVA_SPEC_VERSION < 24]*/
698716
@SuppressWarnings("removal")
699717
SecurityManager sm = null;
700718
if (J9VMInternals.initialized) {
@@ -714,7 +732,7 @@ public ClassLoader run() {
714732
}
715733
});
716734
} else
717-
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
735+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
718736
{
719737
classLoader = module.getClassLoader();
720738
}
@@ -840,7 +858,9 @@ public ClassLoader getClassLoader() {
840858
/**
841859
* Returns the classloader used to load the receiver's class.
842860
* Returns null if the class was loaded by the bootstrap (system) class loader.
861+
/*[IF JAVA_SPEC_VERSION < 24]
843862
* This skips security checks.
863+
/*[ENDIF] JAVA_SPEC_VERSION < 24
844864
* @return the receiver's class loader or null
845865
* @see java.lang.ClassLoader
846866
*/
@@ -1848,7 +1868,9 @@ private Method throwExceptionOrReturnNull(boolean throwException, String name, C
18481868
* public Method getMethod(String name, Class<?>... parameterTypes)
18491869
* List<Method> getDeclaredPublicMethods(String name, Class<?>... parameterTypes)
18501870
* Method findMethod(boolean publicOnly, String methodName, Class<?>... parameterTypes)
1871+
/*[IF JAVA_SPEC_VERSION < 24]
18511872
* without going thorough security checking
1873+
/*[ENDIF] JAVA_SPEC_VERSION < 24
18521874
*
18531875
* @param throwException boolean
18541876
* true - throw exception in this helper;
@@ -2428,8 +2450,10 @@ public String getName() {
24282450
* Note: In order to conserve space in embedded targets, we allow this
24292451
* method to answer null for classes in the system protection domain
24302452
* (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.
2453+
* permissions (i.e. AllPermission).
2454+
/*[IF JAVA_SPEC_VERSION < 24]
2455+
* This is not changeable via the java.security.Policy.
2456+
/*[ENDIF] JAVA_SPEC_VERSION < 24
24332457
*
24342458
* @return ProtectionDomain
24352459
* the receiver's ProtectionDomain.
@@ -3573,9 +3597,12 @@ private MethodHandle getValueMethod(final Class<? extends Annotation> containedT
35733597
MethodHandle valueMethod = localAnnotationVars.valueMethod;
35743598
if (valueMethod == null) {
35753599
final MethodType methodType = MethodType.methodType(Array.newInstance(containedType, 0).getClass());
3600+
/*[IF JAVA_SPEC_VERSION < 24]*/
35763601
valueMethod = AccessController.doPrivileged(new PrivilegedAction<MethodHandle>() {
35773602
@Override
35783603
public MethodHandle run() {
3604+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
3605+
MethodHandle handle = null;
35793606
try {
35803607
MethodHandles.Lookup localImplLookup = implLookup;
35813608
if (localImplLookup == null) {
@@ -3592,7 +3619,7 @@ public MethodHandle run() {
35923619
getUnsafe().putOrderedObject(Class.class, implLookupOffset, localImplLookup);
35933620
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */
35943621
}
3595-
MethodHandle handle = localImplLookup.findVirtual(Class.this, "value", methodType); //$NON-NLS-1$
3622+
handle = localImplLookup.findVirtual(Class.this, "value", methodType); //$NON-NLS-1$
35963623
if (AnnotationVars.valueMethodOffset == -1) {
35973624
Field valueMethodField = AnnotationVars.class.getDeclaredField("valueMethod"); //$NON-NLS-1$
35983625
AnnotationVars.valueMethodOffset = getUnsafe().objectFieldOffset(valueMethodField);
@@ -3604,14 +3631,18 @@ public MethodHandle run() {
36043631
/*[ELSE] JAVA_SPEC_VERSION >= 9 */
36053632
getUnsafe().putOrderedObject(localAnnotationVars, AnnotationVars.valueMethodOffset, handle);
36063633
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */
3607-
return handle;
36083634
} catch (NoSuchMethodException e) {
3609-
return null;
3635+
handle = null;
36103636
} catch (IllegalAccessException | NoSuchFieldException e) {
36113637
throw newInternalError(e);
36123638
}
3639+
/*[IF JAVA_SPEC_VERSION >= 24]*/
3640+
valueMethod = handle;
3641+
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
3642+
return handle;
36133643
}
36143644
});
3645+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
36153646
}
36163647
return valueMethod;
36173648
}
@@ -3981,6 +4012,10 @@ T[] getEnumConstantsShared() {
39814012
T[] enums = localEnumVars.cachedEnumConstants;
39824013
if (null == enums && isEnum()) {
39834014
try {
4015+
/*[IF JAVA_SPEC_VERSION >= 24]*/
4016+
Method values = getMethod("values"); //$NON-NLS-1$
4017+
values.setAccessible(true);
4018+
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
39844019
final PrivilegedExceptionAction<Method> privilegedAction = new PrivilegedExceptionAction<Method>() {
39854020
@Override
39864021
public Method run() throws Exception {
@@ -3993,6 +4028,7 @@ public Method run() throws Exception {
39934028
};
39944029

39954030
Method values = AccessController.doPrivileged(privilegedAction);
4031+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
39964032
Object rawEnums = values.invoke(this);
39974033
if ((rawEnums == null) || !rawEnums.getClass().isArray()) {
39984034
return null;
@@ -4016,7 +4052,14 @@ public Method run() throws Exception {
40164052
/*[ELSE] JAVA_SPEC_VERSION >= 9 */
40174053
getUnsafe().putOrderedObject(localEnumVars, localEnumConstantsOffset, enums);
40184054
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */
4019-
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | PrivilegedActionException e) {
4055+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
4056+
/*[IF JAVA_SPEC_VERSION >= 24]*/
4057+
| NoSuchMethodException
4058+
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
4059+
| PrivilegedActionException
4060+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
4061+
e
4062+
) {
40204063
enums = null;
40214064
}
40224065
}

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

Lines changed: 15 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,10 @@ 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() {
275278
public Object run() {
279+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
276280
Throwable clone;
277281
try {
278282
Class cls = throwable.getClass();
@@ -305,8 +309,10 @@ public Object run() {
305309
clone = new Throwable(Msg.getString("K05c3", e, throwable.toString())); //$NON-NLS-1$
306310
}
307311
return clone;
312+
/*[IF JAVA_SPEC_VERSION < 24]*/
308313
}
309314
});
315+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
310316
}
311317

312318
/**
@@ -612,9 +618,11 @@ private static String[] getClassInfoStrings(final Class<?> clazz, String classPa
612618
ClassLoader classLoader = clazz.getClassLoader();
613619
if (classLoader != null) {
614620
classLoaderStr = classLoader.toString();
621+
/*[IF JAVA_SPEC_VERSION < 24]*/
615622
classPath = AccessController.doPrivileged(new PrivilegedAction<String>() {
616623
@Override
617624
public String run() {
625+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
618626
String path = null;
619627
try {
620628
ProtectionDomain pd = clazz.getProtectionDomain();
@@ -629,9 +637,13 @@ public String run() {
629637
}
630638
} catch (Exception e) {
631639
}
640+
/*[IF JAVA_SPEC_VERSION >= 24]*/
641+
classPath = path;
642+
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
632643
return path;
633644
}
634645
});
646+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
635647
}
636648
}
637649
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 Sidecar16 & (JAVA_SPEC_VERSION < 24)]*/
22
package java.lang;
33

44
/*

0 commit comments

Comments
 (0)