Search in sources :

Example 11 with IAdd

use of net.runelite.asm.attributes.code.instructions.IAdd in project runelite by runelite.

the class ExprArgOrderTest method test5.

@Test
public void test5() {
    ClassGroup group = ClassGroupFactory.generateGroup();
    Code code = group.findClass("test").findMethod("func").getCode();
    Instructions ins = code.getInstructions();
    code.setMaxStack(2);
    // vars[0] = 3
    Instruction[] prepareVariables = { new LDC(ins, 3), new IStore(ins, 0) };
    for (Instruction i : prepareVariables) {
        ins.addInstruction(i);
    }
    Label label = new Label(ins);
    Instruction[] body = { // if (0 == 3 + var0) -> if (var0 + 3 == 0)
    new LDC(ins, 0), new LDC(ins, 3), new ILoad(ins, 0), new IAdd(ins), new IfICmpEq(ins, label), label, new VReturn(ins) };
    for (Instruction i : body) {
        ins.addInstruction(i);
    }
    ExprArgOrder exprArgOrder = new ExprArgOrder();
    exprArgOrder.run(group);
    List<Instruction> instructions = ins.getInstructions();
    // ldc iload add -> iload ldc iadd
    assertEquals(ILOAD, instructions.get(2).getType());
    assertEquals(LDC, instructions.get(3).getType());
    assertEquals(IADD, instructions.get(4).getType());
    // idc moves from 2 to 5
    assertEquals(LDC, instructions.get(5).getType());
    assertEquals(IF_ICMPEQ, instructions.get(6).getType());
}
Also used : IfICmpEq(net.runelite.asm.attributes.code.instructions.IfICmpEq) IStore(net.runelite.asm.attributes.code.instructions.IStore) ILoad(net.runelite.asm.attributes.code.instructions.ILoad) Label(net.runelite.asm.attributes.code.Label) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.InstructionType.LDC) LDC(net.runelite.asm.attributes.code.instructions.LDC) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) VReturn(net.runelite.asm.attributes.code.instructions.VReturn) ClassGroup(net.runelite.asm.ClassGroup) IAdd(net.runelite.asm.attributes.code.instructions.IAdd) Test(org.junit.Test)

Example 12 with IAdd

use of net.runelite.asm.attributes.code.instructions.IAdd in project runelite by runelite.

the class ExprArgOrderTest method test6.

@Test
public void test6() {
    ClassGroup group = ClassGroupFactory.generateGroup();
    Code code = group.findClass("test").findMethod("func").getCode();
    Instructions ins = code.getInstructions();
    code.setMaxStack(2);
    // vars[0] = 3
    Instruction[] prepareVariables = { new LDC(ins, 3), new IStore(ins, 0) };
    for (Instruction i : prepareVariables) {
        ins.addInstruction(i);
    }
    Label label = new Label(ins);
    /*
		iconst_0
		ldc                   8388608
		iload_3
		iadd
		ldc                   -16777216
		iand
		if_icmpeq             LABEL0x49
		 */
    Instruction[] body = { new LDC(ins, 0), new LDC(ins, 8388608), new ILoad(ins, 0), new IAdd(ins), new LDC(ins, -16777216), new IAnd(ins), // 8
    new IfICmpEq(ins, label), label, new VReturn(ins) };
    for (Instruction i : body) {
        ins.addInstruction(i);
    }
    ExprArgOrder exprArgOrder = new ExprArgOrder();
    exprArgOrder.run(group);
    List<Instruction> instructions = ins.getInstructions();
    assertEquals(ILOAD, instructions.get(2).getType());
    assertEquals(LDC, instructions.get(3).getType());
    assertEquals(IADD, instructions.get(4).getType());
    assertEquals(LDC, instructions.get(5).getType());
    assertEquals(IAND, instructions.get(6).getType());
    assertEquals(LDC, instructions.get(7).getType());
    assertEquals(IF_ICMPEQ, instructions.get(8).getType());
}
Also used : IfICmpEq(net.runelite.asm.attributes.code.instructions.IfICmpEq) IStore(net.runelite.asm.attributes.code.instructions.IStore) ILoad(net.runelite.asm.attributes.code.instructions.ILoad) Label(net.runelite.asm.attributes.code.Label) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.InstructionType.LDC) LDC(net.runelite.asm.attributes.code.instructions.LDC) Instruction(net.runelite.asm.attributes.code.Instruction) IAnd(net.runelite.asm.attributes.code.instructions.IAnd) Code(net.runelite.asm.attributes.Code) VReturn(net.runelite.asm.attributes.code.instructions.VReturn) ClassGroup(net.runelite.asm.ClassGroup) IAdd(net.runelite.asm.attributes.code.instructions.IAdd) Test(org.junit.Test)

