Search in sources :

Example 11 with AnalyzerException

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);
}
Also used : AnalyzerException(org.objectweb.asm.tree.analysis.AnalyzerException) ArrayList(java.util.ArrayList) MethodInstrumentation(org.evosuite.instrumentation.coverage.MethodInstrumentation) MutationInstrumentation(org.evosuite.instrumentation.coverage.MutationInstrumentation) MethodNode(org.objectweb.asm.tree.MethodNode) AnnotatedMethodNode(org.evosuite.runtime.instrumentation.AnnotatedMethodNode) BranchInstrumentation(org.evosuite.instrumentation.coverage.BranchInstrumentation) DefUseInstrumentation(org.evosuite.instrumentation.coverage.DefUseInstrumentation) AnnotatedMethodNode(org.evosuite.runtime.instrumentation.AnnotatedMethodNode) HashSet(java.util.HashSet)

Example 12 with AnalyzerException

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);
}
Also used : Frame(org.objectweb.asm.tree.analysis.Frame) AnalyzerException(org.objectweb.asm.tree.analysis.AnalyzerException) LinkedList(java.util.LinkedList) BasicValue(org.objectweb.asm.tree.analysis.BasicValue) MethodVisitor(org.objectweb.asm.MethodVisitor) CheckMethodVisitorFsm(org.apache.drill.exec.compile.CheckMethodVisitorFsm)

Example 13 with AnalyzerException

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;
}
Also used : DexException(com.googlecode.d2j.DexException) BaksmaliDumper(com.googlecode.d2j.smali.BaksmaliDumper) LambadaNameSafeClassAdapter(com.googlecode.d2j.dex.LambadaNameSafeClassAdapter) StdAttributeFactory(com.android.dx.cf.direct.StdAttributeFactory) MethodVisitor(org.objectweb.asm.MethodVisitor) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) Dex2Asm(com.googlecode.d2j.dex.Dex2Asm) DexMethodNode(com.googlecode.d2j.node.DexMethodNode) DexOptions(com.android.dx.dex.DexOptions) AnalyzerException(org.objectweb.asm.tree.analysis.AnalyzerException) DexException(com.googlecode.d2j.DexException) ZipException(java.util.zip.ZipException) ParseException(com.android.dx.cf.iface.ParseException) ClassWriter(org.objectweb.asm.ClassWriter) DxContext(com.android.dx.command.dexer.DxContext) DirectClassFile(com.android.dx.cf.direct.DirectClassFile) ClassReader(org.objectweb.asm.ClassReader) ClassVisitorFactory(com.googlecode.d2j.dex.ClassVisitorFactory) CfOptions(com.android.dx.dex.cf.CfOptions) ParseException(com.android.dx.cf.iface.ParseException)

Aggregations

AnalyzerException (org.objectweb.asm.tree.analysis.AnalyzerException)13 BasicValue (org.objectweb.asm.tree.analysis.BasicValue)5 ArrayList (java.util.ArrayList)4 MethodVisitor (org.objectweb.asm.MethodVisitor)4 MethodNode (org.objectweb.asm.tree.MethodNode)3 Value (org.objectweb.asm.tree.analysis.Value)3 ParseException (com.android.dx.cf.iface.ParseException)2 DexException (com.googlecode.d2j.DexException)2 DexMethodNode (com.googlecode.d2j.node.DexMethodNode)2 LinkedList (java.util.LinkedList)2 ZipException (java.util.zip.ZipException)2 CheckMethodVisitorFsm (org.apache.drill.exec.compile.CheckMethodVisitorFsm)2 Type (org.objectweb.asm.Type)2 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)2 IntInsnNode (org.objectweb.asm.tree.IntInsnNode)2 Frame (org.objectweb.asm.tree.analysis.Frame)2 ClassEntry (co.paralleluniverse.fibers.instrument.MethodDatabase.ClassEntry)1 DirectClassFile (com.android.dx.cf.direct.DirectClassFile)1 StdAttributeFactory (com.android.dx.cf.direct.StdAttributeFactory)1 DxContext (com.android.dx.command.dexer.DxContext)1