Search in sources :

Example 31 with Method

use of net.runelite.asm.Method in project runelite by runelite.

the class Renamer method renameClass.

private void renameClass(ClassGroup group, ClassFile cf, String name) {
    for (ClassFile c : group.getClasses()) {
        // rename on child interfaces and classes
        renameClass(c, cf, name);
        for (Method method : c.getMethods()) {
            // rename on instructions. this includes method calls and field accesses.
            if (method.getCode() != null) {
                Code code = method.getCode();
                // rename on instructions
                for (Instruction i : code.getInstructions().getInstructions()) {
                    i.renameClass(cf.getName(), name);
                }
                // rename on exception handlers
                Exceptions exceptions = code.getExceptions();
                exceptions.renameClass(cf, name);
            }
            // rename on parameters
            Signature.Builder builder = new Signature.Builder();
            Signature signature = method.getDescriptor();
            for (int i = 0; i < signature.size(); ++i) {
                Type type = signature.getTypeOfArg(i);
                if (type.getInternalName().equals(cf.getName())) {
                    builder.addArgument(Type.getType("L" + name + ";", type.getDimensions()));
                } else {
                    builder.addArgument(type);
                }
            }
            // rename return type
            if (signature.getReturnValue().getInternalName().equals(cf.getName())) {
                builder.setReturnType(Type.getType("L" + name + ";", signature.getReturnValue().getDimensions()));
            } else {
                builder.setReturnType(signature.getReturnValue());
            }
            Signature newSignature = builder.build();
            if (!method.getDescriptor().equals(newSignature)) {
                // Signature was updated. Annotate it
                if (method.getAnnotations().find(DeobAnnotations.OBFUSCATED_SIGNATURE) == null) {
                    // Signature was not previously renamed
                    method.getAnnotations().addAnnotation(DeobAnnotations.OBFUSCATED_SIGNATURE, "signature", method.getDescriptor().toString());
                }
            }
            method.setDescriptor(newSignature);
            // rename on exceptions thrown
            if (method.getExceptions() != null) {
                method.getExceptions().renameClass(cf, name);
            }
        }
        // rename on fields
        for (Field field : c.getFields()) {
            if (field.getType().getInternalName().equals(cf.getName())) {
                if (field.getAnnotations().find(DeobAnnotations.OBFUSCATED_SIGNATURE) == null) {
                    // Signature was updated. Annotate it
                    field.getAnnotations().addAnnotation(DeobAnnotations.OBFUSCATED_SIGNATURE, "signature", field.getType().toString());
                }
                field.setType(Type.getType("L" + name + ";", field.getType().getDimensions()));
            }
        }
    }
    if (cf.getAnnotations().find(DeobAnnotations.OBFUSCATED_NAME) == null) {
        cf.getAnnotations().addAnnotation(DeobAnnotations.OBFUSCATED_NAME, "value", cf.getName());
    }
    group.renameClass(cf, name);
}
Also used : Field(net.runelite.asm.Field) Type(net.runelite.asm.Type) ClassFile(net.runelite.asm.ClassFile) Signature(net.runelite.asm.signature.Signature) Exceptions(net.runelite.asm.attributes.code.Exceptions) Method(net.runelite.asm.Method) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code)

Example 32 with Method

use of net.runelite.asm.Method in project runelite by runelite.

the class Renamer method regeneratePool.

private void regeneratePool(ClassGroup group) {
    for (ClassFile cf : group.getClasses()) {
        for (Method m : cf.getMethods()) {
            Code c = m.getCode();
            if (c == null) {
                continue;
            }
            c.getInstructions().regeneratePool();
        }
    }
}
Also used : ClassFile(net.runelite.asm.ClassFile) Method(net.runelite.asm.Method) Code(net.runelite.asm.attributes.Code)

Example 33 with Method

use of net.runelite.asm.Method in project runelite by runelite.

the class UnreachedCode method run.

@Override
public void run(ClassGroup group) {
    group.buildClassGraph();
    execution = new Execution(group);
    execution.populateInitialMethods();
    execution.run();
    int count = 0;
    for (ClassFile cf : group.getClasses()) {
        for (Method m : cf.getMethods()) {
            if (m.getCode() == null)
                continue;
            count += removeUnused(m);
        }
    }
    logger.info("Removed {} unused instructions", count);
}
Also used : Execution(net.runelite.asm.execution.Execution) ClassFile(net.runelite.asm.ClassFile) Method(net.runelite.asm.Method)

Example 34 with Method

use of net.runelite.asm.Method 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 35 with Method

use of net.runelite.asm.Method in project runelite by runelite.

the class StaticStep method stepInto.

public static Frame stepInto(Frame f, InstructionContext i) {
    Execution e = f.getExecution();
    assert i.getInstruction() instanceof InvokeStatic;
    InvokeStatic is = (InvokeStatic) i.getInstruction();
    List<Method> methods = is.getMethods();
    assert methods.size() == 1;
    Method to = methods.get(0);
    if (isLoop(f)) {
        return null;
    }
    if (e.hasInvoked(i, to)) {
        return null;
    }
    if (to.isNative()) {
        return null;
    }
    Frame f2 = new Frame(e, to);
    f2.initialize(i);
    f2.setOrder(f.getOrder());
    if (e.frames.contains(f)) {
        int idx = e.frames.indexOf(f);
        // old frame goes away
        e.frames.remove(f);
        e.frames.add(idx, f2);
    } else {
        e.frames.add(f);
    }
    // only mapping executor has other
    if (f.other != null) {
        assert f.other.other == f;
        // even though theyre in different methods
        f2.other = f.other;
        f.other.other = f2;
        f.other = null;
    }
    // where to go when we're done
    f2.returnTo = new Frame(f);
    // assert f.getInstructions().isEmpty() == false; // this is wrong?
    // also wrong?
    f2.returnTo.getInstructions().addAll(f.getInstructions());
    logger.trace("Stepping into {} from {}", f2, f);
    return f2;
}
Also used : Method(net.runelite.asm.Method) InvokeStatic(net.runelite.asm.attributes.code.instructions.InvokeStatic)

Aggregations

Method (net.runelite.asm.Method)90 ClassFile (net.runelite.asm.ClassFile)64 Field (net.runelite.asm.Field)34 Instruction (net.runelite.asm.attributes.code.Instruction)29 Code (net.runelite.asm.attributes.Code)28 Instructions (net.runelite.asm.attributes.code.Instructions)28 Signature (net.runelite.asm.signature.Signature)28 ClassGroup (net.runelite.asm.ClassGroup)20 ArrayList (java.util.ArrayList)18 Type (net.runelite.asm.Type)18 List (java.util.List)13 Test (org.junit.Test)13 PushConstantInstruction (net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction)12 Logger (org.slf4j.Logger)12 LoggerFactory (org.slf4j.LoggerFactory)12 LDC (net.runelite.asm.attributes.code.instructions.LDC)10 DeobAnnotations (net.runelite.deob.DeobAnnotations)9 Annotation (net.runelite.asm.attributes.annotation.Annotation)8 InstructionType (net.runelite.asm.attributes.code.InstructionType)8 Label (net.runelite.asm.attributes.code.Label)8