Search in sources :

Example 1 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class SpecificMethodNodeTransformer method instructionsInserter.

public static SpecificMethodNodeTransformer instructionsInserter(String target, int priority, Function<AbstractInsnNode, Pair<InsnList, Boolean>> inserter) {
    return instructionsTransformer(priority, target, instructions -> {
        for (AbstractInsnNode node : instructions.toArray()) {
            Pair<InsnList, Boolean> pair = inserter.apply(node);
            if (pair != null)
                if (pair.getRight())
                    instructions.insertBefore(node, pair.getLeft());
                else
                    instructions.insert(node, pair.getLeft());
        }
    });
}
Also used : AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList)

Example 2 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project Minechem by iopleke.

the class MinechemTransformer method injectBytes.

private byte[] injectBytes(Method method, InstructionNode instructionNode, byte[] data) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(data);
    classReader.accept(classNode, ClassReader.EXPAND_FRAMES);
    MethodNode methodNode = getMethodByName(classNode, method);
    AbstractInsnNode pos = findInstructionNode(instructionNode, methodNode);
    if (instructionNode.replace) {
        methodNode.instructions.insertBefore(pos, instructionNode.getInsnList());
        methodNode.instructions.remove(pos);
    } else {
        methodNode.instructions.insertBefore(pos, instructionNode.getInsnList());
    }
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) ClassReader(org.objectweb.asm.ClassReader) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) ClassWriter(org.objectweb.asm.ClassWriter)

Example 3 with AbstractInsnNode

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

the class SandboxClassLoader method box.

public static void box(final Type type, ListIterator<AbstractInsnNode> instructions) {
    if (type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY) {
        return;
    }
    if (type == Type.VOID_TYPE) {
        instructions.add(new InsnNode(ACONST_NULL));
    } else {
        Type boxed = getBoxedType(type);
        instructions.add(new TypeInsnNode(NEW, boxed.getInternalName()));
        if (type.getSize() == 2) {
            // Pp -> Ppo -> oPpo -> ooPpo -> ooPp -> o
            instructions.add(new InsnNode(DUP_X2));
            instructions.add(new InsnNode(DUP_X2));
            instructions.add(new InsnNode(POP));
        } else {
            // p -> po -> opo -> oop -> o
            instructions.add(new InsnNode(DUP_X1));
            instructions.add(new InsnNode(SWAP));
        }
        instructions.add(new MethodInsnNode(INVOKESPECIAL, boxed.getInternalName(), "<init>", "(" + type.getDescriptor() + ")V"));
    }
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InvokeDynamicInsnNode(org.objectweb.asm.tree.InvokeDynamicInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Type(org.objectweb.asm.Type) MethodType.methodType(java.lang.invoke.MethodType.methodType) MethodType(java.lang.invoke.MethodType) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode)

Example 4 with AbstractInsnNode

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

the class CodeSequenceDiagram method execute.

@Override
public void execute(ArrayList<ClassNode> classNodeList) {
    if (BytecodeViewer.viewer.workPane.getCurrentViewer() == null || !(BytecodeViewer.viewer.workPane.getCurrentViewer() instanceof ClassViewer)) {
        BytecodeViewer.showMessage("First open a class file.");
        return;
    }
    ClassNode c = BytecodeViewer.viewer.workPane.getCurrentViewer().cn;
    if (c == null) {
        BytecodeViewer.showMessage("ClassNode is null for CodeSequenceDiagram. Please report to @Konloch");
        return;
    }
    JFrame frame = null;
    if (c.name != null)
        frame = new JFrame("Code Sequence Diagram - " + c.name);
    else
        frame = new JFrame("Code Sequence Diagram - Unknown Name");
    frame.setIconImages(Resources.iconList);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(400, 320);
    mxGraph graph = new mxGraph();
    graph.setVertexLabelsMovable(false);
    graph.setGridEnabled(true);
    graph.setEnabled(false);
    graph.setCellsEditable(false);
    graph.setCellsSelectable(false);
    graph.setCellsMovable(false);
    graph.setCellsLocked(true);
    Object parent = graph.getDefaultParent();
    Font font = UIManager.getDefaults().getFont("TabbedPane.font");
    AffineTransform affinetransform = new AffineTransform();
    FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
    graph.getModel().beginUpdate();
    try {
        int testX = 10;
        int testY = 0;
        double magicNumber = 5.8;
        for (MethodNode m : (ArrayList<MethodNode>) c.methods) {
            String mIdentifier = c.name + "." + m.name + m.desc;
            Object node = graph.insertVertex(parent, null, mIdentifier, testX, testY, mIdentifier.length() * magicNumber, 30);
            Object attach = node;
            testX += (int) (font.getStringBounds(mIdentifier, frc).getWidth()) + 60;
            for (AbstractInsnNode i : m.instructions.toArray()) {
                if (i instanceof MethodInsnNode) {
                    MethodInsnNode mi = (MethodInsnNode) i;
                    String identifier = mi.owner + "." + mi.name + mi.desc;
                    Object node2 = graph.insertVertex(parent, null, identifier, testX, testY, identifier.length() * 5, 30);
                    testX += (int) (font.getStringBounds(identifier, frc).getWidth()) + 60;
                    graph.insertEdge(parent, null, null, attach, node2);
                    attach = node2;
                }
            }
            testY += 60;
            testX = 10;
        }
    } finally {
        graph.getModel().endUpdate();
    }
    mxGraphComponent graphComponent = new mxGraphComponent(graph);
    frame.getContentPane().add(graphComponent);
    frame.setVisible(true);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ArrayList(java.util.ArrayList) com.mxgraph.view.mxGraph(com.mxgraph.view.mxGraph) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) AffineTransform(java.awt.geom.AffineTransform) ClassViewer(the.bytecode.club.bytecodeviewer.gui.ClassViewer) FontRenderContext(java.awt.font.FontRenderContext) com.mxgraph.swing.mxGraphComponent(com.mxgraph.swing.mxGraphComponent)

Example 5 with AbstractInsnNode

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

the class Block method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder(headerString()).append(String.format(" (len=%d)", size()));
    if (PRINT_INSNS && size() > 0) {
        sb.append(System.lineSeparator());
        Iterator<AbstractInsnNode> it = instructions.iterator();
        while (it.hasNext()) {
            AbstractInsnNode ain = it.next();
            if (!(ain instanceof LabelNode)) {
                if (it.hasNext()) {
                    sb.append(String.format("   %s%n", Assembly.toString(ain)));
                } else {
                    sb.append(String.format("   %s", Assembly.toString(ain)));
                }
            }
        }
    }
    sb.append(System.lineSeparator());
    for (Block b : preds) {
        sb.append("   pred: ").append(b.headerString()).append(System.lineSeparator());
    }
    return sb.toString();
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) 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