CleanerJava9 should be able to do its job even with a SecurityManager installed. (#8204)
Motivation: CleanerJava9 currently fails whever a SecurityManager is installed. We should make use of AccessController.doPrivileged(...) so the user can give it the correct rights. Modifications: Use doPrivileged(...) when needed. Result: Fixes https://github.com/netty/netty/issues/8201.
This commit is contained in:
parent
338ef96931
commit
8679c5ef43
@ -21,6 +21,8 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
|
|||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.security.AccessController;
|
||||||
|
import java.security.PrivilegedAction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provide a way to clean a ByteBuffer on Java9+.
|
* Provide a way to clean a ByteBuffer on Java9+.
|
||||||
@ -34,20 +36,26 @@ final class CleanerJava9 implements Cleaner {
|
|||||||
final Method method;
|
final Method method;
|
||||||
final Throwable error;
|
final Throwable error;
|
||||||
if (PlatformDependent0.hasUnsafe()) {
|
if (PlatformDependent0.hasUnsafe()) {
|
||||||
ByteBuffer buffer = ByteBuffer.allocateDirect(1);
|
final ByteBuffer buffer = ByteBuffer.allocateDirect(1);
|
||||||
Object maybeInvokeMethod;
|
Object maybeInvokeMethod = AccessController.doPrivileged(new PrivilegedAction<Object>() {
|
||||||
try {
|
@Override
|
||||||
// See https://bugs.openjdk.java.net/browse/JDK-8171377
|
public Object run() {
|
||||||
Method m = PlatformDependent0.UNSAFE.getClass().getDeclaredMethod("invokeCleaner", ByteBuffer.class);
|
try {
|
||||||
m.invoke(PlatformDependent0.UNSAFE, buffer);
|
// See https://bugs.openjdk.java.net/browse/JDK-8171377
|
||||||
maybeInvokeMethod = m;
|
Method m = PlatformDependent0.UNSAFE.getClass().getDeclaredMethod(
|
||||||
} catch (NoSuchMethodException e) {
|
"invokeCleaner", ByteBuffer.class);
|
||||||
maybeInvokeMethod = e;
|
m.invoke(PlatformDependent0.UNSAFE, buffer);
|
||||||
} catch (InvocationTargetException e) {
|
return m;
|
||||||
maybeInvokeMethod = e;
|
} catch (NoSuchMethodException e) {
|
||||||
} catch (IllegalAccessException e) {
|
return e;
|
||||||
maybeInvokeMethod = e;
|
} catch (InvocationTargetException e) {
|
||||||
}
|
return e;
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (maybeInvokeMethod instanceof Throwable) {
|
if (maybeInvokeMethod instanceof Throwable) {
|
||||||
method = null;
|
method = null;
|
||||||
error = (Throwable) maybeInvokeMethod;
|
error = (Throwable) maybeInvokeMethod;
|
||||||
@ -73,10 +81,35 @@ final class CleanerJava9 implements Cleaner {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void freeDirectBuffer(ByteBuffer buffer) {
|
public void freeDirectBuffer(ByteBuffer buffer) {
|
||||||
try {
|
// Try to minimize overhead when there is no SecurityManager present.
|
||||||
INVOKE_CLEANER.invoke(PlatformDependent0.UNSAFE, buffer);
|
// See https://bugs.openjdk.java.net/browse/JDK-8191053.
|
||||||
} catch (Throwable cause) {
|
if (System.getSecurityManager() == null) {
|
||||||
PlatformDependent0.throwException(cause);
|
try {
|
||||||
|
INVOKE_CLEANER.invoke(PlatformDependent0.UNSAFE, buffer);
|
||||||
|
} catch (Throwable cause) {
|
||||||
|
PlatformDependent0.throwException(cause);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
freeDirectBufferPrivileged(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void freeDirectBufferPrivileged(final ByteBuffer buffer) {
|
||||||
|
Exception error = AccessController.doPrivileged(new PrivilegedAction<Exception>() {
|
||||||
|
@Override
|
||||||
|
public Exception run() {
|
||||||
|
try {
|
||||||
|
INVOKE_CLEANER.invoke(PlatformDependent0.UNSAFE, buffer);
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
return e;
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (error != null) {
|
||||||
|
PlatformDependent0.throwException(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user