Example 13 with IAdd

use of net.runelite.asm.attributes.code.instructions.IAdd in project runelite by runelite.

the class ExprArgOrderTest method test.

@Test
public void test() {
    ClassGroup group = ClassGroupFactory.generateGroup();
    Code code = group.findClass("test").findMethod("func").getCode();
    Instructions ins = code.getInstructions();
    code.setMaxStack(2);
    // vars[0] = 3
    Instruction[] prepareVariables = { new LDC(ins, 3), new IStore(ins, 0) };
    for (Instruction i : prepareVariables) {
        ins.addInstruction(i);
    }
    Instruction[] body = { // 2
    new LDC(ins, 3), new ILoad(ins, 0), new IAdd(ins), new Pop(ins), new VReturn(ins) };
    for (Instruction i : body) {
        ins.addInstruction(i);
    }
    ExprArgOrder exprArgOrder = new ExprArgOrder();
    exprArgOrder.run(group);
    List<Instruction> instructions = ins.getInstructions();
    assertEquals(ILOAD, instructions.get(2).getType());
    assertEquals(LDC, instructions.get(3).getType());
    assertEquals(IADD, instructions.get(4).getType());
}
Also used : IStore(net.runelite.asm.attributes.code.instructions.IStore) ILoad(net.runelite.asm.attributes.code.instructions.ILoad) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.InstructionType.LDC) LDC(net.runelite.asm.attributes.code.instructions.LDC) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) VReturn(net.runelite.asm.attributes.code.instructions.VReturn) Pop(net.runelite.asm.attributes.code.instructions.Pop) ClassGroup(net.runelite.asm.ClassGroup) IAdd(net.runelite.asm.attributes.code.instructions.IAdd) Test(org.junit.Test)

Example 14 with IAdd

use of net.runelite.asm.attributes.code.instructions.IAdd in project runelite by runelite.

the class ModArith method findUses.

