use of net.runelite.asm.attributes.code.Instructions in project runelite by runelite.
the class RuneliteBufferTransformer method injectLengthHeader.
/**
* inject the length header after the packet opcode
*
* @param group
*/
private void injectLengthHeader(ClassGroup group) {
RWOpcodeFinder rw = new RWOpcodeFinder(group);
rw.find();
Method writeOpcode = rw.getWriteOpcode();
Code code = writeOpcode.getCode();
Instructions instructions = code.getInstructions();
List<Instruction> ins = instructions.getInstructions();
Instruction start = ins.get(0);
Instruction end = ins.stream().filter(i -> i.getType() == RETURN).findFirst().get();
Label labelForStart = instructions.createLabelFor(start);
Label labelForEnd = instructions.createLabelFor(end);
final net.runelite.asm.pool.Field runelitePacketField = new net.runelite.asm.pool.Field(new net.runelite.asm.pool.Class(findClient(group).getName()), RUNELITE_PACKET, Type.BOOLEAN);
int idx = ins.indexOf(labelForStart);
instructions.addInstruction(idx++, new GetStatic(instructions, runelitePacketField));
instructions.addInstruction(idx++, new IfEq(instructions, labelForStart));
net.runelite.asm.pool.Method method = new net.runelite.asm.pool.Method(new net.runelite.asm.pool.Class(writeOpcode.getClassFile().getName()), RUNELITE_FINISH_PACKET, new Signature("()V"));
instructions.addInstruction(idx++, new ALoad(instructions, 0));
instructions.addInstruction(idx++, new InvokeVirtual(instructions, method));
idx = ins.indexOf(labelForEnd);
instructions.addInstruction(idx++, new GetStatic(instructions, runelitePacketField));
instructions.addInstruction(idx++, new IfEq(instructions, labelForEnd));
method = new net.runelite.asm.pool.Method(new net.runelite.asm.pool.Class(writeOpcode.getClassFile().getName()), RUNELITE_INIT_PACKET, new Signature("()V"));
instructions.addInstruction(idx++, new ALoad(instructions, 0));
instructions.addInstruction(idx++, new InvokeVirtual(instructions, method));
logger.info("Injected finish/init packet calls into {}", writeOpcode);
}
use of net.runelite.asm.attributes.code.Instructions in project runelite by runelite.
the class MenuActionDeobfuscator method insert.
private void insert(Method method, List<Comparison> comparisons) {
Instructions instructions = method.getCode().getInstructions();
List<Instruction> ins = instructions.getInstructions();
// replace all if(var == constant) with a jump to the false branch
// then, insert before the first jump the ifs to jump to the old
// true branch
//
// this is probably actually lookupswitch but it isn't mappable
// currently...
int min = -1;
for (Comparison comp : comparisons) {
if (min == -1) {
min = ins.indexOf(comp.lvt);
} else {
min = Math.min(min, ins.indexOf(comp.lvt));
}
if (comp.cmp.getType() == InstructionType.IF_ICMPEQ) {
If cmp = (If) comp.cmp;
// remove
instructions.remove(comp.ldc);
instructions.remove((Instruction) comp.lvt);
instructions.remove(comp.cmp);
comp.next = cmp.getJumps().get(0);
} else if (comp.cmp.getType() == InstructionType.IF_ICMPNE) {
// replace with goto dest
If cmp = (If) comp.cmp;
int idx = ins.indexOf(cmp);
assert idx != -1;
comp.next = instructions.createLabelFor(ins.get(idx + 1));
instructions.remove(comp.ldc);
instructions.remove((Instruction) comp.lvt);
instructions.replace(comp.cmp, new Goto(instructions, cmp.getJumps().get(0)));
} else {
throw new IllegalStateException();
}
}
assert min != -1;
// sort comparisons - but if they jump to the same address, they are equal..
List<Comparison> sortedComparisons = new ArrayList<>(comparisons);
Collections.sort(sortedComparisons, (c1, c2) -> compare(comparisons, c1, c2));
// reinsert jumps
for (int i = 0; i < sortedComparisons.size(); ++i) {
Comparison comp = sortedComparisons.get(i);
Instruction lvt = (Instruction) comp.lvt;
lvt.setInstructions(instructions);
comp.ldc.setInstructions(instructions);
instructions.addInstruction(min++, lvt);
instructions.addInstruction(min++, comp.ldc);
// use if_icmpeq if what follows also jumps to the same location
boolean multiple = i + 1 < sortedComparisons.size() && sortedComparisons.get(i + 1).next == comp.next;
if (multiple) {
instructions.addInstruction(min++, new IfICmpEq(instructions, comp.next));
} else {
// fernflower decompiles a series of if_icmpeq as chains of not equal expressions
Label label = instructions.createLabelFor(ins.get(min));
instructions.addInstruction(min++, new IfICmpNe(instructions, label));
instructions.addInstruction(min++, new Goto(instructions, comp.next));
// go past label
++min;
}
}
}
use of net.runelite.asm.attributes.code.Instructions in project runelite by runelite.
the class ClassGroupFactory method addVoidMethod.
private static void addVoidMethod(ClassFile cf, String name) {
Method method = new Method(cf, name, new Signature("()V"));
method.setStatic();
cf.addMethod(method);
Code code = new Code(method);
method.setCode(code);
Instructions ins = code.getInstructions();
ins.addInstruction(new VReturn(ins));
}
use of net.runelite.asm.attributes.code.Instructions 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());
}
use of net.runelite.asm.attributes.code.Instructions in project runelite by runelite.
the class SimpleModArithTest method checkConstants.
private void checkConstants(ClassFile cf) {
for (Method m : cf.getMethods()) {
Code code = m.getCode();
Instructions instructions = code.getInstructions();
for (Instruction i : instructions.getInstructions()) {
if (i instanceof LDC) {
LDC ldc = (LDC) i;
Integer value = (Integer) ldc.getConstantAsInt();
assertFalse(isBig(value));
}
}
}
}
Aggregations