Search in sources :

Example 16 with LdcInsnNode

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

the class CastClassAnalyzer method handleMethodNode.

/**
 * Add all possible calls for a given method
 *
 * @param mn
 */
@SuppressWarnings("unchecked")
public void handleMethodNode(ClassNode cn, MethodNode mn, int depth) {
    if (mn.signature != null) {
        logger.debug("Visiting signature: " + mn.signature);
        CollectParameterTypesVisitor visitor = new CollectParameterTypesVisitor(cn.name);
        new SignatureReader(mn.signature).accept(visitor);
        for (Type castType : visitor.getClasses()) {
            if (!castClassMap.containsKey(castType)) {
                logger.debug("Adding new cast class from signature visitor: " + castType);
                castClassMap.put(castType, depth + 1);
            }
        }
    }
    InsnList instructions = mn.instructions;
    Iterator<AbstractInsnNode> iterator = instructions.iterator();
    // TODO: This really shouldn't be here but in its own class
    while (iterator.hasNext()) {
        AbstractInsnNode insn = iterator.next();
        if (insn.getOpcode() == Opcodes.CHECKCAST) {
            TypeInsnNode typeNode = (TypeInsnNode) insn;
            Type castType = Type.getObjectType(typeNode.desc);
            while (castType.getSort() == Type.ARRAY) {
                castType = castType.getElementType();
            }
            logger.debug("Adding new cast class from cast: " + castType);
            if (!castClassMap.containsKey(castType))
                castClassMap.put(castType, depth + 1);
        } else if (insn.getOpcode() == Opcodes.INSTANCEOF) {
            TypeInsnNode typeNode = (TypeInsnNode) insn;
            Type castType = Type.getObjectType(typeNode.desc);
            while (castType.getSort() == Type.ARRAY) {
                castType = castType.getElementType();
            }
            logger.debug("Adding new cast class from instanceof: " + castType);
            if (!castClassMap.containsKey(castType))
                castClassMap.put(castType, depth + 1);
        } else if (insn.getOpcode() == Opcodes.LDC) {
            LdcInsnNode ldcNode = (LdcInsnNode) insn;
            if (ldcNode.cst instanceof Type) {
                Type type = (Type) ldcNode.cst;
                while (type.getSort() == Type.ARRAY) {
                    type = type.getElementType();
                }
                if (!castClassMap.containsKey(type))
                    castClassMap.put(type, depth + 1);
            }
        }
    }
}
Also used : Type(org.objectweb.asm.Type) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) SignatureReader(org.objectweb.asm.signature.SignatureReader) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 17 with LdcInsnNode

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

the class ClassTransformer method patchEntityLivingBase.

