Instrumenting interceptors module

This commit is contained in:
Andrey Breslav
2013-05-11 17:29:57 +03:00
parent 2e1a7179f0
commit 90661e8706
16 changed files with 731 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
<component name="ArtifactManager">
<artifact type="jar" name="instrumentation:jar">
<output-path>$PROJECT_DIR$/out/artifacts/instrumentation_jar</output-path>
<root id="archive" name="instrumentation.jar">
<element id="module-output" name="instrumentation" />
<element id="extracted-dir" path="$PROJECT_DIR$/ideaSDK/lib/guava-12.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/ideaSDK/lib/asm4-all.jar" path-in-jar="/" />
</root>
</artifact>
</component>
+11
View File
@@ -0,0 +1,11 @@
<component name="libraryTable">
<library name="asm">
<CLASSES>
<root url="jar://$PROJECT_DIR$/ideaSDK/lib/asm4-all.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$PROJECT_DIR$/dependencies/jetbrains-asm-all-4.0-src.zip!/" />
</SOURCES>
</library>
</component>
+11
View File
@@ -0,0 +1,11 @@
<component name="libraryTable">
<library name="guava">
<CLASSES>
<root url="jar://$PROJECT_DIR$/ideaSDK/lib/guava-12.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$PROJECT_DIR$/dependencies/guava-12.0-sources.jar!/" />
</SOURCES>
</library>
</component>
+1
View File
@@ -19,6 +19,7 @@
<module fileurl="file://$PROJECT_DIR$/idea/idea.iml" filepath="$PROJECT_DIR$/idea/idea.iml" />
<module fileurl="file://$PROJECT_DIR$/idea_runner/idea_runner.iml" filepath="$PROJECT_DIR$/idea_runner/idea_runner.iml" />
<module fileurl="file://$PROJECT_DIR$/injector-generator/injector-generator.iml" filepath="$PROJECT_DIR$/injector-generator/injector-generator.iml" />
<module fileurl="file://$PROJECT_DIR$/compiler/preloader/instrumentation/instrumentation.iml" filepath="$PROJECT_DIR$/compiler/preloader/instrumentation/instrumentation.iml" />
<module fileurl="file://$PROJECT_DIR$/j2k/j2k.iml" filepath="$PROJECT_DIR$/j2k/j2k.iml" />
<module fileurl="file://$PROJECT_DIR$/j2k/tests/j2k-tests.iml" filepath="$PROJECT_DIR$/j2k/tests/j2k-tests.iml" />
<module fileurl="file://$PROJECT_DIR$/compiler/jet.as.java.psi/jet.as.java.psi.iml" filepath="$PROJECT_DIR$/compiler/jet.as.java.psi/jet.as.java.psi.iml" />
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="asm" level="project" />
<orderEntry type="library" name="guava" level="project" />
<orderEntry type="module" module-name="preloader" scope="PROVIDED" />
</component>
</module>
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.preloading.instrumentation;
import org.jetbrains.asm4.Type;
interface FieldData extends MemberData {
Type getRuntimeType();
}
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.preloading.instrumentation;
import org.jetbrains.asm4.Type;
class FieldDataImpl extends MemberDataImpl implements FieldData {
private final Type runtimeType;
public FieldDataImpl(String declaringClass, String name, String desc, Type runtimeType) {
super(declaringClass, name, desc);
this.runtimeType = runtimeType;
}
@Override
public Type getRuntimeType() {
return runtimeType;
}
}
@@ -0,0 +1,319 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.preloading.instrumentation;
import org.jetbrains.asm4.*;
import org.jetbrains.asm4.commons.InstructionAdapter;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.regex.Pattern;
import static org.jetbrains.asm4.Opcodes.*;
public class InterceptionInstrumenter implements Instrumenter {
private final Map<String, ClassMatcher> classPatterns = new LinkedHashMap<String, ClassMatcher>();
private final Set<String> neverMatchedClassPatterns = new LinkedHashSet<String>();
private final Set<MethodInstrumenter> neverMatchedInstrumenters = new LinkedHashSet<MethodInstrumenter>();
interface DumpAction {
void dump(PrintStream out);
}
private final List<DumpAction> dumpTasks = new ArrayList<DumpAction>();
public InterceptionInstrumenter(List<Class<?>> handlerClasses) {
for (Class<?> handlerClass : handlerClasses) {
addHandlerClass(handlerClass);
}
}
private void addHandlerClass(Class<?> handlerClass) {
for (Field field : handlerClass.getFields()) {
MethodInterceptor annotation = field.getAnnotation(MethodInterceptor.class);
if (annotation == null) continue;
if ((field.getModifiers() & Modifier.STATIC) == 0) {
throw new IllegalArgumentException("Non-static field annotated @MethodInterceptor: " + field);
}
Pattern classPattern = Pattern.compile(annotation.className());
List<MethodInstrumenter> instrumenters = addClassPattern(classPattern.pattern());
try {
Object interceptor = field.get(null);
if (interceptor == null) {
throw new IllegalArgumentException("Interceptor is null: " + field);
}
Class<?> interceptorClass = interceptor.getClass();
FieldData fieldData = getFieldData(field, interceptorClass);
List<MethodData> enterData = new ArrayList<MethodData>();
List<MethodData> exitData = new ArrayList<MethodData>();
Method[] methods = interceptorClass.getMethods();
for (Method method : methods) {
String name = method.getName();
if (name.startsWith("enter")) {
enterData.add(getMethodData(fieldData, method));
}
else if (name.startsWith("exit")) {
exitData.add(getMethodData(fieldData, method));
}
else if (name.startsWith("dump")) {
Class<?>[] parameterTypes = method.getParameterTypes();
// Dump must have no parameters or one PrintStream parameter
if (parameterTypes.length > 1) continue;
if (parameterTypes.length == 1 && parameterTypes[0] != PrintStream.class) {
continue;
}
addDumpTask(interceptor, method);
}
}
String nameFromAnnotation = annotation.methodName();
String methodName = nameFromAnnotation.isEmpty() ? field.getName() : nameFromAnnotation;
MethodInstrumenterImpl instrumenter = new MethodInstrumenterImpl(
Pattern.compile(methodName),
Pattern.compile(annotation.erasedSignature()),
annotation.allowMultipleMatches(),
enterData,
exitData);
instrumenters.add(instrumenter);
neverMatchedInstrumenters.add(instrumenter);
}
catch (IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
}
}
private List<MethodInstrumenter> addClassPattern(String classPattern) {
ClassMatcher classMatcher = classPatterns.get(classPattern);
if (classMatcher == null) {
classMatcher = new ClassMatcher(Pattern.compile(classPattern));
classPatterns.put(classPattern, classMatcher);
neverMatchedClassPatterns.add(classPattern);
}
return classMatcher.instrumenters;
}
private static FieldData getFieldData(Field field, Class<?> runtimeType) {
return new FieldDataImpl(
Type.getInternalName(field.getDeclaringClass()),
field.getName(),
Type.getDescriptor(field.getType()),
Type.getType(runtimeType));
}
private static MethodData getMethodData(FieldData interceptorField, Method method) {
return new MethodDataImpl(
interceptorField,
Type.getInternalName(method.getDeclaringClass()),
method.getName(),
Type.getMethodDescriptor(method),
method.getParameterTypes().length
);
}
private void addDumpTask(final Object interceptor, final Method method) {
dumpTasks.add(new DumpAction() {
@Override
public void dump(PrintStream out) {
try {
if (method.getParameterTypes().length == 0) {
method.invoke(interceptor);
}
else {
method.invoke(interceptor, out);
}
}
catch (IllegalAccessException e) {
throw new IllegalStateException(e);
}
catch (InvocationTargetException e) {
throw new IllegalStateException(e);
}
}
});
}
public byte[] instrument(String resourceName, byte[] data) {
List<MethodInstrumenter> applicableInstrumenters = new ArrayList<MethodInstrumenter>();
String className = stripClassSuffix(resourceName);
for (Map.Entry<String, ClassMatcher> classPatternEntry : classPatterns.entrySet()) {
String classPattern = classPatternEntry.getKey();
ClassMatcher classMatcher = classPatternEntry.getValue();
if (classMatcher.classPattern.matcher(className).matches()) {
neverMatchedClassPatterns.remove(classPattern);
applicableInstrumenters.addAll(classMatcher.instrumenters);
}
}
if (applicableInstrumenters.isEmpty()) return data;
return instrument(data, applicableInstrumenters);
}
private static String stripClassSuffix(String name) {
String suffix = ".class";
if (!name.endsWith(suffix)) return name;
return name.substring(0, name.length() - suffix.length());
}
private byte[] instrument(byte[] classData, final List<MethodInstrumenter> instrumenters) {
final ClassReader cr = new ClassReader(classData);
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES);
cr.accept(new ClassVisitor(ASM4, cw) {
private final Map<MethodInstrumenter, String> matchedMethods = new HashMap<MethodInstrumenter, String>();
@Override
public MethodVisitor visitMethod(
final int access,
final String name,
final String desc,
String signature,
String[] exceptions
) {
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
// Do not instrument synthetic methods
if ((access & (ACC_BRIDGE | ACC_SYNTHETIC)) != 0) return mv;
List<MethodInstrumenter> applicableInstrumenters = new ArrayList<MethodInstrumenter>();
for (MethodInstrumenter instrumenter : instrumenters) {
if (instrumenter.isApplicable(name, desc)) {
applicableInstrumenters.add(instrumenter);
checkMultipleMatches(instrumenter, name, desc);
neverMatchedInstrumenters.remove(instrumenter);
}
}
if (applicableInstrumenters.isEmpty()) return mv;
InstructionAdapter ia = new InstructionAdapter(mv);
final List<MethodData> exitData = new ArrayList<MethodData>();
for (MethodInstrumenter instrumenter : applicableInstrumenters) {
for (MethodData enterData : instrumenter.getEnterData()) {
invokeMethod(access, name, desc, ia, enterData);
}
exitData.addAll(instrumenter.getExitData());
}
if (exitData.isEmpty()) return mv;
return new MethodVisitor(ASM4, mv) {
private InstructionAdapter ia = null;
private InstructionAdapter getInstructionAdapter() {
if (ia == null) {
ia = new InstructionAdapter(this);
}
return ia;
}
@Override
public void visitInsn(int opcode) {
switch (opcode) {
case RETURN:
case IRETURN:
case LRETURN:
case FRETURN:
case DRETURN:
case ARETURN:
case ATHROW:
for (MethodData methodData : exitData) {
invokeMethod(access, name, desc, getInstructionAdapter(), methodData);
}
break;
}
super.visitInsn(opcode);
}
};
}
private void checkMultipleMatches(MethodInstrumenter instrumenter, String name, String desc) {
if (!instrumenter.allowsMultipleMatches()) {
String erasedSignature = name + desc;
String alreadyMatched = matchedMethods.put(instrumenter, erasedSignature);
if (alreadyMatched != null) {
throw new IllegalStateException(instrumenter + " matched two methods in " + cr.getClassName() + ":\n"
+ alreadyMatched + "\n"
+ erasedSignature);
}
}
}
}, 0);
return cw.toByteArray();
}
private static void invokeMethod(int access, String name, String desc, InstructionAdapter ia, MethodData methodData) {
FieldData field = methodData.getOwnerField();
ia.getstatic(field.getDeclaringClass(), field.getName(), field.getDesc());
ia.checkcast(field.getRuntimeType());
int parameterCount = methodData.getParameterCount();
if (parameterCount > 0) {
org.jetbrains.asm4.commons.Method method = new org.jetbrains.asm4.commons.Method(name, desc);
Type[] parameterTypes = method.getArgumentTypes();
int base = (access & ACC_STATIC) != 0 ? 0 : 1;
for (int i = 0; i < parameterCount; i++) {
ia.load(base + i, parameterTypes[i]);
}
}
ia.invokevirtual(methodData.getDeclaringClass(), methodData.getName(), methodData.getDesc());
}
public void dump(PrintStream out) {
for (DumpAction task : dumpTasks) {
task.dump(out);
}
if (!neverMatchedClassPatterns.isEmpty()) {
out.println("Class patterns never matched:");
for (String classPattern : neverMatchedClassPatterns) {
out.println(" " + classPattern);
neverMatchedInstrumenters.removeAll(classPatterns.get(classPattern).instrumenters);
}
}
if (!neverMatchedInstrumenters.isEmpty()) {
out.println("Instrumenters never matched: ");
for (MethodInstrumenter instrumenter : neverMatchedInstrumenters) {
out.println(" " + instrumenter);
}
}
}
private static class ClassMatcher {
private final Pattern classPattern;
private final List<MethodInstrumenter> instrumenters = new ArrayList<MethodInstrumenter>();
private ClassMatcher(Pattern classPattern) {
this.classPattern = classPattern;
}
}
}
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.preloading.instrumentation;
import java.io.PrintStream;
import java.util.Collections;
public abstract class InterceptionInstrumenterAdaptor implements Instrumenter {
private final InterceptionInstrumenter instrumenter;
public InterceptionInstrumenterAdaptor() {
this.instrumenter = new InterceptionInstrumenter(Collections.<Class<?>>singletonList(getClass()));
}
@Override
public byte[] instrument(String resourceName, byte[] data) {
return instrumenter.instrument(resourceName, data);
}
@Override
public void dump(PrintStream out) {
instrumenter.dump(out);
}
}
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.preloading.instrumentation;
interface MemberData {
String getDeclaringClass();
String getName();
String getDesc();
}
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.preloading.instrumentation;
class MemberDataImpl implements MemberData {
private final String declaringClass;
private final String name;
private final String desc;
public MemberDataImpl(String declaringClass, String name, String desc) {
this.declaringClass = declaringClass;
this.name = name;
this.desc = desc;
}
@Override
public String getDeclaringClass() {
return declaringClass;
}
@Override
public String getName() {
return name;
}
@Override
public String getDesc() {
return desc;
}
}
@@ -0,0 +1,22 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.preloading.instrumentation;
interface MethodData extends MemberData {
FieldData getOwnerField();
int getParameterCount();
}
@@ -0,0 +1,44 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.preloading.instrumentation;
public class MethodDataImpl extends MemberDataImpl implements MethodData {
private final FieldData ownerField;
private final int parameterCount;
MethodDataImpl(
FieldData ownerField,
String declaringClass,
String name,
String desc,
int parameterCount
) {
super(declaringClass, name, desc);
this.ownerField = ownerField;
this.parameterCount = parameterCount;
}
@Override
public FieldData getOwnerField() {
return ownerField;
}
@Override
public int getParameterCount() {
return parameterCount;
}
}
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.preloading.instrumentation;
import java.util.List;
interface MethodInstrumenter {
boolean isApplicable(String name, String desc);
List<MethodData> getExitData();
List<MethodData> getEnterData();
boolean allowsMultipleMatches();
}
@@ -0,0 +1,67 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.preloading.instrumentation;
import java.util.List;
import java.util.regex.Pattern;
class MethodInstrumenterImpl implements MethodInstrumenter {
private final Pattern namePattern;
private final Pattern descPattern;
private final boolean allowMultipleMatches;
private final List<MethodData> enterData;
private final List<MethodData> exitData;
public MethodInstrumenterImpl(
Pattern namePattern,
Pattern descPattern,
boolean allowMultipleMatches,
List<MethodData> enterData,
List<MethodData> exitData
) {
this.namePattern = namePattern;
this.descPattern = descPattern;
this.allowMultipleMatches = allowMultipleMatches;
this.enterData = enterData;
this.exitData = exitData;
}
@Override
public boolean allowsMultipleMatches() {
return allowMultipleMatches;
}
@Override
public boolean isApplicable(String name, String desc) {
return namePattern.matcher(name).matches() && descPattern.matcher(desc).matches();
}
@Override
public List<MethodData> getEnterData() {
return enterData;
}
@Override
public List<MethodData> getExitData() {
return exitData;
}
@Override
public String toString() {
return namePattern + " " + descPattern + (allowMultipleMatches ? "[multiple]" : "");
}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.preloading.instrumentation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodInterceptor {
// JVM internal name like java/util/Map$Entry or short name like FooBar
String className();
// regexp, if omitted, field name is used
String methodName() default "";
// regexp
String erasedSignature() default "";
// if this is false, an exception is thrown when more than one method in the same class matches
boolean allowMultipleMatches() default false;
}