Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -527,20 +527,11 @@ public Spliterator<E> spliterator() {

/** Initializes map field; for use in clone. */
private void setMap(ConcurrentNavigableMap<E,Object> map) {
@SuppressWarnings("removal")
Field mapField = java.security.AccessController.doPrivileged(
(java.security.PrivilegedAction<Field>) () -> {
try {
Field f = ConcurrentSkipListSet.class
.getDeclaredField("m");
f.setAccessible(true);
return f;
} catch (ReflectiveOperationException e) {
throw new Error(e);
}});
try {
Field mapField = ConcurrentSkipListSet.class.getDeclaredField("m");
mapField.setAccessible(true);
mapField.set(this, map);
} catch (IllegalAccessException e) {
} catch (IllegalAccessException | NoSuchFieldException e) {
throw new Error(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2096,20 +2096,11 @@ public List<E> reversed() {

/** Initializes the lock; for use when deserializing or cloning. */
private void resetLock() {
@SuppressWarnings("removal")
Field lockField = java.security.AccessController.doPrivileged(
(java.security.PrivilegedAction<Field>) () -> {
try {
Field f = CopyOnWriteArrayList.class
.getDeclaredField("lock");
f.setAccessible(true);
return f;
} catch (ReflectiveOperationException e) {
throw new Error(e);
}});
try {
Field lockField = CopyOnWriteArrayList.class.getDeclaredField("lock");
lockField.setAccessible(true);
lockField.set(this, new Object());
} catch (IllegalAccessException e) {
} catch (IllegalAccessException | NoSuchFieldException e) {
throw new Error(e);
}
}
Expand Down
98 changes: 17 additions & 81 deletions src/java.base/share/classes/java/util/concurrent/Executors.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@

import static java.lang.ref.Reference.reachabilityFence;
import java.lang.ref.Cleaner.Cleanable;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import jdk.internal.ref.CleanerFactory;
import sun.security.util.SecurityConstants;

/**
* Factory and utility methods for {@link Executor}, {@link
Expand Down Expand Up @@ -559,27 +555,13 @@ public String toString() {
*/
private static final class PrivilegedCallable<T> implements Callable<T> {
final Callable<T> task;
@SuppressWarnings("removal")
final AccessControlContext acc;

@SuppressWarnings("removal")
PrivilegedCallable(Callable<T> task) {
this.task = task;
this.acc = AccessController.getContext();
}

@SuppressWarnings("removal")
public T call() throws Exception {
try {
return AccessController.doPrivileged(
new PrivilegedExceptionAction<T>() {
public T run() throws Exception {
return task.call();
}
}, acc);
} catch (PrivilegedActionException e) {
throw e.getException();
}
return task.call();
}

public String toString() {
Expand All @@ -595,49 +577,26 @@ private static final class PrivilegedCallableUsingCurrentClassLoader<T>
implements Callable<T> {
final Callable<T> task;
@SuppressWarnings("removal")
final AccessControlContext acc;
final ClassLoader ccl;

@SuppressWarnings("removal")
PrivilegedCallableUsingCurrentClassLoader(Callable<T> task) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// Calls to getContextClassLoader from this class
// never trigger a security check, but we check
// whether our callers have this permission anyways.
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);

// Whether setContextClassLoader turns out to be necessary
// or not, we fail fast if permission is not available.
sm.checkPermission(new RuntimePermission("setContextClassLoader"));
}
this.task = task;
this.acc = AccessController.getContext();
this.ccl = Thread.currentThread().getContextClassLoader();
}

@SuppressWarnings("removal")
public T call() throws Exception {
try {
return AccessController.doPrivileged(
new PrivilegedExceptionAction<T>() {
public T run() throws Exception {
Thread t = Thread.currentThread();
ClassLoader cl = t.getContextClassLoader();
if (ccl == cl) {
return task.call();
} else {
t.setContextClassLoader(ccl);
try {
return task.call();
} finally {
t.setContextClassLoader(cl);
}
}
}
}, acc);
} catch (PrivilegedActionException e) {
throw e.getException();
Thread t = Thread.currentThread();
ClassLoader cl = t.getContextClassLoader();
if (ccl == cl) {
return task.call();
} else {
t.setContextClassLoader(ccl);
try {
return task.call();
} finally {
t.setContextClassLoader(cl);
}
}
}

Expand All @@ -656,10 +615,7 @@ private static class DefaultThreadFactory implements ThreadFactory {
private final String namePrefix;

DefaultThreadFactory() {
@SuppressWarnings("removal")
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
group = Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
Expand All @@ -678,41 +634,23 @@ public Thread newThread(Runnable r) {
}

/**
* Thread factory capturing access control context and class loader.
* Thread factory capturing the current class loader.
*/
private static class PrivilegedThreadFactory extends DefaultThreadFactory {
@SuppressWarnings("removal")
final AccessControlContext acc;
final ClassLoader ccl;

@SuppressWarnings("removal")
PrivilegedThreadFactory() {
super();
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// Calls to getContextClassLoader from this class
// never trigger a security check, but we check
// whether our callers have this permission anyways.
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);

// Fail fast
sm.checkPermission(new RuntimePermission("setContextClassLoader"));
}
this.acc = AccessController.getContext();
this.ccl = Thread.currentThread().getContextClassLoader();
}

public Thread newThread(final Runnable r) {
return super.newThread(new Runnable() {
@SuppressWarnings("removal")
public void run() {
AccessController.doPrivileged(new PrivilegedAction<>() {
public Void run() {
Thread.currentThread().setContextClassLoader(ccl);
r.run();
return null;
}
}, acc);
Thread.currentThread().setContextClassLoader(ccl);
r.run();
}
});
}
Expand Down Expand Up @@ -811,9 +749,7 @@ private static class AutoShutdownDelegatedExecutorService
super(executor);
Runnable action = () -> {
if (!executor.isShutdown()) {
PrivilegedAction<Void> pa = () -> { executor.shutdown(); return null; };
@SuppressWarnings("removal")
var ignore = AccessController.doPrivileged(pa);
executor.shutdown();
}
};
cleanable = CleanerFactory.cleaner().register(this, action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.time.Duration;
import java.time.Instant;
import java.util.Objects;
Expand Down Expand Up @@ -688,7 +686,7 @@ public StructuredTaskScope<T> joinUntil(Instant deadline)
/**
* Interrupt all unfinished threads.
*/
private void implInterruptAll() {
private void interruptAll() {
flock.threads()
.filter(t -> t != Thread.currentThread())
.forEach(t -> {
Expand All @@ -698,19 +696,6 @@ private void implInterruptAll() {
});
}

@SuppressWarnings("removal")
private void interruptAll() {
if (System.getSecurityManager() == null) {
implInterruptAll();
} else {
PrivilegedAction<Void> pa = () -> {
implInterruptAll();
return null;
};
AccessController.doPrivileged(pa);
}
}

/**
* Shutdown the task scope if not already shutdown. Return true if this method
* shutdowns the task scope, false if already shutdown.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
package java.util.concurrent;

import java.io.ObjectStreamField;
import java.security.AccessControlContext;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.security.Permission;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
Expand All @@ -48,7 +47,6 @@
*/
class ThreadPerTaskExecutor extends ThreadContainer implements ExecutorService {
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
private static final Permission MODIFY_THREAD = new RuntimePermission("modifyThread");
private static final VarHandle STATE = MhUtil.findVarHandle(
MethodHandles.lookup(), "state", int.class);

Expand Down Expand Up @@ -80,18 +78,6 @@ static ThreadPerTaskExecutor create(ThreadFactory factory) {
return executor;
}

/**
* Throws SecurityException if there is a security manager set and it denies
* RuntimePermission("modifyThread").
*/
@SuppressWarnings("removal")
private void checkPermission() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(MODIFY_THREAD);
}
}

/**
* Throws RejectedExecutionException if the executor has been shutdown.
*/
Expand Down Expand Up @@ -143,14 +129,12 @@ public long threadCount() {

@Override
public void shutdown() {
checkPermission();
if (!isShutdown())
tryShutdownAndTerminate(false);
}

@Override
public List<Runnable> shutdownNow() {
checkPermission();
if (!isTerminated())
tryShutdownAndTerminate(true);
return List.of();
Expand Down Expand Up @@ -202,7 +186,6 @@ private void awaitTermination() {

@Override
public void close() {
checkPermission();
awaitTermination();
}

Expand Down
Loading