Add support for whole program AST transforms

bzip2
Graham 5 years ago
parent a777ae362e
commit 597aa2018e
  1. 16
      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) val printer = PrettyPrinter(printerConfig)
for (module in modules) { val roots = modules.map { SourceRoot(it, config) }
val root = SourceRoot(module, config) val units = mutableMapOf<String, CompilationUnit>()
for (root in roots) {
val results = root.tryToParseParallelized() val results = root.tryToParseParallelized()
for (result in results) { for (result in results) {
require(result.isSuccessful) { result } require(result.isSuccessful) { result }
} }
for (unit in root.compilationUnits) { for (unit in root.compilationUnits) {
for (transformer in TRANSFORMERS) { val name = unit.primaryType.orElseThrow().fullyQualifiedName.orElseThrow()
transformer.transform(unit) units[name] = unit
}
} }
}
for (transformer in TRANSFORMERS) {
transformer.transform(units)
}
for (root in roots) {
root.printer = Function<CompilationUnit, String>(printer::print) root.printer = Function<CompilationUnit, String>(printer::print)
root.saveAll() root.saveAll()
} }

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

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

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

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save