From 4382fae75d0703bded123c10312d5bb767035d00 Mon Sep 17 00:00:00 2001 From: Davinci9196 Date: Mon, 19 Jan 2026 16:30:33 +0800 Subject: [PATCH 1/3] DG: Switch to using HashMapOf --- .../org/microg/gms/droidguard/HandleProxyFactory.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/play-services-droidguard/src/main/kotlin/org/microg/gms/droidguard/HandleProxyFactory.kt b/play-services-droidguard/src/main/kotlin/org/microg/gms/droidguard/HandleProxyFactory.kt index e6fc1707b1..922905f8d4 100644 --- a/play-services-droidguard/src/main/kotlin/org/microg/gms/droidguard/HandleProxyFactory.kt +++ b/play-services-droidguard/src/main/kotlin/org/microg/gms/droidguard/HandleProxyFactory.kt @@ -85,11 +85,11 @@ open class HandleProxyFactory(private val context: Context) { updateCacheTimestamp(vmKey) return cachedClass } - val weakClass = weakClassMap[vmKey] - if (weakClass != null) { - classMap[vmKey] = weakClass + val staticCachedClass = staticCacheMap[vmKey] + if (staticCachedClass != null) { + classMap[vmKey] = staticCachedClass updateCacheTimestamp(vmKey) - return weakClass + return staticCachedClass } if (!isValidCache(vmKey)) { throw BytesException(bytes, "VM key $vmKey not found in cache") @@ -101,7 +101,7 @@ open class HandleProxyFactory(private val context: Context) { val loader = DexClassLoader(getTheApkFile(vmKey).absolutePath, getOptDir(vmKey).absolutePath, null, context.classLoader) val clazz = loader.loadClass(CLASS_NAME) classMap[vmKey] = clazz - weakClassMap[vmKey] = clazz + staticCacheMap[vmKey] = clazz return clazz } } @@ -111,7 +111,7 @@ open class HandleProxyFactory(private val context: Context) { const val CACHE_FOLDER_NAME = "cache_dg" val CLASS_LOCK = Object() @GuardedBy("CLASS_LOCK") - val weakClassMap = WeakHashMap>() + val staticCacheMap = hashMapOf>() 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) } } From bf57263e1beda1377617542b2b62cac257b89054 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Mon, 30 Mar 2026 13:54:12 +0200 Subject: [PATCH 2/3] Drop non-static cache, now that static cache is no longer weak --- .../microg/gms/droidguard/HandleProxyFactory.kt | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/play-services-droidguard/src/main/kotlin/org/microg/gms/droidguard/HandleProxyFactory.kt b/play-services-droidguard/src/main/kotlin/org/microg/gms/droidguard/HandleProxyFactory.kt index 922905f8d4..5e9afbfa86 100644 --- a/play-services-droidguard/src/main/kotlin/org/microg/gms/droidguard/HandleProxyFactory.kt +++ b/play-services-droidguard/src/main/kotlin/org/microg/gms/droidguard/HandleProxyFactory.kt @@ -18,9 +18,6 @@ import java.security.cert.Certificate import java.util.* open class HandleProxyFactory(private val context: Context) { - @GuardedBy("CLASS_LOCK") - protected val classMap = hashMapOf>() - fun createHandle(vmKey: String, pfd: ParcelFileDescriptor, extras: Bundle): HandleProxy { fetchFromFileDescriptor(pfd, vmKey) return createHandleProxy(vmKey, extras) @@ -85,12 +82,6 @@ open class HandleProxyFactory(private val context: Context) { updateCacheTimestamp(vmKey) return cachedClass } - val staticCachedClass = staticCacheMap[vmKey] - if (staticCachedClass != null) { - classMap[vmKey] = staticCachedClass - updateCacheTimestamp(vmKey) - return staticCachedClass - } if (!isValidCache(vmKey)) { throw BytesException(bytes, "VM key $vmKey not found in cache") } @@ -101,7 +92,6 @@ open class HandleProxyFactory(private val context: Context) { val loader = DexClassLoader(getTheApkFile(vmKey).absolutePath, getOptDir(vmKey).absolutePath, null, context.classLoader) val clazz = loader.loadClass(CLASS_NAME) classMap[vmKey] = clazz - staticCacheMap[vmKey] = clazz return clazz } } @@ -109,9 +99,9 @@ open class HandleProxyFactory(private val context: Context) { companion object { const val CLASS_NAME = "com.google.ccc.abuse.droidguard.DroidGuard" const val CACHE_FOLDER_NAME = "cache_dg" - val CLASS_LOCK = Object() + val CLASS_LOCK = Any() @GuardedBy("CLASS_LOCK") - val staticCacheMap = hashMapOf>() + private val classMap = hashMapOf>() 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) } } From 4cf1977e32cfecf758efcba2d970143a7e1a47b4 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Mon, 30 Mar 2026 13:57:01 +0200 Subject: [PATCH 3/3] Use map itself as lock It's private, so noone else can have the lock --- .../org/microg/gms/droidguard/HandleProxyFactory.kt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/play-services-droidguard/src/main/kotlin/org/microg/gms/droidguard/HandleProxyFactory.kt b/play-services-droidguard/src/main/kotlin/org/microg/gms/droidguard/HandleProxyFactory.kt index 5e9afbfa86..0ae6ccfbdb 100644 --- a/play-services-droidguard/src/main/kotlin/org/microg/gms/droidguard/HandleProxyFactory.kt +++ b/play-services-droidguard/src/main/kotlin/org/microg/gms/droidguard/HandleProxyFactory.kt @@ -76,8 +76,8 @@ open class HandleProxyFactory(private val context: Context) { } protected fun loadClass(vmKey: String, bytes: ByteArray = ByteArray(0)): Class<*> { - synchronized(CLASS_LOCK) { - val cachedClass = classMap[vmKey] + synchronized(CLASS_MAP) { + val cachedClass = CLASS_MAP[vmKey] if (cachedClass != null) { updateCacheTimestamp(vmKey) return cachedClass @@ -91,7 +91,7 @@ open class HandleProxyFactory(private val context: Context) { } val loader = DexClassLoader(getTheApkFile(vmKey).absolutePath, getOptDir(vmKey).absolutePath, null, context.classLoader) val clazz = loader.loadClass(CLASS_NAME) - classMap[vmKey] = clazz + CLASS_MAP[vmKey] = clazz return clazz } } @@ -99,9 +99,7 @@ open class HandleProxyFactory(private val context: Context) { companion object { const val CLASS_NAME = "com.google.ccc.abuse.droidguard.DroidGuard" const val CACHE_FOLDER_NAME = "cache_dg" - val CLASS_LOCK = Any() - @GuardedBy("CLASS_LOCK") - private val classMap = hashMapOf>() + private val CLASS_MAP = hashMapOf>() 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) } }