8307944: ClassFileDumper should only load java.nio.file.Path if enabled

Reviewed-by: rriggs
This commit is contained in:
Mandy Chung 2023-05-30 21:01:12 +00:00
parent 7891de331a
commit de7fd1c306
6 changed files with 109 additions and 92 deletions

View file

@ -35,7 +35,6 @@ import sun.security.action.GetBooleanAction;
import java.io.Serializable;
import java.lang.constant.ConstantDescs;
import java.lang.reflect.Modifier;
import java.nio.file.Path;
import java.util.LinkedHashSet;
import java.util.Set;
@ -93,7 +92,7 @@ import static jdk.internal.org.objectweb.asm.Opcodes.*;
// -Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles
// or -Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles=true
final String dumpProxyClassesKey = "jdk.invoke.LambdaMetafactory.dumpProxyClassFiles";
lambdaProxyClassFileDumper = ClassFileDumper.getInstance(dumpProxyClassesKey, Path.of("DUMP_LAMBDA_PROXY_CLASS_FILES"));
lambdaProxyClassFileDumper = ClassFileDumper.getInstance(dumpProxyClassesKey, "DUMP_LAMBDA_PROXY_CLASS_FILES");
final String disableEagerInitializationKey = "jdk.internal.lambda.disableEagerInitialization";
disableEagerInitialization = GetBooleanAction.privilegedGetProperty(disableEagerInitializationKey);

View file

