Search in sources :

Example 31 with Instruction

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

the class ControlFlowDeobfuscator method split.

/**
 * Add gotos at the end of blocks without terminal instructions
 *
 * @param code
 */
private void split(Code code) {
    Instructions ins = code.getInstructions();
    Exceptions exceptions = code.getExceptions();
    ControlFlowGraph graph = new ControlFlowGraph.Builder().build(code);
    List<Exception> exc = new ArrayList<>(exceptions.getExceptions());
    // Must clear this before ins.clear() runs
    exceptions.clear();
    ins.clear();
    // insert jumps where blocks flow into others
    for (Block block : graph.getBlocks()) {
        if (block.getFlowsInto() == null) {
            continue;
        }
        Block into = block.getFlowsInto();
        assert into.getFlowsFrom() == block;
        Instruction first = into.getInstructions().get(0);
        Label label;
        if (!(first instanceof Label)) {
            label = new Label(null);
            into.addInstruction(0, label);
        } else {
            label = (Label) first;
        }
        Goto g = new Goto(null, label);
        block.addInstruction(g);
        block.setFlowsInto(null);
        into.setFlowsFrom(null);
        ++insertedJump;
    }
    // Readd instructions from modified blocks
    for (Block block : graph.getBlocks()) {
        for (Instruction i : block.getInstructions()) {
            assert i.getInstructions() == null;
            // I shouldn't have to do this here
            i.setInstructions(ins);
            ins.addInstruction(i);
        }
    }
    // Readd exceptions
    for (Exception ex : exc) {
        exceptions.add(ex);
    }
}
Also used : Goto(net.runelite.asm.attributes.code.instructions.Goto) Exceptions(net.runelite.asm.attributes.code.Exceptions) ArrayList(java.util.ArrayList) Label(net.runelite.asm.attributes.code.Label) Instructions(net.runelite.asm.attributes.code.Instructions) Instruction(net.runelite.asm.attributes.code.Instruction) Exception(net.runelite.asm.attributes.code.Exception)

Example 32 with Instruction

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

the class FieldInliner method findFieldIns.

private void findFieldIns() {
    for (ClassFile cf : group.getClasses()) {
        for (Method m : cf.getMethods()) {
            Code code = m.getCode();
            if (code == null)
                continue;
            for (Instruction i : code.getInstructions().getInstructions()) {
                if (!(i instanceof FieldInstruction))
                    continue;
                FieldInstruction sf = (FieldInstruction) i;
                if (sf.getMyField() == null)
                    continue;
                fieldInstructions.put(sf.getMyField(), sf);
            }
        }
    }
}
Also used : ClassFile(net.runelite.asm.ClassFile) Method(net.runelite.asm.Method) FieldInstruction(net.runelite.asm.attributes.code.instruction.types.FieldInstruction) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction) FieldInstruction(net.runelite.asm.attributes.code.instruction.types.FieldInstruction) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) 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) Code(net.runelite.asm.attributes.Code)

Example 33 with Instruction

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

the class FieldInliner method inlineUse.

public int inlineUse() {
    int count = 0;
    for (Field f : fields) {
        // replace getfield with constant push
        List<FieldInstruction> fins = fieldInstructions.get(f).stream().filter(f2 -> f2 instanceof GetFieldInstruction).collect(Collectors.toList());
        Object value = f.getValue();
        for (FieldInstruction fin : fins) {
            // remove fin, add push constant
            Instruction i = (Instruction) fin;
            Instruction pushIns = new LDC(i.getInstructions(), value);
            List<Instruction> instructions = i.getInstructions().getInstructions();
            int idx = instructions.indexOf(i);
            assert idx != -1;
            i.getInstructions().remove(i);
            instructions.add(idx, pushIns);
            ++count;
        }
        f.getClassFile().removeField(f);
    }
    return count;
}
Also used : Logger(org.slf4j.Logger) FieldInstruction(net.runelite.asm.attributes.code.instruction.types.FieldInstruction) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) Field(net.runelite.asm.Field) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) LoggerFactory(org.slf4j.LoggerFactory) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction) Code(net.runelite.asm.attributes.Code) Multimap(com.google.common.collect.Multimap) Type(net.runelite.asm.Type) Deobfuscator(net.runelite.deob.Deobfuscator) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ClassGroup(net.runelite.asm.ClassGroup) List(java.util.List) ClassFile(net.runelite.asm.ClassFile) HashMultimap(com.google.common.collect.HashMultimap) Method(net.runelite.asm.Method) LDC(net.runelite.asm.attributes.code.instructions.LDC) Instructions(net.runelite.asm.attributes.code.Instructions) Instruction(net.runelite.asm.attributes.code.Instruction) Field(net.runelite.asm.Field) LDC(net.runelite.asm.attributes.code.instructions.LDC) FieldInstruction(net.runelite.asm.attributes.code.instruction.types.FieldInstruction) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction) FieldInstruction(net.runelite.asm.attributes.code.instruction.types.FieldInstruction) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) 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) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction)

