Java Code Examples for org.objectweb.asm.tree.InsnList#getFirst()

The following examples show how to use org.objectweb.asm.tree.InsnList#getFirst() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilterUtil.java    From diff-check with GNU Lesser General Public License v2.1 7 votes vote down vote up
public static List<LineNumberNodeWrapper> collectLineNumberNodeList(InsnList instructions) {
    List<LineNumberNodeWrapper> list = new ArrayList<>();
    AbstractInsnNode node = instructions.getFirst();
    while (node != instructions.getLast()) {
        if (node instanceof LineNumberNode) {
            if (CollectionUtils.isNotEmpty(list)) {
                list.get(list.size() - 1).setNext(node);
            }
            list.add(new LineNumberNodeWrapper(LineNumberNode.class.cast(node)));
        }
        node = node.getNext();
    }
    if (CollectionUtils.isNotEmpty(list)) {
        list.get(list.size() - 1).setNext(instructions.getLast());
    }
    return list;
}
 
Example 2
Source File: InstructionComparator.java    From NOVA-Core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static InsnList getImportantList(InsnList list) {
	if (list.size() == 0) {
		return list;
	}

	HashMap<LabelNode, LabelNode> labels = new HashMap<LabelNode, LabelNode>();
	for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn instanceof LabelNode) {
			labels.put((LabelNode) insn, (LabelNode) insn);
		}
	}

	InsnList importantNodeList = new InsnList();
	for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn instanceof LabelNode || insn instanceof LineNumberNode) {
			continue;
		}

		importantNodeList.add(insn.clone(labels));
	}
	return importantNodeList;
}
 
Example 3
Source File: ASMMethodVariables.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public void initLocalVariables(final InsnList instructions) {
    // find enter & exit instruction.
    final LabelNode variableStartLabelNode = new LabelNode();
    final LabelNode variableEndLabelNode = new LabelNode();
    if(instructions.getFirst() != null) {
        instructions.insertBefore(instructions.getFirst(), variableStartLabelNode);
    } else {
        instructions.insert(variableStartLabelNode);
    }
    instructions.insert(instructions.getLast(), variableEndLabelNode);

    if (!isStatic()) {
        addLocalVariable("this", Type.getObjectType(this.declaringClassInternalName).getDescriptor(), variableStartLabelNode, variableEndLabelNode);
    }

    for (Type type : this.argumentTypes) {
        addLocalVariable(JavaAssistUtils.javaClassNameToVariableName(type.getClassName()), type.getDescriptor(), variableStartLabelNode, variableEndLabelNode);
    }
}
 
Example 4
Source File: InsnListPrinter.java    From NOVA-Core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void visitInsnList(InsnList list) {
	text.clear();
	if (labelNames == null) {
		labelNames = new HashMap<Label, String>();
	} else {
		labelNames.clear();
	}

	buildingLabelMap = true;
	for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn.getType() == 8) {
			visitLabel(((LabelNode) insn).getLabel());
		}
	}

	text.clear();
	buildingLabelMap = false;

	for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
		_visitInsn(insn);
	}
}
 
Example 5
Source File: InstructionComparator.java    From NOVA-Core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static InsnList getImportantList(InsnList list) {
	if (list.size() == 0) {
		return list;
	}

	HashMap<LabelNode, LabelNode> labels = new HashMap<LabelNode, LabelNode>();
	for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn instanceof LabelNode) {
			labels.put((LabelNode) insn, (LabelNode) insn);
		}
	}

	InsnList importantNodeList = new InsnList();
	for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn instanceof LabelNode || insn instanceof LineNumberNode) {
			continue;
		}

		importantNodeList.add(insn.clone(labels));
	}
	return importantNodeList;
}
 
