Search in sources :

Example 11 with LDC

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

the class CodeVisitor method visitInsn.

@Override
public void visitInsn(int opcode) {
    switch(opcode) {
        case DCONST_0:
            {
                Instruction i = new LDC(code.getInstructions(), 0d);
                code.getInstructions().addInstruction(i);
                break;
            }
        case DCONST_1:
            {
                Instruction i = new LDC(code.getInstructions(), 1d);
                code.getInstructions().addInstruction(i);
                break;
            }
        case FCONST_0:
            {
                Instruction i = new LDC(code.getInstructions(), 0f);
                code.getInstructions().addInstruction(i);
                break;
            }
        case FCONST_1:
            {
                Instruction i = new LDC(code.getInstructions(), 1f);
                code.getInstructions().addInstruction(i);
                break;
            }
        case FCONST_2:
            {
                Instruction i = new LDC(code.getInstructions(), 2f);
                code.getInstructions().addInstruction(i);
                break;
            }
        case ICONST_M1:
            {
                Instruction i = new LDC(code.getInstructions(), -1);
                code.getInstructions().addInstruction(i);
                break;
            }
        case ICONST_0:
            {
                Instruction i = new LDC(code.getInstructions(), 0);
                code.getInstructions().addInstruction(i);
                break;
            }
        case ICONST_1:
            {
                {
                    Instruction i = new LDC(code.getInstructions(), 1);
                    code.getInstructions().addInstruction(i);
                    break;
                }
            }
        case ICONST_2:
            {
                Instruction i = new LDC(code.getInstructions(), 2);
                code.getInstructions().addInstruction(i);
                break;
            }
        case ICONST_3:
            {
                Instruction i = new LDC(code.getInstructions(), 3);
                code.getInstructions().addInstruction(i);
                break;
            }
        case ICONST_4:
            {
                Instruction i = new LDC(code.getInstructions(), 4);
                code.getInstructions().addInstruction(i);
                break;
            }
        case ICONST_5:
            {
                Instruction i = new LDC(code.getInstructions(), 5);
                code.getInstructions().addInstruction(i);
                break;
            }
        case LCONST_0:
            {
                Instruction i = new LDC(code.getInstructions(), 0L);
                code.getInstructions().addInstruction(i);
                break;
            }
        case LCONST_1:
            {
                Instruction i = new LDC(code.getInstructions(), 1L);
                code.getInstructions().addInstruction(i);
                break;
            }
        default:
            createInstructionFromOpcode(opcode);
    }
}
Also used : LDC(net.runelite.asm.attributes.code.instructions.LDC) FieldInstruction(net.runelite.asm.attributes.code.instruction.types.FieldInstruction) TypeInstruction(net.runelite.asm.attributes.code.instruction.types.TypeInstruction) 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) IntInstruction(net.runelite.asm.attributes.code.instruction.types.IntInstruction) Instruction(net.runelite.asm.attributes.code.Instruction)

Example 12 with LDC

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

the class CodeVisitor method visitLdcInsn.

@Override
public void visitLdcInsn(Object cst) {
    Object entry = cst;
    if (cst instanceof org.objectweb.asm.Type) {
        org.objectweb.asm.Type t = (org.objectweb.asm.Type) cst;
        entry = new net.runelite.asm.pool.Class((String) t.getClassName());
    }
    LDC ldc = new LDC(code.getInstructions(), entry);
    code.getInstructions().addInstruction(ldc);
}
Also used : InstructionType(net.runelite.asm.attributes.code.InstructionType) Type(net.runelite.asm.Type) LDC(net.runelite.asm.attributes.code.instructions.LDC)

Example 13 with LDC

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

the class ModArith method insertGetterSetterMuls.

