Search in sources :

Example 6 with TableSwitchInsnNode

use of org.objectweb.asm.tree.TableSwitchInsnNode in project maple-ir by LLVM-but-worse.

the class TableSwitchInsnNodeSerializer method deserialize.

@Override
public TableSwitchInsnNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObject = (JsonObject) json;
    int min = jsonObject.get("min").getAsInt();
    int max = jsonObject.get("max").getAsInt();
    LabelNode dflt = context.deserialize(jsonObject.get("dflt"), LabelNode.class);
    List<LabelNode> labelList = context.deserialize(jsonObject.get("labels"), List.class);
    LabelNode[] labels = new LabelNode[labelList.size()];
    for (int i = 0; i < labels.length; i++) {
        labels[i] = labelList.get(i);
    }
    return new TableSwitchInsnNode(min, max, dflt, labels);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) TableSwitchInsnNode(org.objectweb.asm.tree.TableSwitchInsnNode) JsonObject(com.google.gson.JsonObject)

Example 7 with TableSwitchInsnNode

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

the class LCSAJsInstrumentation method analyze.

/* (non-Javadoc)
	 * @see org.evosuite.cfg.MethodInstrumentation#analyze(org.objectweb.asm.tree.MethodNode, org.jgrapht.Graph, java.lang.String, java.lang.String)
	 */
/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
// using external lib
@Override
public void analyze(ClassLoader classLoader, MethodNode mn, String className, String methodName, int access) {
    Queue<LCSAJ> lcsaj_queue = new LinkedList<LCSAJ>();
    HashSet<Integer> targets_reached = new HashSet<Integer>();
    AbstractInsnNode start = mn.instructions.getFirst();
    int startID = 0;
    // TODO: This should replace the hack below
    if (methodName.startsWith("<init>")) {
        Iterator<AbstractInsnNode> j = mn.instructions.iterator();
        boolean constructorInvoked = false;
        while (j.hasNext()) {
            AbstractInsnNode in = j.next();
            startID++;
            if (!constructorInvoked) {
                if (in.getOpcode() == Opcodes.INVOKESPECIAL) {
                    MethodInsnNode cn = (MethodInsnNode) in;
                    Collection<String> superClasses = DependencyAnalysis.getInheritanceTree().getSuperclasses(className);
                    superClasses.add(className);
                    String classNameWithDots = ResourceList.getClassNameFromResourcePath(cn.owner);
                    if (superClasses.contains(classNameWithDots)) {
                        constructorInvoked = true;
                        break;
                    }
                } else {
                    continue;
                }
            }
        }
    }
    /*
		if (methodName.startsWith("<init>")) {
			if (mn.instructions.size() >= 4) {
				start = mn.instructions.get(4);
				startID = 4;
			}
		}
		*/
    LCSAJ a = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, startID, start));
    lcsaj_queue.add(a);
    targets_reached.add(0);
    ArrayList<TryCatchBlockNode> tc_blocks = (ArrayList<TryCatchBlockNode>) mn.tryCatchBlocks;
    for (TryCatchBlockNode t : tc_blocks) {
        LCSAJ b = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, mn.instructions.indexOf(t.handler), t.handler));
        lcsaj_queue.add(b);
    }
    while (!lcsaj_queue.isEmpty()) {
        LCSAJ currentLCSAJ = lcsaj_queue.poll();
        int position = mn.instructions.indexOf(currentLCSAJ.getLastNodeAccessed());
        // go to next bytecode instruction
        position++;
        if (position >= mn.instructions.size()) {
            // New LCSAJ for current + return
            LCSAJPool.add_lcsaj(className, methodName, currentLCSAJ);
            continue;
        }
        AbstractInsnNode next = mn.instructions.get(position);
        currentLCSAJ.lookupInstruction(position, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, position, next));
        if (next instanceof JumpInsnNode) {
            JumpInsnNode jump = (JumpInsnNode) next;
            // New LCSAJ for current + jump to target
            LCSAJPool.add_lcsaj(className, methodName, currentLCSAJ);
            LabelNode target = jump.label;
            int targetPosition = mn.instructions.indexOf(target);
            if (jump.getOpcode() != Opcodes.GOTO) {
                LCSAJ copy = new LCSAJ(currentLCSAJ);
                lcsaj_queue.add(copy);
            }
            if (!targets_reached.contains(targetPosition)) {
                LCSAJ c = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, targetPosition, target));
                lcsaj_queue.add(c);
                targets_reached.add(targetPosition);
            }
        } else if (next instanceof TableSwitchInsnNode) {
            TableSwitchInsnNode tswitch = (TableSwitchInsnNode) next;
            List<LabelNode> allTargets = tswitch.labels;
            for (LabelNode target : allTargets) {
                int targetPosition = mn.instructions.indexOf(target);
                if (!targets_reached.contains(targetPosition)) {
                    LCSAJ b = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, targetPosition, target));
                    lcsaj_queue.add(b);
                    targets_reached.add(targetPosition);
                }
            }
        } else if (next instanceof InsnNode) {
            InsnNode insn = (InsnNode) next;
            // New LCSAJ for current + throw / return
            if (insn.getOpcode() == Opcodes.ATHROW || insn.getOpcode() == Opcodes.RETURN || insn.getOpcode() == Opcodes.ARETURN || insn.getOpcode() == Opcodes.IRETURN || insn.getOpcode() == Opcodes.DRETURN || insn.getOpcode() == Opcodes.LRETURN || insn.getOpcode() == Opcodes.FRETURN) {
                LCSAJPool.add_lcsaj(className, methodName, currentLCSAJ);
            } else
                lcsaj_queue.add(currentLCSAJ);
        } else
            lcsaj_queue.add(currentLCSAJ);
    }
    if (Properties.STRATEGY != Strategy.EVOSUITE)
        addInstrumentation(classLoader, mn, className, methodName);
