use of net.runelite.asm.attributes.code.Label in project runelite by runelite.
the class ExprArgOrderTest method test5.
@Test
public void test5() {
ClassGroup group = ClassGroupFactory.generateGroup();
Code code = group.findClass("test").findMethod("func").getCode();
Instructions ins = code.getInstructions();
code.setMaxStack(2);
// vars[0] = 3
Instruction[] prepareVariables = { new LDC(ins, 3), new IStore(ins, 0) };
for (Instruction i : prepareVariables) {
ins.addInstruction(i);
}
Label label = new Label(ins);
Instruction[] body = { // if (0 == 3 + var0) -> if (var0 + 3 == 0)
new LDC(ins, 0), new LDC(ins, 3), new ILoad(ins, 0), new IAdd(ins), new IfICmpEq(ins, label), label, new VReturn(ins) };
for (Instruction i : body) {
ins.addInstruction(i);
}
ExprArgOrder exprArgOrder = new ExprArgOrder();
exprArgOrder.run(group);
List<Instruction> instructions = ins.getInstructions();
// ldc iload add -> iload ldc iadd
assertEquals(ILOAD, instructions.get(2).getType());
assertEquals(LDC, instructions.get(3).getType());
assertEquals(IADD, instructions.get(4).getType());
// idc moves from 2 to 5
assertEquals(LDC, instructions.get(5).getType());
assertEquals(IF_ICMPEQ, instructions.get(6).getType());
}
use of net.runelite.asm.attributes.code.Label in project runelite by runelite.
the class ExprArgOrderTest method test6.
@Test
public void test6() {
ClassGroup group = ClassGroupFactory.generateGroup();
Code code = group.findClass("test").findMethod("func").getCode();
Instructions ins = code.getInstructions();
code.setMaxStack(2);
// vars[0] = 3
Instruction[] prepareVariables = { new LDC(ins, 3), new IStore(ins, 0) };
for (Instruction i : prepareVariables) {
ins.addInstruction(i);
}
Label label = new Label(ins);
/*
iconst_0
ldc 8388608
iload_3
iadd
ldc -16777216
iand
if_icmpeq LABEL0x49
*/
Instruction[] body = { new LDC(ins, 0), new LDC(ins, 8388608), new ILoad(ins, 0), new IAdd(ins), new LDC(ins, -16777216), new IAnd(ins), // 8
new IfICmpEq(ins, label), label, new VReturn(ins) };
for (Instruction i : body) {
ins.addInstruction(i);
}
ExprArgOrder exprArgOrder = new ExprArgOrder();
exprArgOrder.run(group);
List<Instruction> instructions = ins.getInstructions();
assertEquals(ILOAD, instructions.get(2).getType());
assertEquals(LDC, instructions.get(3).getType());
assertEquals(IADD, instructions.get(4).getType());
assertEquals(LDC, instructions.get(5).getType());
assertEquals(IAND, instructions.get(6).getType());
assertEquals(LDC, instructions.get(7).getType());
assertEquals(IF_ICMPEQ, instructions.get(8).getType());
}
use of net.runelite.asm.attributes.code.Label in project runelite by runelite.
the class PacketWriteDeobfuscator method insert.
private void insert(ClassGroup group, PacketWrite write) {
Instructions instructions = write.putOpcode.getInstruction().getInstructions();
List<Instruction> ins = instructions.getInstructions();
InstructionContext firstWrite = write.writes.get(0);
InstructionContext lastWrite = write.writes.get(write.writes.size() - 1);
int idx = ins.indexOf(lastWrite.getInstruction());
assert idx != -1;
// past write
++idx;
Label afterWrites = instructions.createLabelFor(ins.get(idx));
// pops arg, getfield
InstructionContext beforeFirstWrite = firstWrite.getPops().get(1).getPushed();
Label putOpcode = instructions.createLabelFor(beforeFirstWrite.getInstruction(), true);
idx = ins.indexOf(beforeFirstWrite.getInstruction());
assert idx != -1;
--idx;
net.runelite.asm.pool.Field field = new net.runelite.asm.pool.Field(new net.runelite.asm.pool.Class(findClient(group).getName()), RUNELITE_PACKET, Type.BOOLEAN);
instructions.addInstruction(idx++, new GetStatic(instructions, field));
instructions.addInstruction(idx++, new IfEq(instructions, putOpcode));
Instruction before = ins.get(idx);
for (InstructionContext ctx : write.writes) {
insert(instructions, ctx, before);
}
idx = ins.indexOf(before);
instructions.addInstruction(idx++, new Goto(instructions, afterWrites));
}
use of net.runelite.asm.attributes.code.Label in project runelite by runelite.
the class HandlerFinder method insertReturn.
private void insertReturn(Instructions ins, Instruction start, Instruction end) {
assert end instanceof Label;
int idx = ins.getInstructions().indexOf(end);
assert idx != -1;
Instruction before = ins.getInstructions().get(idx - 1);
if (// XXX check isTerminal?
before.getType() == InstructionType.RETURN) {
return;
}
// insert return before end
logger.info("Inserting return before {}", end);
Instruction ret = new VReturn(ins);
ins.addInstruction(idx, ret);
Label label = ins.createLabelFor(ret);
// Change jumps which go to the next handler to instead go to return
for (Instruction i : ins.getInstructions()) {
if (i instanceof JumpingInstruction) {
JumpingInstruction j = (JumpingInstruction) i;
if (i == start) {
continue;
}
if (j.getJumps().size() == 1 && j.getJumps().get(0) == end) {
j.setJumps(Collections.singletonList(label));
}
}
}
}
use of net.runelite.asm.attributes.code.Label in project runelite by runelite.
the class ScriptVM method injectScriptVMHooks.
private void injectScriptVMHooks() throws InjectionException {
/*
This hooks local variable assignments in the copied version of runScript:
- The currently executing script > client.currentScript
- The currently executing script's program counter > client.currentScriptPC
- The currently executing opcode > client.vmExecuteOpcode(I)Z
The currently executing script variable is located as the outermost Script local
The PC is located by its use in PutField ScriptState::invokedFromPC
The currently executing opcode is found by searching for iaload with the script's instruction array
The script's instruction array is identified by looking for the getfield from script.instructions
bn.g @ rev 163 :
// Jump back to here if vmExecuteOpcode returns true
aload6 // Script.instructions
iinc 5 1 // ++PC
iload5 // PC
iaload
istore8
// <- Inject here
iload8
bipush 100
if_icmpge L52
*/
String scriptObName = DeobAnnotations.getObfuscatedName(inject.getDeobfuscated().findClass("Script").getAnnotations());
Method runScript = findObMethod("copy$runScript");
Method vmExecuteOpcode = findObMethod("vmExecuteOpcode");
Field scriptInstructions = findDeobField("instructions");
Field scriptStatePC = findDeobField("invokedFromPc");
Field currentScriptField = findObField("currentScript");
Field currentScriptPCField = findObField("currentScriptPC");
Execution e = new Execution(inject.getVanilla());
e.addMethod(runScript);
e.noInvoke = true;
AtomicReference<MethodContext> pcontext = new AtomicReference<>(null);
e.addMethodContextVisitor(pcontext::set);
e.run();
Instructions instrs = runScript.getCode().getInstructions();
Set<AStore> scriptStores = new HashSet<>();
Integer pcLocalVar = null;
Integer instructionArrayLocalVar = null;
IStore currentOpcodeStore = null;
ALoad localInstructionLoad = null;
MethodContext methodContext = pcontext.get();
for (InstructionContext instrCtx : methodContext.getInstructionContexts()) {
Instruction instr = instrCtx.getInstruction();
if (instr instanceof AStore) {
AStore store = (AStore) instr;
StackContext storedVarCtx = instrCtx.getPops().get(0);
// Find AStores that store a Script
if (storedVarCtx.getType().getInternalName().equals(scriptObName)) {
scriptStores.add(store);
}
// Find AStores that store the instructions
InstructionContext pusherCtx = storedVarCtx.getPushed();
if (pusherCtx.getInstruction() instanceof GetField) {
GetField getField = (GetField) pusherCtx.getInstruction();
if (getField.getMyField().equals(scriptInstructions)) {
instructionArrayLocalVar = store.getVariableIndex();
}
}
}
// Find the local that invokedFromPc is set from
if (instr instanceof PutField) {
PutField put = (PutField) instr;
if (put.getMyField() == scriptStatePC) {
StackContext pc = instrCtx.getPops().get(0);
assert Type.INT.equals(pc.getType()) : pc.getType();
InstructionContext mulctx = pc.pushed;
assert mulctx.getInstruction() instanceof IMul;
pcLocalVar = mulctx.getPops().stream().map(StackContext::getPushed).filter(i -> i.getInstruction() instanceof ILoad).map(i -> ((ILoad) i.getInstruction()).getVariableIndex()).findFirst().orElse(null);
}
}
}
// This has to run after the first loop because it relies on instructionArrayLocalVar being set
if (instructionArrayLocalVar == null) {
throw new InjectionException("Unable to find local instruction array");
}
for (InstructionContext instrCtx : methodContext.getInstructionContexts()) {
Instruction instr = instrCtx.getInstruction();
if (instr instanceof IALoad) {
StackContext array = instrCtx.getPops().get(1);
// Check where the array came from (looking for a getField scriptInstructions
InstructionContext pushedCtx = array.getPushed();
Instruction pushed = pushedCtx.getInstruction();
if (pushed instanceof ALoad) {
ALoad arrayLoad = (ALoad) pushed;
if (arrayLoad.getVariableIndex() == instructionArrayLocalVar) {
// Find the istore
IStore istore = (IStore) instrCtx.getPushes().get(0).getPopped().stream().map(InstructionContext::getInstruction).filter(i -> i instanceof IStore).findFirst().orElse(null);
if (istore != null) {
currentOpcodeStore = istore;
localInstructionLoad = arrayLoad;
}
}
}
}
}
// Add PutStatics to all Script AStores
{
int outerSciptIdx = scriptStores.stream().mapToInt(AStore::getVariableIndex).reduce(Math::min).orElseThrow(() -> new InjectionException("Unable to find any Script AStores in runScript"));
log.debug("Found script index {}", outerSciptIdx);
ListIterator<Instruction> instrIter = instrs.getInstructions().listIterator();
while (instrIter.hasNext()) {
Instruction instr = instrIter.next();
if (instr instanceof AStore) {
AStore il = (AStore) instr;
if (il.getVariableIndex() == outerSciptIdx) {
instrIter.previous();
instrIter.add(new Dup(instrs));
instrIter.add(new PutStatic(instrs, currentScriptField));
instrIter.next();
}
}
}
}
// Add PutStatics to all PC IStores and IIncs
{
if (pcLocalVar == null) {
throw new InjectionException("Unable to find ILoad for invokedFromPc IStore");
}
log.debug("Found pc index {}", pcLocalVar);
ListIterator<Instruction> instrIter = instrs.getInstructions().listIterator();
while (instrIter.hasNext()) {
Instruction instr = instrIter.next();
if (instr instanceof IStore) {
IStore il = (IStore) instr;
if (il.getVariableIndex() == pcLocalVar) {
instrIter.previous();
instrIter.add(new Dup(instrs));
instrIter.add(new PutStatic(instrs, currentScriptPCField));
instrIter.next();
}
}
if (instr instanceof IInc) {
IInc iinc = (IInc) instr;
if (iinc.getVariableIndex() == pcLocalVar) {
instrIter.add(new ILoad(instrs, pcLocalVar));
instrIter.add(new PutStatic(instrs, currentScriptPCField));
}
}
}
}
// Inject call to vmExecuteOpcode
log.debug("Found instruction array index {}", instructionArrayLocalVar);
if (currentOpcodeStore == null) {
throw new InjectionException("Unable to find IStore for current opcode");
}
int istorepc = instrs.getInstructions().indexOf(currentOpcodeStore);
assert istorepc >= 0;
Label nextIteration = instrs.createLabelFor(localInstructionLoad);
instrs.addInstruction(istorepc + 1, new ILoad(instrs, currentOpcodeStore.getVariableIndex()));
instrs.addInstruction(istorepc + 2, new InvokeStatic(instrs, vmExecuteOpcode.getPoolMethod()));
instrs.addInstruction(istorepc + 3, new IfNe(instrs, nextIteration));
}
Aggregations