From 0fd9cf2ddfed64566c7d5ec9dfcccce181ab087e Mon Sep 17 00:00:00 2001 From: McTr0 <1334853459@qq.com> Date: Sun, 22 Nov 2026 07:49:00 -0500 Subject: [PATCH] Normalize the system framework's scope name to one spelling The manager stores the System Framework scope as `system`, but the libxposed API, the modules asking for a scope through it, and the CLI all use `android` for the same target - and the rows were stored verbatim. The one query that serves system_server only matched `system`, so a module scoped through any spelling but the manager's was silently absent from the module list: nothing loaded into system_server, onSystemServerStarting dispatched over an empty set, and the modules log showed no system_server entries at all. That is the never-dispatched half of #1101, and it reproduced on clean boot cycles because it was not a race. Writes are normalized at the storage boundary (setModuleScope, the scope request approval, and getModuleScope's read-back), the system_server reader accepts both spellings (deduped, since both rows can exist), removal clears both, and the cache expansion treats the alias the same way. The unresolved apkPath skip in getModulesForSystemServer also stops being silent. Verified on HyperOS 2.0 (Android 15, beryl): with a scope row stored as `android`, system_server now loads the module and dispatches onSystemServerStarting on the first soft reboot after this change. Fixes the dispatch half of #1101. --- .../org/matrix/vector/daemon/VectorService.kt | 15 ++++- .../matrix/vector/daemon/data/ConfigCache.kt | 20 +++++- .../vector/daemon/data/ModuleDatabase.kt | 63 +++++++++++++++---- 3 files changed, 81 insertions(+), 17 deletions(-) diff --git a/daemon/src/main/kotlin/org/matrix/vector/daemon/VectorService.kt b/daemon/src/main/kotlin/org/matrix/vector/daemon/VectorService.kt index aa22bc6a19..cd97f85db5 100644 --- a/daemon/src/main/kotlin/org/matrix/vector/daemon/VectorService.kt +++ b/daemon/src/main/kotlin/org/matrix/vector/daemon/VectorService.kt @@ -532,17 +532,26 @@ object VectorService : IVectorDaemon.Stub() { val scopes = ModuleDatabase.getModuleScope(packageName) ?: mutableListOf() var added = false granted.forEach { scopePackageName -> + // Spelled the way the row will be stored: a module asks for the framework under the + // libxposed name `android`, and a row under that spelling used to be invisible to + // the system_server reader. [ModuleDatabase.setModuleScope] normalizes on the way + // in as well; this is spelled up front so the duplicate check below compares like + // with like. + val storedName = + if (scopePackageName == ModuleDatabase.SYSTEM_FRAMEWORK_ALIAS) + ModuleDatabase.SYSTEM_FRAMEWORK_PACKAGE + else scopePackageName // Compared against where the row will land, not against the user who asked: the // framework is stored under user 0 whoever requested it, so for "system" this test // never matched and every approval appended a duplicate and rewrote the whole // table. - val storedUserId = if (scopePackageName == "system") 0 else userId + val storedUserId = if (storedName == ModuleDatabase.SYSTEM_FRAMEWORK_PACKAGE) 0 else userId val present = - scopes.any { it.packageName == scopePackageName && it.userId == storedUserId } + scopes.any { it.packageName == storedName && it.userId == storedUserId } if (!present) { scopes.add( ScopeEntry().apply { - this.packageName = scopePackageName + this.packageName = storedName this.userId = storedUserId }) added = true diff --git a/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ConfigCache.kt b/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ConfigCache.kt index b79aa11817..6054225ef9 100644 --- a/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ConfigCache.kt +++ b/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ConfigCache.kt @@ -341,14 +341,19 @@ object ConfigCache { } ModuleDatabase.enabledScopeRows().forEach { scopeRow -> - val appPkg = scopeRow.appPackage + val appPkg = + // The framework scope answers to two spellings in the wild (the manager's `system` and + // the libxposed spelling `android`); both name the same target here. + if (scopeRow.appPackage == ModuleDatabase.SYSTEM_FRAMEWORK_ALIAS) + ModuleDatabase.SYSTEM_FRAMEWORK_PACKAGE + else scopeRow.appPackage val modPkg = scopeRow.modulePackage val userId = scopeRow.userId val module = newModules[modPkg] ?: return@forEach val holders = moduleUsers[modPkg].orEmpty() - if (appPkg == "system") { + if (appPkg == ModuleDatabase.SYSTEM_FRAMEWORK_PACKAGE) { // system_server is one process for the whole device and belongs to no user, so any user // holding the module may hook it and the row is stored under user 0 whoever asked. It is // the one target the boundary below does not apply to, because there is no second copy @@ -502,7 +507,16 @@ object ConfigCache { val pkgName = row.packageName // A row with no recorded path has never been resolved; the rebuild will fill it in, // and injecting from a null path is not something to attempt in the meantime. - val apkPath = row.apkPath ?: return@forEach + val apkPath = row.apkPath + if (apkPath == null) { + // The silent version of this skip sent issue #1101's reporter looking at the dispatch + // chain, because nothing anywhere said the module was never served. + Log.w( + TAG, + "Module $pkgName is scoped to the system framework but its APK path is not " + + "resolved yet; it is not served to system_server this boot") + return@forEach + } val cached = currentState.modules[pkgName] if (cached != null) { diff --git a/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ModuleDatabase.kt b/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ModuleDatabase.kt index 733b2c3ee9..aad048e0d1 100644 --- a/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ModuleDatabase.kt +++ b/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ModuleDatabase.kt @@ -36,6 +36,17 @@ private const val TAG = "VectorModuleDatabase" */ object ModuleDatabase { + /** + * The package name the system framework is stored under. The manager uses this spelling; the + * libxposed API, the modules asking for the scope through it, and the CLI (which forwards what + * the user typed) all use [SYSTEM_FRAMEWORK_ALIAS] for the same target — and a scope stored + * under that spelling used to be invisible to the one reader that serves system_server, leaving + * every android-scoped module unloaded without a word. Writes are normalized to this spelling, + * and the readers accept both. + */ + const val SYSTEM_FRAMEWORK_PACKAGE = "system" + const val SYSTEM_FRAMEWORK_ALIAS = "android" + /** The one database handle. [ConfigCache], [PreferenceStore] and the CLI all borrow it. */ val dbHelper = Database() @@ -55,7 +66,12 @@ object ModuleDatabase { while (cursor.moveToNext()) { result.add( ScopeEntry().apply { - this.packageName = cursor.getString(0) + this.packageName = + cursor.getString(0).let { + // One spelling out, so the manager sees its framework entry where a + // pre-normalization row spells it differently. + if (it == SYSTEM_FRAMEWORK_ALIAS) SYSTEM_FRAMEWORK_PACKAGE else it + } this.userId = cursor.getInt(1) }) } @@ -182,8 +198,8 @@ object ModuleDatabase { .query( "scope INNER JOIN modules ON scope.mid = modules.mid", arrayOf("module_pkg_name", "apk_path"), - "app_pkg_name=? AND enabled=1", - arrayOf("system"), + "app_pkg_name IN (?, ?) AND enabled=1", + arrayOf(SYSTEM_FRAMEWORK_PACKAGE, SYSTEM_FRAMEWORK_ALIAS), null, null, null) @@ -192,7 +208,9 @@ object ModuleDatabase { rows += EnabledModuleRow(cursor.getString(0), cursor.getString(1)) } } - return rows + // A module scoped under both spellings (a CLI row from before the writes were normalized, then + // the manager's) must be served once: twice in this list is twice loaded into system_server. + return rows.distinctBy { it.packageName } } /** @@ -319,8 +337,15 @@ object ModuleDatabase { // a backup written by an older manager, which recorded the framework under the module's // own user, silently lost the one target the module may have cared about — and said // nothing about it. - val userId = if (app.packageName == "system") 0 else app.userId - values.put("app_pkg_name", app.packageName) + // + // The framework target is normalized to one spelling here: the manager says `system`, the + // libxposed API, the modules and the CLI say `android`, and a row stored under the alias + // spelling used to match no reader at all. + val appPackage = + if (app.packageName == SYSTEM_FRAMEWORK_ALIAS) SYSTEM_FRAMEWORK_PACKAGE + else app.packageName + val userId = if (appPackage == SYSTEM_FRAMEWORK_PACKAGE) 0 else app.userId + values.put("app_pkg_name", appPackage) values.put("user_id", userId) db.insertWithOnConflict("scope", null, values, SQLiteDatabase.CONFLICT_IGNORE) } @@ -369,16 +394,32 @@ object ModuleDatabase { // matches no row at all - which meant a module outside user 0 could take system scope and then // never give it back. It reached that state through the very same call: removeScope returns // nothing, so the module was told its request had been honoured. - val storedUserId = if (scopePackageName == "system") 0 else userId + // + // Both spellings go, not just the caller's: a row written before the writes were normalized + // must not survive a removal the caller believes removed it. + val isFrameworkTarget = + scopePackageName == SYSTEM_FRAMEWORK_PACKAGE || scopePackageName == SYSTEM_FRAMEWORK_ALIAS + val storedUserId = if (isFrameworkTarget) 0 else userId val db = dbHelper.writableDatabase val mid = db.compileStatement("SELECT mid FROM modules WHERE module_pkg_name = ?") .apply { bindString(1, packageName) } .simpleQueryForLong() - db.delete( - "scope", - "mid = ? AND app_pkg_name = ? AND user_id = ?", - arrayOf(mid.toString(), scopePackageName, storedUserId.toString())) + if (isFrameworkTarget) { + db.delete( + "scope", + "mid = ? AND app_pkg_name IN (?, ?) AND user_id = ?", + arrayOf( + mid.toString(), + SYSTEM_FRAMEWORK_PACKAGE, + SYSTEM_FRAMEWORK_ALIAS, + storedUserId.toString())) + } else { + db.delete( + "scope", + "mid = ? AND app_pkg_name = ? AND user_id = ?", + arrayOf(mid.toString(), scopePackageName, storedUserId.toString())) + } ConfigCache.requestCacheUpdate() return true }