Example 6
Source File: ClinitCutter.java    From zelixkiller with GNU General Public License v3.0 6 votes vote down vote up
public static AbstractInsnNode findEndLabel(InsnList insns) {
	AbstractInsnNode ain = insns.getFirst();
	while (ain != null) {
		if (ain.getOpcode() == GOTO && ain.getPrevious() != null
				&& (blockContainsSetter(ain.getPrevious()) || ain.getPrevious().getOpcode() == ASTORE)) {
			return ((JumpInsnNode) ain).label;
		}
		ain = ain.getNext();
	}
	ain = insns.getLast();
	while (ain != null) {
		if (ain.getOpcode() == IF_ICMPGE && ain.getPrevious() != null && (ain.getPrevious().getOpcode() == ILOAD)) {
			return ((JumpInsnNode) ain).label;
		}
		ain = ain.getPrevious();
	}
	throw new RuntimeException();
}
 
Example 7
Source File: InsnListPrinter.java    From NOVA-Core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void visitInsnList(InsnList list) {
	text.clear();
	if (labelNames == null) {
		labelNames = new HashMap<Label, String>();
	} else {
		labelNames.clear();
	}

	buildingLabelMap = true;
	for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn.getType() == 8) {
			visitLabel(((LabelNode) insn).getLabel());
		}
	}

	text.clear();
	buildingLabelMap = false;

	for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
		_visitInsn(insn);
	}
}
 
Example 8
Source File: InstructionComparator.java    From NOVA-Core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static InsnList getImportantList(InsnList list) {
	if (list.size() == 0) {
		return list;
	}

	HashMap<LabelNode, LabelNode> labels = new HashMap<LabelNode, LabelNode>();
	for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn instanceof LabelNode) {
			labels.put((LabelNode) insn, (LabelNode) insn);
		}
	}

	InsnList importantNodeList = new InsnList();
	for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn instanceof LabelNode || insn instanceof LineNumberNode) {
			continue;
		}

		importantNodeList.add(insn.clone(labels));
	}
	return importantNodeList;
}
 
Example 9
Source File: ASMHelper.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static InsnList cloneInsnList(Map<LabelNode, LabelNode> labelMap, InsnList insns) {
	InsnList clone = new InsnList();
	for (AbstractInsnNode insn = insns.getFirst(); insn != null; insn = insn.getNext()) {
		clone.add(insn.clone(labelMap));
	}

	return clone;
}
 
Example 10
Source File: ASMHelper.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static InsnList cloneInsnList(Map<LabelNode, LabelNode> labelMap, InsnList insns) {
	InsnList clone = new InsnList();
	for (AbstractInsnNode insn = insns.getFirst(); insn != null; insn = insn.getNext()) {
		clone.add(insn.clone(labelMap));
	}

	return clone;
}
 
Example 11
Source File: ASMHelper.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Map<LabelNode, LabelNode> cloneLabels(InsnList insns) {
	HashMap<LabelNode, LabelNode> labelMap = new HashMap<LabelNode, LabelNode>();
	for (AbstractInsnNode insn = insns.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn.getType() == 8) {
			labelMap.put((LabelNode) insn, new LabelNode());
		}
	}
	return labelMap;
}
 
Example 12
Source File: ASMHelper.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static InsnList cloneInsnList(Map<LabelNode, LabelNode> labelMap, InsnList insns) {
	InsnList clone = new InsnList();
	for (AbstractInsnNode insn = insns.getFirst(); insn != null; insn = insn.getNext()) {
		clone.add(insn.clone(labelMap));
	}

	return clone;
}
 
Example 13
Source File: MethodUtils.java    From zelixkiller with GNU General Public License v3.0 5 votes vote down vote up
/**
 * copy InsnList start included, end excluded
 * 
 * @param list
 * @param start
 * @param end
 * @return
 */
public static InsnList copy(InsnList list, AbstractInsnNode start, AbstractInsnNode end) {
	InsnList newList = new InsnList();
	Map<LabelNode, LabelNode> labelMap = getLabelMap(list);
	AbstractInsnNode ain = start == null ? list.getFirst() : start;
	while (ain != null && ain != end) {
		newList.add(ain.clone(labelMap));
		ain = ain.getNext();
	}
	return newList;
}
 
