Repository navigation
Commit 3fd80c7
authored
Fix use-after-free of unpacked constants in the XNNPACK weights cache (pytorch#22779)
### Summary
`XNNWeightsCache::finalize_for_runtime()` frees every unpacked buffer,
on the grounds that
```cpp
// All data has been packed by create_runtime
// so we clear the unpacked data as it is no longer needed
```
That holds for weights XNNPACK packs, but not for operators that take
their constants unpacked. PReLU reads its slope straight out of that
memory for the life of the runtime, so the subgraph is left pointing at
freed memory.
pytorch#21480 fixed exactly this for the path that does not use the weights
cache, by parking the `FreeableBuffer`s in
`XNNExecutor::unpacked_buffers_`. The weights-cache branch of
`getConstantDataPtr()` was never given the same treatment, so a build
with `EXECUTORCH_XNNPACK_ENABLE_WEIGHT_CACHE=ON` still frees them. That
is the default in `tools/cmake/preset/android.cmake` and
`apple_common.cmake`.
`XNNCompiler` now moves those buffers to the executor as each value is
defined, taking everything the cache loaded for that value via the index
`get_num_unpacked_data()` returned just before it, which mirrors how the
non-cache path decides what to retain. `finalize_for_runtime()` then
frees whatever is left, all of which really was packed.
`initialize_for_runtime()` also drops anything a previous compile left
behind, since a compile that bails out never reaches a finalize and the
cache outlives any one model.
### Repro
Two chained PReLUs, arranged so the slope is readable from the output:
`prelu(prelu(x - 10) - 10)` with slopes 0.25 and 0.5. For a zero input
the answer is `-6.25`. On a Galaxy S26 Ultra, arm64, weights cache on:
| | before | after |
| --- | --- | --- |
| `executor_runner` | -0.0982864 | -6.25 |
| same, 2 MB allocated between init and execute | 4.33931e+29 | -6.25 |
| MediaPipe Face Mesh (23 PReLUs), boxes | all NaN | `41.2182, 37.6233,
159.611, 170.073` |
The after column was measured on the first revision of this patch, which
held the buffers back inside `finalize_for_runtime()`. The current
revision retains the same set at value-definition time instead, so the
numbers have not been re-taken on the code as it now stands.
It is undefined behaviour, so it presents as environment-dependent: one
build of `executor_runner` returned the right answer, and the same
source rebuilt with unrelated flags did not. A single PReLU usually
survives, since nothing has reused the block yet; two or more do not.
### Test plan
Verified on device as above, plus two cases in
`test_xnn_weights_cache.cpp`: a taken buffer stays readable across
`finalize_for_runtime()` while the packed one is freed, and a load with
no finalize is cleared by the next `initialize_for_runtime()`. Both fail
on the base branch. Note they only build under buck today, so they will
not turn OSS CI red until the cmake target lists that file.
An end-to-end test is still out of reach:
`backends/xnnpack/test/ops/test_prelu.py` runs through the pybindings,
which load the whole `.pte` into one buffer, and there
`FreeableBuffer::Free()` is a no-op, so the dangling read still lands on
valid memory.
### Note
`getConstantDataPtr()` has a sibling case I did not touch: for inline
constants it returns `constant_data_ptr + offset` into `processed`,
which `XNNPACKBackend` frees right after `compileModel`. That looks like
the same defect for `.pte`s without named data, but I could not produce
one that reaches it, so I left it alone rather than change it blind.
cc @GregoryComer @digantdesai @cbilgin @JakeStevens1 parent de1a8ce commit 3fd80c7
4 files changed
Lines changed: 116 additions & 2 deletions
File tree
- backends/xnnpack
- runtime
- test/runtime
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2236 | 2236 | | |
2237 | 2237 | | |
2238 | 2238 | | |
| 2239 | + | |
| 2240 | + | |
| 2241 | + | |
| 2242 | + | |
2239 | 2243 | | |
2240 | 2244 | | |
2241 | 2245 | | |
| |||
2264 | 2268 | | |
2265 | 2269 | | |
2266 | 2270 | | |
| 2271 | + | |
| 2272 | + | |
| 2273 | + | |
| 2274 | + | |
2267 | 2275 | | |
2268 | 2276 | | |
2269 | 2277 | | |
| |||
2312 | 2320 | | |
2313 | 2321 | | |
2314 | 2322 | | |
| 2323 | + | |
| 2324 | + | |
2315 | 2325 | | |
2316 | 2326 | | |
2317 | 2327 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
215 | 215 | | |
216 | 216 | | |
217 | 217 | | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
218 | 230 | | |
219 | 231 | | |
220 | 232 | | |
| |||
296 | 308 | | |
297 | 309 | | |
298 | 310 | | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
299 | 331 | | |
300 | 332 | | |
301 | 333 | | |
302 | | - | |
303 | | - | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
304 | 340 | | |
305 | 341 | | |
306 | 342 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
94 | 114 | | |
95 | 115 | | |
96 | 116 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
320 | 320 | | |
321 | 321 | | |
322 | 322 | | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
323 | 371 | | |
324 | 372 | | |
325 | 373 | | |
| |||
0 commit comments