private byte[] patchEntityLivingBase(byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    logger.log(Level.DEBUG, "Found EntityLivingBase Class: " + classNode.name);
    MethodNode updatePotionEffects = null;
    MethodNode travel = null;
    for (MethodNode mn : classNode.methods) {
        if (mn.name.equals(MCPNames.method("func_70679_bo"))) {
            updatePotionEffects = mn;
        } else if (mn.name.equals(MCPNames.method("func_191986_a"))) {
            travel = mn;
        }
    }
    if (updatePotionEffects != null) {
        logger.log(Level.DEBUG, "- Found updatePotionEffects (1/2)");
        for (int i = 0; i < updatePotionEffects.instructions.size(); i++) {
            AbstractInsnNode ain = updatePotionEffects.instructions.get(i);
            if (ain instanceof FieldInsnNode) {
                FieldInsnNode fin = (FieldInsnNode) ain;
                if (fin.name.equals(MCPNames.field("field_70180_af"))) {
                    AbstractInsnNode aload = updatePotionEffects.instructions.get(i - 1);
                    InsnList toInsert = new InsnList();
                    LabelNode l1 = new LabelNode(new Label());
                    toInsert.add(new VarInsnNode(ALOAD, 0));
                    toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "shouldRenderPotionParticles", "(Lnet/minecraft/entity/EntityLivingBase;)Z", false));
                    toInsert.add(new JumpInsnNode(IFGT, l1));
                    toInsert.add(new InsnNode(RETURN));
                    toInsert.add(l1);
                    updatePotionEffects.instructions.insertBefore(aload, toInsert);
                    break;
                }
            }
        }
    }
    if (travel != null) {
        logger.log(Level.DEBUG, "- Found travel (2/2)");
        for (int i = 0; i < travel.instructions.size(); i++) {
            AbstractInsnNode ain = travel.instructions.get(i);
            if (ain instanceof LdcInsnNode) {
                LdcInsnNode lin = (LdcInsnNode) ain;
                if (lin.cst.equals(new Float("0.91"))) {
                    AbstractInsnNode next = travel.instructions.get(i + 1);
                    if (next.getOpcode() == Opcodes.FMUL) {
                        InsnList toInsert = new InsnList();
                        toInsert.add(new VarInsnNode(ALOAD, 0));
                        toInsert.add(new VarInsnNode(ALOAD, 0));
                        toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraft/entity/EntityLivingBase", MCPNames.field("field_70170_p"), "Lnet/minecraft/world/World;"));
                        toInsert.add(new VarInsnNode(ALOAD, 5));
                        toInsert.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraft/world/World", MCPNames.method("func_180495_p"), "(Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/block/state/IBlockState;", false));
                        toInsert.add(new MethodInsnNode(INVOKEINTERFACE, "net/minecraft/block/state/IBlockState", MCPNames.method("func_177230_c"), "()Lnet/minecraft/block/Block;", true));
                        toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "slipFix", "(FLnet/minecraft/entity/EntityLivingBase;Lnet/minecraft/block/Block;)F", false));
                        travel.instructions.insert(next, toInsert);
                        i += 6;
                    }
                }
            }
        }
    }
    CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ClassNode(org.objectweb.asm.tree.ClassNode) Label(org.objectweb.asm.Label) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList) 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) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 18 with LdcInsnNode

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

the class ClassTransformer method patchRenderItem.

private byte[] patchRenderItem(byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    logger.log(Level.DEBUG, "Found RenderItem Class: " + classNode.name);
    MethodNode renderEffect = null;
    MethodNode renderItem = null;
    MethodNode renderQuads = null;
    for (MethodNode mn : classNode.methods) {
        if (mn.name.equals(MCPNames.method("func_191966_a"))) {
            renderEffect = mn;
        } else if (mn.name.equals(MCPNames.method("func_180454_a")) && mn.desc.equals("(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/renderer/block/model/IBakedModel;)V")) {
            renderItem = mn;
        } else if (mn.name.equals(MCPNames.method("func_191970_a"))) {
            renderQuads = mn;
        }
    }
    if (renderEffect != null) {
        logger.log(Level.DEBUG, "- Found renderEffect (1/3)");
        renderEffect.instructions.insert(new MethodInsnNode(INVOKESTATIC, asmHandler, "preEnchantment", "()V", false));
        for (int i = 0; i < renderEffect.instructions.size(); i++) {
            AbstractInsnNode ain = renderEffect.instructions.get(i);
            if (ain instanceof LdcInsnNode) {
                LdcInsnNode lin = (LdcInsnNode) ain;
                if (lin.cst.equals(new Integer(-8372020))) {
                    logger.log(Level.DEBUG, "- Found Texture Binding");
                    renderEffect.instructions.insert(lin, new MethodInsnNode(INVOKESTATIC, asmHandler, "enchantmentColorHook", "(I)I", false));
                }
            } else if (ain.getOpcode() == RETURN) {
                renderEffect.instructions.insertBefore(ain, new MethodInsnNode(INVOKESTATIC, asmHandler, "postEnchantment", "()V", false));
                i++;
            }
        }
    }
    if (renderItem != null) {
        boolean found = false;
        logger.log(Level.DEBUG, "- Found renderItem (2/3) (" + renderItem.desc + ")");
        for (int i = 0; i < renderItem.instructions.size(); i++) {
            AbstractInsnNode ain = renderItem.instructions.get(i);
            if (ain instanceof MethodInsnNode) {
                MethodInsnNode min = (MethodInsnNode) ain;
                if (!found) {
                    if (min.name.equals(MCPNames.method("func_191966_a"))) {
                        logger.log(Level.DEBUG, "- Found renderEffect calling");
                        InsnList toInsert = new InsnList();
                        toInsert.add(new VarInsnNode(ALOAD, 1));
                        toInsert.add(new FieldInsnNode(PUTSTATIC, asmHandler, "currentlyRendering", "Lnet/minecraft/item/ItemStack;"));
                        renderItem.instructions.insertBefore(min, toInsert);
                        toInsert = new InsnList();
                        toInsert.add(new InsnNode(ACONST_NULL));
                        toInsert.add(new FieldInsnNode(PUTSTATIC, asmHandler, "currentlyRendering", "Lnet/minecraft/item/ItemStack;"));
                        renderItem.instructions.insert(min, toInsert);
                        found = true;
                    }
                }
                if (min.name.equals(MCPNames.method("func_179022_a"))) {
                    LabelNode l1 = new LabelNode(new Label());
                    LabelNode l2 = new LabelNode(new Label());
                    logger.log(Level.DEBUG, "- Inserting TE Item Renderer");
                    InsnList insertBefore = new InsnList();
                    insertBefore.add(new FieldInsnNode(GETSTATIC, "lumien/randomthings/client/RandomThingsTEItemRenderer", "instance", "Llumien/randomthings/client/RandomThingsTEItemRenderer;"));
                    insertBefore.add(new VarInsnNode(ALOAD, 1));
                    insertBefore.add(new MethodInsnNode(INVOKEVIRTUAL, "lumien/randomthings/client/RandomThingsTEItemRenderer", "renderByItem", "(Lnet/minecraft/item/ItemStack;)Z", false));
                    insertBefore.add(new JumpInsnNode(IFEQ, l2));
                    insertBefore.add(new InsnNode(POP));
                    insertBefore.add(new InsnNode(POP));
                    insertBefore.add(new JumpInsnNode(GOTO, l1));
                    insertBefore.add(l2);
                    InsnList insertAfter = new InsnList();
                    insertAfter.add(l1);
                    renderItem.instructions.insertBefore(min, insertBefore);
                    renderItem.instructions.insert(min, insertAfter);
                    i += 8;
                }
            }
        }
    }
    if (renderQuads != null) {
        logger.log(Level.DEBUG, "- Found renderQuads (3/3) (" + renderQuads.desc + ")");
        String luminousHandler = "lumien/randomthings/handler/LuminousHandler";
        InsnList startInsrt = new InsnList();
        startInsrt.add(new VarInsnNode(ALOAD, 4));
        startInsrt.add(new VarInsnNode(ALOAD, 1));
        startInsrt.add(new MethodInsnNode(INVOKESTATIC, luminousHandler, "luminousHookStart", "(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/renderer/BufferBuilder;)V", false));
        renderQuads.instructions.insert(startInsrt);
        for (int i = 0; i < renderQuads.instructions.size(); i++) {
            AbstractInsnNode ain = renderQuads.instructions.get(i);
            if (ain instanceof MethodInsnNode) {
                MethodInsnNode min = (MethodInsnNode) ain;
                if (min.name.equals("renderQuadColor")) {
                    InsnList toInsert = new InsnList();
                    toInsert.add(new VarInsnNode(ALOAD, 8));
                    toInsert.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraft/client/renderer/block/model/BakedQuad", MCPNames.method("func_178211_c"), "()I", false));
                    toInsert.add(new MethodInsnNode(INVOKESTATIC, luminousHandler, "luminousHookPre", "(I)V", false));
                    toInsert.add(new InsnNode(POP));
                    toInsert.add(new VarInsnNode(ALOAD, 4));
                    toInsert.add(new VarInsnNode(ILOAD, 9));
                    toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "getColorFromItemStack", "(Lnet/minecraft/item/ItemStack;I)I", false));
                    renderQuads.instructions.insertBefore(min, toInsert);
                    i += 7;
                    renderQuads.instructions.insert(min, new MethodInsnNode(INVOKESTATIC, luminousHandler, "luminousHookPost", "()V", false));
                }
            } else if (ain.getOpcode() == RETURN) {
                renderQuads.instructions.insertBefore(ain, new MethodInsnNode(INVOKESTATIC, luminousHandler, "luminousHookEnd", "()V", false));
                i++;
            }
        }
    }
    CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ClassNode(org.objectweb.asm.tree.ClassNode) Label(org.objectweb.asm.Label) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList) 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) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 19 with LdcInsnNode

use of org.objectweb.asm.tree.LdcInsnNode 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 20 with LdcInsnNode

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

the class RegexInsnFinder method getInsString.

private static String getInsString(AbstractInsnNode ain) {
    String insnString = "";
    switch(ain.getType()) {
        case AbstractInsnNode.INT_INSN:
            final IntInsnNode iin = (IntInsnNode) ain;
            insnString += "{" + iin.operand + "}";
            break;
        case AbstractInsnNode.LDC_INSN:
            final LdcInsnNode lin = (LdcInsnNode) ain;
            insnString += "{" + lin.cst.toString().replace("}", "\\}") + "}";
            break;
        case AbstractInsnNode.VAR_INSN:
            final VarInsnNode vin = (VarInsnNode) ain;
            insnString += "_" + vin.var;
            break;
        case AbstractInsnNode.IINC_INSN:
            final IincInsnNode iiin = (IincInsnNode) ain;
            insnString += "{" + iiin.var + "," + iiin.incr + "}";
            break;
        case AbstractInsnNode.FIELD_INSN:
            final FieldInsnNode fin = (FieldInsnNode) ain;
            insnString += "{" + fin.desc + "," + fin.owner + "," + fin.name + "}";
            break;
        case AbstractInsnNode.METHOD_INSN:
            final MethodInsnNode min = (MethodInsnNode) ain;
            insnString += "{" + min.desc + "," + min.owner + "," + min.name + "}";
            break;
        case AbstractInsnNode.TYPE_INSN:
            final TypeInsnNode tin = (TypeInsnNode) ain;
            insnString += "{" + tin.desc + "}";
            break;
        case AbstractInsnNode.MULTIANEWARRAY_INSN:
            final MultiANewArrayInsnNode manain = (MultiANewArrayInsnNode) ain;
            insnString += "{" + manain.dims + "," + manain.desc + "}";
            break;
    }
    return insnString;
}
Also used : LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MultiANewArrayInsnNode(org.objectweb.asm.tree.MultiANewArrayInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Aggregations

LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)50 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)32 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)30 InsnList (org.objectweb.asm.tree.InsnList)22 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)18 InsnNode (org.objectweb.asm.tree.InsnNode)18 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)18 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)15 Type (org.objectweb.asm.Type)14 MethodNode (org.objectweb.asm.tree.MethodNode)10 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)9 LabelNode (org.objectweb.asm.tree.LabelNode)8 IntInsnNode (org.objectweb.asm.tree.IntInsnNode)7 Label (org.objectweb.asm.Label)6 ClassNode (org.objectweb.asm.tree.ClassNode)5 FieldNode (org.objectweb.asm.tree.FieldNode)4 Mutation (org.evosuite.coverage.mutation.Mutation)3 IincInsnNode (org.objectweb.asm.tree.IincInsnNode)3 LookupSwitchInsnNode (org.objectweb.asm.tree.LookupSwitchInsnNode)3 TableSwitchInsnNode (org.objectweb.asm.tree.TableSwitchInsnNode)3