forked from openrs2/openrs2
parent
fdd1835fdd
commit
00526587b0
@ -1,71 +0,0 @@ |
|||||||
package dev.openrs2.deob.transform; |
|
||||||
|
|
||||||
import dev.openrs2.asm.InsnListUtilsKt; |
|
||||||
import dev.openrs2.asm.classpath.ClassPath; |
|
||||||
import dev.openrs2.asm.classpath.Library; |
|
||||||
import dev.openrs2.asm.transform.Transformer; |
|
||||||
import org.objectweb.asm.Opcodes; |
|
||||||
import org.objectweb.asm.tree.ClassNode; |
|
||||||
import org.objectweb.asm.tree.MethodNode; |
|
||||||
import org.objectweb.asm.tree.VarInsnNode; |
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
public final class DummyLocalTransformer extends Transformer { |
|
||||||
private static final Logger logger = LoggerFactory.getLogger(DummyLocalTransformer.class); |
|
||||||
|
|
||||||
private int localsRemoved; |
|
||||||
|
|
||||||
@Override |
|
||||||
protected void preTransform(ClassPath classPath) { |
|
||||||
localsRemoved = 0; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
protected boolean transformCode(ClassPath classPath, Library library, ClassNode clazz, MethodNode method) { |
|
||||||
/* |
|
||||||
* XXX(gpe): this is primitive (ideally we'd do a proper data flow |
|
||||||
* analysis, but we'd need to do it in reverse and ASM only supports |
|
||||||
* forward data flow), however, it seems to be good enough to catch |
|
||||||
* most dummy locals. |
|
||||||
*/ |
|
||||||
var loads = new boolean[method.maxLocals]; |
|
||||||
|
|
||||||
for (var it = method.instructions.iterator(); it.hasNext(); ) { |
|
||||||
var insn = it.next(); |
|
||||||
|
|
||||||
var opcode = insn.getOpcode(); |
|
||||||
if (opcode != Opcodes.ILOAD) { |
|
||||||
continue; |
|
||||||
} |
|
||||||
|
|
||||||
var load = (VarInsnNode) insn; |
|
||||||
loads[load.var] = true; |
|
||||||
} |
|
||||||
|
|
||||||
for (var it = method.instructions.iterator(); it.hasNext(); ) { |
|
||||||
var insn = it.next(); |
|
||||||
|
|
||||||
var opcode = insn.getOpcode(); |
|
||||||
if (opcode != Opcodes.ISTORE) { |
|
||||||
continue; |
|
||||||
} |
|
||||||
|
|
||||||
var store = (VarInsnNode) insn; |
|
||||||
if (loads[store.var]) { |
|
||||||
continue; |
|
||||||
} |
|
||||||
|
|
||||||
if (InsnListUtilsKt.deleteSimpleExpression(method.instructions, insn)) { |
|
||||||
localsRemoved++; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
protected void postTransform(ClassPath classPath) { |
|
||||||
logger.info("Removed {} dummy local variables", localsRemoved); |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,59 @@ |
|||||||
|
package dev.openrs2.deob.transform |
||||||
|
|
||||||
|
import com.github.michaelbull.logging.InlineLogger |
||||||
|
import dev.openrs2.asm.classpath.ClassPath |
||||||
|
import dev.openrs2.asm.classpath.Library |
||||||
|
import dev.openrs2.asm.deleteSimpleExpression |
||||||
|
import dev.openrs2.asm.transform.Transformer |
||||||
|
import org.objectweb.asm.Opcodes |
||||||
|
import org.objectweb.asm.tree.ClassNode |
||||||
|
import org.objectweb.asm.tree.MethodNode |
||||||
|
import org.objectweb.asm.tree.VarInsnNode |
||||||
|
|
||||||
|
class DummyLocalTransformer : Transformer() { |
||||||
|
private var localsRemoved = 0 |
||||||
|
|
||||||
|
override fun preTransform(classPath: ClassPath) { |
||||||
|
localsRemoved = 0 |
||||||
|
} |
||||||
|
|
||||||
|
override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { |
||||||
|
/* |
||||||
|
* XXX(gpe): this is primitive (ideally we'd do a proper data flow |
||||||
|
* analysis, but we'd need to do it in reverse and ASM only supports |
||||||
|
* forward data flow), however, it seems to be good enough to catch |
||||||
|
* most dummy locals. |
||||||
|
*/ |
||||||
|
val loads = BooleanArray(method.maxLocals) |
||||||
|
|
||||||
|
for (insn in method.instructions) { |
||||||
|
if (insn is VarInsnNode && insn.opcode == Opcodes.ILOAD) { |
||||||
|
loads[insn.`var`] = true |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
for (insn in method.instructions) { |
||||||
|
if (insn !is VarInsnNode || insn.opcode != Opcodes.ISTORE) { |
||||||
|
continue |
||||||
|
} |
||||||
|
|
||||||
|
if (loads[insn.`var`]) { |
||||||
|
continue |
||||||
|
} |
||||||
|
|
||||||
|
if (method.instructions.deleteSimpleExpression(insn)) { |
||||||
|
localsRemoved++ |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return false |
||||||
|
} |
||||||
|
|
||||||
|
override fun postTransform(classPath: ClassPath) { |
||||||
|
logger.info { "Removed $localsRemoved dummy local variables" } |
||||||
|
} |
||||||
|
|
||||||
|
companion object { |
||||||
|
private val logger = InlineLogger() |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue