Search in sources :

Example 56 with Code

use of net.runelite.asm.attributes.Code in project runelite by runelite.

the class ClassGroupFactory method generateGroup.

public static ClassGroup generateGroup() {
    ClassGroup group = new ClassGroup();
    ClassFile cf = new ClassFile(group);
    cf.setName("test");
    cf.setSuperName("java/lang/Object");
    group.addClass(cf);
    Field field = new Field(cf, "field", Type.INT);
    field.setStatic();
    cf.addField(field);
    Method method = new Method(cf, "func", new Signature("()V"));
    method.setStatic();
    cf.addMethod(method);
    Code code = new Code(method);
    method.setCode(code);
    {
        method = new Method(cf, "func2", new Signature("(III)V"));
        method.setStatic();
        cf.addMethod(method);
        code = new Code(method);
        method.setCode(code);
        Instructions ins = code.getInstructions();
        ins.addInstruction(new VReturn(ins));
    }
    addVoidMethod(cf, "void1");
    addVoidMethod(cf, "void2");
    addVoidMethod(cf, "void3");
    addVoidMethod(cf, "void4");
    return group;
}
Also used : Field(net.runelite.asm.Field) ClassFile(net.runelite.asm.ClassFile) ClassGroup(net.runelite.asm.ClassGroup) Signature(net.runelite.asm.signature.Signature) Instructions(net.runelite.asm.attributes.code.Instructions) Method(net.runelite.asm.Method) Code(net.runelite.asm.attributes.Code) VReturn(net.runelite.asm.attributes.code.instructions.VReturn)

Example 57 with Code

use of net.runelite.asm.attributes.Code in project runelite by runelite.

the class InjectGetter method injectGetter.

public void injectGetter(ClassFile clazz, java.lang.reflect.Method method, Field field, Number getter) {
    assert clazz.findMethod(method.getName()) == null;
    assert field.isStatic() || field.getClassFile() == clazz;
    Signature sig = new Signature.Builder().setReturnType(Inject.classToType(method.getReturnType())).build();
    Method getterMethod = new Method(clazz, method.getName(), sig);
    getterMethod.setAccessFlags(ACC_PUBLIC);
    // create code
    Code code = new Code(getterMethod);
    getterMethod.setCode(code);
    Instructions instructions = code.getInstructions();
    List<Instruction> ins = instructions.getInstructions();
    if (field.isStatic()) {
        code.setMaxStack(1);
        ins.add(new GetStatic(instructions, field.getPoolField()));
    } else {
        code.setMaxStack(2);
        ins.add(new ALoad(instructions, 0));
        ins.add(new GetField(instructions, field.getPoolField()));
    }
    if (getter != null) {
        code.setMaxStack(2);
        assert getter instanceof Integer || getter instanceof Long;
        if (getter instanceof Integer) {
            ins.add(new LDC(instructions, (int) getter));
            ins.add(new IMul(instructions));
        } else {
            ins.add(new LDC(instructions, (long) getter));
            ins.add(new LMul(instructions));
        }
    }
    InstructionType returnType;
    if (field.getType().isPrimitive() && field.getType().getDimensions() == 0) {
        switch(field.getType().toString()) {
            case "B":
            case "C":
            case "I":
            case "S":
            case "Z":
                returnType = InstructionType.IRETURN;
                break;
            case "D":
                returnType = InstructionType.DRETURN;
                break;
            case "F":
                returnType = InstructionType.FRETURN;
                break;
            case "J":
                returnType = InstructionType.LRETURN;
                break;
            default:
                throw new RuntimeException("Unknown type");
        }
    } else {
        returnType = InstructionType.ARETURN;
    }
    ins.add(new Return(instructions, returnType));
    clazz.addMethod(getterMethod);
    ++injectedGetters;
}
Also used : GetField(net.runelite.asm.attributes.code.instructions.GetField) Return(net.runelite.asm.attributes.code.instructions.Return) InstructionType(net.runelite.asm.attributes.code.InstructionType) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) Method(net.runelite.asm.Method) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) GetStatic(net.runelite.asm.attributes.code.instructions.GetStatic) Signature(net.runelite.asm.signature.Signature) IMul(net.runelite.asm.attributes.code.instructions.IMul) LMul(net.runelite.asm.attributes.code.instructions.LMul) ALoad(net.runelite.asm.attributes.code.instructions.ALoad)

Example 58 with Code

use of net.runelite.asm.attributes.Code in project runelite by runelite.

the class InjectSetterTest method testInjectSetterInt.

