Skip to content

Commit

Permalink
Merge pull request #21039 from theresa-m/securitymanager_3
Browse files Browse the repository at this point in the history
JDK24 remove System.getSecurityManager part 3
  • Loading branch information
keithc-ca authored Jan 31, 2025
2 parents e88bbde + f5a3419 commit de14fb4
Show file tree
Hide file tree
Showing 20 changed files with 162 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
package com.ibm.sharedclasses.spi;

import java.net.URL;
/*[IF JAVA_SPEC_VERSION < 24]*/
import java.security.BasicPermission;
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
import java.util.function.IntConsumer;

/**
Expand Down Expand Up @@ -216,6 +218,7 @@ public interface SharedClassProvider {
*/
public long getFreeSpace();

/*[IF JAVA_SPEC_VERSION < 24]*/
/**
* <p>Constructs a new instance of SharedClassPermission which is a sub-class of BasicPermission.</p>
*
Expand All @@ -227,4 +230,5 @@ public interface SharedClassProvider {
* A new instance of SharedClassPermission which is a sub-class of BasicPermission, or null if shared classes are not enabled.
*/
public BasicPermission createPermission(String classLoaderClassName, String actions);
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
/*[IF JAVA_SPEC_VERSION >= 9]*/
import com.ibm.sharedclasses.spi.SharedClassProvider;
import java.net.URL;
/*[IF JAVA_SPEC_VERSION < 24]*/
import java.security.BasicPermission;
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
import java.util.ServiceLoader;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.IntConsumer;
Expand Down Expand Up @@ -106,10 +108,12 @@ private static final class DisabledSharedClassProvider implements SharedClassPro
super();
}

