Skip to content

Give the daemon's drawable cache a context on MediaTek ROMs - #1115

Open
ShakaRover wants to merge 1 commit into
JingMatrix:masterfrom
ShakaRover:daemon-drawable-cache-context
Open

ShakaRover wants to merge 1 commit into
JingMatrix:masterfrom
ShakaRover:daemon-drawable-cache-context

Conversation

@ShakaRover

Copy link
Copy Markdown

Problem

On MTK devices running MIUI/HyperOS, the daemon dies with

android.content.res.Resources$NotFoundException: Drawable org.matrix.vector.daemon:drawable/ic_statue_monochrome with resource ID #0x7f010000
Caused by: java.lang.NullPointerException: ... on a null object reference
    at com.mediatek.res.AsyncDrawableCache.putCacheList(AsyncDrawableCache.java:189)
    at com.mediatek.res.ResOptExtImpl.putCacheList(ResOptExtImpl.java:69)
    at android.content.res.ResourcesImpl.cacheDrawable(...)
    ...
    at hh.d(r8-map-id-...:17)   ← daemon's getBitmap

Known reports in this family: #271 (ic_notification), #996 (fatal coroutine exception in the daemon), LSPosed/LSPosed#1339 (same chain in the old daemon). All were closed as ROM-specific without a code fix.

Root cause (verified against a real ROM, not guessed)

I pulled /system_ext/framework/mediatek-framework.jar and /system/framework/framework.jar off a Redmi 23046PNC9C (MIUI 14 V816, Android 13, Dimensity) and read the smali:

  1. MTK's "res opt" patch calls ResOptExtImpl.putCacheList(key, dr, origResId, mAppContext) from ResourcesImpl.cacheDrawable for every cached non-color drawable. mAppContext is a static field on ResourcesImpl whose only writer is ContextImpl.createAppContext — so it always holds "the last app context created in this process".
  2. AsyncDrawableCache.putCacheList (line 189 on this build) evaluates, in order: isEnableFeature() → skipPreload(context) (the only null guard) → context.getApplicationInfo().processName.equals("system") → isSystemApp().
  3. The daemon process never calls createAppContext (it builds its context-less FileSystem.resources as a bare Resources(am, null, null)), so mAppContext is null there and every ROM variant of that guard chain has some way to NPE: older builds dereference the null context directly (#1339 line 164, Parasitic manager failed due to icon resource not found #271 line 165), newer builds null-deref a field of the application info ([Bug] Temporary-root (locked BL, KernelSU LKM late-load) on Android 17 / HyperOS 4: VectorDaemon dies with StackOverflowError (kk.onTransact recursion 32×) → framework never activates #996 line 189).

LSPosed/LSPosed#1954 worked around the same family by resetprop ro.vendor.pref_scale_enable_cfg 0 (the feature gate) — this PR fixes it in our code instead.

Fix

Wherever the ROM exposes ResourcesImpl.setAppContext, the daemon's FileSystem.resources now hands the cache a FakeContext whose ApplicationInfo carries a process name, which keeps putCacheList on its early-return path (isSystemApp() is true for the framework info; the fallback claims the system process). Everywhere else the reflection misses and the change is a no-op. FakeContext.getApplicationInfo's blank fallback also gets a process name now — the archive parser fills in nothing, so a blank info would walk the same guard straight into the null-base ContextWrapper (getSharedPreferences, getSystemService).

ParasiticManagerHooker needed no change: it already transplants the host's process name into its fabricated ApplicationInfo.

Verification

Fixes #271, fixes #996 (the drawable half; the kk.onTransact recursion in #996 is a separate daemon binder issue).

MediaTek's "res opt" patch, which ships on MTK devices running MIUI and
HyperOS, parks the process's app context in a static ResourcesImpl.mAppContext
that only ContextImpl.createAppContext writes, then dereferences it from
AsyncDrawableCache.putCacheList with no null guard on several builds
(JingMatrix/Vector JingMatrix#271, JingMatrix#996). The daemon never calls createAppContext, so
loading ic_statue_monochrome through the bare FileSystem.resources hands that
cache a null context — the fatal coroutine exception in JingMatrix#996's report is
exactly this path.

Wherever the ROM exposes ResourcesImpl.setAppContext, hand the cache a
FakeContext whose ApplicationInfo carries a process name, which keeps
putCacheList on its early-return path; everywhere else the reflection misses
and nothing changes. Complete FakeContext's fallback ApplicationInfo along the
way: the archive parser fills in no process name at all, so a blank info would
walk the same guard straight into the null-base ContextWrapper.

Verified against the ROM of a Redmi 23046PNC9C (MIUI 14 V816, Android 13,
MTK): the smali shows putCacheList evaluating isEnableFeature, skipPreload,
processName.equals("system") and isSystemApp in that order, with that static
mAppContext as the only context the cache ever sees.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant