Search in sources :

Example 6 with Instructions

use of net.runelite.asm.attributes.code.Instructions in project runelite by runelite.

the class RuneliteBufferTransformer method injectLengthHeader.

/**
 * inject the length header after the packet opcode
 *
 * @param group
 */
private void injectLengthHeader(ClassGroup group) {
    RWOpcodeFinder rw = new RWOpcodeFinder(group);
    rw.find();
    Method writeOpcode = rw.getWriteOpcode();
    Code code = writeOpcode.getCode();
    Instructions instructions = code.getInstructions();
    List<Instruction> ins = instructions.getInstructions();
    Instruction start = ins.get(0);
    Instruction end = ins.stream().filter(i -> i.getType() == RETURN).findFirst().get();
    Label labelForStart = instructions.createLabelFor(start);
    Label labelForEnd = instructions.createLabelFor(end);
    final net.runelite.asm.pool.Field runelitePacketField = new net.runelite.asm.pool.Field(new net.runelite.asm.pool.Class(findClient(group).getName()), RUNELITE_PACKET, Type.BOOLEAN);
    int idx = ins.indexOf(labelForStart);
    instructions.addInstruction(idx++, new GetStatic(instructions, runelitePacketField));
    instructions.addInstruction(idx++, new IfEq(instructions, labelForStart));
    net.runelite.asm.pool.Method method = new net.runelite.asm.pool.Method(new net.runelite.asm.pool.Class(writeOpcode.getClassFile().getName()), RUNELITE_FINISH_PACKET, new Signature("()V"));
    instructions.addInstruction(idx++, new ALoad(instructions, 0));
    instructions.addInstruction(idx++, new InvokeVirtual(instructions, method));
    idx = ins.indexOf(labelForEnd);
    instructions.addInstruction(idx++, new GetStatic(instructions, runelitePacketField));
    instructions.addInstruction(idx++, new IfEq(instructions, labelForEnd));
    method = new net.runelite.asm.pool.Method(new net.runelite.asm.pool.Class(writeOpcode.getClassFile().getName()), RUNELITE_INIT_PACKET, new Signature("()V"));
    instructions.addInstruction(idx++, new ALoad(instructions, 0));
    instructions.addInstruction(idx++, new InvokeVirtual(instructions, method));
    logger.info("Injected finish/init packet calls into {}", writeOpcode);
}
Also used : RWOpcodeFinder(net.runelite.deob.c2s.RWOpcodeFinder) Label(net.runelite.asm.attributes.code.Label) Instructions(net.runelite.asm.attributes.code.Instructions) Method(net.runelite.asm.Method) IfEq(net.runelite.asm.attributes.code.instructions.IfEq) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) Field(net.runelite.asm.Field) GetStatic(net.runelite.asm.attributes.code.instructions.GetStatic) InvokeVirtual(net.runelite.asm.attributes.code.instructions.InvokeVirtual) Signature(net.runelite.asm.signature.Signature) ALoad(net.runelite.asm.attributes.code.instructions.ALoad)

Example 7 with Instructions

use of net.runelite.asm.attributes.code.Instructions in project runelite by runelite.

the class MenuActionDeobfuscator method insert.

private void insert(Method method, List<Comparison> comparisons) {
    Instructions instructions = method.getCode().getInstructions();
    List<Instruction> ins = instructions.getInstructions();
    // replace all if(var == constant) with a jump to the false branch
    // then, insert before the first jump the ifs to jump to the old
    // true branch
    // 
    // this is probably actually lookupswitch but it isn't mappable
    // currently...
    int min = -1;
    for (Comparison comp : comparisons) {
        if (min == -1) {
            min = ins.indexOf(comp.lvt);
        } else {
            min = Math.min(min, ins.indexOf(comp.lvt));
        }
        if (comp.cmp.getType() == InstructionType.IF_ICMPEQ) {
            If cmp = (If) comp.cmp;
            // remove
            instructions.remove(comp.ldc);
            instructions.remove((Instruction) comp.lvt);
            instructions.remove(comp.cmp);
            comp.next = cmp.getJumps().get(0);
        } else if (comp.cmp.getType() == InstructionType.IF_ICMPNE) {
            // replace with goto dest
            If cmp = (If) comp.cmp;
            int idx = ins.indexOf(cmp);
            assert idx != -1;
            comp.next = instructions.createLabelFor(ins.get(idx + 1));
            instructions.remove(comp.ldc);
            instructions.remove((Instruction) comp.lvt);
            instructions.replace(comp.cmp, new Goto(instructions, cmp.getJumps().get(0)));
        } else {
            throw new IllegalStateException();
        }
    }
    assert min != -1;
    // sort comparisons - but if they jump to the same address, they are equal..
    List<Comparison> sortedComparisons = new ArrayList<>(comparisons);
    Collections.sort(sortedComparisons, (c1, c2) -> compare(comparisons, c1, c2));
    // reinsert jumps
    for (int i = 0; i < sortedComparisons.size(); ++i) {
        Comparison comp = sortedComparisons.get(i);
        Instruction lvt = (Instruction) comp.lvt;
        lvt.setInstructions(instructions);
        comp.ldc.setInstructions(instructions);
        instructions.addInstruction(min++, lvt);
        instructions.addInstruction(min++, comp.ldc);
        // use if_icmpeq if what follows also jumps to the same location
        boolean multiple = i + 1 < sortedComparisons.size() && sortedComparisons.get(i + 1).next == comp.next;
        if (multiple) {
            instructions.addInstruction(min++, new IfICmpEq(instructions, comp.next));
        } else {
            // fernflower decompiles a series of if_icmpeq as chains of not equal expressions
            Label label = instructions.createLabelFor(ins.get(min));
            instructions.addInstruction(min++, new IfICmpNe(instructions, label));
            instructions.addInstruction(min++, new Goto(instructions, comp.next));
            // go past label
            ++min;
        }
    }
}
Also used : IfICmpEq(net.runelite.asm.attributes.code.instructions.IfICmpEq) Goto(net.runelite.asm.attributes.code.instructions.Goto) ArrayList(java.util.ArrayList) Label(net.runelite.asm.attributes.code.Label) Instructions(net.runelite.asm.attributes.code.Instructions) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) IfICmpNe(net.runelite.asm.attributes.code.instructions.IfICmpNe) If(net.runelite.asm.attributes.code.instructions.If)

