use of net.runelite.asm.execution.InstructionContext in project runelite by runelite.
the class MultiplicationDeobfuscator method visit.
private void visit(MethodContext ctx) {
for (InstructionContext ictx : ctx.getInstructionContexts()) {
Instruction instruction = ictx.getInstruction();
if (!(instruction instanceof IMul) && !(instruction instanceof LMul)) {
continue;
}
MultiplicationExpression expression;
try {
expression = parseExpression(ictx, instruction.getClass());
} catch (IllegalStateException ex) {
continue;
}
if (expression == null) {
continue;
}
if (done.contains(instruction)) {
continue;
}
done.add(instruction);
assert instruction instanceof IMul || instruction instanceof LMul;
if (instruction instanceof IMul) {
count += expression.simplify(1);
} else if (instruction instanceof LMul) {
count += expression.simplify(1L);
} else {
throw new IllegalStateException();
}
}
}
use of net.runelite.asm.execution.InstructionContext in project runelite by runelite.
the class MultiplicationExpression method simplify.
int simplify(Number start) {
int count = 0;
Number result = start;
// calculate result
for (InstructionContext i : instructions) {
PushConstantInstruction pci = (PushConstantInstruction) i.getInstruction();
Number value = (Number) pci.getConstant();
result = DMath.multiply(result, value);
}
if (dupmagic != null) {
// mul dupmagic by result of dup ins?
PushConstantInstruction pci = (PushConstantInstruction) dupmagic.getInstruction();
Number value = (Number) pci.getConstant();
for (InstructionContext ic : dupedInstructions) {
PushConstantInstruction pci2 = (PushConstantInstruction) ic.getInstruction();
Number value2 = (Number) pci2.getConstant();
value = DMath.multiply(value, value2);
}
Instruction newIns = pci.setConstant(value);
assert newIns == (Instruction) pci;
}
// multiply subexpressions by result
if (!subexpressions.isEmpty()) {
for (MultiplicationExpression me : subexpressions) {
count += me.simplify(result);
}
if (dupmagic != null) {
PushConstantInstruction pci = (PushConstantInstruction) dupmagic.getInstruction();
Number value = (Number) pci.getConstant();
value = DMath.multiply(value, DMath.modInverse(result));
pci.setConstant(value);
}
// constant has been distributed, outer numbers all go to 1
if (result instanceof Long)
result = 1L;
else
result = 1;
}
// set result on ins
for (InstructionContext i : instructions) {
PushConstantInstruction pci = (PushConstantInstruction) i.getInstruction();
Instruction newIns = pci.setConstant(result);
++count;
assert newIns == pci;
// rest of the results go to 1
if (result instanceof Long)
result = 1L;
else
result = 1;
}
return count;
}
use of net.runelite.asm.execution.InstructionContext 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);
}
}
use of net.runelite.asm.execution.InstructionContext in project runelite by runelite.
the class DupDeobfuscator method copy.
/**
* copy the instruction which pushed sctx and insert into instructions
* starting at index idx
*/
private int copy(StackContext sctx, Instructions instructions, int idx) {
InstructionContext ictx = sctx.getPushed();
if (ictx.getInstruction() instanceof DupInstruction) {
// we only care about one path
DupInstruction di = (DupInstruction) ictx.getInstruction();
sctx = di.getOriginal(sctx);
ictx = sctx.getPushed();
}
// the first thing popped was pushed last, so reverse
for (StackContext s : Lists.reverse(ictx.getPops())) {
idx = copy(s, instructions, idx);
}
// copy instruction
Instruction i = ictx.getInstruction();
logger.debug("Duplicating instruction {} at index {}", i, idx);
i = i.clone();
instructions.addInstruction(idx, i);
// move on to next instruction
return idx + 1;
}
use of net.runelite.asm.execution.InstructionContext in project runelite by runelite.
the class Pop2 method execute.
@Override
public InstructionContext execute(Frame frame) {
InstructionContext ins = new InstructionContext(this, frame);
Stack stack = frame.getStack();
StackContext value = stack.pop();
ins.pop(value);
if (value.getType().getSize() == 2)
return ins;
value = stack.pop();
ins.pop(value);
return ins;
}
Aggregations