@ -31,7 +31,6 @@ import jdk.internal.util.ClassFileDumper;
import sun.security.action.GetPropertyAction;
import java.lang.reflect.ClassFileFormatVersion;
import java.nio.file.Path;
import java.util.Properties;
import static java.lang.invoke.LambdaForm.basicTypeSignature;
@ -98,7 +97,7 @@ class MethodHandleStatics {
props.getProperty("java.lang.invoke.MethodHandleImpl.MAX_ARITY", "255"));
DUMP_CLASS_FILES = ClassFileDumper.getInstance("jdk.invoke.MethodHandle.dumpMethodHandleInternals",
Path.of("DUMP_METHOD_HANDLE_INTERNALS"));
"DUMP_METHOD_HANDLE_INTERNALS");
if (CUSTOMIZE_THRESHOLD < -1 || CUSTOMIZE_THRESHOLD > 127) {
throw newInternalError("CUSTOMIZE_THRESHOLD should be in [-1...127] range");

View file

@ -56,7 +56,6 @@ import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.ByteOrder;
import java.nio.file.Path;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Arrays;
@ -2252,7 +2251,7 @@ public class MethodHandles {
}
private static final ClassFileDumper DEFAULT_DUMPER = ClassFileDumper.getInstance(
"jdk.invoke.MethodHandle.dumpClassFiles", Path.of("DUMP_CLASS_FILES"));
"jdk.invoke.MethodHandle.dumpClassFiles", "DUMP_CLASS_FILES");
static class ClassFile {
final String name; // internal name

View file

@ -27,16 +27,13 @@ package jdk.internal.util;
import jdk.internal.misc.VM;
import sun.security.action.GetPropertyAction;
import java.io.FilePermission;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HexFormat;
import java.util.Objects;
import java.util.PropertyPermission;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
@ -52,36 +49,6 @@ public final class ClassFileDumper {
private static final ConcurrentHashMap<String, ClassFileDumper> DUMPER_MAP
= new ConcurrentHashMap<>();
/**
* Returns a ClassFileDumper instance for the given key. To enable
* dumping of the generated classes, set the system property via
* -D<key>=<path>.
*
* The system property is read only once when it is the first time
* the dumper instance for the given key is created.
*
* If not enabled, this method returns ClassFileDumper with null
* dump path.
*/
public static ClassFileDumper getInstance(String key) {
Objects.requireNonNull(key);
var dumper = DUMPER_MAP.get(key);
if (dumper == null) {
String path = GetPropertyAction.privilegedGetProperty(key);
Path dir;
if (path == null || path.trim().isEmpty()) {
dir = null;
} else {
dir = validateDumpDir(Path.of(path.trim()));
}
var newDumper = new ClassFileDumper(key, dir);
var v = DUMPER_MAP.putIfAbsent(key, newDumper);
dumper = v != null ? v : newDumper;
}
return dumper;
}
/**
* Returns a ClassFileDumper instance for the given key with a given
* dump path. To enable dumping of the generated classes
@ -89,53 +56,49 @@ public final class ClassFileDumper {
*
* The system property is read only once when it is the first time
* the dumper instance for the given key is created.
*
* If not enabled, this method returns ClassFileDumper with null
* dump path.
*/
public static ClassFileDumper getInstance(String key, Path path) {
public static ClassFileDumper getInstance(String key, String path) {
Objects.requireNonNull(key);
Objects.requireNonNull(path);
var dumper = DUMPER_MAP.get(key);
if (dumper == null) {
String value = GetPropertyAction.privilegedGetProperty(key);
boolean enabled = value != null && value.isEmpty()
? true : Boolean.parseBoolean(value);
Path dir = enabled ? validateDumpDir(path) : null;
var newDumper = new ClassFileDumper(key, dir);
var newDumper = new ClassFileDumper(key, path);
var v = DUMPER_MAP.putIfAbsent(key, newDumper);
dumper = v != null ? v : newDumper;
}
if (dumper.isEnabled() && !path.equals(dumper.dumpPath())) {
if (dumper.isEnabled() && !path.equals(dumper.dumpDir)) {
throw new IllegalArgumentException("mismatched dump path for " + key);
}
return dumper;
}
private final String key;
private final Path dumpDir;
private final String dumpDir;
private final boolean enabled;
private final AtomicInteger counter = new AtomicInteger();
private ClassFileDumper(String key, Path path) {
private ClassFileDumper(String key, String path) {
String value = GetPropertyAction.privilegedGetProperty(key);
this.key = key;
boolean enabled = value != null && value.isEmpty() ? true : Boolean.parseBoolean(value);
if (enabled) {
validateDumpDir(path);
}
this.dumpDir = path;
this.enabled = enabled;
}
public String key() {
return key;
}
public boolean isEnabled() {
return dumpDir != null;
return enabled;
}
public Path dumpPath() {
return dumpDir;
}
public Path pathname(String internalName) {
return dumpDir.resolve(encodeForFilename(internalName) + ".class");
private Path pathname(String name) {
return Path.of(dumpDir, encodeForFilename(name) + ".class");
}
/**
@ -169,46 +132,47 @@ public final class ClassFileDumper {
@SuppressWarnings("removal")
private void write(Path path, byte[] bytes) {
AccessController.doPrivileged(new PrivilegedAction<>() {
@Override public Void run() {
try {
Path dir = path.getParent();
Files.createDirectories(dir);
Files.write(path, bytes);
} catch (Exception ex) {
if (VM.isModuleSystemInited()) {
// log only when lambda is ready to use
System.getLogger(ClassFileDumper.class.getName())
.log(System.Logger.Level.WARNING, "Exception writing to " +
path.toString() + " " + ex.getMessage());
}
// simply don't care if this operation failed
@Override public Void run() {
try {
Files.createDirectories(path.getParent());
Files.write(path, bytes);
} catch (Exception ex) {
if (VM.isModuleSystemInited()) {
// log only when lambda is ready to use
System.getLogger(ClassFileDumper.class.getName())
.log(System.Logger.Level.WARNING, "Exception writing to " +
path + " " + ex.getMessage());
}
return null;
}},
null,
new FilePermission("<<ALL FILES>>", "read, write"),
// createDirectories may need it
new PropertyPermission("user.dir", "read"));
// simply don't care if this operation failed
}
return null;
}});
}
/*
* Validate if the given dir is a writeable directory if exists.
*/
@SuppressWarnings("removal")
private static Path validateDumpDir(Path path) {
return AccessController.doPrivileged(new PrivilegedAction<>() {
@Override
public Path run() {
private static Path validateDumpDir(String dir) {
return AccessController.doPrivileged(new PrivilegedAction<>() {
@Override
public Path run() {
Path path = Path.of(dir);
if (Files.notExists(path)) {
try {
Files.createDirectories(path);
} catch (IOException ex) {
throw new UncheckedIOException("Fail to create " + path, ex);
throw new IllegalArgumentException("Fail to create " + path, ex);
}
if (!Files.isDirectory(path)) {
throw new IllegalArgumentException("Path " + path + " is not a directory");
} else if (!Files.isWritable(path)) {
throw new IllegalArgumentException("Directory " + path + " is not writable");
}
return path;
}
});
if (!Files.isDirectory(path)) {
throw new IllegalArgumentException("Path " + path + " is not a directory");
} else if (!Files.isWritable(path)) {
throw new IllegalArgumentException("Directory " + path + " is not writable");
}
return path;
}
});
}
private static final HexFormat HEX = HexFormat.of().withUpperCase();