Search in sources :

Example 36 with AbstractInsnNode

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

the class ReplaceVariable method getInfectionDistance.

/**
 * <p>
 * getInfectionDistance
 * </p>
 *
 * @param type
 *            a {@link org.objectweb.asm.Type} object.
 * @param original
 *            a {@link org.objectweb.asm.tree.AbstractInsnNode} object.
 * @param mutant
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public InsnList getInfectionDistance(Type type, AbstractInsnNode original, InsnList mutant) {
    // TODO: Treat reference types different!
    InsnList distance = new InsnList();
    if (original instanceof VarInsnNode) {
        VarInsnNode node = (VarInsnNode) original;
        distance.add(new VarInsnNode(node.getOpcode(), node.var));
        if (type.getDescriptor().startsWith("L") || type.getDescriptor().startsWith("["))
            addReferenceDistanceCheck(distance, type, mutant);
        else
            addPrimitiveDistanceCheck(distance, type, mutant);
    } else if (original instanceof FieldInsnNode) {
        if (original.getOpcode() == Opcodes.GETFIELD)
            // make sure to re-load this for GETFIELD
            distance.add(new InsnNode(Opcodes.DUP));
        FieldInsnNode node = (FieldInsnNode) original;
        distance.add(new FieldInsnNode(node.getOpcode(), node.owner, node.name, node.desc));
        if (type.getDescriptor().startsWith("L") || type.getDescriptor().startsWith("["))
            addReferenceDistanceCheck(distance, type, mutant);
        else
            addPrimitiveDistanceCheck(distance, type, mutant);
    } else if (original instanceof IincInsnNode) {
        distance.add(Mutation.getDefaultInfectionDistance());
    }
    return distance;
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 37 with AbstractInsnNode

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

the class RegressionClassDiff method getClassInstructions.

/*
   * Get bytecode instructions for the class based on the following format: Method -> List of
   * instructions
   */
private static Map<String, List<Integer>> getClassInstructions(InputStream classAsInputStream) {
    HashMap<String, List<Integer>> methodInstructionsMap = new HashMap<>();
    try {
        ClassReader reader = new ClassReader(classAsInputStream);
        ClassNode classNode = new ClassNode();
        reader.accept(classNode, 0);
        @SuppressWarnings("unchecked") final List<MethodNode> methods = classNode.methods;
        Printer printer = new Textifier();
        TraceMethodVisitor mp = new TraceMethodVisitor(printer);
        for (MethodNode m : methods) {
            List<Integer> instructions = new ArrayList<>();
            InsnList inList = m.instructions;
            String mathodID = m.name + ": " + m.desc;
            System.out.println(mathodID);
            int[] methodInstructions = new int[inList.size()];
            for (int i = 0; i < inList.size(); i++) {
                int op = inList.get(i).getOpcode();
                methodInstructions[i] = op;
                AbstractInsnNode insn = inList.get(i);
                insn.accept(mp);
                // logger.warn("{} -> {}", sw.toString(), op);
                if (op != -1)
                    instructions.add(op);
            }
            methodInstructionsMap.put(mathodID, instructions);
        }
    } catch (IOException e) {
        // Will fail if ClassReader fails
        e.printStackTrace();
    }
    return methodInstructionsMap;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Printer(org.objectweb.asm.util.Printer) Textifier(org.objectweb.asm.util.Textifier) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) MethodNode(org.objectweb.asm.tree.MethodNode) ClassReader(org.objectweb.asm.ClassReader) ArrayList(java.util.ArrayList) InsnList(org.objectweb.asm.tree.InsnList) List(java.util.List) ResourceList(org.evosuite.classpath.ResourceList)

Example 38 with AbstractInsnNode

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

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

the class ClassTransformer method patchWorldGenTrees.

private byte[] patchWorldGenTrees(byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    logger.log(Level.DEBUG, "Found WorldGenAbstractTree Class: " + classNode.name);
    MethodNode setDirtAt = null;
    for (MethodNode mn : classNode.methods) {
        if (mn.name.equals(MCPNames.method("func_175921_a"))) {
            setDirtAt = mn;
            break;
        }
    }
    if (setDirtAt != null) {
        logger.log(Level.DEBUG, " - Patching setDirtAt");
        for (int i = 0; i < setDirtAt.instructions.size(); i++) {
            AbstractInsnNode ain = setDirtAt.instructions.get(i);
            if (ain instanceof JumpInsnNode) {
                JumpInsnNode jin = (JumpInsnNode) ain;
                if (jin.getOpcode() == Opcodes.IF_ACMPEQ) {
                    LabelNode l = jin.label;
                    InsnList toInsert = new InsnList();
                    toInsert.add(new VarInsnNode(ALOAD, 1));
                    toInsert.add(new VarInsnNode(ALOAD, 2));
                    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, "protectGround", "(Lnet/minecraft/block/Block;)Z", false));
                    toInsert.add(new JumpInsnNode(IFGT, new LabelNode(l.getLabel())));
                    setDirtAt.instructions.insert(jin, toInsert);
                    logger.log(Level.DEBUG, " - Patched setDirtAt");
                    break;
                }
            }
        }
    }
    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) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 40 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode 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)

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