Search in sources :

Example 81 with InstructionContext

use of net.runelite.asm.execution.InstructionContext in project runelite by runelite.

the class MultiplicationDeobfuscator method visit.

private void visit(MethodContext ctx) {
    for (InstructionContext ictx : ctx.getInstructionContexts()) {
        Instruction instruction = ictx.getInstruction();
        if (!(instruction instanceof IMul) && !(instruction instanceof LMul)) {
            continue;
        }
        MultiplicationExpression expression;
        try {
            expression = parseExpression(ictx, instruction.getClass());
        } catch (IllegalStateException ex) {
            continue;
        }
        if (expression == null) {
            continue;
        }
        if (done.contains(instruction)) {
            continue;
        }
        done.add(instruction);
        assert instruction instanceof IMul || instruction instanceof LMul;
        if (instruction instanceof IMul) {
            count += expression.simplify(1);
        } else if (instruction instanceof LMul) {
            count += expression.simplify(1L);
        } else {
            throw new IllegalStateException();
        }
    }
}
Also used : InstructionContext(net.runelite.asm.execution.InstructionContext) IMul(net.runelite.asm.attributes.code.instructions.IMul) LMul(net.runelite.asm.attributes.code.instructions.LMul) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) DupInstruction(net.runelite.asm.attributes.code.instruction.types.DupInstruction) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction) Instruction(net.runelite.asm.attributes.code.Instruction)

Example 82 with InstructionContext

use of net.runelite.asm.execution.InstructionContext in project runelite by runelite.

the class MultiplicationExpression method simplify.

int simplify(Number start) {
    int count = 0;
    Number result = start;
    // calculate result
    for (InstructionContext i : instructions) {
        PushConstantInstruction pci = (PushConstantInstruction) i.getInstruction();
        Number value = (Number) pci.getConstant();
        result = DMath.multiply(result, value);
    }
    if (dupmagic != null) {
        // mul dupmagic by result of dup ins?
        PushConstantInstruction pci = (PushConstantInstruction) dupmagic.getInstruction();
        Number value = (Number) pci.getConstant();
        for (InstructionContext ic : dupedInstructions) {
            PushConstantInstruction pci2 = (PushConstantInstruction) ic.getInstruction();
            Number value2 = (Number) pci2.getConstant();
            value = DMath.multiply(value, value2);
        }
        Instruction newIns = pci.setConstant(value);
        assert newIns == (Instruction) pci;
    }
    // multiply subexpressions by result
    if (!subexpressions.isEmpty()) {
        for (MultiplicationExpression me : subexpressions) {
            count += me.simplify(result);
        }
        if (dupmagic != null) {
            PushConstantInstruction pci = (PushConstantInstruction) dupmagic.getInstruction();
            Number value = (Number) pci.getConstant();
            value = DMath.multiply(value, DMath.modInverse(result));
            pci.setConstant(value);
        }
        // constant has been distributed, outer numbers all go to 1
        if (result instanceof Long)
            result = 1L;
        else
            result = 1;
    }
    // set result on ins
    for (InstructionContext i : instructions) {
        PushConstantInstruction pci = (PushConstantInstruction) i.getInstruction();
        Instruction newIns = pci.setConstant(result);
        ++count;
        assert newIns == pci;
        // rest of the results go to 1
        if (result instanceof Long)
            result = 1L;
        else
            result = 1;
    }
    return count;
}
Also used : InstructionContext(net.runelite.asm.execution.InstructionContext) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) FieldInstruction(net.runelite.asm.attributes.code.instruction.types.FieldInstruction) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) Instruction(net.runelite.asm.attributes.code.Instruction)

Example 83 with InstructionContext

use of net.runelite.asm.execution.InstructionContext in project runelite by runelite.

the class UnusedParameters method removeParameter.

