Search in sources :

Example 36 with ClassFile

use of net.runelite.asm.ClassFile 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 37 with ClassFile

use of net.runelite.asm.ClassFile 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 38 with ClassFile

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

the class PutField method getMyField.

@Override
public net.runelite.asm.Field getMyField() {
    Class clazz = field.getClazz();
    ClassGroup group = this.getInstructions().getCode().getMethod().getClassFile().getGroup();
    ClassFile cf = group.findClass(clazz.getName());
    if (cf == null) {
        return null;
    }
    net.runelite.asm.Field f2 = cf.findFieldDeep(field.getName(), field.getType());
    return f2;
}
Also used : ClassFile(net.runelite.asm.ClassFile) ClassGroup(net.runelite.asm.ClassGroup) Class(net.runelite.asm.pool.Class)

Example 39 with ClassFile

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

the class EnumDeobfuscator method run.

@Override
public void run(ClassGroup group) {
    for (ClassFile cf : group.getClasses()) {
        if (!isEnum(cf) || cf.isEnum()) {
            continue;
        }
        logger.info("Converting {} to an enum", cf.getName());
        makeEnum(cf);
    }
}
Also used : ClassFile(net.runelite.asm.ClassFile)

Example 40 with ClassFile

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

the class VirtualMethods method findBaseMethods.

// find the base methods for a method. search goes up from there to see if two
// different methods can be invoked with the same instruction.
private static List<Method> findBaseMethods(List<Method> methods, ClassFile cf, String name, Signature type) {
    if (cf == null)
        return methods;
    Method m = cf.findMethod(name, type);
    if (m != null && !m.isStatic())
        methods.add(m);
    List<Method> parentMethods = findBaseMethods(new ArrayList<>(), cf.getParent(), name, type);
    for (ClassFile inter : cf.getInterfaces().getMyInterfaces()) parentMethods.addAll(findBaseMethods(new ArrayList<>(), inter, name, type));
    // parentMethods take precedence over our methods
    return parentMethods.isEmpty() ? methods : parentMethods;
}
Also used : ClassFile(net.runelite.asm.ClassFile) Method(net.runelite.asm.Method)

Aggregations

ClassFile (net.runelite.asm.ClassFile)103 Method (net.runelite.asm.Method)62 Field (net.runelite.asm.Field)39 ClassGroup (net.runelite.asm.ClassGroup)32 Code (net.runelite.asm.attributes.Code)21 Instruction (net.runelite.asm.attributes.code.Instruction)18 Test (org.junit.Test)18 Signature (net.runelite.asm.signature.Signature)17 Type (net.runelite.asm.Type)16 Instructions (net.runelite.asm.attributes.code.Instructions)16 ArrayList (java.util.ArrayList)14 List (java.util.List)13 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)10 IOException (java.io.IOException)9 InputStream (java.io.InputStream)9 Annotation (net.runelite.asm.attributes.annotation.Annotation)9 PushConstantInstruction (net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction)9 HashMap (java.util.HashMap)8 GetFieldInstruction (net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction)7