Search in sources :

Example 1 with ComparisonInstruction

use of net.runelite.asm.attributes.code.instruction.types.ComparisonInstruction in project runelite by runelite.

the class IllegalStateExceptions method findInteresting.

/* find if, new, ..., athrow, replace with goto */
private void findInteresting(ClassGroup group) {
    for (ClassFile cf : group.getClasses()) {
        for (Method m : cf.getMethods()) {
            Code c = m.getCode();
            if (c == null)
                continue;
            Instructions instructions = c.getInstructions();
            List<Instruction> ilist = instructions.getInstructions();
            for (int i = 0; i < ilist.size(); ++i) {
                Instruction ins = ilist.get(i);
                if (// the if
                !(ins instanceof ComparisonInstruction))
                    continue;
                Instruction ins2 = ilist.get(i + 1);
                if (!(ins2 instanceof New))
                    continue;
                New new2 = (New) ins2;
                net.runelite.asm.pool.Class clazz = new2.getNewClass();
                if (!clazz.getName().contains("java/lang/IllegalStateException"))
                    continue;
                interesting.add(ins);
            }
        }
    }
}
Also used : New(net.runelite.asm.attributes.code.instructions.New) ClassFile(net.runelite.asm.ClassFile) ComparisonInstruction(net.runelite.asm.attributes.code.instruction.types.ComparisonInstruction) Instructions(net.runelite.asm.attributes.code.Instructions) Method(net.runelite.asm.Method) ComparisonInstruction(net.runelite.asm.attributes.code.instruction.types.ComparisonInstruction) JumpingInstruction(net.runelite.asm.attributes.code.instruction.types.JumpingInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code)

Example 2 with ComparisonInstruction

use of net.runelite.asm.attributes.code.instruction.types.ComparisonInstruction in project runelite by runelite.

the class PacketHandlerOrder method follow.

private List<Instruction> follow(Instructions instructions, Instruction start, Instruction end) {
    List<Instruction> list = new ArrayList<>();
    int idx = instructions.getInstructions().indexOf(start);
    assert idx != -1;
    for (; ; ) {
        Instruction i = instructions.getInstructions().get(idx);
        // end is the following instruction post read.. not included
        if (i == end) {
            break;
        }
        if (i instanceof Goto) {
            Goto g = (Goto) i;
            Label to = g.getTo();
            idx = instructions.getInstructions().indexOf(to);
            assert idx != -1;
            continue;
        }
        list.add(i);
        if (i instanceof ComparisonInstruction) {
            return list;
        }
        ++idx;
    }
    return list;
}
Also used : Goto(net.runelite.asm.attributes.code.instructions.Goto) ArrayList(java.util.ArrayList) Label(net.runelite.asm.attributes.code.Label) ComparisonInstruction(net.runelite.asm.attributes.code.instruction.types.ComparisonInstruction) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction) ComparisonInstruction(net.runelite.asm.attributes.code.instruction.types.ComparisonInstruction) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) InvokeInstruction(net.runelite.asm.attributes.code.instruction.types.InvokeInstruction) JumpingInstruction(net.runelite.asm.attributes.code.instruction.types.JumpingInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) MappableInstruction(net.runelite.asm.attributes.code.instruction.types.MappableInstruction)

Example 3 with ComparisonInstruction

use of net.runelite.asm.attributes.code.instruction.types.ComparisonInstruction in project runelite by runelite.

the class ConstantParameter method findDeadParameters.

