Search in sources :

Example 6 with If

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

the class PacketWriteDeobfuscator method isEnd.

private boolean isEnd(InstructionContext ctx) {
    // conditions where packet write ends:
    // any invoke that isn't to the packet buffer
    // any variable assignment
    // any field assignment
    // any conditional jump
    // any return
    Instruction i = ctx.getInstruction();
    if (i instanceof InvokeInstruction) {
        InvokeInstruction ii = (InvokeInstruction) i;
        Method method = ii.getMethod();
        if (!method.getClazz().equals(rw.getSecretBuffer().getPoolClass()) && !method.getClazz().equals(rw.getBuffer().getPoolClass())) {
            return true;
        }
    }
    if (i instanceof LVTInstruction) {
        LVTInstruction lvt = (LVTInstruction) i;
        if (lvt.store()) {
            return true;
        }
    }
    if (i instanceof SetFieldInstruction) {
        return true;
    }
    if (i instanceof If || i instanceof If0) {
        return true;
    }
    if (i instanceof ReturnInstruction) {
        return true;
    }
    return false;
}
Also used : InvokeInstruction(net.runelite.asm.attributes.code.instruction.types.InvokeInstruction) ReturnInstruction(net.runelite.asm.attributes.code.instruction.types.ReturnInstruction) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) If0(net.runelite.asm.attributes.code.instructions.If0) Method(net.runelite.asm.pool.Method) InvokeInstruction(net.runelite.asm.attributes.code.instruction.types.InvokeInstruction) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) ReturnInstruction(net.runelite.asm.attributes.code.instruction.types.ReturnInstruction) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) If(net.runelite.asm.attributes.code.instructions.If)

Example 7 with If

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

the class ModArith method guess.

private void guess() {
    for (ClassFile cf : group.getClasses()) {
        for (Field f : cf.getFields()) {
            FieldInfo fieldInfo = getFieldInfo(f);
            // all constants in instructions associated with the field
            Collection<AssociatedConstant> col = fieldInfo.constants;
            if (col.isEmpty()) {
                continue;
            }
            Type type = f.getType();
            assert type.equals(Type.INT) || type.equals(Type.LONG);
            Class typeOfField = type.equals(Type.INT) ? Integer.class : Long.class;
            // filter collect constants of the correct type
            Collection<AssociatedConstant> col2 = col.stream().filter(i -> i.value.getClass() == typeOfField).collect(Collectors.toList());
            // filer out ones that have another field in the expression
            List<Number> noOther = col2.stream().filter(i -> !i.other && !i.constant).map(i -> i.value).distinct().collect(Collectors.toList());
            List<Number> other = col2.stream().filter(i -> i.other || i.constant).map(i -> i.value).collect(Collectors.toList());
            other.addAll(noOther);
            other = ImmutableSet.copyOf(other).asList();
            // guess with constants not associated with other fields
            Pair p = this.guess(f, noOther);
            if (p == null) {
                // fall back to all constants
                p = this.guess(f, other);
            }
            // check that this guess doesn't increase constants
            if (p != null && !fieldInfo.guessDecreasesConstants(p)) {
                continue;
            }
            if (p != null) {
                pairs.add(p);
            }
        }
    }
}
Also used : 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) IMul(net.runelite.asm.attributes.code.instructions.IMul) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Code(net.runelite.asm.attributes.Code) ArrayList(java.util.ArrayList) ClassGroup(net.runelite.asm.ClassGroup) StackContext(net.runelite.asm.execution.StackContext) HashSet(java.util.HashSet) ArrayStoreInstruction(net.runelite.asm.attributes.code.instruction.types.ArrayStoreInstruction) DMath.modInverse(net.runelite.deob.deobfuscators.arithmetic.DMath.modInverse) Method(net.runelite.asm.Method) Map(java.util.Map) InvokeInstruction(net.runelite.asm.attributes.code.instruction.types.InvokeInstruction) If(net.runelite.asm.attributes.code.instructions.If) LAdd(net.runelite.asm.attributes.code.instructions.LAdd) ISub(net.runelite.asm.attributes.code.instructions.ISub) ArrayLoad(net.runelite.asm.attributes.code.instruction.types.ArrayLoad) ImmutableSet(com.google.common.collect.ImmutableSet) DeobAnnotations(net.runelite.deob.DeobAnnotations) Logger(org.slf4j.Logger) Collection(java.util.Collection) LCmp(net.runelite.asm.attributes.code.instructions.LCmp) Field(net.runelite.asm.Field) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) Set(java.util.Set) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction) DMath.multiply(net.runelite.deob.deobfuscators.arithmetic.DMath.multiply) Type(net.runelite.asm.Type) Deobfuscator(net.runelite.deob.Deobfuscator) Collectors(java.util.stream.Collectors) InstructionContext(net.runelite.asm.execution.InstructionContext) Execution(net.runelite.asm.execution.Execution) List(java.util.List) ClassFile(net.runelite.asm.ClassFile) LDC(net.runelite.asm.attributes.code.instructions.LDC) MethodContext(net.runelite.asm.execution.MethodContext) Instructions(net.runelite.asm.attributes.code.Instructions) LMul(net.runelite.asm.attributes.code.instructions.LMul) IAdd(net.runelite.asm.attributes.code.instructions.IAdd) Instruction(net.runelite.asm.attributes.code.Instruction) If0(net.runelite.asm.attributes.code.instructions.If0) LSub(net.runelite.asm.attributes.code.instructions.LSub) IShR(net.runelite.asm.attributes.code.instructions.IShR) Field(net.runelite.asm.Field) Type(net.runelite.asm.Type) ClassFile(net.runelite.asm.ClassFile)

