Search in sources :

Example 6 with LocalVariableNode

use of org.objectweb.asm.tree.LocalVariableNode in project evosuite by EvoSuite.

the class ImplicitElseTransformer method isDefinedBefore.

@SuppressWarnings("unchecked")
private boolean isDefinedBefore(MethodNode mn, VarInsnNode var, AbstractInsnNode position) {
    // TODO: Iterate over local variables and check if local is defined here
    List<LocalVariableNode> localVar = mn.localVariables;
    if (localVar.isEmpty()) {
        // If we have no debug information, try to guess
        AbstractInsnNode pos = position.getPrevious();
        while (pos != mn.instructions.getFirst()) {
            if (pos instanceof VarInsnNode) {
                VarInsnNode vn = (VarInsnNode) pos;
                if (var.var == vn.var) {
                    return true;
                }
            }
            pos = pos.getPrevious();
        }
    } else {
        int current = mn.instructions.indexOf(position);
        for (LocalVariableNode local : localVar) {
            if (local.index == var.var) {
                int start = mn.instructions.indexOf(local.start);
                int end = mn.instructions.indexOf(local.end);
                if (current >= start && current <= end)
                    return true;
            }
        }
    }
    return false;
}
Also used : AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) LocalVariableNode(org.objectweb.asm.tree.LocalVariableNode)

Example 7 with LocalVariableNode

use of org.objectweb.asm.tree.LocalVariableNode in project evosuite by EvoSuite.

the class ReplaceVariable method getLocalReplacements.

private Map<String, InsnList> getLocalReplacements(MethodNode mn, String desc, AbstractInsnNode node, Frame frame) {
    Map<String, InsnList> replacements = new HashMap<String, InsnList>();
    // if (desc.equals("I"))
    // return replacements;
    int otherNum = -1;
    if (node instanceof VarInsnNode) {
        VarInsnNode vNode = (VarInsnNode) node;
        otherNum = vNode.var;
    }
    if (otherNum == -1)
        return replacements;
    int currentId = mn.instructions.indexOf(node);
    logger.info("Looking for replacements at position " + currentId + " of variable " + otherNum + " of type " + desc);
    for (Object v : mn.localVariables) {
        LocalVariableNode localVar = (LocalVariableNode) v;
        int startId = mn.instructions.indexOf(localVar.start);
        int endId = mn.instructions.indexOf(localVar.end);
        logger.info("Checking local variable " + localVar.name + " of type " + localVar.desc + " at index " + localVar.index);
        if (!localVar.desc.equals(desc))
            logger.info("- Types do not match");
        if (localVar.index == otherNum)
            logger.info("- Replacement = original");
        if (currentId < startId)
            logger.info("- Out of scope (start)");
        if (currentId > endId)
            logger.info("- Out of scope (end)");
        BasicValue newValue = (BasicValue) frame.getLocal(localVar.index);
        if (newValue == BasicValue.UNINITIALIZED_VALUE)
            logger.info("- Not initialized");
        if (localVar.desc.equals(desc) && localVar.index != otherNum && currentId >= startId && currentId <= endId && newValue != BasicValue.UNINITIALIZED_VALUE) {
            logger.info("Adding local variable " + localVar.name + " of type " + localVar.desc + " at index " + localVar.index + ",  " + startId + "-" + endId + ", " + currentId);
            InsnList list = new InsnList();
            if (node.getOpcode() == Opcodes.GETFIELD) {
                // Remove field owner from stack
                list.add(new InsnNode(Opcodes.POP));
            }
            list.add(new VarInsnNode(getLoadOpcode(localVar), localVar.index));
            replacements.put(localVar.name, list);
        }
    }
    return replacements;
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) HashMap(java.util.HashMap) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) LocalVariableNode(org.objectweb.asm.tree.LocalVariableNode) BasicValue(org.objectweb.asm.tree.analysis.BasicValue)

Example 8 with LocalVariableNode

use of org.objectweb.asm.tree.LocalVariableNode in project evosuite by EvoSuite.

the class ReplaceVariable method getName.