private void insertGetterSetterMuls(Encryption encr) {
    // before setfield insert imul * getter
    for (ClassFile cf : group.getClasses()) {
        for (Method m : cf.getMethods()) {
            Code code = m.getCode();
            if (code == null) {
                continue;
            }
            Instructions ins = code.getInstructions();
            List<Instruction> ilist = ins.getInstructions();
            for (int i = 0; i < ilist.size(); ++i) {
                Instruction in = ilist.get(i);
                if (in instanceof SetFieldInstruction) {
                    SetFieldInstruction sfi = (SetFieldInstruction) in;
                    Field f = sfi.getMyField();
                    if (f == null) {
                        continue;
                    }
                    Pair p = encr.getField(f.getPoolField());
                    if (p == null) {
                        continue;
                    }
                    // insert imul
                    if (p.getType() == Integer.class) {
                        ilist.add(i++, new LDC(ins, (int) p.getter));
                        ilist.add(i++, new IMul(ins));
                    } else if (p.getType() == Long.class) {
                        ilist.add(i++, new LDC(ins, (long) p.getter));
                        ilist.add(i++, new LMul(ins));
                    } else {
                        throw new IllegalStateException();
                    }
                } else if (in instanceof GetFieldInstruction) {
                    GetFieldInstruction sfi = (GetFieldInstruction) in;
                    Field f = sfi.getMyField();
                    if (f == null) {
                        continue;
                    }
                    Pair p = encr.getField(f.getPoolField());
                    if (p == null) {
                        continue;
                    }
                    // imul
                    if (p.getType() == Integer.class) {
                        ilist.add(++i, new LDC(ins, (int) p.setter));
                        ilist.add(++i, new IMul(ins));
                    } else if (p.getType() == Long.class) {
                        ilist.add(++i, new LDC(ins, (long) p.setter));
                        ilist.add(++i, new LMul(ins));
                    } else {
                        throw new IllegalStateException();
                    }
                }
            }
        }
    }
}
Also used : SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) ClassFile(net.runelite.asm.ClassFile) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) Method(net.runelite.asm.Method) 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) ArrayStoreInstruction(net.runelite.asm.attributes.code.instruction.types.ArrayStoreInstruction) InvokeInstruction(net.runelite.asm.attributes.code.instruction.types.InvokeInstruction) 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) Field(net.runelite.asm.Field) IMul(net.runelite.asm.attributes.code.instructions.IMul) LMul(net.runelite.asm.attributes.code.instructions.LMul) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction)

Example 14 with LDC

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

the class MultiplicationDeobfuscator method parseExpression.