Example 8 with If

use of net.runelite.asm.attributes.code.instructions.If 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)

Example 9 with If

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

the class ConstantParameter method removeDeadOperations.

// remove logically dead comparisons
private int removeDeadOperations(MethodContext mctx) {
    int count = 0;
    for (ConstantMethodParameter cmp : parameters.values()) {
        if (cmp.invalid) {
            continue;
        }
        if (!cmp.methods.contains(mctx.getMethod())) {
            continue;
        }
        // only annotate garbage value of last param
        if (cmp.paramIndex + 1 == mctx.getMethod().getDescriptor().size()) {
            annotateObfuscatedSignature(cmp);
        }
        for (// comparisons
        Instruction ins : // comparisons
        cmp.operations) {
            if (ins.getInstructions() == null || ins.getInstructions().getCode().getMethod() != mctx.getMethod()) {
                continue;
            }
            InstructionContext ctx = mctx.getInstructonContexts(ins).toArray(new InstructionContext[0])[0];
            // branch that is always taken
            boolean branch = cmp.result;
            if (ins.getInstructions() == null) {
                // ins already removed?
                continue;
            }
            Instructions instructions = ins.getInstructions();
            // remove the if
            if (ctx.getInstruction() instanceof If) {
                ctx.removeStack(1);
            }
            ctx.removeStack(0);
            int idx = instructions.getInstructions().indexOf(ins);
            if (idx == -1) {
                // already removed?
                continue;
            }
            ++count;
            Instruction to;
            if (branch) {
                JumpingInstruction jumpIns = (JumpingInstruction) ins;
                assert jumpIns.getJumps().size() == 1;
                to = jumpIns.getJumps().get(0);
            } else {
                // just go to next instruction
                to = instructions.getInstructions().get(idx + 1);
            }
            assert to.getInstructions() == instructions;
            assert ins != to;
            assert instructions.getInstructions().contains(to);
            instructions.remove(ins);
            assert instructions.getInstructions().contains(to);
            if (branch) {
                Goto gotoins = new Goto(instructions, instructions.createLabelFor(to));
                // insert goto
                instructions.getInstructions().add(idx, gotoins);
            }
        }
    }
    return count;
}
Also used : InstructionContext(net.runelite.asm.execution.InstructionContext) JumpingInstruction(net.runelite.asm.attributes.code.instruction.types.JumpingInstruction) Goto(net.runelite.asm.attributes.code.instructions.Goto) Instructions(net.runelite.asm.attributes.code.Instructions) 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) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) ComparisonInstruction(net.runelite.asm.attributes.code.instruction.types.ComparisonInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) If(net.runelite.asm.attributes.code.instructions.If)

