Search in sources :

Example 86 with Instruction

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

the class MultiplyZeroDeobfuscator method visit.

private void visit(MethodContext mctx) {
    for (InstructionContext ictx : mctx.getInstructionContexts()) {
        Instruction instruction = ictx.getInstruction();
        Instructions ins = instruction.getInstructions();
        if (ins == null) {
            continue;
        }
        if (!(instruction instanceof IMul) && !(instruction instanceof LMul)) {
            continue;
        }
        List<Instruction> ilist = ins.getInstructions();
        StackContext one = ictx.getPops().get(0);
        StackContext two = ictx.getPops().get(1);
        Instruction ione = one.getPushed().getInstruction(), itwo = two.getPushed().getInstruction();
        boolean remove = false;
        if (ione instanceof PushConstantInstruction) {
            PushConstantInstruction pci = (PushConstantInstruction) ione;
            Number value = (Number) pci.getConstant();
            if (DMath.equals(value, 0)) {
                remove = true;
            }
        }
        if (itwo instanceof PushConstantInstruction) {
            PushConstantInstruction pci = (PushConstantInstruction) itwo;
            Number value = (Number) pci.getConstant();
            if (DMath.equals(value, 0)) {
                remove = true;
            }
        }
        if (remove == false) {
            continue;
        }
        if (!ilist.contains(instruction)) {
            // already done
            continue;
        }
        if (!MultiplicationDeobfuscator.isOnlyPath(ictx, null)) {
            continue;
        }
        // remove both, remove imul, push 0
        ictx.removeStack(1);
        ictx.removeStack(0);
        if (instruction instanceof IMul) {
            ins.replace(instruction, new LDC(ins, 0));
        } else if (instruction instanceof LMul) {
            ins.replace(instruction, new LDC(ins, 0L));
        } else {
            throw new IllegalStateException();
        }
        ++count;
    }
}
Also used : InstructionContext(net.runelite.asm.execution.InstructionContext) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) StackContext(net.runelite.asm.execution.StackContext) IMul(net.runelite.asm.attributes.code.instructions.IMul) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) LMul(net.runelite.asm.attributes.code.instructions.LMul) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) Instruction(net.runelite.asm.attributes.code.Instruction)

Example 87 with Instruction

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

the class Block method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("Block ID ").append(id).append("\n");
    if (flowsFrom != null) {
        sb.append(" flows from ").append(flowsFrom.id).append("\n");
    }
    for (Instruction i : instructions) {
        sb.append("  ").append(i.toString()).append("\n");
    }
    if (flowsInto != null) {
        sb.append(" flows into ").append(flowsInto.id).append("\n");
    }
    sb.append("\n");
    return sb.toString();
}
Also used : Instruction(net.runelite.asm.attributes.code.Instruction)

Example 88 with Instruction

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

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

the class ExprArgOrder method compare.

public static int compare(Method method, InstructionType type, Expression expr1, Expression expr2) {
    Instruction i1 = expr1.getHead().getInstruction();
    Instruction i2 = expr2.getHead().getInstruction();
    if (type == IF_ICMPEQ || type == IF_ICMPNE || type == IADD || type == IMUL) {
        if (!(i1 instanceof PushConstantInstruction) && (i2 instanceof PushConstantInstruction)) {
            return 1;
        }
        if (i1 instanceof PushConstantInstruction && !(i2 instanceof PushConstantInstruction)) {
            return -1;
        }
    }
    if (type == IF_ACMPEQ || type == IF_ACMPNE) {
        if (!(i1 instanceof AConstNull) && (i2 instanceof AConstNull)) {
            return 1;
        }
        if (i1 instanceof AConstNull && !(i2 instanceof AConstNull)) {
            return -1;
        }
    }
    int hash1 = hash(method, expr1.getHead());
    int hash2 = hash(method, expr2.getHead());
    if (hash1 == hash2) {
        logger.debug("Unable to differentiate {} from {}", expr1.getHead(), expr2.getHead());
    }
    return Integer.compare(hash1, hash2);
}
Also used : PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) AConstNull(net.runelite.asm.attributes.code.instructions.AConstNull) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) 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)

Example 90 with Instruction

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

the class ExprArgOrder method insert.

private void insert(Instructions ins, Instruction next, Expression expression) {
    int count = 0;
    if (expression.sortedExprs != null) {
        for (Expression sub : expression.sortedExprs) {
            if (count == 2) {
                Instruction newOp;
                if (isCommutative(expression.getType())) {
                    // there might be >2 sortedExprs so we can't reuse instructions
                    newOp = newInstruction(ins, expression.getHead().getInstruction(), expression.getType());
                } else {
                    newOp = expression.getHead().getInstruction();
                    assert newOp.getInstructions() == null;
                    newOp.setInstructions(ins);
                }
                int idx = ins.getInstructions().indexOf(next);
                assert idx != -1;
                ins.addInstruction(idx, newOp);
                count = 1;
            }
            insert(ins, next, sub);
            ++count;
        }
    }
    List<Expression> reverseExpr = new ArrayList<>(expression.getExprs());
    Collections.reverse(reverseExpr);
    for (Expression sub : reverseExpr) {
        insert(ins, next, sub);
    }
    insert(ins, expression.getHead(), next);
}
Also used : ArrayList(java.util.ArrayList) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) 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)

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