Example 8 with Instructions

use of net.runelite.asm.attributes.code.Instructions in project runelite by runelite.

the class ClassGroupFactory method addVoidMethod.

private static void addVoidMethod(ClassFile cf, String name) {
    Method method = new Method(cf, name, new Signature("()V"));
    method.setStatic();
    cf.addMethod(method);
    Code code = new Code(method);
    method.setCode(code);
    Instructions ins = code.getInstructions();
    ins.addInstruction(new VReturn(ins));
}
Also used : Signature(net.runelite.asm.signature.Signature) Instructions(net.runelite.asm.attributes.code.Instructions) Method(net.runelite.asm.Method) Code(net.runelite.asm.attributes.Code) VReturn(net.runelite.asm.attributes.code.instructions.VReturn)

Example 9 with Instructions

use of net.runelite.asm.attributes.code.Instructions in project runelite by runelite.

the class CastNullTest method testRun.

@Test
public void testRun() {
    ClassGroup group = ClassGroupFactory.generateGroup();
    Code code = group.findClass("test").findMethod("func").getCode();
    Instructions ins = code.getInstructions();
    code.setMaxStack(3);
    CheckCast checkCast = new CheckCast(ins);
    checkCast.setType(new Type("test"));
    Instruction[] instructions = { new LDC(ins, 2), new AConstNull(ins), checkCast, new LDC(ins, 2), new IAdd(ins), new Return(ins, InstructionType.IRETURN) };
    for (Instruction i : instructions) {
        ins.addInstruction(i);
    }
    Assert.assertEquals(6, ins.getInstructions().size());
    CastNull lvt = new CastNull();
    lvt.run(group);
    Assert.assertEquals(5, ins.getInstructions().size());
    Optional<Instruction> o = ins.getInstructions().stream().filter(i -> i instanceof CheckCast).findAny();
    Assert.assertFalse(o.isPresent());
}
Also used : AConstNull(net.runelite.asm.attributes.code.instructions.AConstNull) InstructionType(net.runelite.asm.attributes.code.InstructionType) Code(net.runelite.asm.attributes.Code) Test(org.junit.Test) Type(net.runelite.asm.Type) ClassGroup(net.runelite.asm.ClassGroup) ClassGroupFactory(net.runelite.deob.ClassGroupFactory) LDC(net.runelite.asm.attributes.code.instructions.LDC) Return(net.runelite.asm.attributes.code.instructions.Return) Instructions(net.runelite.asm.attributes.code.Instructions) CheckCast(net.runelite.asm.attributes.code.instructions.CheckCast) Optional(java.util.Optional) IAdd(net.runelite.asm.attributes.code.instructions.IAdd) Instruction(net.runelite.asm.attributes.code.Instruction) Assert(org.junit.Assert) Return(net.runelite.asm.attributes.code.instructions.Return) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) AConstNull(net.runelite.asm.attributes.code.instructions.AConstNull) CheckCast(net.runelite.asm.attributes.code.instructions.CheckCast) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) InstructionType(net.runelite.asm.attributes.code.InstructionType) Type(net.runelite.asm.Type) ClassGroup(net.runelite.asm.ClassGroup) IAdd(net.runelite.asm.attributes.code.instructions.IAdd) Test(org.junit.Test)

Example 10 with Instructions

use of net.runelite.asm.attributes.code.Instructions in project runelite by runelite.

the class SimpleModArithTest method checkConstants.

private void checkConstants(ClassFile cf) {
    for (Method m : cf.getMethods()) {
        Code code = m.getCode();
        Instructions instructions = code.getInstructions();
        for (Instruction i : instructions.getInstructions()) {
            if (i instanceof LDC) {
                LDC ldc = (LDC) i;
                Integer value = (Integer) ldc.getConstantAsInt();
                assertFalse(isBig(value));
            }
        }
    }
}
Also used : Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) Method(net.runelite.asm.Method) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code)

Aggregations

Instructions (net.runelite.asm.attributes.code.Instructions)86 Instruction (net.runelite.asm.attributes.code.Instruction)72 Code (net.runelite.asm.attributes.Code)47 LDC (net.runelite.asm.attributes.code.instructions.LDC)40 ClassGroup (net.runelite.asm.ClassGroup)32 VReturn (net.runelite.asm.attributes.code.instructions.VReturn)30 Test (org.junit.Test)30 Method (net.runelite.asm.Method)26 IMul (net.runelite.asm.attributes.code.instructions.IMul)26 ILoad (net.runelite.asm.attributes.code.instructions.ILoad)24 IStore (net.runelite.asm.attributes.code.instructions.IStore)24 Execution (net.runelite.asm.execution.Execution)22 Deobfuscator (net.runelite.deob.Deobfuscator)22 ClassFile (net.runelite.asm.ClassFile)17 Field (net.runelite.asm.Field)17 Type (net.runelite.asm.Type)17 Label (net.runelite.asm.attributes.code.Label)17 PushConstantInstruction (net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction)16 Signature (net.runelite.asm.signature.Signature)16 Pop (net.runelite.asm.attributes.code.instructions.Pop)14