Search in sources :

Example 1 with AConstNull

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

the class CastNullTest method testRun.

@Test
public void testRun() {
    ClassGroup group = ClassGroupFactory.generateGroup();
    Code code = group.findClass("test").findMethod("func").getCode();
    Instructions ins = code.getInstructions();
    code.setMaxStack(3);
    CheckCast checkCast = new CheckCast(ins);
    checkCast.setType(new Type("test"));
    Instruction[] instructions = { new LDC(ins, 2), new AConstNull(ins), checkCast, new LDC(ins, 2), new IAdd(ins), new Return(ins, InstructionType.IRETURN) };
    for (Instruction i : instructions) {
        ins.addInstruction(i);
    }
    Assert.assertEquals(6, ins.getInstructions().size());
    CastNull lvt = new CastNull();
    lvt.run(group);
    Assert.assertEquals(5, ins.getInstructions().size());
    Optional<Instruction> o = ins.getInstructions().stream().filter(i -> i instanceof CheckCast).findAny();
    Assert.assertFalse(o.isPresent());
}
Also used : AConstNull(net.runelite.asm.attributes.code.instructions.AConstNull) InstructionType(net.runelite.asm.attributes.code.InstructionType) Code(net.runelite.asm.attributes.Code) Test(org.junit.Test) Type(net.runelite.asm.Type) ClassGroup(net.runelite.asm.ClassGroup) ClassGroupFactory(net.runelite.deob.ClassGroupFactory) LDC(net.runelite.asm.attributes.code.instructions.LDC) Return(net.runelite.asm.attributes.code.instructions.Return) Instructions(net.runelite.asm.attributes.code.Instructions) CheckCast(net.runelite.asm.attributes.code.instructions.CheckCast) Optional(java.util.Optional) IAdd(net.runelite.asm.attributes.code.instructions.IAdd) Instruction(net.runelite.asm.attributes.code.Instruction) Assert(org.junit.Assert) Return(net.runelite.asm.attributes.code.instructions.Return) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) AConstNull(net.runelite.asm.attributes.code.instructions.AConstNull) CheckCast(net.runelite.asm.attributes.code.instructions.CheckCast) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) InstructionType(net.runelite.asm.attributes.code.InstructionType) Type(net.runelite.asm.Type) ClassGroup(net.runelite.asm.ClassGroup) IAdd(net.runelite.asm.attributes.code.instructions.IAdd) Test(org.junit.Test)

Example 2 with AConstNull

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

the class ExprArgOrder method compare.

public static int compare(Method method, InstructionType type, InstructionContext ic1, InstructionContext ic2) {
    Instruction i1 = ic1.getInstruction();
    Instruction i2 = ic2.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, ic1);
    int hash2 = hash(method, ic2);
    if (hash1 == hash2) {
        logger.debug("Unable to differentiate {} from {}", ic1, ic2);
    }
    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 3 with AConstNull

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

the class InjectHook method run.