// if (Properties.WRITE_CFG)
// for (LCSAJ l : LCSAJPool.getLCSAJs(className, methodName)) {
// LCSAJGraph graph = new LCSAJGraph(l, false);
// String graphDestination = "evosuite-graphs/LCSAJGraphs/" + className
// + "/" + methodName;
// File dir = new File(graphDestination);
// if (dir.mkdirs())
// graph.generate(new File(graphDestination + "/LCSAJGraph no: "
// + l.getID() + ".dot"));
// else if (dir.exists())
// graph.generate(new File(graphDestination + "/LCSAJGraph no: "
// + l.getID() + ".dot"));
// }
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) TableSwitchInsnNode(org.objectweb.asm.tree.TableSwitchInsnNode) ArrayList(java.util.ArrayList) LCSAJ(org.evosuite.coverage.lcsaj.LCSAJ) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) LinkedList(java.util.LinkedList) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TableSwitchInsnNode(org.objectweb.asm.tree.TableSwitchInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) InsnList(org.objectweb.asm.tree.InsnList) List(java.util.List) ResourceList(org.evosuite.classpath.ResourceList) HashSet(java.util.HashSet)

Aggregations

TableSwitchInsnNode (org.objectweb.asm.tree.TableSwitchInsnNode)7 LabelNode (org.objectweb.asm.tree.LabelNode)6 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)3 LookupSwitchInsnNode (org.objectweb.asm.tree.LookupSwitchInsnNode)3 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)3 ArrayList (java.util.ArrayList)2 IincInsnNode (org.objectweb.asm.tree.IincInsnNode)2 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)2 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)2 MultiANewArrayInsnNode (org.objectweb.asm.tree.MultiANewArrayInsnNode)2 JsonObject (com.google.gson.JsonObject)1 ArrayDeque (java.util.ArrayDeque)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ResourceList (org.evosuite.classpath.ResourceList)1 LCSAJ (org.evosuite.coverage.lcsaj.LCSAJ)1 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)1 FrameNode (org.objectweb.asm.tree.FrameNode)1 InsnList (org.objectweb.asm.tree.InsnList)1