// find potential getters/setters for each field
private void findUses(MethodContext mctx) {
    for (InstructionContext ctx : mctx.getInstructionContexts()) {
        if (ctx.getInstruction() instanceof IMul || ctx.getInstruction() instanceof LMul) {
            Instruction one = ctx.getPops().get(0).getPushed().getInstruction();
            Instruction two = ctx.getPops().get(1).getPushed().getInstruction();
            PushConstantInstruction pc = null;
            GetFieldInstruction gf = null;
            if (one instanceof PushConstantInstruction && two instanceof GetFieldInstruction) {
                pc = (PushConstantInstruction) one;
                gf = (GetFieldInstruction) two;
            } else if (two instanceof PushConstantInstruction && one instanceof GetFieldInstruction) {
                pc = (PushConstantInstruction) two;
                gf = (GetFieldInstruction) one;
            }
            if (pc == null) {
                continue;
            }
            Field field = gf.getMyField();
            if (field == null) {
                continue;
            }
            FieldInfo fieldInfo = getFieldInfo(field);
            // parse the full multiplication expression to
            // get all associated constants
            List<InstructionContext> insInExpr = getInsInExpr(ctx, new HashSet(), true);
            for (InstructionContext ctx2 : insInExpr) {
                if (!(ctx2.getInstruction() instanceof PushConstantInstruction)) {
                    continue;
                }
                PushConstantInstruction pci3 = (PushConstantInstruction) ctx2.getInstruction();
                Number value = (Number) pci3.getConstant();
                // field * constant
                if (value instanceof Integer || value instanceof Long) {
                    fieldInfo.getters.add(value);
                }
            }
        } else if (ctx.getInstruction() instanceof SetFieldInstruction) {
            SetFieldInstruction sf = (SetFieldInstruction) ctx.getInstruction();
            Field field = sf.getMyField();
            if (field == null) {
                continue;
            }
            FieldInfo fieldInfo = getFieldInfo(field);
            // value being set
            InstructionContext pushedsfi = ctx.getPops().get(0).getPushed();
            pushedsfi = pushedsfi.resolve(ctx.getPops().get(0));
            if (!(pushedsfi.getInstruction() instanceof IMul) && !(pushedsfi.getInstruction() instanceof LMul) && !(pushedsfi.getInstruction() instanceof IAdd) && !(pushedsfi.getInstruction() instanceof LAdd) && !(pushedsfi.getInstruction() instanceof ISub) && !(pushedsfi.getInstruction() instanceof LSub)) {
                if (pushedsfi.getInstruction() instanceof LDC) {
                    PushConstantInstruction ldc = (PushConstantInstruction) pushedsfi.getInstruction();
                    if (ldc.getConstant() instanceof Integer || ldc.getConstant() instanceof Long) {
                        Number i = (Number) ldc.getConstant();
                        // field = constant
                        fieldInfo.setters.add(i);
                    }
                }
                continue;
            }
            Instruction one = pushedsfi.getPops().get(0).getPushed().getInstruction();
            Instruction two = pushedsfi.getPops().get(1).getPushed().getInstruction();
            // field = field + imul
            if (pushedsfi.getInstruction() instanceof IAdd) {
                if (one instanceof IMul && two instanceof GetFieldInstruction) {
                    one = pushedsfi.getPops().get(0).getPushed().getPops().get(0).getPushed().getInstruction();
                    two = pushedsfi.getPops().get(0).getPushed().getPops().get(1).getPushed().getInstruction();
                }
            }
            // if both one and two are constants then one of them must not be a setter
            PushConstantInstruction pc = null;
            if (one instanceof PushConstantInstruction && !(two instanceof PushConstantInstruction)) {
                pc = (PushConstantInstruction) one;
            } else if (two instanceof PushConstantInstruction && !(one instanceof PushConstantInstruction)) {
                pc = (PushConstantInstruction) two;
            }
            if (pc == null) {
                continue;
            }
            Number value2 = (Number) pc.getConstant();
            // field = something * constant
            if (value2 instanceof Integer || value2 instanceof Long) {
                fieldInfo.setters.add(value2);
            }
        }
    }
}
Also used : InstructionContext(net.runelite.asm.execution.InstructionContext) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) LDC(net.runelite.asm.attributes.code.instructions.LDC) DivisionInstruction(net.runelite.asm.attributes.code.instruction.types.DivisionInstruction) FieldInstruction(net.runelite.asm.attributes.code.instruction.types.FieldInstruction) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) ArrayStoreInstruction(net.runelite.asm.attributes.code.instruction.types.ArrayStoreInstruction) InvokeInstruction(net.runelite.asm.attributes.code.instruction.types.InvokeInstruction) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) Field(net.runelite.asm.Field) ISub(net.runelite.asm.attributes.code.instructions.ISub) LAdd(net.runelite.asm.attributes.code.instructions.LAdd) LSub(net.runelite.asm.attributes.code.instructions.LSub) IMul(net.runelite.asm.attributes.code.instructions.IMul) IAdd(net.runelite.asm.attributes.code.instructions.IAdd) LMul(net.runelite.asm.attributes.code.instructions.LMul) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction) HashSet(java.util.HashSet)

Aggregations

IAdd (net.runelite.asm.attributes.code.instructions.IAdd)14 LDC (net.runelite.asm.attributes.code.instructions.LDC)13 Instruction (net.runelite.asm.attributes.code.Instruction)12 ClassGroup (net.runelite.asm.ClassGroup)10 Code (net.runelite.asm.attributes.Code)10 Instructions (net.runelite.asm.attributes.code.Instructions)10 Test (org.junit.Test)10 ILoad (net.runelite.asm.attributes.code.instructions.ILoad)9 IStore (net.runelite.asm.attributes.code.instructions.IStore)9 VReturn (net.runelite.asm.attributes.code.instructions.VReturn)9 LDC (net.runelite.asm.attributes.code.InstructionType.LDC)6 IMul (net.runelite.asm.attributes.code.instructions.IMul)6 Pop (net.runelite.asm.attributes.code.instructions.Pop)6 PushConstantInstruction (net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction)3 IfICmpEq (net.runelite.asm.attributes.code.instructions.IfICmpEq)3 SiPush (net.runelite.asm.attributes.code.instructions.SiPush)3 Execution (net.runelite.asm.execution.Execution)3 Deobfuscator (net.runelite.deob.Deobfuscator)3 Type (net.runelite.asm.Type)2 Label (net.runelite.asm.attributes.code.Label)2