use of org.objectweb.asm.tree.analysis.AnalyzerException in project evosuite by EvoSuite.
the class CFGMethodAdapter method visitEnd.
/**
* {@inheritDoc}
*/
@Override
public void visitEnd() {
logger.debug("Creating CFG of " + className + "." + methodName);
boolean isExcludedMethod = excludeMethod || EXCLUDE.contains(methodName);
boolean isMainMethod = plain_name.equals("main") && Modifier.isStatic(access);
List<MethodInstrumentation> instrumentations = new ArrayList<MethodInstrumentation>();
if (DependencyAnalysis.shouldInstrument(className, methodName)) {
if (ArrayUtil.contains(Properties.CRITERION, Criterion.DEFUSE) || ArrayUtil.contains(Properties.CRITERION, Criterion.ALLDEFS)) {
instrumentations.add(new BranchInstrumentation());
instrumentations.add(new DefUseInstrumentation());
} else if (ArrayUtil.contains(Properties.CRITERION, Criterion.MUTATION) || ArrayUtil.contains(Properties.CRITERION, Criterion.WEAKMUTATION) || ArrayUtil.contains(Properties.CRITERION, Criterion.ONLYMUTATION) || ArrayUtil.contains(Properties.CRITERION, Criterion.STRONGMUTATION)) {
instrumentations.add(new BranchInstrumentation());
instrumentations.add(new MutationInstrumentation());
} else {
instrumentations.add(new BranchInstrumentation());
}
} else {
// instrumentations.add(new BranchInstrumentation());
}
boolean executeOnMain = false;
boolean executeOnExcluded = false;
for (MethodInstrumentation instrumentation : instrumentations) {
executeOnMain = executeOnMain || instrumentation.executeOnMainMethod();
executeOnExcluded = executeOnExcluded || instrumentation.executeOnExcludedMethods();
}
// super.visitEnd();
// Generate CFG of method
MethodNode mn = (AnnotatedMethodNode) mv;
boolean checkForMain = false;
if (Properties.CONSIDER_MAIN_METHODS) {
checkForMain = true;
} else {
checkForMain = !isMainMethod || executeOnMain;
}
// MethodInstrumentation wants it anyway)
if (checkForMain && (!isExcludedMethod || executeOnExcluded) && (access & Opcodes.ACC_ABSTRACT) == 0 && (access & Opcodes.ACC_NATIVE) == 0) {
logger.info("Analyzing method " + methodName + " in class " + className);
// MethodNode mn = new CFGMethodNode((MethodNode)mv);
// System.out.println("Generating CFG for "+ className+"."+mn.name +
// " ("+mn.desc +")");
BytecodeAnalyzer bytecodeAnalyzer = new BytecodeAnalyzer();
logger.info("Generating CFG for method " + methodName);
try {
bytecodeAnalyzer.analyze(classLoader, className, methodName, mn);
logger.trace("Method graph for " + className + "." + methodName + " contains " + bytecodeAnalyzer.retrieveCFGGenerator().getRawGraph().vertexSet().size() + " nodes for " + bytecodeAnalyzer.getFrames().length + " instructions");
// compute Raw and ActualCFG and put both into GraphPool
bytecodeAnalyzer.retrieveCFGGenerator().registerCFGs();
logger.info("Created CFG for method " + methodName);
if (DependencyAnalysis.shouldInstrument(className, methodName)) {
if (!methods.get(classLoader).containsKey(className))
methods.get(classLoader).put(className, new HashSet<String>());
// add the actual instrumentation
logger.info("Instrumenting method " + methodName + " in class " + className);
for (MethodInstrumentation instrumentation : instrumentations) instrumentation.analyze(classLoader, mn, className, methodName, access);
handleBranchlessMethods();
String id = className + "." + methodName;
if (isUsable()) {
methods.get(classLoader).get(className).add(id);
logger.debug("Counting: " + id);
}
}
} catch (AnalyzerException e) {
logger.error("Analyzer exception while analyzing " + className + "." + methodName + ": " + e);
e.printStackTrace();
}
} else {
logger.debug("NOT Creating CFG of " + className + "." + methodName + ": " + checkForMain + ", " + ((!isExcludedMethod || executeOnExcluded)) + ", " + ((access & Opcodes.ACC_ABSTRACT) == 0) + ", " + ((access & Opcodes.ACC_NATIVE) == 0));
super.visitEnd();
}
mn.accept(next);
}
use of org.objectweb.asm.tree.analysis.AnalyzerException in project drill by apache.
the class ScalarReplacementNode method visitEnd.
@Override
public void visitEnd() {
/*
* Note this is a MethodNode, not a MethodVisitor. As a result, calls to the various visitX()
* methods will be building up a method. Then, once we analyze it, we use accept() to visit that
* method and transform it with the InstructionModifier at the bottom.
*/
super.visitEnd();
final LinkedList<ReplacingBasicValue> valueList = new LinkedList<>();
final MethodAnalyzer<BasicValue> analyzer = new MethodAnalyzer<>(new ReplacingInterpreter(className, valueList));
Frame<BasicValue>[] frames;
try {
frames = analyzer.analyze(className, this);
} catch (final AnalyzerException e) {
throw new IllegalStateException(e);
}
if (logger.isTraceEnabled()) {
final StringBuilder sb = new StringBuilder();
sb.append("ReplacingBasicValues for " + className + "\n");
for (final ReplacingBasicValue value : valueList) {
value.dump(sb, 2);
sb.append('\n');
}
logger.debug(sb.toString());
}
// wrap the instruction handler so that we can do additional things
final TrackingInstructionList list = new TrackingInstructionList(frames, this.instructions);
this.instructions = list;
MethodVisitor methodVisitor = inner;
if (verifyBytecode) {
methodVisitor = new CheckMethodVisitorFsm(CompilationConfig.ASM_API_VERSION, methodVisitor);
}
final InstructionModifier holderV = new InstructionModifier(this.access, this.name, this.desc, this.signature, this.exceptionsArr, list, methodVisitor);
accept(holderV);
}
use of org.objectweb.asm.tree.analysis.AnalyzerException in project dex2jar by pxb1988.
the class TestUtils method translateAndCheck.
public static byte[] translateAndCheck(DexFileNode fileNode, DexClassNode clzNode) throws AnalyzerException, IllegalAccessException {
// 1. convert to .class
Dex2Asm dex2Asm = new Dex2Asm() {
@Override
public void convertCode(DexMethodNode methodNode, MethodVisitor mv, ClzCtx clzCtx) {
try {
super.convertCode(methodNode, mv, clzCtx);
} catch (Exception ex) {
BaksmaliDumper d = new BaksmaliDumper();
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.err, "UTF-8"));
d.baksmaliMethod(methodNode, out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
throw new DexException(ex, "Failed to convert code for %s", methodNode.method);
}
}
};
final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
final LambadaNameSafeClassAdapter rca = new LambadaNameSafeClassAdapter(cw);
ClassVisitorFactory cvf = new ClassVisitorFactory() {
@Override
public ClassVisitor create(String classInternalName) {
return rca;
}
};
if (fileNode != null) {
dex2Asm.convertClass(clzNode, cvf, fileNode);
} else {
dex2Asm.convertClass(clzNode, cvf);
}
byte[] data = cw.toByteArray();
// 2. verify .class
ClassReader cr = new ClassReader(data);
TestUtils.verify(cr);
// 3. convert back to dex
CfOptions cfOptions = new CfOptions();
cfOptions.strictNameCheck = false;
DexOptions dexOptions = new DexOptions();
if (fileNode != null && fileNode.dexVersion >= DexConstants.DEX_037) {
dexOptions.minSdkVersion = 26;
}
DirectClassFile dcf = new DirectClassFile(data, rca.getClassName() + ".class", true);
dcf.setAttributeFactory(new StdAttributeFactory());
com.android.dx.dex.file.DexFile dxFile = new com.android.dx.dex.file.DexFile(dexOptions);
try {
CfTranslator.translate(new DxContext(), dcf, data, cfOptions, dexOptions, dxFile);
} catch (ParseException e) {
if ("MethodHandle not supported".equals(e.getMessage())) {
e.printStackTrace();
} else {
throw e;
}
}
return data;
}
Aggregations