Search in sources :

Example 46 with AbstractInsnNode

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

the class ClassTransformer method patchVillageChurch.

private byte[] patchVillageChurch(byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    logger.log(Level.DEBUG, "Found VillagePiece Church Class: " + classNode.name);
    MethodNode addComponentParts = null;
    for (MethodNode mn : classNode.methods) {
        if (mn.name.equals(MCPNames.method("func_74875_a"))) {
            addComponentParts = mn;
        }
    }
    if (addComponentParts != null) {
        logger.log(Level.DEBUG, " - Found addComponentParts");
        for (int i = 0; i < addComponentParts.instructions.size(); i++) {
            AbstractInsnNode ain = addComponentParts.instructions.get(i);
            if (ain instanceof InsnNode) {
                InsnNode in = (InsnNode) ain;
                if (in.getOpcode() == Opcodes.IRETURN) {
                    logger.log(Level.DEBUG, " - Patched addComponentParts");
                    AbstractInsnNode before = addComponentParts.instructions.get(i - 1);
                    InsnList toInsert = new InsnList();
                    toInsert.add(new VarInsnNode(Opcodes.ALOAD, 1));
                    toInsert.add(new VarInsnNode(Opcodes.ALOAD, 2));
                    toInsert.add(new VarInsnNode(Opcodes.ALOAD, 3));
                    toInsert.add(new VarInsnNode(Opcodes.ALOAD, 0));
                    toInsert.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "lumien/randomthings/worldgen/WorldGenPeaceCandle", "addComponentParts", "(Lnet/minecraft/world/World;Ljava/util/Random;Lnet/minecraft/world/gen/structure/StructureBoundingBox;Lnet/minecraft/world/gen/structure/StructureVillagePieces$Church;)V", false));
                    i += 5;
                    addComponentParts.instructions.insertBefore(before, toInsert);
                }
            }
        }
    }
    CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 47 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project openj9 by eclipse.

the class ClassFileCompare method compareInstructions.