Example 34 with Instruction

use of net.runelite.asm.attributes.code.Instruction 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 35 with Instruction

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

the class PacketHandlerOrder method insertPacketLength.

private void insertPacketLength(ClassGroup group, PacketTypeFinder ptf) {
    PacketLengthFinder pfl = new PacketLengthFinder(group, ptf);
    pfl.find();
    GetStatic getArray = pfl.getGetArray();
    // instruction to store packet length
    PutStatic ps = pfl.getStore();
    Instructions instructions = ps.getInstructions();
    List<Instruction> ins = instructions.getInstructions();
    Label getArrayLabel = instructions.createLabelFor(getArray);
    Label storeLabel = instructions.createLabelFor(ps);
    int idx = ins.indexOf(getArray);
    assert idx != -1;
    // to go before label, which must exist
    --idx;
    net.runelite.asm.pool.Field field = new net.runelite.asm.pool.Field(new net.runelite.asm.pool.Class(findClient(group).getName()), RUNELITE_PACKET, Type.BOOLEAN);
    instructions.addInstruction(idx++, new GetStatic(instructions, field));
    instructions.addInstruction(idx++, new IfEq(instructions, getArrayLabel));
    // 2 byte length
    instructions.addInstruction(idx++, new LDC(instructions, -2));
    instructions.addInstruction(idx++, new Goto(instructions, storeLabel));
}
Also used : Goto(net.runelite.asm.attributes.code.instructions.Goto) Label(net.runelite.asm.attributes.code.Label) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) PacketLengthFinder(net.runelite.deob.deobfuscators.packethandler.PacketLengthFinder) IfEq(net.runelite.asm.attributes.code.instructions.IfEq) PutStatic(net.runelite.asm.attributes.code.instructions.PutStatic) 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) Field(net.runelite.asm.Field) GetStatic(net.runelite.asm.attributes.code.instructions.GetStatic)

Aggregations

Instruction (net.runelite.asm.attributes.code.Instruction)109 Instructions (net.runelite.asm.attributes.code.Instructions)69 Code (net.runelite.asm.attributes.Code)48 LDC (net.runelite.asm.attributes.code.instructions.LDC)39 LVTInstruction (net.runelite.asm.attributes.code.instruction.types.LVTInstruction)32 PushConstantInstruction (net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction)32 ClassGroup (net.runelite.asm.ClassGroup)31 InvokeInstruction (net.runelite.asm.attributes.code.instruction.types.InvokeInstruction)29 IMul (net.runelite.asm.attributes.code.instructions.IMul)28 VReturn (net.runelite.asm.attributes.code.instructions.VReturn)28 Test (org.junit.Test)27 ILoad (net.runelite.asm.attributes.code.instructions.ILoad)25 Method (net.runelite.asm.Method)24 IStore (net.runelite.asm.attributes.code.instructions.IStore)24 Execution (net.runelite.asm.execution.Execution)23 Deobfuscator (net.runelite.deob.Deobfuscator)22 Label (net.runelite.asm.attributes.code.Label)19 ArrayList (java.util.ArrayList)17 InstructionContext (net.runelite.asm.execution.InstructionContext)17 Field (net.runelite.asm.Field)16