Skip to content

Commit aa19f49

Browse files
Revert "Fix OOM errors by processing files individually (#32)" (#33)
This reverts commit 667f2a9.
1 parent 667f2a9 commit aa19f49

File tree

6 files changed

+32
-42
lines changed

6 files changed

+32
-42
lines changed

processor/src/main/kotlin/org/lsposed/lsparanoid/processor/Analyzer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Analyzer(private val grip: Grip, private val classFilter: ((className: Str
3636
{ it },
3737
{ createObfuscationConfiguration(it) }
3838
)
39-
return AnalysisResult(obfuscationConfigurationsByType.toMutableMap(), typesToObfuscate.toMutableSet())
39+
return AnalysisResult(obfuscationConfigurationsByType)
4040
}
4141

4242
private fun findTypesToObfuscate(inputs: List<Path>): Set<Type.Object> {

processor/src/main/kotlin/org/lsposed/lsparanoid/processor/DeobfuscatorGenerator.kt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,18 @@ import org.objectweb.asm.Opcodes.ACC_SUPER
2929
import org.objectweb.asm.Type
3030
import org.objectweb.asm.commons.Method
3131

32-
// MEMORY FIX: Remove FileRegistry dependency.
33-
// IMPACT: Simplifies the generator, avoids holding file paths in memory.
3432
class DeobfuscatorGenerator(
35-
private val deobfuscator: Deobfuscator,
36-
private val stringRegistry: StringRegistry,
37-
private val classRegistry: ClassRegistry
33+
private val deobfuscator: Deobfuscator,
34+
private val stringRegistry: StringRegistry,
35+
private val classRegistry: ClassRegistry,
36+
private val fileRegistry: FileRegistry
3837
) {
3938

40-
fun generateDeobfuscator(): ByteArray {
41-
val writer = StandaloneClassWriter(
42-
ClassWriter.COMPUTE_MAXS or ClassWriter.COMPUTE_FRAMES,
43-
classRegistry
44-
)
45-
writer.visit(
46-
Opcodes.V1_6,
47-
ACC_PUBLIC or ACC_SUPER,
39+
fun generateDeobfuscator(): ByteArray {
40+
val writer = StandaloneClassWriter(ClassWriter.COMPUTE_MAXS or ClassWriter.COMPUTE_FRAMES, classRegistry, fileRegistry)
41+
writer.visit(
42+
Opcodes.V1_6,
43+
ACC_PUBLIC or ACC_SUPER,
4844
deobfuscator.type.internalName,
4945
null,
5046
OBJECT_TYPE.internalName,

processor/src/main/kotlin/org/lsposed/lsparanoid/processor/Model.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@ package org.lsposed.lsparanoid.processor
1919

2020
import com.joom.grip.mirrors.Type
2121

22-
// MEMORY FIX: Immutable map -> Mutable map.
23-
// IMPACT: Avoids creating new map instances, reducing GC overhead.
2422
data class AnalysisResult(
25-
val configurationsByType: MutableMap<Type.Object, ClassConfiguration>,
26-
val obfuscatedTypes: MutableSet<Type.Object>
23+
val configurationsByType: Map<Type.Object, ClassConfiguration>
2724
)
2825

2926
data class ClassConfiguration(
30-
val container: Type.Object,
31-
val constantStringsByFieldName: Map<String, String>
27+
val container: Type.Object,
28+
val constantStringsByFieldName: Map<String, String>
3229
)

processor/src/main/kotlin/org/lsposed/lsparanoid/processor/ParanoidProcessor.kt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ import org.objectweb.asm.commons.Method
3131
import java.nio.file.Path
3232
import java.util.jar.JarOutputStream
3333

34-
// MEMORY FIX: Loading all inputs at once -> Processing inputs individually.
35-
// IMPACT: Memory usage is proportional to the largest input, not all of them.
3634
class ParanoidProcessor(
37-
private val seed: Int,
38-
private val classpath: Set<Path>,
35+
seed: Int,
36+
classpath: Set<Path>,
3937
private val inputs: List<Path>,
4038
private val output: JarOutputStream,
4139
private val asmApi: Int = Opcodes.ASM9,
@@ -45,18 +43,13 @@ class ParanoidProcessor(
4543

4644
private val logger = getLogger()
4745

46+
private val grip: Grip = GripFactory.newInstance(asmApi).create(classpath + inputs)
4847
private val stringRegistry = StringRegistryImpl(seed)
4948

5049
fun process() {
5150
dumpConfiguration()
5251

53-
val analysisResult = AnalysisResult(mutableMapOf(), mutableSetOf())
54-
inputs.forEach { input ->
55-
val grip = GripFactory.newInstance(asmApi).create(classpath + input)
56-
val result = Analyzer(grip, classFilter).analyze(listOf(input))
57-
analysisResult.configurationsByType.putAll(result.configurationsByType)
58-
analysisResult.obfuscatedTypes.addAll(result.obfuscatedTypes)
59-
}
52+
val analysisResult = Analyzer(grip, classFilter).analyze(inputs)
6053
analysisResult.dump()
6154

6255
val deobfuscator = createDeobfuscator()
@@ -67,19 +60,20 @@ class ParanoidProcessor(
6760
}
6861

6962
try {
70-
val grip = GripFactory.newInstance(asmApi).create(classpath + inputs)
7163
Patcher(
7264
deobfuscator,
7365
stringRegistry,
7466
analysisResult,
7567
grip.classRegistry,
68+
grip.fileRegistry,
7669
asmApi
7770
).copyAndPatchClasses(sources, output)
7871
val deobfuscatorBytes =
7972
DeobfuscatorGenerator(
8073
deobfuscator,
8174
stringRegistry,
82-
grip.classRegistry
75+
grip.classRegistry,
76+
grip.fileRegistry
8377
).generateDeobfuscator()
8478
output.createFile("${deobfuscator.type.internalName}.class", deobfuscatorBytes)
8579
} finally {

processor/src/main/kotlin/org/lsposed/lsparanoid/processor/Patcher.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ import org.objectweb.asm.ClassWriter
3232
import org.objectweb.asm.Opcodes
3333
import java.util.jar.JarOutputStream
3434

35-
// MEMORY FIX: Decouple Patcher from global Grip instance.
36-
// IMPACT: Patcher operates on a per-file basis, reducing memory load.
3735
class Patcher(
3836
private val deobfuscator: Deobfuscator,
3937
private val stringRegistry: StringRegistry,
4038
private val analysisResult: AnalysisResult,
4139
private val classRegistry: ClassRegistry,
40+
private val fileRegistry: FileRegistry,
4241
private val asmApi: Int,
4342
) {
4443

@@ -94,7 +93,8 @@ class Patcher(
9493
val reader = ClassReader(source.readFile(name))
9594
val writer = StandaloneClassWriter(
9695
ClassWriter.COMPUTE_MAXS or ClassWriter.COMPUTE_FRAMES,
97-
classRegistry
96+
classRegistry,
97+
fileRegistry
9898
)
9999
val shouldObfuscateLiterals = reader.access and Opcodes.ACC_INTERFACE == 0
100100
val patcher =

processor/src/main/kotlin/org/lsposed/lsparanoid/processor/StandaloneClassWriter.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,28 @@ import org.lsposed.lsparanoid.processor.logging.getLogger
2727
import org.objectweb.asm.ClassReader
2828
import org.objectweb.asm.ClassWriter
2929

30-
// MEMORY FIX: Remove FileRegistry dependency.
31-
// IMPACT: Simplifies the class writer, avoids holding file paths in memory.
3230
class StandaloneClassWriter : ClassWriter {
3331
private val logger = getLogger()
3432
private val classRegistry: ClassRegistry
33+
private val fileRegistry: FileRegistry
3534

3635
constructor(
3736
flags: Int,
38-
classRegistry: ClassRegistry
37+
classRegistry: ClassRegistry,
38+
fileRegistry: FileRegistry
3939
) : super(flags) {
4040
this.classRegistry = classRegistry
41+
this.fileRegistry = fileRegistry
4142
}
4243

4344
constructor(
4445
classReader: ClassReader,
4546
flags: Int,
46-
classRegistry: ClassRegistry
47+
classRegistry: ClassRegistry,
48+
fileRegistry: FileRegistry
4749
) : super(classReader, flags) {
4850
this.classRegistry = classRegistry
51+
this.fileRegistry = fileRegistry
4952
}
5053

5154
override fun getCommonSuperClass(type1: String, type2: String): String {
@@ -72,9 +75,9 @@ class StandaloneClassWriter : ClassWriter {
7275
}
7376

7477
private fun ClassRegistry.getClassMirrorOrObject(type: Type.Object): ClassMirror {
75-
return try {
78+
return fileRegistry.findPathForType(type)?.let { _ ->
7679
getClassMirror(type)
77-
} catch (exception: Exception) {
80+
} ?: run {
7881
logger.info("[getClassMirrorOrObject]: {} not found", type)
7982
getClassMirror(OBJECT_TYPE)
8083
}

0 commit comments

Comments
 (0)