use of net.runelite.asm.attributes.code.Instruction in project runelite by runelite.
the class PacketHandlerOrder method follow.
private List<Instruction> follow(Instructions instructions, Instruction start, Instruction end) {
List<Instruction> list = new ArrayList<>();
int idx = instructions.getInstructions().indexOf(start);
assert idx != -1;
for (; ; ) {
Instruction i = instructions.getInstructions().get(idx);
// end is the following instruction post read.. not included
if (i == end) {
break;
}
if (i instanceof Goto) {
Goto g = (Goto) i;
Label to = g.getTo();
idx = instructions.getInstructions().indexOf(to);
assert idx != -1;
continue;
}
list.add(i);
if (i instanceof ComparisonInstruction) {
return list;
}
++idx;
}
return list;
}
use of net.runelite.asm.attributes.code.Instruction in project runelite by runelite.
the class UnusedMethods method run.
private void run(Method method) {
Code code = method.getCode();
if (code == null) {
return;
}
for (Instruction i : code.getInstructions().getInstructions()) {
if (!(i instanceof InvokeInstruction)) {
continue;
}
InvokeInstruction ii = (InvokeInstruction) i;
methods.addAll(ii.getMethods());
}
}
use of net.runelite.asm.attributes.code.Instruction in project runelite by runelite.
the class Renamer method renameClass.
private void renameClass(ClassGroup group, ClassFile cf, String name) {
for (ClassFile c : group.getClasses()) {
// rename on child interfaces and classes
renameClass(c, cf, name);
for (Method method : c.getMethods()) {
// rename on instructions. this includes method calls and field accesses.
if (method.getCode() != null) {
Code code = method.getCode();
// rename on instructions
for (Instruction i : code.getInstructions().getInstructions()) {
i.renameClass(cf.getName(), name);
}
// rename on exception handlers
Exceptions exceptions = code.getExceptions();
exceptions.renameClass(cf, name);
}
// rename on parameters
Signature.Builder builder = new Signature.Builder();
Signature signature = method.getDescriptor();
for (int i = 0; i < signature.size(); ++i) {
Type type = signature.getTypeOfArg(i);
if (type.getInternalName().equals(cf.getName())) {
builder.addArgument(Type.getType("L" + name + ";", type.getDimensions()));
} else {
builder.addArgument(type);
}
}
// rename return type
if (signature.getReturnValue().getInternalName().equals(cf.getName())) {
builder.setReturnType(Type.getType("L" + name + ";", signature.getReturnValue().getDimensions()));
} else {
builder.setReturnType(signature.getReturnValue());
}
Signature newSignature = builder.build();
if (!method.getDescriptor().equals(newSignature)) {
// Signature was updated. Annotate it
if (method.getAnnotations().find(DeobAnnotations.OBFUSCATED_SIGNATURE) == null) {
// Signature was not previously renamed
method.getAnnotations().addAnnotation(DeobAnnotations.OBFUSCATED_SIGNATURE, "signature", method.getDescriptor().toString());
}
}
method.setDescriptor(newSignature);
// rename on exceptions thrown
if (method.getExceptions() != null) {
method.getExceptions().renameClass(cf, name);
}
}
// rename on fields
for (Field field : c.getFields()) {
if (field.getType().getInternalName().equals(cf.getName())) {
if (field.getAnnotations().find(DeobAnnotations.OBFUSCATED_SIGNATURE) == null) {
// Signature was updated. Annotate it
field.getAnnotations().addAnnotation(DeobAnnotations.OBFUSCATED_SIGNATURE, "signature", field.getType().toString());
}
field.setType(Type.getType("L" + name + ";", field.getType().getDimensions()));
}
}
}
if (cf.getAnnotations().find(DeobAnnotations.OBFUSCATED_NAME) == null) {
cf.getAnnotations().addAnnotation(DeobAnnotations.OBFUSCATED_NAME, "value", cf.getName());
}
group.renameClass(cf, name);
}
use of net.runelite.asm.attributes.code.Instruction in project runelite by runelite.
the class UnreachedCode method removeUnused.
private int removeUnused(Method m) {
Instructions ins = m.getCode().getInstructions();
int count = 0;
List<Instruction> insCopy = new ArrayList<>(ins.getInstructions());
for (int j = 0; j < insCopy.size(); ++j) {
Instruction i = insCopy.get(j);
if (!execution.executed.contains(i)) {
// if this is an exception handler, the exception handler is never used...
for (net.runelite.asm.attributes.code.Exception e : new ArrayList<>(m.getCode().getExceptions().getExceptions())) {
if (e.getStart().next() == i) {
e.setStart(ins.createLabelFor(insCopy.get(j + 1)));
if (e.getStart().next() == e.getEnd().next()) {
m.getCode().getExceptions().remove(e);
continue;
}
}
if (e.getHandler().next() == i) {
m.getCode().getExceptions().remove(e);
}
}
if (i instanceof Label)
continue;
ins.remove(i);
++count;
}
}
return count;
}
use of net.runelite.asm.attributes.code.Instruction 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);
}
}
Aggregations