public void run() {
    Execution e = new Execution(inject.getVanilla());
    e.populateInitialMethods();
    Set<Instruction> done = new HashSet<>();
    Set<Instruction> doneIh = new HashSet<>();
    e.addExecutionVisitor((InstructionContext ic) -> {
        Instruction i = ic.getInstruction();
        Instructions ins = i.getInstructions();
        Code code = ins.getCode();
        Method method = code.getMethod();
        if (method.getName().equals(CLINIT)) {
            return;
        }
        if (!(i instanceof SetFieldInstruction)) {
            return;
        }
        if (!done.add(i)) {
            return;
        }
        SetFieldInstruction sfi = (SetFieldInstruction) i;
        Field fieldBeingSet = sfi.getMyField();
        if (fieldBeingSet == null) {
            return;
        }
        HookInfo hookInfo = hooked.get(fieldBeingSet);
        if (hookInfo == null) {
            return;
        }
        String hookName = hookInfo.fieldName;
        assert hookName != null;
        logger.trace("Found injection location for hook {} at instruction {}", hookName, sfi);
        ++injectedHooks;
        Instruction objectInstruction = new AConstNull(ins);
        StackContext objectStackContext = null;
        if (sfi instanceof PutField) {
            // Object being set on
            StackContext objectStack = ic.getPops().get(1);
            objectStackContext = objectStack;
        }
        int idx = ins.getInstructions().indexOf(sfi);
        assert idx != -1;
        try {
            // idx + 1 to insert after the set
            injectCallback(ins, idx + 1, hookInfo, null, objectStackContext);
        } catch (InjectionException ex) {
            throw new RuntimeException(ex);
        }
    });
    // these look like:
    // getfield
    // iload_0
    // iconst_0
    // iastore
    e.addExecutionVisitor((InstructionContext ic) -> {
        Instruction i = ic.getInstruction();
        Instructions ins = i.getInstructions();
        Code code = ins.getCode();
        Method method = code.getMethod();
        if (method.getName().equals(CLINIT)) {
            return;
        }
        if (!(i instanceof ArrayStore)) {
            return;
        }
        if (!doneIh.add(i)) {
            return;
        }
        ArrayStore as = (ArrayStore) i;
        Field fieldBeingSet = as.getMyField(ic);
        if (fieldBeingSet == null) {
            return;
        }
        HookInfo hookInfo = hooked.get(fieldBeingSet);
        if (hookInfo == null) {
            return;
        }
        String hookName = hookInfo.fieldName;
        // assume this is always at index 1
        StackContext index = ic.getPops().get(1);
        StackContext arrayReference = ic.getPops().get(2);
        InstructionContext arrayReferencePushed = arrayReference.getPushed();
        StackContext objectStackContext = null;
        if (arrayReferencePushed.getInstruction().getType() == InstructionType.GETFIELD) {
            StackContext objectReference = arrayReferencePushed.getPops().get(0);
            objectStackContext = objectReference;
        }
        // inject hook after 'i'
        logger.info("Found array injection location for hook {} at instruction {}", hookName, i);
        ++injectedHooks;
        int idx = ins.getInstructions().indexOf(i);
        assert idx != -1;
        try {
            injectCallback(ins, idx + 1, hookInfo, index, objectStackContext);
        } catch (InjectionException ex) {
            throw new RuntimeException(ex);
        }
    });
    e.run();
}
Also used : InstructionContext(net.runelite.asm.execution.InstructionContext) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) Instructions(net.runelite.asm.attributes.code.Instructions) AConstNull(net.runelite.asm.attributes.code.instructions.AConstNull) Method(net.runelite.asm.Method) DupInstruction(net.runelite.asm.attributes.code.instruction.types.DupInstruction) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) ArrayStore(net.runelite.asm.attributes.code.instructions.ArrayStore) Code(net.runelite.asm.attributes.Code) Field(net.runelite.asm.Field) PutField(net.runelite.asm.attributes.code.instructions.PutField) Execution(net.runelite.asm.execution.Execution) StackContext(net.runelite.asm.execution.StackContext) HashSet(java.util.HashSet) PutField(net.runelite.asm.attributes.code.instructions.PutField)

Example 4 with AConstNull

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

the class CastNull method visit.

private void visit(InstructionContext ictx) {
    if (!(ictx.getInstruction() instanceof CheckCast))
        return;
    if (notInteresting.contains(ictx.getInstruction()) || interesting.contains(ictx.getInstruction()))
        return;
    StackContext sctx = ictx.getPops().get(0);
    if (sctx.getPushed().getInstruction() instanceof AConstNull) {
        interesting.add(ictx.getInstruction());
    } else {
        interesting.remove(ictx.getInstruction());
        notInteresting.add(ictx.getInstruction());
    }
}
Also used : StackContext(net.runelite.asm.execution.StackContext) AConstNull(net.runelite.asm.attributes.code.instructions.AConstNull) CheckCast(net.runelite.asm.attributes.code.instructions.CheckCast)

Example 5 with AConstNull

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

the class DupDeobfuscatorTest method test.

