use of net.runelite.asm.attributes.code.instructions.AConstNull 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.AConstNull in project runelite by runelite.
the class ExprArgOrder method compare.
public static int compare(Method method, InstructionType type, InstructionContext ic1, InstructionContext ic2) {
Instruction i1 = ic1.getInstruction();
Instruction i2 = ic2.getInstruction();
if (type == IF_ICMPEQ || type == IF_ICMPNE || type == IADD || type == IMUL) {
if (!(i1 instanceof PushConstantInstruction) && (i2 instanceof PushConstantInstruction)) {
return 1;
}
if (i1 instanceof PushConstantInstruction && !(i2 instanceof PushConstantInstruction)) {
return -1;
}
}
if (type == IF_ACMPEQ || type == IF_ACMPNE) {
if (!(i1 instanceof AConstNull) && (i2 instanceof AConstNull)) {
return 1;
}
if (i1 instanceof AConstNull && !(i2 instanceof AConstNull)) {
return -1;
}
}
int hash1 = hash(method, ic1);
int hash2 = hash(method, ic2);
if (hash1 == hash2) {
logger.debug("Unable to differentiate {} from {}", ic1, ic2);
}
return Integer.compare(hash1, hash2);
}
use of net.runelite.asm.attributes.code.instructions.AConstNull in project runelite by runelite.
the class InjectHook method run.
public void run() {
Execution e = new Execution(inject.getVanilla());
e.populateInitialMethods();
Set<Instruction> done = new HashSet<>();
Set<Instruction> doneIh = new HashSet<>();
e.addExecutionVisitor((InstructionContext ic) -> {
Instruction i = ic.getInstruction();
Instructions ins = i.getInstructions();
Code code = ins.getCode();
Method method = code.getMethod();
if (method.getName().equals(CLINIT)) {
return;
}
if (!(i instanceof SetFieldInstruction)) {
return;
}
if (!done.add(i)) {
return;
}
SetFieldInstruction sfi = (SetFieldInstruction) i;
Field fieldBeingSet = sfi.getMyField();
if (fieldBeingSet == null) {
return;
}
HookInfo hookInfo = hooked.get(fieldBeingSet);
if (hookInfo == null) {
return;
}
String hookName = hookInfo.fieldName;
assert hookName != null;
logger.trace("Found injection location for hook {} at instruction {}", hookName, sfi);
++injectedHooks;
Instruction objectInstruction = new AConstNull(ins);
StackContext objectStackContext = null;
if (sfi instanceof PutField) {
// Object being set on
StackContext objectStack = ic.getPops().get(1);
objectStackContext = objectStack;
}
int idx = ins.getInstructions().indexOf(sfi);
assert idx != -1;
try {
// idx + 1 to insert after the set
injectCallback(ins, idx + 1, hookInfo, null, objectStackContext);
} catch (InjectionException ex) {
throw new RuntimeException(ex);
}
});
// these look like:
// getfield
// iload_0
// iconst_0
// iastore
e.addExecutionVisitor((InstructionContext ic) -> {
Instruction i = ic.getInstruction();
Instructions ins = i.getInstructions();
Code code = ins.getCode();
Method method = code.getMethod();
if (method.getName().equals(CLINIT)) {
return;
}
if (!(i instanceof ArrayStore)) {
return;
}
if (!doneIh.add(i)) {
return;
}
ArrayStore as = (ArrayStore) i;
Field fieldBeingSet = as.getMyField(ic);
if (fieldBeingSet == null) {
return;
}
HookInfo hookInfo = hooked.get(fieldBeingSet);
if (hookInfo == null) {
return;
}
String hookName = hookInfo.fieldName;
// assume this is always at index 1
StackContext index = ic.getPops().get(1);
StackContext arrayReference = ic.getPops().get(2);
InstructionContext arrayReferencePushed = arrayReference.getPushed();
StackContext objectStackContext = null;
if (arrayReferencePushed.getInstruction().getType() == InstructionType.GETFIELD) {
StackContext objectReference = arrayReferencePushed.getPops().get(0);
objectStackContext = objectReference;
}
// inject hook after 'i'
logger.info("Found array injection location for hook {} at instruction {}", hookName, i);
++injectedHooks;
int idx = ins.getInstructions().indexOf(i);
assert idx != -1;
try {
injectCallback(ins, idx + 1, hookInfo, index, objectStackContext);
} catch (InjectionException ex) {
throw new RuntimeException(ex);
}
});
e.run();
}
use of net.runelite.asm.attributes.code.instructions.AConstNull in project runelite by runelite.
the class CastNull method visit.
private void visit(InstructionContext ictx) {
if (!(ictx.getInstruction() instanceof CheckCast))
return;
if (notInteresting.contains(ictx.getInstruction()) || interesting.contains(ictx.getInstruction()))
return;
StackContext sctx = ictx.getPops().get(0);
if (sctx.getPushed().getInstruction() instanceof AConstNull) {
interesting.add(ictx.getInstruction());
} else {
interesting.remove(ictx.getInstruction());
notInteresting.add(ictx.getInstruction());
}
}
use of net.runelite.asm.attributes.code.instructions.AConstNull in project runelite by runelite.
the class DupDeobfuscatorTest method test.
@Test
public void test() {
ClassGroup group = ClassGroupFactory.generateGroup();
Code code = group.findClass("test").findMethod("func").getCode();
Instructions ins = code.getInstructions();
code.setMaxStack(5);
Instruction[] prepareVariables = { new LDC(ins, 1), new IStore(ins, 0) };
for (Instruction i : prepareVariables) {
ins.addInstruction(i);
}
LDC constant1 = new LDC(ins, 1129258489), constant2 = new LDC(ins, -1692330935), constant3 = new LDC(ins, 1641298955), constant4 = new LDC(ins, 1043501435);
Instruction[] body = { // this
new AConstNull(ins), // this
new AConstNull(ins), new ILoad(ins, 0), constant1, new IMul(ins), new Dup_X1(ins), constant2, new IMul(ins), // putfield
new Pop2(ins), constant3, new IMul(ins), constant4, new IMul(ins), // putfield
new Pop2(ins), new VReturn(ins) };
for (Instruction i : body) {
ins.addInstruction(i);
}
Execution e = new Execution(group);
e.populateInitialMethods();
e.run();
assert constant1.getConstantAsInt() * constant2.getConstantAsInt() == 1;
assert constant3.getConstantAsInt() * constant4.getConstantAsInt() * constant1.getConstantAsInt() == 1;
Deobfuscator d = new DupDeobfuscator();
d.run(group);
// assert the dup_x1 was removed
long dupCount = ins.getInstructions().stream().filter(i -> i instanceof Dup_X1).count();
Assert.assertEquals(0, dupCount);
}
Aggregations