Commit 8336afa
committed
Guard the hook dispatch against re-entering itself
Hooking Object.getClass() made the dispatch call itself. R8 compiles Kotlin's
parameter null checks into obj.getClass(), and one of those is the first
instruction of the trampoline callback, so the first getClass the process ran
after the hook landed re-entered the trampoline, and re-entered again from its
own prologue, until the stack was gone and before any hooker had run. Nothing
recovers from there: lsplant marks a hooked method non-compilable, so the call
site can never be inlined away afterwards, and the framework dex is loaded from
memory and never gets an oat file, so the interpreter reaches the trampoline
every time.
The lowering is new. Built from the same source, 3046 has 16 Object.getClass()
call sites in the framework dex and 3047 has 207; the difference between them is
#792, which moved the build from AGP 8.13.1 to 9.3.1. Reported as #798, where a
module that hooks every method named getClass over Class.getMethods() - a list
that always contains the one inherited from Object - crash-looped com.miui.home
through six process starts. The build it was compared against turned out to be a
branch artefact from before the migration; the version code counts commits on
master rather than on the branch being built, which is why it read as 3047.
The trampoline entry point is native now, so the re-entrancy check runs ahead of
anything a compiler can put in front of it. A Kotlin body cannot promise that,
which is the whole of the bug. While the guard is raised, a hooked method entered
from the framework's own frames runs its original instead of dispatching again.
Where the guard comes down took several attempts on a device to get right, and
the shape that finally works comes from writing VectorChain in Java. The chain is
the surface the API hands to modules, so every method on it is entered from module
code with the guard down, and the guard cannot cover a callee's prologue - in
Kotlin, R8 opens each with a parameter null check compiled into Object.getClass(),
a hookable call ahead of any statement of ours. A hooker that rebuilds its
arguments then re-enters twice per frame and the nesting cap bounds a tree rather
than a chain. The parameter types come from a Java @nonnull interface and cannot
be made nullable, so the only way not to emit the checks is not to write this in
Kotlin; javac emits none.
Being Java, the chain's own bookkeeping calls nothing a module can hook - a
constructor, an array read, two field writes. So it needs no guard of its own: one
lower per node covers both calls that leave the framework, the hooker and the
terminal, and nothing raises. The guard comes down for those two, and for the
original run through an Invoker; it is raised only by the native trampoline and,
for its own bookkeeping, by the legacy bridge.
Because a site that gets this wrong is invisible until a device hangs, the two
sides live in DispatchGuard.kt as callIntoModule and enterFramework, the raw
primitives are named nowhere else, and checkDispatchGuard fails the build if they
are. That check is not sufficient on its own: the Java-facing wrappers in that
same file were at one point recursing into themselves through a SAM conversion,
which reads correctly in source and only shows in the bytecode.
A hooker that calls the method it hooks recurses in module code, where the guard
does not reach, and hooking Object.getClass has the compiler write such calls on
the module's behalf. Past a nesting of thirty-two the thread latches into serving
originals until it unwinds, and names the method once. Latching rather than
re-arming per frame matters for the same reason as above: a hooker that re-enters
more than once per frame would otherwise branch at every level.
Three things follow from the entry point being native. A registration that fails
now refuses the hook, because the alternative is UnsatisfiedLinkError thrown out
of whatever the application was calling, which is far harder to trace back. The
two invoke bridges take the argument array rather than a vararg, since the spread
copied it on every dispatch and the JVM descriptor is the same either way. And
the trampoline's package joins the ones the daemon renames as it loads the dex:
keeping a native method keeps its class name, so R8 stopped renaming it, and it
would otherwise stand as a fixed string in every injected process.
tests/dispatch-guard asserts five properties of dispatch, of which the fourth is
the one that is easy to lose and hard to see: the dispatch must not dispatch its
own internal calls. A dispatch that re-enters itself still returns the right
answers, at a multiple of the cost, until a real workload turns that into an ANR -
so it is asserted by the cap staying silent rather than by any result. Its checks
are chosen for the shapes that reach different parts of the framework rather than
for what any one module does, since three defects here survived a reading of the
bytecode and were only caught by running something shaped differently.
Verified on a Pixel 7a running Android 16, against builds of this branch and of
master without it. Without: the target hangs on the first dispatch after the hook
lands and is killed by an ANR whose trace holds no main thread, which is the
signature reported in #798. With, and with that hook live throughout: 33 results,
one process id, no ANR, and the cap firing once, for the hooker written to recurse
into itself. That covers ordinary, static and constructor hooks, a class
initializer, both Invoker types, a hooker that rebuilds its arguments, a legacy
de.robv hook, a hooker seeing another module's hook, and an Invoker whose original
calls a method hooked elsewhere. An empty pass-through hook measured about ten
percent slower per dispatch than master, from the added native round trip.
Fixes #798.1 parent 3d8090f commit 8336afa
31 files changed
Lines changed: 1628 additions & 192 deletions
File tree
- daemon/src/main/jni
- legacy/src/main/java/org/matrix/vector/legacy
- native/src/jni
- tests/dispatch-guard
- gradle/wrapper
- module
- src/main
- java/org/matrix/dgmodule
- resources/META-INF/xposed
- target
- src/main
- java/org/matrix/dgtarget
- xposed
- src/main/kotlin/org/matrix/vector
- impl/hooks
- nativebridge
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
25 | 30 | | |
26 | 31 | | |
27 | 32 | | |
28 | 33 | | |
29 | | - | |
| 34 | + | |
| 35 | + | |
30 | 36 | | |
31 | 37 | | |
32 | 38 | | |
| |||
Lines changed: 31 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
71 | 80 | | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
78 | 90 | | |
79 | | - | |
80 | 91 | | |
81 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
82 | 96 | | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
87 | 102 | | |
88 | 103 | | |
89 | 104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
89 | 90 | | |
90 | 91 | | |
91 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
92 | 270 | | |
93 | 271 | | |
94 | 272 | | |
| |||
121 | 299 | | |
122 | 300 | | |
123 | 301 | | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
124 | 341 | | |
125 | 342 | | |
126 | 343 | | |
| |||
215 | 432 | | |
216 | 433 | | |
217 | 434 | | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
| 435 | + | |
| 436 | + | |
222 | 437 | | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
231 | 468 | | |
232 | 469 | | |
233 | 470 | | |
| |||
691 | 928 | | |
692 | 929 | | |
693 | 930 | | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
694 | 934 | | |
695 | 935 | | |
696 | 936 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
0 commit comments