Add support for whole program AST transforms

bzip2
Graham 5 years ago
parent a777ae362e
commit 597aa2018e
  1. 14
      deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt
  2. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt
  3. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt
  4. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt
  5. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt
  6. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt
  7. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt
  8. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt
  9. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt
  10. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt
  11. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt
  12. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt
  13. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt
  14. 22
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/Transformer.kt
  15. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt
  16. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt

@ -59,20 +59,26 @@ class AstDeobfuscator(private val modules: List<Path>) {
val printer = PrettyPrinter(printerConfig)
for (module in modules) {
val root = SourceRoot(module, config)
val roots = modules.map { SourceRoot(it, config) }
val units = mutableMapOf<String, CompilationUnit>()
for (root in roots) {
val results = root.tryToParseParallelized()
for (result in results) {
require(result.isSuccessful) { result }
}
for (unit in root.compilationUnits) {
for (transformer in TRANSFORMERS) {
transformer.transform(unit)
val name = unit.primaryType.orElseThrow().fullyQualifiedName.orElseThrow()
units[name] = unit
}
}
for (transformer in TRANSFORMERS) {
transformer.transform(units)
}
for (root in roots) {
root.printer = Function<CompilationUnit, String>(printer::print)
root.saveAll()
}

@ -12,7 +12,10 @@ import dev.openrs2.deob.ast.util.negate
import dev.openrs2.deob.ast.util.walk
class AddSubTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
override fun transformUnit(
units: Map<String, CompilationUnit>,
unit: CompilationUnit
) {
unit.walk { expr: BinaryExpr ->
val op = expr.operator
val left = expr.left

@ -6,7 +6,10 @@ import dev.openrs2.deob.ast.util.isString
import dev.openrs2.deob.ast.util.walk
class BinaryExprOrderTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
override fun transformUnit(
units: Map<String, CompilationUnit>,
unit: CompilationUnit
) {
unit.walk { expr: BinaryExpr ->
val op = expr.operator.flip() ?: return@walk

@ -10,7 +10,10 @@ import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral
import dev.openrs2.deob.ast.util.walk
class BitMaskTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
override fun transformUnit(
units: Map<String, CompilationUnit>,
unit: CompilationUnit
) {
unit.walk { expr: BinaryExpr ->
val shiftOp = expr.operator
val left = expr.left

@ -12,7 +12,10 @@ import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral
import dev.openrs2.deob.ast.util.walk
class ComplementTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
override fun transformUnit(
units: Map<String, CompilationUnit>,
unit: CompilationUnit
) {
unit.walk { expr: BinaryExpr ->
val op = complement(expr.operator) ?: return@walk

@ -71,7 +71,10 @@ class EncloseTransformer : Transformer() {
}
}
override fun transform(unit: CompilationUnit) {
override fun transformUnit(
units: Map<String, CompilationUnit>,
unit: CompilationUnit
) {
unit.walk { expr: Expression ->
when {
expr.isArrayAccessExpr -> {

@ -7,7 +7,10 @@ import dev.openrs2.deob.ast.util.hasSideEffects
import dev.openrs2.deob.ast.util.walk
class ForLoopConditionTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
override fun transformUnit(
units: Map<String, CompilationUnit>,
unit: CompilationUnit
) {
unit.walk { stmt: ForStmt ->
stmt.compare.ifPresent { compare ->
if (!compare.isBinaryExpr) {

@ -7,7 +7,10 @@ import com.github.javaparser.ast.expr.UnaryExpr
import dev.openrs2.deob.ast.util.walk
class IdentityTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
override fun transformUnit(
units: Map<String, CompilationUnit>,
unit: CompilationUnit
) {
unit.walk { expr: BinaryExpr ->
@Suppress("NON_EXHAUSTIVE_WHEN")
when (expr.operator) {

@ -9,7 +9,10 @@ import dev.openrs2.deob.ast.util.not
import dev.openrs2.deob.ast.util.walk
class IfElseTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
override fun transformUnit(
units: Map<String, CompilationUnit>,
unit: CompilationUnit
) {
unit.walk { stmt: IfStmt ->
stmt.elseStmt.ifPresent { elseStmt: Statement ->
val condition = stmt.condition

@ -7,7 +7,10 @@ import com.github.javaparser.ast.stmt.ForStmt
import dev.openrs2.deob.ast.util.walk
class IncrementTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
override fun transformUnit(
units: Map<String, CompilationUnit>,
unit: CompilationUnit
) {
unit.walk { stmt: ExpressionStmt ->
if (!stmt.expression.isUnaryExpr) {
return@walk

@ -7,7 +7,10 @@ import dev.openrs2.deob.ast.util.negate
import dev.openrs2.deob.ast.util.walk
class NegativeLiteralTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
override fun transformUnit(
units: Map<String, CompilationUnit>,
unit: CompilationUnit
) {
unit.walk { expr: UnaryExpr ->
val operand = expr.expression
if (!operand.isIntegerOrLongLiteral()) {

@ -5,7 +5,10 @@ import com.github.javaparser.ast.expr.MethodCallExpr
import dev.openrs2.deob.ast.util.walk
class NewInstanceTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
override fun transformUnit(
units: Map<String, CompilationUnit>,
unit: CompilationUnit
) {
unit.walk { expr: MethodCallExpr ->
if (expr.nameAsString != "newInstance") {
return@walk

@ -7,7 +7,10 @@ import dev.openrs2.deob.ast.util.not
import dev.openrs2.deob.ast.util.walk
class TernaryTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
override fun transformUnit(
units: Map<String, CompilationUnit>,
unit: CompilationUnit
) {
unit.walk { expr: ConditionalExpr ->
val condition = expr.condition
val notCondition = condition.not()

@ -3,5 +3,25 @@ package dev.openrs2.deob.ast.transform
import com.github.javaparser.ast.CompilationUnit
abstract class Transformer {
abstract fun transform(unit: CompilationUnit)
fun transform(units: Map<String, CompilationUnit>) {
preTransform(units)
for (unit in units.values) {
transformUnit(units, unit)
}
postTransform(units)
}
protected open fun preTransform(units: Map<String, CompilationUnit>) {
// empty
}
protected open fun transformUnit(units: Map<String, CompilationUnit>, unit: CompilationUnit) {
// empty
}
protected open fun postTransform(units: Map<String, CompilationUnit>) {
// empty
}
}

@ -5,7 +5,10 @@ import com.github.javaparser.ast.expr.EnclosedExpr
import dev.openrs2.deob.ast.util.walk
class UnencloseTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
override fun transformUnit(
units: Map<String, CompilationUnit>,
unit: CompilationUnit
) {
unit.walk { expr: EnclosedExpr ->
expr.replace(expr.inner.clone())
}

@ -7,7 +7,10 @@ import com.github.javaparser.ast.expr.TypeExpr
import dev.openrs2.deob.ast.util.walk
class ValueOfTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
override fun transformUnit(
units: Map<String, CompilationUnit>,
unit: CompilationUnit
) {
unit.walk { expr: ObjectCreationExpr ->
if (expr.type.isBoxedType) {
expr.replace(MethodCallExpr(TypeExpr(expr.type), "valueOf", expr.arguments))

Loading…
Cancel
Save