/*[IF JAVA_SPEC_VERSION < 24]*/
@Override
public BasicPermission createPermission(String classLoaderClassName, String actions) {
return null;
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

@Override
public byte[] findSharedClassURL(URL path, String className) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
package com.ibm.oti.shared;

import java.lang.ref.WeakReference;
/*[IF JAVA_SPEC_VERSION < 24]*/
import java.security.AccessControlException;
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

import com.ibm.oti.util.Msg;

Expand All @@ -36,11 +38,12 @@
public abstract class SharedAbstractHelper implements SharedHelper {
private Boolean verbose;
private WeakReference<ClassLoader> loaderRef;
/*[IF JAVA_SPEC_VERSION < 24]*/
private SharedClassPermission readPerm;
private SharedClassPermission writePerm;

boolean canFind;
boolean canStore;
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
int id;

/**
Expand All @@ -50,14 +53,23 @@ public SharedAbstractHelper() {
super();
}

/*[IF JAVA_SPEC_VERSION >= 24]*/
void initialize(ClassLoader loader, int loaderId) {
this.id = loaderId;
this.loaderRef = new WeakReference<>(loader);
/*[MSG "K0591", "Created {0} with id {1}"]*/
printVerboseInfo(Msg.getString("K0591", getHelperType(), Integer.valueOf(id))); //$NON-NLS-1$
}
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
void initialize(ClassLoader loader, int loaderId, boolean canLoaderFind, boolean canLoaderStore) {
this.id = loaderId;
this.canFind = canLoaderFind;
this.canStore = canLoaderStore;
loaderRef = new WeakReference<>(loader);
this.loaderRef = new WeakReference<>(loader);
/*[MSG "K0591", "Created {0} with id {1}"]*/
printVerboseInfo(Msg.getString("K0591", getHelperType(), Integer.valueOf(id))); //$NON-NLS-1$
}
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */

/**
* Utility function. Returns the ClassLoader that owns this SharedHelper.
Expand All @@ -73,6 +85,7 @@ public ClassLoader getClassLoader() {

private native boolean getIsVerboseImpl();

/*[IF JAVA_SPEC_VERSION < 24]*/
/* Do not cache the permission objects, else classloader references will prevent class GC */
@SuppressWarnings("removal")
private static boolean checkPermission(SecurityManager sm, ClassLoader loader, String type) {
Expand Down Expand Up @@ -103,6 +116,7 @@ static boolean checkWritePermission(ClassLoader loader) {
}
return true; // no security manager means the check is successful
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

private boolean isVerbose() {
if (verbose == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
*/
package com.ibm.oti.shared;

/*[IF JAVA_SPEC_VERSION < 24]*/
import java.security.AccessControlException;
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
import java.util.concurrent.atomic.AtomicInteger;

/**
Expand All @@ -36,6 +38,7 @@ public abstract class SharedAbstractHelperFactory {
*/
private static final AtomicInteger idCount = new AtomicInteger(1);

/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
static boolean checkPermission(ClassLoader loader, String type) {
boolean result = true;
Expand All @@ -57,6 +60,7 @@ static boolean canFind(ClassLoader loader) {
static boolean canStore(ClassLoader loader) {
return checkPermission(loader, "write"); //$NON-NLS-1$
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

static int getNewID() {
return idCount.getAndIncrement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,11 @@ public Integer run() {
* @param filter the filter to use when finding and storing classes
*/
@Override
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
public synchronized void setSharingFilter(SharedClassFilter filter) {
/*[IF JAVA_SPEC_VERSION < 24]*/
if (System.getSecurityManager() != null) {
ClassLoader loader = getClassLoader();
if (loader == null) {
Expand All @@ -243,6 +246,7 @@ public synchronized void setSharingFilter(SharedClassFilter filter) {
return;
}
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
this.sharedClassFilter = filter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ public SharedClassTokenHelper getTokenHelper(ClassLoader loader)
throw new HelperAlreadyDefinedException(Msg.getString("K059d")); //$NON-NLS-1$
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */
} else {
/*[IF JAVA_SPEC_VERSION >= 24]*/
SharedClassTokenHelper result = new SharedClassTokenHelperImpl(loader, getNewID());
SharedClassFilter filter = getGlobalSharingFilter();
if (filter != null) {
result.setSharingFilter(filter);
}
helpers.put(loader, result);
return result;
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
boolean canFind = canFind(loader);
boolean canStore = canStore(loader);

Expand All @@ -161,9 +170,10 @@ public SharedClassTokenHelper getTokenHelper(ClassLoader loader)
helpers.put(loader, result);
return result;
}
return null;
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
}
}
return null;
}

@Override
Expand Down Expand Up @@ -207,6 +217,15 @@ public SharedClassURLHelper getURLHelper(ClassLoader loader)
throw new HelperAlreadyDefinedException(Msg.getString("K059d")); //$NON-NLS-1$
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */
} else {
/*[IF JAVA_SPEC_VERSION >= 24]*/
SharedClassURLHelper result = new SharedClassURLHelperImpl(loader, getNewID());
SharedClassFilter filter = getGlobalSharingFilter();
if (filter != null) {
result.setSharingFilter(filter);
}
helpers.put(loader, result);
return result;
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
boolean canFind = canFind(loader);
boolean canStore = canStore(loader);

Expand All @@ -219,9 +238,10 @@ public SharedClassURLHelper getURLHelper(ClassLoader loader)
helpers.put(loader, result);
return result;
}
return null;
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
}
}
return null;
}

@Override
Expand Down Expand Up @@ -277,6 +297,15 @@ public SharedClassURLClasspathHelper getURLClasspathHelper(
}
/*[ENDIF] JAVA_SPEC_VERSION == 8 */
} else {
/*[IF JAVA_SPEC_VERSION >= 24]*/
result = new SharedClassURLClasspathHelperImpl(loader, classpath, getNewID());
SharedClassFilter filter = getGlobalSharingFilter();
if (filter != null) {
result.setSharingFilter(filter);
}
helpers.put(loader, result);
return result;
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
boolean canFind = canFind(loader);
boolean canStore = canStore(loader);

Expand All @@ -289,9 +318,10 @@ public SharedClassURLClasspathHelper getURLClasspathHelper(
helpers.put(loader, result);
return result;
}
return null;
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
}
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*[INCLUDE-IF SharedClasses]*/
/*[INCLUDE-IF SharedClasses & (JAVA_SPEC_VERSION < 24)]*/
/*
* Copyright IBM Corp. and others 1998
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*[INCLUDE-IF SharedClasses]*/
/*[INCLUDE-IF SharedClasses & (JAVA_SPEC_VERSION < 24)]*/
/*
* Copyright IBM Corp. and others 1998
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@
* <p>If a ClassLoader stores multiple versions of the same class by using the same token, only the most recent will be returned by findSharedClass. </p>
* <h2>Security</h2>
* <p>A SharedClassHelper will only allow classes to be stored in the cache which were defined by the ClassLoader that owns the SharedClassHelper.</p>
/*[IF JAVA_SPEC_VERSION < 24]
* <p>If a SecurityManager is installed, SharedClassPermissions must be used to permit read/write access to the shared class cache.
* Permissions are granted by ClassLoader classname in the java.policy file and are fixed when the SharedClassHelper is created.</p>
/*[ENDIF] JAVA_SPEC_VERSION < 24
* <p>Note also that if the createClassLoader RuntimePermission is not granted, ClassLoaders cannot be created,
* which in turn means that SharedClassHelpers cannot be created.</p>
* <h2>Compatibility with other SharedClassHelpers</h2>
* <p>Classes stored using the SharedClassTokenHelper cannot be retrieved using any other type of helper, and vice versa. </p>
*
* @see SharedClassHelper
* @see SharedClassHelperFactory
/*[IF JAVA_SPEC_VERSION < 24]
* @see SharedClassPermission
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public interface SharedClassTokenHelper extends SharedClassHelper {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@
*/
final class SharedClassTokenHelperImpl extends SharedClassAbstractHelper implements SharedClassTokenHelper {
/* Not public - should only be created by factory */
/*[IF JAVA_SPEC_VERSION >= 24]*/
SharedClassTokenHelperImpl(ClassLoader loader, int id) {
initialize(loader, id);
initializeShareableClassloader(loader);
}
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
SharedClassTokenHelperImpl(ClassLoader loader, int id, boolean canFind, boolean canStore) {
initialize(loader, id, canFind, canStore);
initializeShareableClassloader(loader);
}
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */

private native boolean findSharedClassImpl2(int loaderId, String className, ClassLoader loader, String token,
boolean doFind, boolean doStore, byte[] romClassCookie);
Expand All @@ -51,9 +58,11 @@ public byte[] findSharedClass(String token, String className) {
printVerboseInfo(Msg.getString("K059f")); //$NON-NLS-1$
return null;
}
/*[IF JAVA_SPEC_VERSION < 24]*/
if (!canFind) {
return null;
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
if (token==null) {
/*[MSG "K05a0", "Cannot call findSharedClass with null token. Returning null."]*/
printVerboseError(Msg.getString("K05a0")); //$NON-NLS-1$
Expand Down Expand Up @@ -90,9 +99,11 @@ public byte[] findSharedClass(String token, String className) {

@Override
public boolean storeSharedClass(String token, Class<?> clazz) {
/*[IF JAVA_SPEC_VERSION < 24]*/
if (!canStore) {
return false;
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
if (token==null) {
/*[MSG "K05a2", "Cannot call storeSharedClass with null token. Returning false."]*/
printVerboseError(Msg.getString("K05a2")); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@
* <p>If findSharedClass returns null, then load the class from disk, cache the metadata from the entry anyway, define the class, and store it.</p>
* <h2>Security</h2>
* <p>A SharedClassHelper will only allow classes that were defined by the ClassLoader that owns the SharedClassHelper to be stored in the cache.</p>
/*[IF JAVA_SPEC_VERSION < 24]
* <p>If a SecurityManager is installed, SharedClassPermissions must be used to permit read/write access to the shared class cache.
* Permissions are granted by ClassLoader classname in the java.policy file and are fixed when the SharedClassHelper is created.</p>
/*[ENDIF] JAVA_SPEC_VERSION < 24
* <p>Note also that if the createClassLoader RuntimePermission is not granted, ClassLoaders cannot be created,
* which in turn means that SharedClassHelpers cannot be created.</p>
* <h2>Efficient use of the SharedClassURLClasspathHelper</h2>
Expand All @@ -131,7 +133,9 @@
*
* @see SharedClassURLClasspathHelper
* @see SharedClassHelperFactory
/*[IF JAVA_SPEC_VERSION < 24]
* @see SharedClassPermission
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public interface SharedClassURLClasspathHelper extends SharedClassHelper {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ final class SharedClassURLClasspathHelperImpl extends SharedClassAbstractHelper
}

/* Not public - should only be created by factory */
/*[IF JAVA_SPEC_VERSION >= 24]*/
SharedClassURLClasspathHelperImpl(ClassLoader loader, URL[] classpath, int id) {
this.origurls = classpath;
this.urls = new URL[classpath.length];
this.urlCount = classpath.length;
this.validated = new boolean[classpath.length];
this.confirmedCount = 0;
this.invalidURLExists = false;
urlcpReadWriteLock = new ReentrantReadWriteLock();
initialize(loader, id);
initializeShareableClassloader(loader);
initializeURLs();
if (!invalidURLExists) {
notifyClasspathChange3(id, loader, this.urls, 0, this.urlCount, true);
}
}
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
SharedClassURLClasspathHelperImpl(ClassLoader loader, URL[] classpath, int id, boolean canFind, boolean canStore) {
this.origurls = classpath;
this.urls = new URL[classpath.length];
Expand All @@ -64,6 +81,7 @@ final class SharedClassURLClasspathHelperImpl extends SharedClassAbstractHelper
notifyClasspathChange3(id, loader, this.urls, 0, this.urlCount, true);
}
}
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */

private void initializeURLs() {
for (int i=0; i<urlCount; i++) {
Expand Down Expand Up @@ -102,9 +120,11 @@ public byte[] findSharedClass(String partition, String className, IndexHolder in
printVerboseInfo(Msg.getString("K059f")); //$NON-NLS-1$
return null;
}
/*[IF JAVA_SPEC_VERSION < 24]*/
if (!canFind) {
return null;
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
if (className==null) {
/*[MSG "K05a1", "Cannot call findSharedClass with null class name. Returning null."]*/
printVerboseError(Msg.getString("K05a1")); //$NON-NLS-1$
Expand Down Expand Up @@ -168,9 +188,11 @@ public boolean storeSharedClass(Class<?> clazz, int foundAtIndex) {

@Override
public boolean storeSharedClass(String partition, Class<?> clazz, int foundAtIndex) {
/*[IF JAVA_SPEC_VERSION < 24]*/
if (!canStore) {
return false;
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
if (clazz==null) {
/*[MSG "K05a3", "Cannot call storeSharedClass with null Class. Returning false."]*/
printVerboseError(Msg.getString("K05a3")); //$NON-NLS-1$
Expand Down
Loading

0 comments on commit de14fb4

Please sign in to comment.