package sun.instrument;
import java.lang.instrument.UnmodifiableModuleException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.AccessibleObject;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.ClassDefinition;
import java.lang.instrument.Instrumentation;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.Collections;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarFile;
import jdk.internal.module.Modules;
import jdk.internal.vm.annotation.IntrinsicCandidate;
public class InstrumentationImpl implements Instrumentation {
private final TransformerManager mTransformerManager;
private TransformerManager mRetransfomableTransformerManager;
private final long mNativeAgent;
private final boolean mEnvironmentSupportsRedefineClasses;
private volatile boolean mEnvironmentSupportsRetransformClassesKnown;
private volatile boolean mEnvironmentSupportsRetransformClasses;
private final boolean mEnvironmentSupportsNativeMethodPrefix;
private
InstrumentationImpl(long nativeAgent,
boolean environmentSupportsRedefineClasses,
boolean environmentSupportsNativeMethodPrefix) {
mTransformerManager = new TransformerManager(false);
mRetransfomableTransformerManager = null;
mNativeAgent = nativeAgent;
mEnvironmentSupportsRedefineClasses = environmentSupportsRedefineClasses;
mEnvironmentSupportsRetransformClassesKnown = false;
mEnvironmentSupportsRetransformClasses = false;
mEnvironmentSupportsNativeMethodPrefix = environmentSupportsNativeMethodPrefix;
}
public void
addTransformer(ClassFileTransformer transformer) {
addTransformer(transformer, false);
}
public synchronized void
addTransformer(ClassFileTransformer transformer, boolean canRetransform) {
if (transformer == null) {
throw new NullPointerException("null passed as 'transformer' in addTransformer");
}
if (canRetransform) {
if (!isRetransformClassesSupported()) {
throw new UnsupportedOperationException(
"adding retransformable transformers is not supported in this environment");
}
if (mRetransfomableTransformerManager == null) {
mRetransfomableTransformerManager = new TransformerManager(true);
}
mRetransfomableTransformerManager.addTransformer(transformer);
if (mRetransfomableTransformerManager.getTransformerCount() == 1) {
setHasRetransformableTransformers(mNativeAgent, true);
}
} else {
mTransformerManager.addTransformer(transformer);
if (mTransformerManager.getTransformerCount() == 1) {
setHasTransformers(mNativeAgent, true);
}
}
}
public synchronized boolean
removeTransformer(ClassFileTransformer transformer) {
if (transformer == null) {
throw new NullPointerException("null passed as 'transformer' in removeTransformer");
}
TransformerManager mgr = findTransformerManager(transformer);
if (mgr != null) {
mgr.removeTransformer(transformer);
if (mgr.getTransformerCount() == 0) {
if (mgr.isRetransformable()) {
setHasRetransformableTransformers(mNativeAgent, false);
} else {
setHasTransformers(mNativeAgent, false);
}
}
return true;
}
return false;
}
public boolean
isModifiableClass(Class<?> theClass) {
if (theClass == null) {
throw new NullPointerException(
"null passed as 'theClass' in isModifiableClass");
}
return isModifiableClass0(mNativeAgent, theClass);
}
public boolean isModifiableModule(Module module) {
if (module == null) {
throw new NullPointerException("'module' is null");
}
return true;
}
public boolean
isRetransformClassesSupported() {
if (!mEnvironmentSupportsRetransformClassesKnown) {
mEnvironmentSupportsRetransformClasses = isRetransformClassesSupported0(mNativeAgent);
mEnvironmentSupportsRetransformClassesKnown = true;
}
return mEnvironmentSupportsRetransformClasses;
}
public void
retransformClasses(Class<?>... classes) {
if (!isRetransformClassesSupported()) {
throw new UnsupportedOperationException(
"retransformClasses is not supported in this environment");
}
if (classes.length == 0) {
return;
}
retransformClasses0(mNativeAgent, classes);
}
public boolean
isRedefineClassesSupported() {
return mEnvironmentSupportsRedefineClasses;
}
public void
redefineClasses(ClassDefinition... definitions)
throws ClassNotFoundException {
if (!isRedefineClassesSupported()) {
throw new UnsupportedOperationException("redefineClasses is not supported in this environment");
}
if (definitions == null) {
throw new NullPointerException("null passed as 'definitions' in redefineClasses");
}
for (int i = 0; i < definitions.length; ++i) {
if (definitions[i] == null) {
throw new NullPointerException("element of 'definitions' is null in redefineClasses");
}
}
if (definitions.length == 0) {
return;
}
redefineClasses0(mNativeAgent, definitions);
}
@SuppressWarnings("rawtypes")
public Class[]
getAllLoadedClasses() {
return getAllLoadedClasses0(mNativeAgent);
}
@SuppressWarnings("rawtypes")
public Class[]
getInitiatedClasses(ClassLoader loader) {
return getInitiatedClasses0(mNativeAgent, loader);
}
public long
getObjectSize(Object objectToSize) {
if (objectToSize == null) {
throw new NullPointerException("null passed as 'objectToSize' in getObjectSize");
}
return getObjectSize0(mNativeAgent, objectToSize);
}
public void
appendToBootstrapClassLoaderSearch(JarFile jarfile) {
appendToClassLoaderSearch0(mNativeAgent, jarfile.getName(), true);
}
public void
appendToSystemClassLoaderSearch(JarFile jarfile) {
appendToClassLoaderSearch0(mNativeAgent, jarfile.getName(), false);
}
public boolean
isNativeMethodPrefixSupported() {
return mEnvironmentSupportsNativeMethodPrefix;
}
public synchronized void
setNativeMethodPrefix(ClassFileTransformer transformer, String prefix) {
if (!isNativeMethodPrefixSupported()) {
throw new UnsupportedOperationException(
"setNativeMethodPrefix is not supported in this environment");
}
if (transformer == null) {
throw new NullPointerException(
"null passed as 'transformer' in setNativeMethodPrefix");
}
TransformerManager mgr = findTransformerManager(transformer);
if (mgr == null) {
throw new IllegalArgumentException(
"transformer not registered in setNativeMethodPrefix");
}
mgr.setNativeMethodPrefix(transformer, prefix);
String[] prefixes = mgr.getNativeMethodPrefixes();
setNativeMethodPrefixes(mNativeAgent, prefixes, mgr.isRetransformable());
}
@Override
public void redefineModule(Module module,
Set<Module> extraReads,
Map<String, Set<Module>> extraExports,
Map<String, Set<Module>> extraOpens,
Set<Class<?>> extraUses,
Map<Class<?>, List<Class<?>>> extraProvides)
{
if (!module.isNamed())
return;
if (!isModifiableModule(module))
throw new UnmodifiableModuleException(module.getName());
extraReads = new HashSet<>(extraReads);
if (extraReads.contains(null))
throw new NullPointerException("'extraReads' contains null");
extraExports = cloneAndCheckMap(module, extraExports);
extraOpens = cloneAndCheckMap(module, extraOpens);
extraUses = new HashSet<>(extraUses);
if (extraUses.contains(null))
throw new NullPointerException("'extraUses' contains null");
Map<Class<?>, List<Class<?>>> tmpProvides = new HashMap<>();
for (Map.Entry<Class<?>, List<Class<?>>> e : extraProvides.entrySet()) {
Class<?> service = e.getKey();
if (service == null)
throw new NullPointerException("'extraProvides' contains null");
List<Class<?>> providers = new ArrayList<>(e.getValue());
if (providers.isEmpty())
throw new IllegalArgumentException("list of providers is empty");
providers.forEach(p -> {
if (p.getModule() != module)
throw new IllegalArgumentException(p + " not in " + module);
if (!service.isAssignableFrom(p))
throw new IllegalArgumentException(p + " is not a " + service);
});
tmpProvides.put(service, providers);
}
extraProvides = tmpProvides;
extraReads.forEach(m -> Modules.addReads(module, m));
for (Map.Entry<String, Set<Module>> e : extraExports.entrySet()) {
String pkg = e.getKey();
Set<Module> targets = e.getValue();
targets.forEach(m -> Modules.addExports(module, pkg, m));
}
for (Map.Entry<String, Set<Module>> e : extraOpens.entrySet()) {
String pkg = e.getKey();
Set<Module> targets = e.getValue();
targets.forEach(m -> Modules.addOpens(module, pkg, m));
}
extraUses.forEach(service -> Modules.addUses(module, service));
for (Map.Entry<Class<?>, List<Class<?>>> e : extraProvides.entrySet()) {
Class<?> service = e.getKey();
List<Class<?>> providers = e.getValue();
providers.forEach(p -> Modules.addProvides(module, service, p));
}
}
private Map<String, Set<Module>>
cloneAndCheckMap(Module module, Map<String, Set<Module>> map)
{
if (map.isEmpty())
return Collections.emptyMap();
Map<String, Set<Module>> result = new HashMap<>();
Set<String> packages = module.getPackages();
for (Map.Entry<String, Set<Module>> e : map.entrySet()) {
String pkg = e.getKey();
if (pkg == null)
throw new NullPointerException("package cannot be null");
if (!packages.contains(pkg))
throw new IllegalArgumentException(pkg + " not in module");
Set<Module> targets = new HashSet<>(e.getValue());
if (targets.isEmpty())
throw new IllegalArgumentException("set of targets is empty");
if (targets.contains(null))
throw new NullPointerException("set of targets cannot include null");
result.put(pkg, targets);
}
return result;
}
private TransformerManager
findTransformerManager(ClassFileTransformer transformer) {
if (mTransformerManager.includesTransformer(transformer)) {
return mTransformerManager;
}
if (mRetransfomableTransformerManager != null &&
mRetransfomableTransformerManager.includesTransformer(transformer)) {
return mRetransfomableTransformerManager;
}
return null;
}
private native boolean
isModifiableClass0(long nativeAgent, Class<?> theClass);
private native boolean
isRetransformClassesSupported0(long nativeAgent);
private native void
setHasTransformers(long nativeAgent, boolean has);
private native void
setHasRetransformableTransformers(long nativeAgent, boolean has);
private native void
retransformClasses0(long nativeAgent, Class<?>[] classes);
private native void
redefineClasses0(long nativeAgent, ClassDefinition[] definitions)
throws ClassNotFoundException;
@SuppressWarnings("rawtypes")
private native Class[]
getAllLoadedClasses0(long nativeAgent);
@SuppressWarnings("rawtypes")
private native Class[]
getInitiatedClasses0(long nativeAgent, ClassLoader loader);
@IntrinsicCandidate
private native long
getObjectSize0(long nativeAgent, Object objectToSize);
private native void
appendToClassLoaderSearch0(long nativeAgent, String jarfile, boolean bootLoader);
private native void
setNativeMethodPrefixes(long nativeAgent, String[] prefixes, boolean isRetransformable);
static {
System.loadLibrary("instrument");
}
private static void setAccessible(final AccessibleObject ao, final boolean accessible) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
ao.setAccessible(accessible);
return null;
}});
}
private void
loadClassAndStartAgent( String classname,
String methodname,
String optionsString)
throws Throwable {
ClassLoader mainAppLoader = ClassLoader.getSystemClassLoader();
Class<?> javaAgentClass = mainAppLoader.loadClass(classname);
Method m = null;
NoSuchMethodException firstExc = null;
boolean twoArgAgent = false;
try {
m = javaAgentClass.getDeclaredMethod( methodname,
new Class<?>[] {
String.class,
java.lang.instrument.Instrumentation.class
}
);
twoArgAgent = true;
} catch (NoSuchMethodException x) {
firstExc = x;
}
if (m == null) {
try {
m = javaAgentClass.getDeclaredMethod(methodname,
new Class<?>[] { String.class });
} catch (NoSuchMethodException x) {
throw firstExc;
}
}
if (!Modifier.isPublic(m.getModifiers())) {
String msg = "method " + classname + "." + methodname + " must be declared public";
throw new IllegalAccessException(msg);
}
if (!Modifier.isPublic(javaAgentClass.getModifiers()) &&
!javaAgentClass.getModule().isNamed()) {
setAccessible(m, true);
}
if (twoArgAgent) {
m.invoke(null, new Object[] { optionsString, this });
} else {
m.invoke(null, new Object[] { optionsString });
}
}
private void
loadClassAndCallPremain( String classname,
String optionsString)
throws Throwable {
loadClassAndStartAgent( classname, "premain", optionsString );
}
private void
loadClassAndCallAgentmain( String classname,
String optionsString)
throws Throwable {
loadClassAndStartAgent( classname, "agentmain", optionsString );
}
private byte[]
transform( Module module,
ClassLoader loader,
String classname,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer,
boolean isRetransformer) {
TransformerManager mgr = isRetransformer?
mRetransfomableTransformerManager :
mTransformerManager;
if (module == null) {
if (classBeingRedefined != null) {
module = classBeingRedefined.getModule();
} else {
module = (loader == null) ? jdk.internal.loader.BootLoader.getUnnamedModule()
: loader.getUnnamedModule();
}
}
if (mgr == null) {
return null;
} else {
return mgr.transform( module,
loader,
classname,
classBeingRedefined,
protectionDomain,
classfileBuffer);
}
}
public static void loadAgent(String path) {
loadAgent0(path);
}
private static native void loadAgent0(String path);
}