public void removeParameter(ClassGroup group, List<Method> methods, Signature signature, Execution execution, int paramIndex, int lvtIndex) {
    int slots = signature.getTypeOfArg(paramIndex).getSize();
    for (ClassFile cf : group.getClasses()) {
        for (Method m : cf.getMethods()) {
            Code c = m.getCode();
            if (c == null) {
                continue;
            }
            for (Instruction i : new ArrayList<>(c.getInstructions().getInstructions())) {
                if (!(i instanceof InvokeInstruction)) {
                    continue;
                }
                InvokeInstruction ii = (InvokeInstruction) i;
                if (!ii.getMethods().stream().anyMatch(me -> methods.contains(me))) {
                    continue;
                }
                // remove parameter from instruction
                ii.removeParameter(paramIndex);
                Collection<InstructionContext> ics = invokes.get(i);
                assert ics != null;
                if (ics != null) {
                    for (InstructionContext ins : ics) {
                        // index from top of stack of parameter. 0 is the last parameter
                        int pops = signature.size() - paramIndex - 1;
                        StackContext sctx = ins.getPops().get(pops);
                        if (sctx.getPushed().getInstruction().getInstructions() == null) {
                            continue;
                        }
                        // remove parameter from stack
                        ins.removeStack(pops);
                    }
                }
            }
        }
    }
    for (Method method : methods) {
        if (method.getCode() != null) // adjust lvt indexes to get rid of idx in the method
        {
            for (Instruction ins : method.getCode().getInstructions().getInstructions()) {
                if (ins instanceof LVTInstruction) {
                    LVTInstruction lins = (LVTInstruction) ins;
                    int i = lins.getVariableIndex();
                    // current unused variable detection just looks for no accesses
                    assert i != lvtIndex;
                    // reassign
                    if (i > lvtIndex) {
                        assert i > 0;
                        assert i >= lvtIndex + slots;
                        Instruction newIns = lins.setVariableIndex(i - slots);
                        assert ins == newIns;
                    }
                }
            }
        }
    }
    for (Method method : methods) {
        method.getDescriptor().remove(paramIndex);
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Code(net.runelite.asm.attributes.Code) Multimap(com.google.common.collect.Multimap) ArrayList(java.util.ArrayList) ClassGroup(net.runelite.asm.ClassGroup) StackContext(net.runelite.asm.execution.StackContext) HashMultimap(com.google.common.collect.HashMultimap) Method(net.runelite.asm.Method) Map(java.util.Map) InvokeInstruction(net.runelite.asm.attributes.code.instruction.types.InvokeInstruction) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) VirtualMethods(net.runelite.asm.signature.util.VirtualMethods) ImmutableSet(com.google.common.collect.ImmutableSet) DeobAnnotations(net.runelite.deob.DeobAnnotations) Logger(org.slf4j.Logger) Deob(net.runelite.deob.Deob) Collection(java.util.Collection) Set(java.util.Set) Deobfuscator(net.runelite.deob.Deobfuscator) InstructionContext(net.runelite.asm.execution.InstructionContext) Sets(com.google.common.collect.Sets) Execution(net.runelite.asm.execution.Execution) List(java.util.List) ClassFile(net.runelite.asm.ClassFile) Signature(net.runelite.asm.signature.Signature) Instruction(net.runelite.asm.attributes.code.Instruction) Collections(java.util.Collections) InvokeInstruction(net.runelite.asm.attributes.code.instruction.types.InvokeInstruction) InstructionContext(net.runelite.asm.execution.InstructionContext) ClassFile(net.runelite.asm.ClassFile) StackContext(net.runelite.asm.execution.StackContext) ArrayList(java.util.ArrayList) Method(net.runelite.asm.Method) InvokeInstruction(net.runelite.asm.attributes.code.instruction.types.InvokeInstruction) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) Code(net.runelite.asm.attributes.Code)

Example 84 with InstructionContext

use of net.runelite.asm.execution.InstructionContext in project runelite by runelite.

the class DupDeobfuscator method copy.

/**
 * copy the instruction which pushed sctx and insert into instructions
 * starting at index idx
 */
private int copy(StackContext sctx, Instructions instructions, int idx) {
    InstructionContext ictx = sctx.getPushed();
    if (ictx.getInstruction() instanceof DupInstruction) {
        // we only care about one path
        DupInstruction di = (DupInstruction) ictx.getInstruction();
        sctx = di.getOriginal(sctx);
        ictx = sctx.getPushed();
    }
    // the first thing popped was pushed last, so reverse
    for (StackContext s : Lists.reverse(ictx.getPops())) {
        idx = copy(s, instructions, idx);
    }
    // copy instruction
    Instruction i = ictx.getInstruction();
    logger.debug("Duplicating instruction {} at index {}", i, idx);
    i = i.clone();
    instructions.addInstruction(idx, i);
    // move on to next instruction
    return idx + 1;
}
Also used : InstructionContext(net.runelite.asm.execution.InstructionContext) StackContext(net.runelite.asm.execution.StackContext) DupInstruction(net.runelite.asm.attributes.code.instruction.types.DupInstruction) DupInstruction(net.runelite.asm.attributes.code.instruction.types.DupInstruction) Instruction(net.runelite.asm.attributes.code.Instruction)

Example 85 with InstructionContext

use of net.runelite.asm.execution.InstructionContext in project runelite by runelite.

the class Pop2 method execute.

@Override
public InstructionContext execute(Frame frame) {
    InstructionContext ins = new InstructionContext(this, frame);
    Stack stack = frame.getStack();
    StackContext value = stack.pop();
    ins.pop(value);
    if (value.getType().getSize() == 2)
        return ins;
    value = stack.pop();
    ins.pop(value);
    return ins;
}
Also used : InstructionContext(net.runelite.asm.execution.InstructionContext) StackContext(net.runelite.asm.execution.StackContext) Stack(net.runelite.asm.execution.Stack)

Aggregations

InstructionContext (net.runelite.asm.execution.InstructionContext)179 StackContext (net.runelite.asm.execution.StackContext)153 Stack (net.runelite.asm.execution.Stack)120 Value (net.runelite.asm.execution.Value)48 Field (net.runelite.asm.Field)18 Instruction (net.runelite.asm.attributes.code.Instruction)18 Variables (net.runelite.asm.execution.Variables)16 GetFieldInstruction (net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction)15 VariableContext (net.runelite.asm.execution.VariableContext)14 Instructions (net.runelite.asm.attributes.code.Instructions)12 Frame (net.runelite.asm.execution.Frame)12 PushConstantInstruction (net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction)11 Execution (net.runelite.asm.execution.Execution)11 LVTInstruction (net.runelite.asm.attributes.code.instruction.types.LVTInstruction)9 InvokeInstruction (net.runelite.asm.attributes.code.instruction.types.InvokeInstruction)8 IMul (net.runelite.asm.attributes.code.instructions.IMul)7 ClassFile (net.runelite.asm.ClassFile)6 Method (net.runelite.asm.Method)6 Label (net.runelite.asm.attributes.code.Label)6 DupInstruction (net.runelite.asm.attributes.code.instruction.types.DupInstruction)6