@Test
public void test() {
    ClassGroup group = ClassGroupFactory.generateGroup();
    Code code = group.findClass("test").findMethod("func").getCode();
    Instructions ins = code.getInstructions();
    code.setMaxStack(5);
    Instruction[] prepareVariables = { new LDC(ins, 1), new IStore(ins, 0) };
    for (Instruction i : prepareVariables) {
        ins.addInstruction(i);
    }
    LDC constant1 = new LDC(ins, 1129258489), constant2 = new LDC(ins, -1692330935), constant3 = new LDC(ins, 1641298955), constant4 = new LDC(ins, 1043501435);
    Instruction[] body = { // this
    new AConstNull(ins), // this
    new AConstNull(ins), new ILoad(ins, 0), constant1, new IMul(ins), new Dup_X1(ins), constant2, new IMul(ins), // putfield
    new Pop2(ins), constant3, new IMul(ins), constant4, new IMul(ins), // putfield
    new Pop2(ins), new VReturn(ins) };
    for (Instruction i : body) {
        ins.addInstruction(i);
    }
    Execution e = new Execution(group);
    e.populateInitialMethods();
    e.run();
    assert constant1.getConstantAsInt() * constant2.getConstantAsInt() == 1;
    assert constant3.getConstantAsInt() * constant4.getConstantAsInt() * constant1.getConstantAsInt() == 1;
    Deobfuscator d = new DupDeobfuscator();
    d.run(group);
    // assert the dup_x1 was removed
    long dupCount = ins.getInstructions().stream().filter(i -> i instanceof Dup_X1).count();
    Assert.assertEquals(0, dupCount);
}
Also used : Pop2(net.runelite.asm.attributes.code.instructions.Pop2) AConstNull(net.runelite.asm.attributes.code.instructions.AConstNull) GetField(net.runelite.asm.attributes.code.instructions.GetField) IMul(net.runelite.asm.attributes.code.instructions.IMul) Dup(net.runelite.asm.attributes.code.instructions.Dup) Code(net.runelite.asm.attributes.Code) Test(org.junit.Test) Type(net.runelite.asm.Type) Deobfuscator(net.runelite.deob.Deobfuscator) Execution(net.runelite.asm.execution.Execution) ClassGroup(net.runelite.asm.ClassGroup) Class(net.runelite.asm.pool.Class) ClassGroupFactory(net.runelite.deob.ClassGroupFactory) ILoad(net.runelite.asm.attributes.code.instructions.ILoad) LDC(net.runelite.asm.attributes.code.instructions.LDC) IStore(net.runelite.asm.attributes.code.instructions.IStore) VReturn(net.runelite.asm.attributes.code.instructions.VReturn) Instructions(net.runelite.asm.attributes.code.Instructions) Field(net.runelite.asm.pool.Field) IAdd(net.runelite.asm.attributes.code.instructions.IAdd) Instruction(net.runelite.asm.attributes.code.Instruction) Assert(org.junit.Assert) Dup_X1(net.runelite.asm.attributes.code.instructions.Dup_X1) IStore(net.runelite.asm.attributes.code.instructions.IStore) ILoad(net.runelite.asm.attributes.code.instructions.ILoad) Pop2(net.runelite.asm.attributes.code.instructions.Pop2) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) AConstNull(net.runelite.asm.attributes.code.instructions.AConstNull) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) VReturn(net.runelite.asm.attributes.code.instructions.VReturn) Deobfuscator(net.runelite.deob.Deobfuscator) Execution(net.runelite.asm.execution.Execution) ClassGroup(net.runelite.asm.ClassGroup) Dup_X1(net.runelite.asm.attributes.code.instructions.Dup_X1) IMul(net.runelite.asm.attributes.code.instructions.IMul) Test(org.junit.Test)

Aggregations

AConstNull (net.runelite.asm.attributes.code.instructions.AConstNull)10 Instruction (net.runelite.asm.attributes.code.Instruction)9 Code (net.runelite.asm.attributes.Code)7 Instructions (net.runelite.asm.attributes.code.Instructions)7 ClassGroup (net.runelite.asm.ClassGroup)6 LDC (net.runelite.asm.attributes.code.instructions.LDC)6 Test (org.junit.Test)6 IStore (net.runelite.asm.attributes.code.instructions.IStore)5 VReturn (net.runelite.asm.attributes.code.instructions.VReturn)5 Execution (net.runelite.asm.execution.Execution)5 ILoad (net.runelite.asm.attributes.code.instructions.ILoad)4 IMul (net.runelite.asm.attributes.code.instructions.IMul)4 Pop2 (net.runelite.asm.attributes.code.instructions.Pop2)4 Deobfuscator (net.runelite.deob.Deobfuscator)4 Type (net.runelite.asm.Type)3 Dup_X1 (net.runelite.asm.attributes.code.instructions.Dup_X1)3 IAdd (net.runelite.asm.attributes.code.instructions.IAdd)3 ClassGroupFactory (net.runelite.deob.ClassGroupFactory)3 Assert (org.junit.Assert)3 InvokeInstruction (net.runelite.asm.attributes.code.instruction.types.InvokeInstruction)2