Search in sources :

Example 21 with LdcInsnNode

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

the class LDCSearch method search.

public void search(final ResourceContainer container, final String resourceWorkingName, final ClassNode node, boolean caseSensitive) {
    final Iterator<MethodNode> methods = node.methods.iterator();
    final String srchText = searchText.getText();
    final String srchTextLowerCase = searchText.getText().toLowerCase();
    if (srchText.isEmpty())
        return;
    while (methods.hasNext()) {
        final MethodNode method = methods.next();
        final InsnList insnlist = method.instructions;
        for (AbstractInsnNode insnNode : insnlist) {
            if (insnNode instanceof LdcInsnNode) {
                final LdcInsnNode ldcObject = ((LdcInsnNode) insnNode);
                final String ldcString = ldcObject.cst.toString();
                // TODO re-add this at some point when the search pane is redone
                boolean exact = false;
                final boolean exactMatch = exact && ldcString.equals(srchText);
                final boolean caseInsensitiveMatch = !exact && caseSensitive && ldcString.contains(srchText);
                final boolean caseSensitiveMatch = !exact && !caseSensitive && ldcString.toLowerCase().contains(srchTextLowerCase);
                final boolean anyMatch = exactMatch || caseInsensitiveMatch || caseSensitiveMatch;
                if (anyMatch) {
                    BytecodeViewer.viewer.searchBoxPane.treeRoot.add(new LDCSearchTreeNodeResult(container, resourceWorkingName, node, method, null, ldcString, ldcObject.cst.getClass().getCanonicalName()));
                }
            }
        }
    }
    final Iterator<FieldNode> fields = node.fields.iterator();
    while (methods.hasNext()) {
        final FieldNode field = fields.next();
        if (field.value instanceof String) {
            BytecodeViewer.viewer.resourcePane.treeRoot.add(new LDCSearchTreeNodeResult(container, resourceWorkingName, node, null, field, String.valueOf(field.value), field.value.getClass().getCanonicalName()));
        }
    }
}
Also used : LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) FieldNode(org.objectweb.asm.tree.FieldNode) LDCSearchTreeNodeResult(the.bytecode.club.bytecodeviewer.searching.LDCSearchTreeNodeResult) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 22 with LdcInsnNode

use of org.objectweb.asm.tree.LdcInsnNode in project maple-ir by LLVM-but-worse.

the class LdcInsnNodeSerializer method deserialize.

@Override
public LdcInsnNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObject = (JsonObject) json;
    Object cst = context.deserialize(jsonObject.get("cst"), Object.class);
    return new LdcInsnNode(cst);
}
Also used : LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject)

Example 23 with LdcInsnNode

use of org.objectweb.asm.tree.LdcInsnNode in project malmo by Microsoft.

the class OverclockingClassTransformer method overclockServer.

private static void overclockServer(ClassNode node, boolean isObfuscated) {
    // We're attempting to replace this code (from the heart of MinecraftServer.run):
    /*       
            {
                while (i > 50L)
                {
                    i -= 50L;
                    this.tick();
                }
            }
    
            Thread.sleep(Math.max(1L, 50L - i));
        */
    // With this:
    /*       
        {
            while (i > TimeHelper.serverTickLength)
            {
                i -= TimeHelper.serverTickLength;
                this.tick();
            }
        }

        Thread.sleep(Math.max(1L, TimeHelper.serverTickLength - i));
    */
    // This allows us to alter the tick length via TimeHelper.
    final String methodName = "run";
    // No params, returns void.
    final String methodDescriptor = "()V";
    System.out.println("MALMO: Found MinecraftServer, attempting to transform it");
    for (MethodNode method : node.methods) {
        if (method.name.equals(methodName) && method.desc.equals(methodDescriptor)) {
            System.out.println("MALMO: Found MinecraftServer.run() method, attempting to transform it");
            for (AbstractInsnNode instruction : method.instructions.toArray()) {
                if (instruction.getOpcode() == Opcodes.LDC) {
                    Object cst = ((LdcInsnNode) instruction).cst;
                    if ((cst instanceof Long) && (Long) cst == 50) {
                        System.out.println("MALMO: Transforming LDC");
                        AbstractInsnNode replacement = new FieldInsnNode(Opcodes.GETSTATIC, "com/microsoft/Malmo/Utils/TimeHelper", "serverTickLength", "J");
                        method.instructions.set(instruction, replacement);
                    }
                }
            }
        }
    }
}
Also used : LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 24 with LdcInsnNode

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

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

the class ReplaceComparisonOperator method apply.

/* (non-Javadoc)
	 * @see org.evosuite.cfg.instrumentation.MutationOperator#apply(org.objectweb.asm.tree.MethodNode, java.lang.String, java.lang.String, org.evosuite.cfg.BytecodeInstruction)
	 */
/**
 * {@inheritDoc}
 */
@Override
public List<Mutation> apply(MethodNode mn, String className, String methodName, BytecodeInstruction instruction, Frame frame) {
    JumpInsnNode node = (JumpInsnNode) instruction.getASMNode();
    List<Mutation> mutations = new LinkedList<Mutation>();
    LabelNode target = node.label;
    boolean isBoolean = frame.getStack(frame.getStackSize() - 1) == BooleanValueInterpreter.BOOLEAN_VALUE;
    for (Integer op : getOperators(node.getOpcode(), isBoolean)) {
        logger.debug("Adding replacement " + op);
        if (op >= 0) {
            // insert mutation into bytecode with conditional
            JumpInsnNode mutation = new JumpInsnNode(op, target);
            // insert mutation into pool
            Mutation mutationObject = MutationPool.addMutation(className, methodName, NAME + " " + getOp(node.getOpcode()) + " -> " + getOp(op), instruction, mutation, getInfectionDistance(node.getOpcode(), op));
            mutations.add(mutationObject);
        } else {
            // Replace relational operator with TRUE/FALSE
            InsnList mutation = new InsnList();
            if (opcodesInt.contains(node.getOpcode()))
                mutation.add(new InsnNode(Opcodes.POP));
            else
                mutation.add(new InsnNode(Opcodes.POP2));
            if (op == TRUE) {
                mutation.add(new LdcInsnNode(1));
            } else {
                mutation.add(new LdcInsnNode(0));
            }
            mutation.add(new JumpInsnNode(Opcodes.IFNE, target));
            Mutation mutationObject = MutationPool.addMutation(className, methodName, NAME + " " + getOp(node.getOpcode()) + " -> " + op, instruction, mutation, getInfectionDistance(node.getOpcode(), op));
            mutations.add(mutationObject);
        }
    }
    return mutations;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) Mutation(org.evosuite.coverage.mutation.Mutation) InsnList(org.objectweb.asm.tree.InsnList) LinkedList(java.util.LinkedList)

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