private void findDeadParameters(InstructionContext ins) {
    List<ConstantMethodParameter> parameters = this.findParametersForMethod(ins.getFrame().getMethod());
    for (ConstantMethodParameter parameter : parameters) {
        int lvtIndex = parameter.lvtIndex;
        if (parameter.invalid) {
            continue;
        }
        if (ins.getInstruction() instanceof LVTInstruction) {
            LVTInstruction lvt = (LVTInstruction) ins.getInstruction();
            if (lvt.getVariableIndex() != lvtIndex) {
                continue;
            }
            if (lvt.store() || ins.getInstruction().getType() == InstructionType.IINC) {
                parameter.invalid = true;
                // value changes at some point, parameter is used
                continue;
            }
            // check what pops the parameter is a comparison
            assert ins.getPushes().size() == 1;
            StackContext sctx = ins.getPushes().get(0);
            if (sctx.getPopped().size() != 1 || !(sctx.getPopped().get(0).getInstruction() instanceof ComparisonInstruction)) {
                parameter.invalid = true;
                continue;
            }
        }
        if (!(ins.getInstruction() instanceof ComparisonInstruction)) {
            continue;
        }
        // assume that this will always be variable index #paramIndex comp with a constant.
        ComparisonInstruction comp = (ComparisonInstruction) ins.getInstruction();
        StackContext one, two = null;
        if (comp instanceof If0) {
            one = ins.getPops().get(0);
        } else if (comp instanceof If) {
            one = ins.getPops().get(0);
            two = ins.getPops().get(1);
        } else {
            throw new RuntimeException("Unknown comp ins");
        }
        // find if one is a lvt ins
        LVTInstruction lvt = null;
        StackContext other = null;
        if (one.getPushed().getInstruction() instanceof LVTInstruction) {
            lvt = (LVTInstruction) one.getPushed().getInstruction();
            other = two;
        } else if (two != null && two.getPushed().getInstruction() instanceof LVTInstruction) {
            lvt = (LVTInstruction) two.getPushed().getInstruction();
            other = one;
        }
        assert lvt == null || !lvt.store();
        if (lvt == null || lvt.getVariableIndex() != lvtIndex) {
            continue;
        }
        Number otherValue = null;
        if (// two is null for if0
        two != null) {
            if (!(other.getPushed().getInstruction() instanceof PushConstantInstruction)) {
                parameter.invalid = true;
                continue;
            }
            PushConstantInstruction pc = (PushConstantInstruction) other.getPushed().getInstruction();
            otherValue = (Number) pc.getConstant();
        }
        for (Number value : parameter.values) {
            // the result of the comparison doesn't matter, only that it always goes the same direction for every invocation
            boolean result = doLogicalComparison(value, comp, otherValue);
            // not that all ifs for a specific parameter always take the same path
            if (parameter.result != null && parameter.result != result) {
                parameter.invalid = true;
            } else {
                parameter.operations.add(ins.getInstruction());
                parameter.result = result;
            }
        }
    }
}
Also used : If0(net.runelite.asm.attributes.code.instructions.If0) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) StackContext(net.runelite.asm.execution.StackContext) ComparisonInstruction(net.runelite.asm.attributes.code.instruction.types.ComparisonInstruction) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) If(net.runelite.asm.attributes.code.instructions.If)

Aggregations

ComparisonInstruction (net.runelite.asm.attributes.code.instruction.types.ComparisonInstruction)3 Instruction (net.runelite.asm.attributes.code.Instruction)2 JumpingInstruction (net.runelite.asm.attributes.code.instruction.types.JumpingInstruction)2 LVTInstruction (net.runelite.asm.attributes.code.instruction.types.LVTInstruction)2 PushConstantInstruction (net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction)2 ArrayList (java.util.ArrayList)1 ClassFile (net.runelite.asm.ClassFile)1 Method (net.runelite.asm.Method)1 Code (net.runelite.asm.attributes.Code)1 Instructions (net.runelite.asm.attributes.code.Instructions)1 Label (net.runelite.asm.attributes.code.Label)1 GetFieldInstruction (net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction)1 InvokeInstruction (net.runelite.asm.attributes.code.instruction.types.InvokeInstruction)1 MappableInstruction (net.runelite.asm.attributes.code.instruction.types.MappableInstruction)1 SetFieldInstruction (net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction)1 Goto (net.runelite.asm.attributes.code.instructions.Goto)1 If (net.runelite.asm.attributes.code.instructions.If)1 If0 (net.runelite.asm.attributes.code.instructions.If0)1 New (net.runelite.asm.attributes.code.instructions.New)1 StackContext (net.runelite.asm.execution.StackContext)1