Search in sources :

Example 1 with Execution

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

the class MappingExecutorUtil method map.

public static ParallelExecutorMapping map(Method m1, Method m2) {
    ClassGroup group1 = m1.getClassFile().getGroup();
    ClassGroup group2 = m2.getClassFile().getGroup();
    Execution e = new Execution(group1);
    e.step = true;
    Frame frame = new Frame(e, m1);
    frame.initialize();
    e.frames.add(frame);
    Execution e2 = new Execution(group2);
    e2.step = true;
    Frame frame2 = new Frame(e2, m2);
    frame2.initialize();
    e2.frames.add(frame2);
    frame.other = frame2;
    frame2.other = frame;
    ParallellMappingExecutor parallel = new ParallellMappingExecutor(e, e2);
    ParallelExecutorMapping mappings = new ParallelExecutorMapping(m1.getClassFile().getGroup(), m2.getClassFile().getGroup());
    mappings.m1 = m1;
    mappings.m2 = m2;
    parallel.mappings = mappings;
    int same = 0;
    while (parallel.step()) {
        // get what each frame is paused/exited on
        InstructionContext p1 = parallel.getP1(), p2 = parallel.getP2();
        assert p1.getInstruction() instanceof MappableInstruction;
        assert p2.getInstruction() instanceof MappableInstruction;
        MappableInstruction mi1 = (MappableInstruction) p1.getInstruction(), mi2 = (MappableInstruction) p2.getInstruction();
        boolean isSame = mi1.isSame(p1, p2);
        assert isSame == mi2.isSame(p2, p1) : "isSame fail " + p1.getInstruction() + " <> " + p2.getInstruction();
        if (!isSame) {
            mappings.crashed = true;
            p1.getFrame().stop();
            p2.getFrame().stop();
            continue;
        }
        ++same;
        mi1.map(mappings, p1, p2);
    }
    mappings.same = same;
    return mappings;
}
Also used : InstructionContext(net.runelite.asm.execution.InstructionContext) Frame(net.runelite.asm.execution.Frame) MappableInstruction(net.runelite.asm.attributes.code.instruction.types.MappableInstruction) Execution(net.runelite.asm.execution.Execution) ClassGroup(net.runelite.asm.ClassGroup) ParallellMappingExecutor(net.runelite.asm.execution.ParallellMappingExecutor)

Example 2 with Execution

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

the class MenuActionDeobfuscator method run.

private void run(Method method) {
    if (method.getCode() == null) {
        return;
    }
    Execution execution = new Execution(method.getClassFile().getGroup());
    execution.addMethod(method);
    execution.noInvoke = true;
    Multimap<Integer, Comparison> comps = HashMultimap.create();
    execution.addExecutionVisitor((InstructionContext ictx) -> {
        Instruction i = ictx.getInstruction();
        Frame frame = ictx.getFrame();
        if (i instanceof If) {
            // constant
            InstructionContext ctx1 = ictx.getPops().get(0).getPushed();
            // lvt
            InstructionContext ctx2 = ictx.getPops().get(1).getPushed();
            if (ctx1.getInstruction() instanceof PushConstantInstruction && ctx2.getInstruction() instanceof LVTInstruction) {
                Comparison comparison = new Comparison();
                comparison.cmp = i;
                comparison.ldc = ctx1.getInstruction();
                comparison.lvt = (LVTInstruction) ctx2.getInstruction();
                comps.put(comparison.lvt.getVariableIndex(), comparison);
            }
        }
    });
    execution.run();
    for (int i : comps.keySet()) {
        Collection<Comparison> get = comps.get(i);
        long l = get.stream().filter(c -> c.cmp.getType() == IF_ICMPGE || c.cmp.getType() == IF_ICMPGT || c.cmp.getType() == IF_ICMPLE || c.cmp.getType() == IF_ICMPLT).count();
        List<Comparison> eqcmp = get.stream().filter(c -> c.cmp.getType() == IF_ICMPEQ || c.cmp.getType() == IF_ICMPNE).collect(Collectors.toList());
        if (get.size() > THRESHOLD_EQ && l <= THRESHOLD_LT) {
            logger.info("Sorting {} comparisons in {}", eqcmp.size(), method);
            insert(method, eqcmp);
        }
    }
}
Also used : IfICmpEq(net.runelite.asm.attributes.code.instructions.IfICmpEq) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) LoggerFactory(org.slf4j.LoggerFactory) Multimap(com.google.common.collect.Multimap) IF_ICMPGE(net.runelite.asm.attributes.code.InstructionType.IF_ICMPGE) Goto(net.runelite.asm.attributes.code.instructions.Goto) ArrayList(java.util.ArrayList) ClassGroup(net.runelite.asm.ClassGroup) HashMultimap(com.google.common.collect.HashMultimap) Method(net.runelite.asm.Method) IF_ICMPNE(net.runelite.asm.attributes.code.InstructionType.IF_ICMPNE) If(net.runelite.asm.attributes.code.instructions.If) IF_ICMPEQ(net.runelite.asm.attributes.code.InstructionType.IF_ICMPEQ) LVTInstruction(net.runelite.asm.attributes.code.instruction.types.LVTInstruction) IF_ICMPGT(net.runelite.asm.attributes.code.InstructionType.IF_ICMPGT) Frame(net.runelite.asm.execution.Frame) Logger(org.slf4j.Logger) InstructionType(net.runelite.asm.attributes.code.InstructionType) IF_ICMPLT(net.runelite.asm.attributes.code.InstructionType.IF_ICMPLT) Collection(java.util.Collection) IF_ICMPLE(net.runelite.asm.attributes.code.InstructionType.IF_ICMPLE) 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) Label(net.runelite.asm.attributes.code.Label) IfICmpNe(net.runelite.asm.attributes.code.instructions.IfICmpNe) Instructions(net.runelite.asm.attributes.code.Instructions) Instruction(net.runelite.asm.attributes.code.Instruction) Collections(java.util.Collections) InstructionContext(net.runelite.asm.execution.InstructionContext) Frame(net.runelite.asm.execution.Frame) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) 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) Execution(net.runelite.asm.execution.Execution) If(net.runelite.asm.attributes.code.instructions.If)

Example 3 with Execution

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

the class HandlerFinder method prepareFrame.

private void prepareFrame(Execution e, PacketHandlers handlers) {
    List<Method> methods = handlers.getHandlers().stream().map(handler -> handler.getMethod()).distinct().collect(Collectors.toList());
    for (Method method : methods) {
        List<PacketHandler> phandlers = handlers.getHandlers().stream().filter(handler -> handler.getMethod() == method).collect(Collectors.toList());
        prepareFrame(e, method, phandlers);
    }
}
Also used : Frame(net.runelite.asm.execution.Frame) Logger(org.slf4j.Logger) InstructionType(net.runelite.asm.attributes.code.InstructionType) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) Field(net.runelite.asm.Field) LoggerFactory(org.slf4j.LoggerFactory) Multimap(com.google.common.collect.Multimap) Collectors(java.util.stream.Collectors) InstructionContext(net.runelite.asm.execution.InstructionContext) Execution(net.runelite.asm.execution.Execution) ArrayList(java.util.ArrayList) ClassGroup(net.runelite.asm.ClassGroup) List(java.util.List) ClassFile(net.runelite.asm.ClassFile) Label(net.runelite.asm.attributes.code.Label) HashMultimap(com.google.common.collect.HashMultimap) Method(net.runelite.asm.Method) VReturn(net.runelite.asm.attributes.code.instructions.VReturn) Instructions(net.runelite.asm.attributes.code.Instructions) JumpingInstruction(net.runelite.asm.attributes.code.instruction.types.JumpingInstruction) If(net.runelite.asm.attributes.code.instructions.If) Instruction(net.runelite.asm.attributes.code.Instruction) GetStatic(net.runelite.asm.attributes.code.instructions.GetStatic) Collections(java.util.Collections) Method(net.runelite.asm.Method)

Example 4 with Execution

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

the class MultiplicationDeobfuscatorTest method test1.