public static MultiplicationExpression parseExpression(InstructionContext ctx, Class want) {
    MultiplicationExpression me = new MultiplicationExpression();
    if (ctx.getInstruction() instanceof LVTInstruction) {
        LVTInstruction lvt = (LVTInstruction) ctx.getInstruction();
        // loading a variable
        if (!lvt.store()) {
            // var index
            int idx = lvt.getVariableIndex();
            // variables at time of execution
            Variables vars = ctx.getVariables();
            // get the variable
            VariableContext vctx = vars.get(idx);
            if (// ?
            vctx.getRead().size() == 1) {
                // this is an istore
                InstructionContext storeCtx = vctx.getInstructionWhichStored();
                if (storeCtx.getInstruction() instanceof LVTInstruction) {
                    // invoking funcs can put stuff in lvt
                    LVTInstruction storelvt = (LVTInstruction) storeCtx.getInstruction();
                    if (storelvt instanceof IInc)
                        throw new IllegalStateException();
                    assert storelvt.store();
                    InstructionContext pushed = storeCtx.getPops().get(0).getPushed();
                    return parseExpression(pushed, want);
                }
            }
        }
    }
    if (ctx.getInstruction() instanceof PushConstantInstruction) {
        if (ctx.getInstruction() instanceof BiPush || ctx.getInstruction() instanceof SiPush) {
            throw new IllegalStateException();
        }
        me.instructions.add(ctx);
        return me;
    }
    for (StackContext sctx : ctx.getPops()) {
        if (ctx.getInstruction().getClass() == want) {
            if (!isOnlyPath(ctx, sctx))
                continue;
        }
        InstructionContext i = sctx.getPushed();
        // if this instruction is imul, look at pops
        if (ctx.getInstruction().getClass() == want) {
            if (i.getInstruction() instanceof Swap) {
                logger.debug("Resolving swap");
                Swap swap = (Swap) i.getInstruction();
                sctx = swap.getOriginal(sctx);
                i = sctx.getPushed();
            }
            if (i.getInstruction() instanceof PushConstantInstruction) {
                // bipush/sipush are always not obfuscated
                if (i.getInstruction() instanceof BiPush || i.getInstruction() instanceof SiPush)
                    continue;
                // a constant of imul
                me.instructions.add(i);
            } else if (i.getInstruction().getClass() == want) {
                // chained imul, append to me
                try {
                    MultiplicationExpression other = parseExpression(i, want);
                    if (other.dupmagic != null) {
                        assert me.dupmagic == null;
                        me.dupmagic = other.dupmagic;
                    }
                    me.instructions.addAll(other.instructions);
                    me.dupedInstructions.addAll(other.dupedInstructions);
                    me.subexpressions.addAll(other.subexpressions);
                } catch (IllegalStateException ex) {
                // this is ok? just don't include it?
                }
            } else if (i.getInstruction() instanceof IAdd || i.getInstruction() instanceof ISub || i.getInstruction() instanceof LAdd || i.getInstruction() instanceof LSub) {
                // imul using result of iadd or isub. evaluate expression
                try {
                    MultiplicationExpression other = parseExpression(i, want);
                    assert other.dupmagic == null;
                    // subexpr
                    me.subexpressions.add(other);
                } catch (IllegalStateException ex) {
                    assert me.subexpressions.isEmpty();
                // subexpression is too complex. we can still simplify the top level though
                }
            } else if (i.getInstruction() instanceof DupInstruction) {
                DupInstruction dup = (DupInstruction) i.getInstruction();
                // find other branch of the dup instruction
                // sctx = what dup pushed, find other
                // other side of dup
                StackContext otherCtx = dup.getOtherBranch(sctx);
                // what popped other side of dup. is this right?
                InstructionContext otherCtxI = otherCtx.getPopped().get(0);
                if (otherCtxI.getInstruction().getClass() == want) {
                    // assert otherCtxI.getInstruction() instanceof IMul;
                    // other side of that imul
                    InstructionContext pushConstant = otherCtxI.getPops().get(0).getPushed();
                    assert pushConstant.getInstruction() instanceof LDC;
                    me.dupmagic = pushConstant;
                    // original
                    StackContext orig = dup.getOriginal(sctx);
                    try {
                        MultiplicationExpression other = parseExpression(orig.getPushed(), want);
                        // done to it affect that, too. so multiply it by existing values?
                        if (orig.getPushed().getInstruction() instanceof IAdd || orig.getPushed().getInstruction() instanceof ISub || orig.getPushed().getInstruction() instanceof LAdd || orig.getPushed().getInstruction() instanceof LSub) {
                            me.subexpressions.add(other);
                        } else {
                            assert other.dupmagic == null;
                            me.instructions.addAll(other.instructions);
                            me.dupedInstructions.addAll(other.instructions);
                            me.subexpressions.addAll(other.subexpressions);
                        }
                    } catch (IllegalStateException ex) {
                        assert me.subexpressions.isEmpty();
                    }
                }
            } else if (i.getInstruction() instanceof GetFieldInstruction) {
                me.fieldInstructions.add(i);
            // non constant, ignore
            } else {
            // System.out.println("imul pops something I don't know " + i.getInstruction());
            }
        } else // this is an iadd/sub
        if (ctx.getInstruction() instanceof IAdd || ctx.getInstruction() instanceof ISub || ctx.getInstruction() instanceof LAdd || ctx.getInstruction() instanceof LSub) {
            // parse this side of the add/sub
            MultiplicationExpression other = parseExpression(i, want);
            me.subexpressions.add(other);
        } else {
        // System.out.println(ctx.getInstruction() + " pops something I dont know " + i.getInstruction());
        }
    }
    if (me.instructions.isEmpty() && me.subexpressions.isEmpty())
        throw new IllegalStateException();
    return me;
}
Also used : SiPush(net.runelite.asm.attributes.code.instructions.SiPush) InstructionContext(net.runelite.asm.execution.InstructionContext) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) DupInstruction(net.runelite.asm.attributes.code.instruction.types.DupInstruction) LDC(net.runelite.asm.attributes.code.instructions.LDC) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) VariableContext(net.runelite.asm.execution.VariableContext) BiPush(net.runelite.asm.attributes.code.instructions.BiPush) Variables(net.runelite.asm.execution.Variables) Swap(net.runelite.asm.attributes.code.instructions.Swap) ISub(net.runelite.asm.attributes.code.instructions.ISub) StackContext(net.runelite.asm.execution.StackContext) LAdd(net.runelite.asm.attributes.code.instructions.LAdd) IInc(net.runelite.asm.attributes.code.instructions.IInc) LSub(net.runelite.asm.attributes.code.instructions.LSub) IAdd(net.runelite.asm.attributes.code.instructions.IAdd) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction)

Example 15 with LDC

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

Aggregations

LDC (net.runelite.asm.attributes.code.instructions.LDC)46 Instruction (net.runelite.asm.attributes.code.Instruction)39 Instructions (net.runelite.asm.attributes.code.Instructions)38 Code (net.runelite.asm.attributes.Code)32 ClassGroup (net.runelite.asm.ClassGroup)29 Test (org.junit.Test)27 VReturn (net.runelite.asm.attributes.code.instructions.VReturn)26 IMul (net.runelite.asm.attributes.code.instructions.IMul)23 IStore (net.runelite.asm.attributes.code.instructions.IStore)23 ILoad (net.runelite.asm.attributes.code.instructions.ILoad)22 Deobfuscator (net.runelite.deob.Deobfuscator)20 Execution (net.runelite.asm.execution.Execution)19 IAdd (net.runelite.asm.attributes.code.instructions.IAdd)14 Pop (net.runelite.asm.attributes.code.instructions.Pop)13 Method (net.runelite.asm.Method)11 Type (net.runelite.asm.Type)10 Label (net.runelite.asm.attributes.code.Label)10 Field (net.runelite.asm.Field)9 PushConstantInstruction (net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction)9 Signature (net.runelite.asm.signature.Signature)8