Example 10 with If

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

the class HandlerFinder method findHandlers.

private List<PacketHandler> findHandlers(Method process, Field packetOpcode) {
    List<PacketHandler> handlers = new ArrayList<>();
    Instructions ins = process.getCode().getInstructions();
    for (int j = 0; j < ins.getInstructions().size(); ++j) {
        Instruction i = ins.getInstructions().get(j);
        if (i.getType() != InstructionType.GETSTATIC) {
            continue;
        }
        GetStatic gs = (GetStatic) i;
        if (gs.getMyField() != packetOpcode) {
            continue;
        }
        Instruction push = ins.getInstructions().get(j + 1);
        if (!(push instanceof PushConstantInstruction)) {
            continue;
        }
        PushConstantInstruction pci = (PushConstantInstruction) push;
        if (!(pci.getConstant() instanceof Number)) {
            continue;
        }
        int opcode = ((Number) pci.getConstant()).intValue();
        if (opcode == -1) {
            continue;
        }
        Instruction jump = ins.getInstructions().get(j + 2);
        if (jump.getType() != InstructionType.IF_ICMPEQ && jump.getType() != InstructionType.IF_ICMPNE) {
            continue;
        }
        Instruction start, end;
        if (jump.getType() == InstructionType.IF_ICMPEQ) {
            // this seems to not ever happen
            start = ((If) jump).getJumps().get(0);
            // end = ins.getInstructions().get(j + 3);
            end = null;
        } else {
            start = ins.getInstructions().get(j + 3);
            end = ((If) jump).getJumps().get(0);
        }
        PacketHandler handler = new PacketHandler(process, jump, start, push, opcode);
        handlers.add(handler);
        if (end != null) {
            // Anything else which jumps to here instead needs to return.
            insertReturn(ins, jump, end);
        }
        logger.info("Found packet handler {} opcode {}", handler, handler.getOpcode());
    }
    return handlers;
}
Also used : GetStatic(net.runelite.asm.attributes.code.instructions.GetStatic) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) ArrayList(java.util.ArrayList) Instructions(net.runelite.asm.attributes.code.Instructions) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) JumpingInstruction(net.runelite.asm.attributes.code.instruction.types.JumpingInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) If(net.runelite.asm.attributes.code.instructions.If)

Aggregations

If (net.runelite.asm.attributes.code.instructions.If)10 Instruction (net.runelite.asm.attributes.code.Instruction)8 Instructions (net.runelite.asm.attributes.code.Instructions)7 PushConstantInstruction (net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction)7 ArrayList (java.util.ArrayList)6 LVTInstruction (net.runelite.asm.attributes.code.instruction.types.LVTInstruction)6 InvokeInstruction (net.runelite.asm.attributes.code.instruction.types.InvokeInstruction)5 Goto (net.runelite.asm.attributes.code.instructions.Goto)5 InstructionContext (net.runelite.asm.execution.InstructionContext)5 ComparisonInstruction (net.runelite.asm.attributes.code.instruction.types.ComparisonInstruction)4 JumpingInstruction (net.runelite.asm.attributes.code.instruction.types.JumpingInstruction)4 If0 (net.runelite.asm.attributes.code.instructions.If0)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 ClassFile (net.runelite.asm.ClassFile)3 ClassGroup (net.runelite.asm.ClassGroup)3 Method (net.runelite.asm.Method)3 SetFieldInstruction (net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction)3 Execution (net.runelite.asm.execution.Execution)3 StackContext (net.runelite.asm.execution.StackContext)3