Example 14
Source File: InsnUtils.java    From zelixkiller with GNU General Public License v3.0 5 votes vote down vote up
public static AbstractInsnNode findFirst(InsnList il, int op) {
	AbstractInsnNode ain = il.getFirst();
	while (ain != null) {
		if (ain.getOpcode() == op) {
			return ain;
		}
		ain = ain.getNext();
	}
	return null;
}
 
Example 15
Source File: InsnUtils.java    From zelixkiller with GNU General Public License v3.0 5 votes vote down vote up
public static boolean callsRef(InsnList il) {
	AbstractInsnNode ain = il.getFirst();
	while (ain != null) {
		if (ain instanceof MethodInsnNode || ain instanceof FieldInsnNode) {
			return true;
		}
		ain = ain.getNext();
	}
	return false;
}
 
Example 16
Source File: InsnUtils.java    From zelixkiller with GNU General Public License v3.0 5 votes vote down vote up
public static boolean callsField(InsnList il) {
	AbstractInsnNode ain = il.getFirst();
	while (ain != null) {
		if (ain instanceof MethodInsnNode) {
			return true;
		}
		ain = ain.getNext();
	}
	return false;
}
 
Example 17
Source File: ASMHelper.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Map<LabelNode, LabelNode> cloneLabels(InsnList insns) {
	HashMap<LabelNode, LabelNode> labelMap = new HashMap<LabelNode, LabelNode>();
	for (AbstractInsnNode insn = insns.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn.getType() == 8) {
			labelMap.put((LabelNode) insn, new LabelNode());
		}
	}
	return labelMap;
}
 
Example 18
Source File: FieldGetterDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private static Map<String, String> checkMethods(ClassNode classNode, Set<String> names) {
    Map<String, String> validGetters = Maps.newHashMap();
    @SuppressWarnings("rawtypes")
    List methods = classNode.methods;
    String fieldName = null;
    checkMethod:
    for (Object methodObject : methods) {
        MethodNode method = (MethodNode) methodObject;
        if (names.contains(method.name)
                && method.desc.startsWith("()")) { //$NON-NLS-1$ // (): No arguments
            InsnList instructions = method.instructions;
            int mState = 1;
            for (AbstractInsnNode curr = instructions.getFirst();
                    curr != null;
                    curr = curr.getNext()) {
                switch (curr.getOpcode()) {
                    case -1:
                        // Skip label and line number nodes
                        continue;
                    case Opcodes.ALOAD:
                        if (mState == 1) {
                            fieldName = null;
                            mState = 2;
                        } else {
                            continue checkMethod;
                        }
                        break;
                    case Opcodes.GETFIELD:
                        if (mState == 2) {
                            FieldInsnNode field = (FieldInsnNode) curr;
                            fieldName = field.name;
                            mState = 3;
                        } else {
                            continue checkMethod;
                        }
                        break;
                    case Opcodes.ARETURN:
                    case Opcodes.FRETURN:
                    case Opcodes.IRETURN:
                    case Opcodes.DRETURN:
                    case Opcodes.LRETURN:
                    case Opcodes.RETURN:
                        if (mState == 3) {
                            validGetters.put(method.name, fieldName);
                        }
                        continue checkMethod;
                    default:
                        continue checkMethod;
                }
            }
        }
    }

    return validGetters;
}
 