private void compareInstructions(ClassNode clazz1, MethodNode method1, ClassNode clazz2, MethodNode method2) {
    if (nullCheck(method1.instructions, method2.instructions, "Instructions (" + method1.name + ")")) {
        return;
    }
    if (method1.instructions.size() != method2.instructions.size()) {
        reportDifference("Method " + method1.name + ": instructions differ: " + method1.instructions.size() + ", " + method2.instructions.size());
    } else {
        @SuppressWarnings("unchecked") Iterator<AbstractInsnNode> iter1 = method1.instructions.iterator();
        @SuppressWarnings("unchecked") Iterator<AbstractInsnNode> iter2 = method2.instructions.iterator();
        int index = 0;
        while (iter1.hasNext()) {
            AbstractInsnNode instruction1 = iter1.next();
            AbstractInsnNode instruction2 = iter2.next();
            if (instruction1.getOpcode() != instruction2.getOpcode()) {
                /* Check for J9 opcode mapping */
                compareJ9OpcodeMapping(clazz2, method2, instruction1, instruction2, index);
            } else {
                switch(instruction1.getType()) {
                    case INSN:
                        /* Do nothing */
                        break;
                    case INT_INSN:
                        compare(((IntInsnNode) instruction1).operand, ((IntInsnNode) instruction2).operand, "Operands of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")", false);
                        break;
                    case VAR_INSN:
                        compare(((VarInsnNode) instruction1).var, ((VarInsnNode) instruction2).var, "Operands of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")", false);
                        break;
                    case TYPE_INSN:
                        compare(((TypeInsnNode) instruction1).desc, ((TypeInsnNode) instruction2).desc, "Operands of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                        break;
                    case FIELD_INSN:
                        {
                            FieldInsnNode fieldInsn1 = (FieldInsnNode) instruction1;
                            FieldInsnNode fieldInsn2 = (FieldInsnNode) instruction2;
                            compare(fieldInsn1.owner, fieldInsn2.owner, "Owning class name of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                            compare(fieldInsn1.name, fieldInsn2.name, "Field name of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                            compare(fieldInsn1.desc, fieldInsn2.desc, "Descriptor of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                            break;
                        }
                    case METHOD_INSN:
                        {
                            MethodInsnNode methodInsn1 = (MethodInsnNode) instruction1;
                            MethodInsnNode methodInsn2 = (MethodInsnNode) instruction2;
                            compare(methodInsn1.owner, methodInsn2.owner, "Owning class name of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                            compare(methodInsn1.name, methodInsn2.name, "Method name of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                            compare(methodInsn1.desc, methodInsn2.desc, "Descriptor of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                            break;
                        }
                    case INVOKE_DYNAMIC_INSN:
                        compareInvokeDynamic((InvokeDynamicInsnNode) instruction1, (InvokeDynamicInsnNode) instruction2, method1, index);
                        break;
                    case JUMP_INSN:
                        compare(method1, ((JumpInsnNode) instruction1).label, method2, ((JumpInsnNode) instruction2).label, "Operands of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                        break;
                    case LABEL:
                        /* Do nothing */
                        break;
                    case LDC_INSN:
                        if (!((LdcInsnNode) instruction1).cst.equals(((LdcInsnNode) instruction2).cst)) {
                            reportDifference("Operands of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") differ: " + ((LdcInsnNode) instruction1).cst + ", " + ((LdcInsnNode) instruction2).cst);
                        }
                        break;
                    case IINC_INSN:
                        {
                            IincInsnNode iincInsn1 = (IincInsnNode) instruction1;
                            IincInsnNode iincInsn2 = (IincInsnNode) instruction2;
                            compare(iincInsn1.var, iincInsn2.var, "Variable operands of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")", false);
                            compare(iincInsn1.incr, iincInsn2.incr, "Increment operands of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")", false);
                            break;
                        }
                    case TABLESWITCH_INSN:
                        {
                            TableSwitchInsnNode tableSwitchInsn1 = (TableSwitchInsnNode) instruction1;
                            TableSwitchInsnNode tableSwitchInsn2 = (TableSwitchInsnNode) instruction2;
                            compare(tableSwitchInsn1.min, tableSwitchInsn2.min, Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") minimum key value", false);
                            compare(tableSwitchInsn1.max, tableSwitchInsn2.max, Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") maximum key value", false);
                            compare(method1, tableSwitchInsn1.dflt, method2, tableSwitchInsn2.dflt, Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") default_handler_block");
                            compareLabels(method1, tableSwitchInsn1.labels, method2, tableSwitchInsn2.labels, Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") handler_blocks");
                            break;
                        }
                    case LOOKUPSWITCH_INSN:
                        {
                            LookupSwitchInsnNode lookupSwitchInsn1 = (LookupSwitchInsnNode) instruction1;
                            LookupSwitchInsnNode lookupSwitchInsn2 = (LookupSwitchInsnNode) instruction2;
                            compare(method1, lookupSwitchInsn1.dflt, method2, lookupSwitchInsn2.dflt, Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") default_handler_block");
                            compare(lookupSwitchInsn1.keys, lookupSwitchInsn2.keys, Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") key values");
                            compareLabels(method1, lookupSwitchInsn1.labels, method2, lookupSwitchInsn2.labels, Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") handler_blocks");
                            break;
                        }
                    case MULTIANEWARRAY_INSN:
                        {
                            MultiANewArrayInsnNode arrayInsn1 = (MultiANewArrayInsnNode) instruction1;
                            MultiANewArrayInsnNode arrayInsn2 = (MultiANewArrayInsnNode) instruction2;
                            compare(arrayInsn1.desc, arrayInsn2.desc, "Desc operand of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                            compare(arrayInsn1.dims, arrayInsn2.dims, "Dimension operand of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")", false);
                            break;
                        }
                    case FRAME:
                        {
                            FrameNode frameInsn1 = (FrameNode) instruction1;
                            FrameNode frameInsn2 = (FrameNode) instruction2;
                            compare(frameInsn1.type, frameInsn2.type, "Stack map frame (" + method1.name + ":" + index + ") frame type", false);
                            compareFrames(frameInsn1.local, frameInsn2.local, "Stack map frame (" + method1.name + ":" + index + ") types of the local variables");
                            compareFrames(frameInsn1.stack, frameInsn2.stack, "Stack map frame (" + method1.name + ":" + index + ") types of the operand stack elements");
                        }
                        break;
                    case LINE:
                        {
                            assert shouldCompareDebugInfo;
                            LineNumberNode lineInsn1 = (LineNumberNode) instruction1;
                            LineNumberNode lineInsn2 = (LineNumberNode) instruction2;
                            compare(lineInsn1.line, lineInsn2.line, "Line number (" + method1.name + ":" + index + ") line No.", false);
                            compare(method1, lineInsn1.start, method2, lineInsn2.start, "Line number (" + method1.name + ":" + index + ") offset of the 1st instruction");
                            break;
                        }
                    default:
                        assert false;
                }
            }
            index++;
        }
    }
}
Also used : TableSwitchInsnNode(org.objectweb.asm.tree.TableSwitchInsnNode) FrameNode(org.objectweb.asm.tree.FrameNode) MultiANewArrayInsnNode(org.objectweb.asm.tree.MultiANewArrayInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) LineNumberNode(org.objectweb.asm.tree.LineNumberNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) LookupSwitchInsnNode(org.objectweb.asm.tree.LookupSwitchInsnNode)

Example 48 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project Galacticraft by micdoodle8.

the class InsnComparator method getImportantList.

public static InsnListSection getImportantList(InsnListSection list) {
    if (list.size() == 0) {
        return list;
    }
    Set<LabelNode> controlFlowLabels = getControlFlowLabels(list);
    Map<LabelNode, LabelNode> labelMap = Maps.asMap(controlFlowLabels, new Function<LabelNode, LabelNode>() {

        @Override
        public LabelNode apply(LabelNode input) {
            return input;
        }
    });
    InsnListSection importantNodeList = new InsnListSection();
    for (AbstractInsnNode insn : list) {
        if (insnImportant(insn, controlFlowLabels)) {
            importantNodeList.add(insn.clone(labelMap));
        }
    }
    return importantNodeList;
}
Also used : AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 49 with AbstractInsnNode

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

the class InstructionDeserializer method deserialize.

@Override
public AbstractInsnNode deserialize(JsonElement var1, Type var2, JsonDeserializationContext var3) {
    JsonObject var4 = (JsonObject) var1;
    int var5 = var4.get("opcode").getAsInt();
    if (var5 != 21 && var5 != 25 && var5 != 58 && var5 != 54 && var5 != 22 && var5 != 55) {
        String var7;
        String var8;
        String var10;
        if (var5 != 184 && var5 != 182 && var5 != 183) {
            if (var5 == 18) {
                try {
                    return new LdcInsnNode(Integer.valueOf(var4.get("cst").getAsInt()));
                } catch (Exception var9) {
                    return new LdcInsnNode(var4.get("cst").getAsString());
                }
            } else if (var5 != 187 && var5 != 189) {
                if (var5 != 16 && var5 != 17) {
                    if (var5 != 179 && var5 != 178 && var5 != 180 && var5 != 181) {
                        return new InsnNode(var5);
                    } else {
                        var10 = var4.get("owner").getAsString();
                        var7 = var4.get("name").getAsString();
                        var8 = var4.get("desc").getAsString();
                        return new FieldInsnNode(var5, var10, var7, var8);
                    }
                } else {
                    return new IntInsnNode(var5, var4.get("operand").getAsInt());
                }
            } else {
                return new TypeInsnNode(var5, var4.get("desc").getAsString());
            }
        } else {
            var10 = var4.get("owner").getAsString();
            var7 = var4.get("name").getAsString();
            var8 = var4.get("desc").getAsString();
            return new MethodInsnNode(var5, var10, var7, var8);
        }
    } else {
        int var6 = var4.get("var").getAsInt();
        return new VarInsnNode(var5, var6);
    }
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) JsonObject(com.google.gson.JsonObject) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 50 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project DynamicSurroundings by OreCruncher.

the class PatchSoundManagerPlayTime method transmorgrify.

@Override
public boolean transmorgrify(final ClassNode cn) {
    final String[] names = { "playSound", "func_148611_c" };
    final String sig = "(Lnet/minecraft/client/audio/ISound;)V";
    final MethodNode m = findMethod(cn, sig, names);
    if (m != null) {
        logMethod(Transformer.log(), m, "Found!");
        for (int i = 0; i < m.instructions.size(); i++) {
            final AbstractInsnNode node = m.instructions.get(i);
            if (node instanceof IntInsnNode) {
                final IntInsnNode intNode = (IntInsnNode) node;
                if (intNode.operand == 20) {
                    m.instructions.set(node, new IntInsnNode(Opcodes.BIPUSH, 0));
                    return true;
                }
            }
        }
    } else {
        Transformer.log().error("Unable to locate method {}{}", names[0], sig);
    }
    Transformer.log().info("Unable to patch [{}]!", getClassName());
    return false;
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) 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