8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass

Reviewed-by: alanb, egahlin
This commit is contained in:
Mandy Chung 2018-10-04 08:45:21 -07:00
parent 4af2374271
commit 73a6313038
9 changed files with 56 additions and 41 deletions

View file

@ -37,7 +37,6 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -578,23 +577,17 @@ abstract class ClassSpecializer<T,K,S extends ClassSpecializer<T,K,S>.SpeciesDat
InvokerBytecodeGenerator.maybeDump(classBCName(className), classFile);
Class<?> speciesCode;
ClassLoader cl = topClass().getClassLoader();
ProtectionDomain pd = null;
if (cl != null) {
pd = AccessController.doPrivileged(
new PrivilegedAction<>() {
@Override
public ProtectionDomain run() {
return topClass().getProtectionDomain();
}
});
}
try {
speciesCode = UNSAFE.defineClass(className, classFile, 0, classFile.length, cl, pd);
} catch (Exception ex) {
throw newInternalError(ex);
}
MethodHandles.Lookup lookup = IMPL_LOOKUP.in(topClass());
speciesCode = AccessController.doPrivileged(new PrivilegedAction<>() {
@Override
public Class<?> run() {
try {
return lookup.defineClass(classFile);
} catch (Exception ex) {
throw newInternalError(ex);
}
}
});
return speciesCode.asSubclass(topClass());
}

View file

@ -39,12 +39,11 @@ import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jdk.internal.loader.BootLoader;
import jdk.internal.misc.JavaLangAccess;
import jdk.internal.misc.SharedSecrets;
import jdk.internal.module.Modules;
import jdk.internal.misc.Unsafe;
import jdk.internal.misc.VM;
import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.Reflection;
@ -468,7 +467,7 @@ public class Proxy implements java.io.Serializable {
* in which the proxy class will be defined.
*/
private static final class ProxyBuilder {
private static final Unsafe UNSAFE = Unsafe.getUnsafe();
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
// prefix for all proxy class names
private static final String proxyClassNamePrefix = "$Proxy";
@ -535,9 +534,8 @@ public class Proxy implements java.io.Serializable {
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces.toArray(EMPTY_CLASS_ARRAY), accessFlags);
try {
Class<?> pc = UNSAFE.defineClass(proxyName, proxyClassFile,
0, proxyClassFile.length,
loader, null);
Class<?> pc = JLA.defineClass(loader, proxyName, proxyClassFile,
null, "__dynamic_proxy__");
reverseProxyCache.sub(pc).putIfAbsent(loader, Boolean.TRUE);
return pc;
} catch (ClassFormatError e) {