Skip to content

Commit 8a9690d

Browse files
DroidGuard: Store loaded classes statically (#3239)
* DG: Switch to using HashMapOf * Drop non-static cache, now that static cache is no longer weak * Use map itself as lock It's private, so noone else can have the lock --------- Co-authored-by: Marvin W <git@larma.de>
1 parent 3add873 commit 8a9690d

1 file changed

Lines changed: 4 additions & 16 deletions

File tree

play-services-droidguard/src/main/kotlin/org/microg/gms/droidguard/HandleProxyFactory.kt

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ import java.security.cert.Certificate
1818
import java.util.*
1919

2020
open class HandleProxyFactory(private val context: Context) {
21-
@GuardedBy("CLASS_LOCK")
22-
protected val classMap = hashMapOf<String, Class<*>>()
23-
2421
fun createHandle(vmKey: String, pfd: ParcelFileDescriptor, extras: Bundle): HandleProxy {
2522
fetchFromFileDescriptor(pfd, vmKey)
2623
return createHandleProxy(vmKey, extras)
@@ -79,18 +76,12 @@ open class HandleProxyFactory(private val context: Context) {
7976
}
8077

8178
protected fun loadClass(vmKey: String, bytes: ByteArray = ByteArray(0)): Class<*> {
82-
synchronized(CLASS_LOCK) {
83-
val cachedClass = classMap[vmKey]
79+
synchronized(CLASS_MAP) {
80+
val cachedClass = CLASS_MAP[vmKey]
8481
if (cachedClass != null) {
8582
updateCacheTimestamp(vmKey)
8683
return cachedClass
8784
}
88-
val weakClass = weakClassMap[vmKey]
89-
if (weakClass != null) {
90-
classMap[vmKey] = weakClass
91-
updateCacheTimestamp(vmKey)
92-
return weakClass
93-
}
9485
if (!isValidCache(vmKey)) {
9586
throw BytesException(bytes, "VM key $vmKey not found in cache")
9687
}
@@ -100,18 +91,15 @@ open class HandleProxyFactory(private val context: Context) {
10091
}
10192
val loader = DexClassLoader(getTheApkFile(vmKey).absolutePath, getOptDir(vmKey).absolutePath, null, context.classLoader)
10293
val clazz = loader.loadClass(CLASS_NAME)
103-
classMap[vmKey] = clazz
104-
weakClassMap[vmKey] = clazz
94+
CLASS_MAP[vmKey] = clazz
10595
return clazz
10696
}
10797
}
10898

10999
companion object {
110100
const val CLASS_NAME = "com.google.ccc.abuse.droidguard.DroidGuard"
111101
const val CACHE_FOLDER_NAME = "cache_dg"
112-
val CLASS_LOCK = Object()
113-
@GuardedBy("CLASS_LOCK")
114-
val weakClassMap = WeakHashMap<String, Class<*>>()
102+
private val CLASS_MAP = hashMapOf<String, Class<*>>()
115103
val PROD_CERT_HASH = byteArrayOf(61, 122, 18, 35, 1, -102, -93, -99, -98, -96, -29, 67, 106, -73, -64, -119, 107, -5, 79, -74, 121, -12, -34, 95, -25, -62, 63, 50, 108, -113, -103, 74)
116104
}
117105
}

0 commit comments

Comments
 (0)