Search in sources :

Example 61 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project Engine by VoltzEngine-Project.

the class InstructionComparator method insnListMatchesL.

private static InsnListSection insnListMatchesL(InsnList haystack, InsnList needle, int start, HashSet<LabelNode> controlFlowLabels) {
    int h = start, n = 0;
    for (; h < haystack.size() && n < needle.size(); h++) {
        AbstractInsnNode insn = haystack.get(h);
        if (insn.getType() == 15) {
            continue;
        }
        if (insn.getType() == 8 && !controlFlowLabels.contains(insn)) {
            continue;
        }
        if (!insnEqual(haystack.get(h), needle.get(n))) {
            return null;
        }
        n++;
    }
    if (n != needle.size()) {
        return null;
    }
    return new InsnListSection(haystack, start, h - 1);
}
Also used : AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 62 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project Engine by VoltzEngine-Project.

the class InstructionComparator method insnListFindL.

public static List<InsnListSection> insnListFindL(InsnList haystack, InsnList needle) {
    HashSet<LabelNode> controlFlowLabels = new HashSet<>();
    for (AbstractInsnNode insn = haystack.getFirst(); insn != null; insn = insn.getNext()) {
        switch(insn.getType()) {
            case 8:
            case 15:
                break;
            case 7:
                JumpInsnNode jinsn = (JumpInsnNode) insn;
                controlFlowLabels.add(jinsn.label);
                break;
            case 11:
                TableSwitchInsnNode tsinsn = (TableSwitchInsnNode) insn;
                for (LabelNode label : tsinsn.labels) {
                    controlFlowLabels.add(label);
                }
                break;
            case 12:
                LookupSwitchInsnNode lsinsn = (LookupSwitchInsnNode) insn;
                for (LabelNode label : lsinsn.labels) {
                    controlFlowLabels.add(label);
                }
                break;
        }
    }
    LinkedList<InsnListSection> list = new LinkedList<>();
    nextsection: for (int start = 0; start <= haystack.size() - needle.size(); start++) {
        InsnListSection section = insnListMatchesL(haystack, needle, start, controlFlowLabels);
        if (section != null) {
            for (InsnListSection asection : list) {
                if (asection.last == section.last) {
                    continue nextsection;
                }
            }
            list.add(section);
        }
    }
    return list;
}
Also used : AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 63 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project robolectric by robolectric.

the class ClassInstrumentor method rewriteMethodBody.

/**
 * Filters methods that might need special treatment because of various reasons
 */
private void rewriteMethodBody(MutableClass mutableClass, MethodNode callingMethod) {
    ListIterator<AbstractInsnNode> instructions = callingMethod.instructions.iterator();
    while (instructions.hasNext()) {
        AbstractInsnNode node = instructions.next();
        switch(node.getOpcode()) {
            case Opcodes.NEW:
                TypeInsnNode newInsnNode = (TypeInsnNode) node;
                newInsnNode.desc = mutableClass.config.mappedTypeName(newInsnNode.desc);
                break;
            case Opcodes.GETFIELD:
            /* falls through */
            case Opcodes.PUTFIELD:
            /* falls through */
            case Opcodes.GETSTATIC:
            /* falls through */
            case Opcodes.PUTSTATIC:
                FieldInsnNode fieldInsnNode = (FieldInsnNode) node;
                // todo test
                fieldInsnNode.desc = mutableClass.config.mappedTypeName(fieldInsnNode.desc);
                break;
            case Opcodes.INVOKESTATIC:
            /* falls through */
            case Opcodes.INVOKEINTERFACE:
            /* falls through */
            case Opcodes.INVOKESPECIAL:
            /* falls through */
            case Opcodes.INVOKEVIRTUAL:
                MethodInsnNode targetMethod = (MethodInsnNode) node;
                targetMethod.desc = mutableClass.config.remapParams(targetMethod.desc);
                if (isGregorianCalendarBooleanConstructor(targetMethod)) {
                    replaceGregorianCalendarBooleanConstructor(instructions, targetMethod);
                } else if (mutableClass.config.shouldIntercept(targetMethod)) {
                    interceptInvokeVirtualMethod(mutableClass, instructions, targetMethod);
                }
                break;
            case Opcodes.INVOKEDYNAMIC:
                /* no unusual behavior */
                break;
            default:
                break;
        }
    }
}
Also used : MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 64 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project bytecode-viewer by Konloch.

the class RegexInsnFinder method findAllGroups.

/**
 * Searches for a regex in the instruction list and returns all groups for
 * all matches.
 *
 * @param regex the regular expression
 * @return a list with all sets of groups with matching instructions
 */
public List<AbstractInsnNode[][]> findAllGroups(final String regex) {
    final List<AbstractInsnNode[][]> results = new ArrayList<>();
    try {
        final Matcher regexMatcher = Pattern.compile(processRegex(regex), Pattern.MULTILINE).matcher(insnString);
        if (regexMatcher.find()) {
            final AbstractInsnNode[][] result = new AbstractInsnNode[regexMatcher.groupCount() + 1][0];
            for (int i = 0; i <= regexMatcher.groupCount(); i++) {
                result[i] = makeResult(regexMatcher.start(i), regexMatcher.end(i));
            }
            results.add(result);
        }
    } catch (final PatternSyntaxException ex) {
        BytecodeViewer.handleException(ex);
    }
    return results;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 65 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project bytecode-viewer by Konloch.

the class RegexInsnFinder method refresh.

/**
 * Refreshes the internal instruction list when you have made changes to the
 * method.
 */
public void refresh() {
    origInstructions = cleanInsn(mn.instructions);
    final List<AbstractInsnNode> il = new ArrayList<>();
    for (final AbstractInsnNode ain : mn.instructions.toArray()) if (ain.getOpcode() >= 0) {
        il.add(ain);
    }
    AbstractInsnNode[] instructions = il.toArray(new AbstractInsnNode[0]);
    offsets = new int[instructions.length];
    StringBuilder insnStringBuilder = new StringBuilder();
    for (int i = 0; i < instructions.length; i++) {
        offsets[i] = -1;
        final AbstractInsnNode ain = instructions[i];
        if (ain.getOpcode() >= 0) {
            if (ain.getOpcode() >= opcodes.length) {
                try {
                    throw new UnexpectedException("Unknown opcode encountered: " + ain.getOpcode());
                } catch (final UnexpectedException e) {
                    BytecodeViewer.handleException(e);
                }
            }
            offsets[i] = insnStringBuilder.length();
            insnStringBuilder.append(opcodes[ain.getOpcode()]);
            insnStringBuilder = new StringBuilder(getInsString(ain));
            insnStringBuilder.append(" ");
        }
    }
    insnString = insnStringBuilder.toString();
}
Also used : UnexpectedException(java.rmi.UnexpectedException) ArrayList(java.util.ArrayList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Aggregations

AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)185 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)68 MethodNode (org.objectweb.asm.tree.MethodNode)54 InsnList (org.objectweb.asm.tree.InsnList)53 InsnNode (org.objectweb.asm.tree.InsnNode)41 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)38 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)37 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)35 ClassNode (org.objectweb.asm.tree.ClassNode)33 LabelNode (org.objectweb.asm.tree.LabelNode)27 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)26 Test (org.junit.Test)25 Label (org.objectweb.asm.Label)25 ClassReader (org.objectweb.asm.ClassReader)23 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)23 HashSet (java.util.HashSet)16 Type (org.objectweb.asm.Type)15 Frame (org.objectweb.asm.tree.analysis.Frame)13 ArrayList (java.util.ArrayList)12 TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)11