// aload                 2
// ldc_w                 1587543155
// iload                 4
// imul
// dup_x1
// ldc_w                 -2130376517
// imul
// putfield              class2/field279 I
// ldc_w                 -67313687
// imul
// putstatic             class29/field949 I
@Test
public void test1() {
    ClassGroup group = ClassGroupFactory.generateGroup();
    Code code = group.findClass("test").findMethod("func").getCode();
    Instructions ins = code.getInstructions();
    code.setMaxStack(5);
    // vars[0] = 3
    Instruction[] prepareVariables = { new LDC(ins, 3), new IStore(ins, 0) };
    for (Instruction i : prepareVariables) {
        ins.addInstruction(i);
    }
    LDC constant1 = new LDC(ins, 1587543155), constant2 = new LDC(ins, -2130376517), constant3 = new LDC(ins, -67313687);
    Instruction[] body = { // for dup_x1 to place before this
    new LDC(ins, 0), constant1, new ILoad(ins, 0), new IMul(ins), new Dup_X1(ins), constant2, new IMul(ins), new Pop(ins), new Pop(ins), constant3, new IMul(ins), new Pop(ins), new VReturn(ins) };
    for (Instruction i : body) {
        ins.addInstruction(i);
    }
    // check execution runs ok
    Execution e = new Execution(group);
    e.populateInitialMethods();
    e.run();
    assert constant1.getConstantAsInt() * constant2.getConstantAsInt() == 1;
    assert constant1.getConstantAsInt() * constant3.getConstantAsInt() == -1_095_175_765;
    Deobfuscator d = new MultiplicationDeobfuscator();
    d.run(group);
    Assert.assertEquals(1, constant1.getConstantAsInt());
    Assert.assertEquals(1, constant2.getConstantAsInt());
    Assert.assertEquals(-1_095_175_765, constant3.getConstantAsInt());
}
Also used : IStore(net.runelite.asm.attributes.code.instructions.IStore) ILoad(net.runelite.asm.attributes.code.instructions.ILoad) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) 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) Pop(net.runelite.asm.attributes.code.instructions.Pop) 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)

Example 5 with Execution

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

the class MultiplicationDeobfuscatorTest method test4.

@Test
public void test4() {
    ClassGroup group = ClassGroupFactory.generateGroup();
    Code code = group.findClass("test").findMethod("func").getCode();
    Instructions ins = code.getInstructions();
    code.setMaxStack(2);
    Instruction[] prepareVariables = { new LDC(ins, 3), new IStore(ins, 0) };
    for (Instruction i : prepareVariables) {
        ins.addInstruction(i);
    }
    LDC constant1 = new LDC(ins, 1807370871);
    LDC constant2 = new LDC(ins, 981643079);
    Label label1 = new Label(ins);
    Instruction[] body = { new ILoad(ins, 0), new LDC(ins, 2), new IMul(ins), new LDC(ins, 0), new IfEq(ins, label1), new Pop(ins), new LDC(ins, 3), label1, constant1, new IMul(ins), constant2, new IMul(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;
    Deobfuscator d = new MultiplicationDeobfuscator();
    d.run(group);
    Assert.assertEquals(1, constant1.getConstantAsInt());
    Assert.assertEquals(1, constant2.getConstantAsInt());
}
Also used : IStore(net.runelite.asm.attributes.code.instructions.IStore) ILoad(net.runelite.asm.attributes.code.instructions.ILoad) Label(net.runelite.asm.attributes.code.Label) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) IfEq(net.runelite.asm.attributes.code.instructions.IfEq) 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) Pop(net.runelite.asm.attributes.code.instructions.Pop) Execution(net.runelite.asm.execution.Execution) ClassGroup(net.runelite.asm.ClassGroup) IMul(net.runelite.asm.attributes.code.instructions.IMul) Test(org.junit.Test)

Aggregations

Execution (net.runelite.asm.execution.Execution)44 Instruction (net.runelite.asm.attributes.code.Instruction)25 ClassGroup (net.runelite.asm.ClassGroup)23 Instructions (net.runelite.asm.attributes.code.Instructions)23 Code (net.runelite.asm.attributes.Code)21 Deobfuscator (net.runelite.deob.Deobfuscator)21 LDC (net.runelite.asm.attributes.code.instructions.LDC)19 VReturn (net.runelite.asm.attributes.code.instructions.VReturn)19 IMul (net.runelite.asm.attributes.code.instructions.IMul)18 Test (org.junit.Test)18 IStore (net.runelite.asm.attributes.code.instructions.IStore)17 ILoad (net.runelite.asm.attributes.code.instructions.ILoad)16 InstructionContext (net.runelite.asm.execution.InstructionContext)12 Label (net.runelite.asm.attributes.code.Label)10 Method (net.runelite.asm.Method)8 ClassFile (net.runelite.asm.ClassFile)7 Pop (net.runelite.asm.attributes.code.instructions.Pop)7 StackContext (net.runelite.asm.execution.StackContext)7 Field (net.runelite.asm.Field)5 Dup_X1 (net.runelite.asm.attributes.code.instructions.Dup_X1)5