private String getName(MethodNode mn, AbstractInsnNode node) throws VariableNotFoundException {
    if (node instanceof VarInsnNode) {
        LocalVariableNode var = getLocal(mn, node, ((VarInsnNode) node).var);
        return var.name;
    } else if (node instanceof FieldInsnNode) {
        return ((FieldInsnNode) node).name;
    } else if (node instanceof IincInsnNode) {
        IincInsnNode incNode = (IincInsnNode) node;
        LocalVariableNode var = getLocal(mn, node, incNode.var);
        return var.name;
    } else {
        throw new RuntimeException("Unknown variable node: " + node);
    }
}
Also used : IincInsnNode(org.objectweb.asm.tree.IincInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) LocalVariableNode(org.objectweb.asm.tree.LocalVariableNode)

Example 9 with LocalVariableNode

use of org.objectweb.asm.tree.LocalVariableNode in project evosuite by EvoSuite.

the class ReplaceVariable method getLocalReplacementsInc.

private Map<String, InsnList> getLocalReplacementsInc(MethodNode mn, String desc, IincInsnNode node, Frame frame) {
    Map<String, InsnList> replacements = new HashMap<String, InsnList>();
    int otherNum = -1;
    otherNum = node.var;
    int currentId = mn.instructions.indexOf(node);
    for (Object v : mn.localVariables) {
        LocalVariableNode localVar = (LocalVariableNode) v;
        int startId = mn.instructions.indexOf(localVar.start);
        int endId = mn.instructions.indexOf(localVar.end);
        logger.info("Checking local variable " + localVar.name + " of type " + localVar.desc + " at index " + localVar.index);
        if (!localVar.desc.equals(desc))
            logger.info("- Types do not match: " + localVar.name);
        if (localVar.index == otherNum)
            logger.info("- Replacement = original " + localVar.name);
        if (currentId < startId)
            logger.info("- Out of scope (start) " + localVar.name);
        if (currentId > endId)
            logger.info("- Out of scope (end) " + localVar.name);
        BasicValue newValue = (BasicValue) frame.getLocal(localVar.index);
        if (newValue == BasicValue.UNINITIALIZED_VALUE)
            logger.info("- Not initialized");
        if (localVar.desc.equals(desc) && localVar.index != otherNum && currentId >= startId && currentId <= endId && newValue != BasicValue.UNINITIALIZED_VALUE) {
            logger.info("Adding local variable " + localVar.name + " of type " + localVar.desc + " at index " + localVar.index);
            InsnList list = new InsnList();
            list.add(new IincInsnNode(localVar.index, node.incr));
            replacements.put(localVar.name, list);
        }
    }
    return replacements;
}
Also used : HashMap(java.util.HashMap) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) InsnList(org.objectweb.asm.tree.InsnList) LocalVariableNode(org.objectweb.asm.tree.LocalVariableNode) BasicValue(org.objectweb.asm.tree.analysis.BasicValue)

Example 10 with LocalVariableNode

use of org.objectweb.asm.tree.LocalVariableNode in project Random-Things by lumien231.

the class ClassTransformer method getNextIndex.

public int getNextIndex(MethodNode mn) {
    Iterator it = mn.localVariables.iterator();
    int max = 0;
    int next = 0;
    while (it.hasNext()) {
        LocalVariableNode var = (LocalVariableNode) it.next();
        int index = var.index;
        if (index >= max) {
            max = index;
            next = max + Type.getType(var.desc).getSize();
        }
    }
    return next;
}
Also used : Iterator(java.util.Iterator) LocalVariableNode(org.objectweb.asm.tree.LocalVariableNode)

Aggregations

LocalVariableNode (org.objectweb.asm.tree.LocalVariableNode)41 Label (org.objectweb.asm.Label)11 MethodNode (org.objectweb.asm.tree.MethodNode)9 Type (org.objectweb.asm.Type)8 LabelNode (org.objectweb.asm.tree.LabelNode)8 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)8 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)6 IincInsnNode (org.objectweb.asm.tree.IincInsnNode)6 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)5 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)5 InsnList (org.objectweb.asm.tree.InsnList)5 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)5 ClassNode (org.objectweb.asm.tree.ClassNode)4 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3 FieldNode (org.objectweb.asm.tree.FieldNode)3 FrameNode (org.objectweb.asm.tree.FrameNode)3 TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)3 NeverNullArgAnalyzerAdapter (edu.columbia.cs.psl.phosphor.instrumenter.analyzer.NeverNullArgAnalyzerAdapter)2 ControlTaintTagStack (edu.columbia.cs.psl.phosphor.struct.ControlTaintTagStack)2