@Test
public void testInjectSetterInt() throws NoSuchMethodException {
    Inject inject = mock(Inject.class);
    when(inject.findImportMethodOnApi(any(Class.class), anyString(), any(Boolean.class))).thenReturn(APIClass.class.getDeclaredMethod("setTest", int.class));
    when(inject.createLoadForTypeIndex(any(Instructions.class), any(Type.class), anyInt())).thenReturn(mock(Instruction.class));
    InjectSetter instance = new InjectSetter(inject);
    ClassFile targetClass = new ClassFile();
    targetClass.setName("test");
    Field field = new Field(targetClass, "test", Type.INT);
    targetClass.addField(field);
    instance.injectSetter(targetClass, APIClass.class, field, "test", null);
    Method injectedMethod = targetClass.findMethod("setTest");
    assertNotNull(injectedMethod);
    Code code = injectedMethod.getCode();
    Instructions instructions = code.getInstructions();
    assertFalse(instructions.getInstructions().stream().filter(i -> i.getType() == CHECKCAST).findAny().isPresent());
}
Also used : Assert.assertNotNull(org.junit.Assert.assertNotNull) Field(net.runelite.asm.Field) Assert.assertTrue(org.junit.Assert.assertTrue) Code(net.runelite.asm.attributes.Code) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Type(net.runelite.asm.Type) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.any(org.mockito.Matchers.any) ClassFile(net.runelite.asm.ClassFile) CHECKCAST(net.runelite.asm.attributes.code.InstructionType.CHECKCAST) Method(net.runelite.asm.Method) Assert.assertFalse(org.junit.Assert.assertFalse) Instructions(net.runelite.asm.attributes.code.Instructions) Matchers.anyInt(org.mockito.Matchers.anyInt) Instruction(net.runelite.asm.attributes.code.Instruction) Mockito.mock(org.mockito.Mockito.mock) ClassFile(net.runelite.asm.ClassFile) Instructions(net.runelite.asm.attributes.code.Instructions) Method(net.runelite.asm.Method) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) Field(net.runelite.asm.Field) Type(net.runelite.asm.Type) Test(org.junit.Test)

Example 59 with Code

use of net.runelite.asm.attributes.Code in project runelite by runelite.

the class InjectSetterTest method testInjectSetterObject.

@Test
public void testInjectSetterObject() throws NoSuchMethodException {
    Inject inject = mock(Inject.class);
    when(inject.findImportMethodOnApi(any(Class.class), anyString(), any(Boolean.class))).thenReturn(APIClass.class.getDeclaredMethod("setTestObject", Object.class));
    when(inject.createLoadForTypeIndex(any(Instructions.class), any(Type.class), anyInt())).thenReturn(mock(Instruction.class));
    InjectSetter instance = new InjectSetter(inject);
    ClassFile targetClass = new ClassFile();
    targetClass.setName("test");
    Field field = new Field(targetClass, "testObject", Type.STRING);
    targetClass.addField(field);
    instance.injectSetter(targetClass, APIClass.class, field, "testObject", null);
    Method injectedMethod = targetClass.findMethod("setTestObject");
    assertNotNull(injectedMethod);
    Code code = injectedMethod.getCode();
    Instructions instructions = code.getInstructions();
    assertTrue(instructions.getInstructions().stream().filter(i -> i.getType() == CHECKCAST).findAny().isPresent());
}
Also used : Assert.assertNotNull(org.junit.Assert.assertNotNull) Field(net.runelite.asm.Field) Assert.assertTrue(org.junit.Assert.assertTrue) Code(net.runelite.asm.attributes.Code) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Type(net.runelite.asm.Type) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.any(org.mockito.Matchers.any) ClassFile(net.runelite.asm.ClassFile) CHECKCAST(net.runelite.asm.attributes.code.InstructionType.CHECKCAST) Method(net.runelite.asm.Method) Assert.assertFalse(org.junit.Assert.assertFalse) Instructions(net.runelite.asm.attributes.code.Instructions) Matchers.anyInt(org.mockito.Matchers.anyInt) Instruction(net.runelite.asm.attributes.code.Instruction) Mockito.mock(org.mockito.Mockito.mock) ClassFile(net.runelite.asm.ClassFile) Instructions(net.runelite.asm.attributes.code.Instructions) Method(net.runelite.asm.Method) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) Field(net.runelite.asm.Field) Type(net.runelite.asm.Type) Test(org.junit.Test)

Example 60 with Code

use of net.runelite.asm.attributes.Code in project runelite by runelite.

the class ClassGroup method lookup.

public void lookup() {
    for (ClassFile cf : this.getClasses()) {
        for (Method m : cf.getMethods()) {
            Code code = m.getCode();
            if (code == null) {
                continue;
            }
            code.getInstructions().lookup();
        }
    }
}
Also used : Code(net.runelite.asm.attributes.Code)

Aggregations

Code (net.runelite.asm.attributes.Code)62 Instruction (net.runelite.asm.attributes.code.Instruction)49 Instructions (net.runelite.asm.attributes.code.Instructions)45 LDC (net.runelite.asm.attributes.code.instructions.LDC)31 ClassGroup (net.runelite.asm.ClassGroup)30 VReturn (net.runelite.asm.attributes.code.instructions.VReturn)30 Test (org.junit.Test)29 Method (net.runelite.asm.Method)26 IStore (net.runelite.asm.attributes.code.instructions.IStore)23 ILoad (net.runelite.asm.attributes.code.instructions.ILoad)22 IMul (net.runelite.asm.attributes.code.instructions.IMul)21 Execution (net.runelite.asm.execution.Execution)21 Deobfuscator (net.runelite.deob.Deobfuscator)19 ClassFile (net.runelite.asm.ClassFile)18 Signature (net.runelite.asm.signature.Signature)15 Type (net.runelite.asm.Type)12 Pop (net.runelite.asm.attributes.code.instructions.Pop)12 IAdd (net.runelite.asm.attributes.code.instructions.IAdd)11 Label (net.runelite.asm.attributes.code.Label)10 Field (net.runelite.asm.Field)8