Example 19
Source File: BlockSplitter.java    From radon with GNU General Public License v3.0 4 votes vote down vote up
private static void doSplit(MethodNode methodNode, AtomicInteger counter, int callStackSize) {
    InsnList insns = methodNode.instructions;

    if (insns.size() > 10 && callStackSize < LIMIT_SIZE) {
        LabelNode p1 = new LabelNode();
        LabelNode p2 = new LabelNode();

        AbstractInsnNode p2Start = insns.get((insns.size() - 1) / 2);
        AbstractInsnNode p2End = insns.getLast();

        AbstractInsnNode p1Start = insns.getFirst();

        // We can't have trap ranges mutilated by block splitting
        if (methodNode.tryCatchBlocks.stream().anyMatch(tcbn ->
                insns.indexOf(tcbn.end) >= insns.indexOf(p2Start)
                        && insns.indexOf(tcbn.start) <= insns.indexOf(p2Start)))
            return;

        ArrayList<AbstractInsnNode> insnNodes = new ArrayList<>();
        AbstractInsnNode currentInsn = p1Start;

        InsnList p1Block = new InsnList();

        while (currentInsn != p2Start) {
            insnNodes.add(currentInsn);

            currentInsn = currentInsn.getNext();
        }

        insnNodes.forEach(insn -> {
            insns.remove(insn);
            p1Block.add(insn);
        });

        p1Block.insert(p1);
        p1Block.add(new JumpInsnNode(GOTO, p2));

        insns.insert(p2End, p1Block);
        insns.insertBefore(p2Start, new JumpInsnNode(GOTO, p1));
        insns.insertBefore(p2Start, p2);

        counter.incrementAndGet();

        // We might have messed up variable ranges when rearranging the block order.
        if (methodNode.localVariables != null)
            new ArrayList<>(methodNode.localVariables).stream().filter(lvn ->
                    insns.indexOf(lvn.end) < insns.indexOf(lvn.start)
            ).forEach(methodNode.localVariables::remove);

        doSplit(methodNode, counter, callStackSize + 1);
    }
}
 
Example 20
Source File: BogusJumpInserter.java    From radon with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Generates a generic "escape" pattern to avoid inserting multiple copies of the same bytecode instructions.
 *
 * @param methodNode the {@link MethodNode} we are inserting into.
 * @return a {@link LabelNode} which "escapes" all other flow.
 */
private static LabelNode exitLabel(MethodNode methodNode) {
    LabelNode lb = new LabelNode();
    LabelNode escapeNode = new LabelNode();

    InsnList insns = methodNode.instructions;
    AbstractInsnNode target = insns.getFirst();

    insns.insertBefore(target, new JumpInsnNode(GOTO, escapeNode));
    insns.insertBefore(target, lb);

    switch (Type.getReturnType(methodNode.desc).getSort()) {
        case Type.VOID:
            insns.insertBefore(target, new InsnNode(RETURN));
            break;
        case Type.BOOLEAN:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils.getRandomInt(2)));
            insns.insertBefore(target, new InsnNode(IRETURN));
            break;
        case Type.CHAR:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils
                    .getRandomInt(Character.MAX_VALUE + 1)));
            insns.insertBefore(target, new InsnNode(IRETURN));
            break;
        case Type.BYTE:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils.getRandomInt(Byte.MAX_VALUE + 1)));
            insns.insertBefore(target, new InsnNode(IRETURN));
            break;
        case Type.SHORT:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils.getRandomInt(Short.MAX_VALUE + 1)));
            insns.insertBefore(target, new InsnNode(IRETURN));
            break;
        case Type.INT:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils.getRandomInt()));
            insns.insertBefore(target, new InsnNode(IRETURN));
            break;
        case Type.LONG:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils.getRandomLong()));
            insns.insertBefore(target, new InsnNode(LRETURN));
            break;
        case Type.FLOAT:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils.getRandomFloat()));
            insns.insertBefore(target, new InsnNode(FRETURN));
            break;
        case Type.DOUBLE:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils.getRandomDouble()));
            insns.insertBefore(target, new InsnNode(DRETURN));
            break;
        default:
            insns.insertBefore(target, new InsnNode(ACONST_NULL));
            insns.insertBefore(target, new InsnNode(ARETURN));
            break;
    }
    insns.insertBefore(target, escapeNode);

    return lb;
}