Skip to content

Commit 3fd80c7

Browse files
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 @JakeStevens
1 parent de1a8ce commit 3fd80c7

4 files changed

Lines changed: 116 additions & 2 deletions

File tree

‎backends/xnnpack/runtime/XNNCompiler.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,10 @@ ET_NODISCARD Error XNNCompiler::compileModel(
22362236
Error err = Error::Ok;
22372237
for (auto value : *flatbuffer_graph->xvalues()) {
22382238
size_t prev_buffers = unpacked_buffers.size();
2239+
// With the weights cache the buffers land in the cache rather than in
2240+
// unpacked_buffers, so track its list too.
2241+
size_t prev_cached_buffers =
2242+
use_weight_cache ? weights_cache->get_num_unpacked_data() : 0;
22392243
err = defineTensor(
22402244
subgraph.get(),
22412245
remapped_ids,
@@ -2264,6 +2268,10 @@ ET_NODISCARD Error XNNCompiler::compileModel(
22642268
executor->unpacked_buffers_.push_back(std::move(unpacked_buffers[i]));
22652269
}
22662270
unpacked_buffers.resize(prev_buffers);
2271+
if (use_weight_cache) {
2272+
weights_cache->take_unpacked_data_from(
2273+
prev_cached_buffers, executor->unpacked_buffers_);
2274+
}
22672275
}
22682276
}
22692277

@@ -2312,6 +2320,8 @@ ET_NODISCARD Error XNNCompiler::compileModel(
23122320

23132321
std::vector<std::string> packed_weights_names;
23142322
if (use_weight_cache) {
2323+
// Constants XNNPACK does not pack were already moved to the executor in
2324+
// the value loop above, so everything left here is safe to free.
23152325
auto packed_weights_names_result = weights_cache->finalize_for_runtime();
23162326
ET_CHECK_OR_RETURN_ERROR(
23172327
packed_weights_names_result.ok(),

‎backends/xnnpack/runtime/XNNWeightsCache.cpp‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@ Error XNNWeightsCache::initialize_for_runtime(
215215
named_data_map_ = named_data_map;
216216
is_finalized_ = false;
217217

218+
// An earlier compile can bail out between its first load_unpacked_data and
219+
// its finalize_for_runtime, leaving its constants here. A successful compile
220+
// frees whatever is left in finalize_for_runtime, but a failed one never
221+
// reaches it, so back to back failures would pile up on an instance that
222+
// lives as long as the process. The model that loaded them is gone, so free
223+
// them here.
224+
for (FreeableBuffer& buffer : unpacked_data_) {
225+
buffer.Free();
226+
}
227+
unpacked_data_.clear();
228+
unpacked_data_to_name_.clear();
229+
218230
#ifndef _WIN32
219231
if (packed_cache_path_.empty() || packed_file_fd_ >= 0) {
220232
return Error::Ok;
@@ -296,11 +308,35 @@ Error XNNWeightsCache::initialize_for_runtime(
296308
return Error::Ok;
297309
}
298310

311+
void XNNWeightsCache::take_unpacked_data_from(
312+
size_t first_index,
313+
std::vector<FreeableBuffer>& out) {
314+
// No bounds check: the loop below is empty when first_index is at or past
315+
// the end, and the list only grows during a compile so that cannot happen
316+
// anyway. An index that is too small cannot be detected here, and would hand
317+
// the executor buffers of values XNNPACK did pack; callers must pass the
318+
// get_num_unpacked_data() taken immediately before the value was defined.
319+
for (size_t i = first_index; i < unpacked_data_.size(); i++) {
320+
// The name map is keyed on the data pointer, which a move preserves, so
321+
// look_up_or_insert keeps naming packed entries correctly after this.
322+
out.push_back(std::move(unpacked_data_[i]));
323+
}
324+
// The moved-from entries stay in the list rather than being erased:
325+
// FreeableBuffer deletes move assignment, so vector::erase does not compile,
326+
// and a moved-from buffer holds a null pointer, which makes the Free() in
327+
// finalize_for_runtime a no-op. Leaving them also keeps the indices handed
328+
// out by get_num_unpacked_data() stable across calls.
329+
}
330+
299331
Result<std::vector<std::string>> XNNWeightsCache::finalize_for_runtime() {
300332
is_finalized_ = true;
301333

302-
// All data has been packed by create_runtime
303-
// so we clear the unpacked data as it is no longer needed
334+
// Everything still here belongs to a value XNNPACK packed, so create_runtime
335+
// has copied it into the packed region and the unpacked copy can go. Buffers
336+
// for values XNNPACK does not pack (PReLU slopes, for example) were handed to
337+
// the executor by take_unpacked_data_from while the graph was being built;
338+
// the subgraph keeps pointers into those, so freeing them here would leave
339+
// the runtime reading freed memory.
304340
for (FreeableBuffer& buffer : unpacked_data_) {
305341
buffer.Free();
306342
}

‎backends/xnnpack/runtime/XNNWeightsCache.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ class XNNWeightsCache {
9191
*/
9292
Result<std::vector<std::string>> finalize_for_runtime();
9393

94+
/**
95+
* Transfers ownership of the unpacked buffers loaded since `first_index`
96+
* out of this cache. finalize_for_runtime() will not free them.
97+
*
98+
* For values XNNPACK does not pack (PReLU slopes, for example) the subgraph
99+
* keeps a pointer into the unpacked memory for the life of the runtime, so
100+
* something has to keep those buffers alive past finalize_for_runtime().
101+
* Callers pair this with get_num_unpacked_data() taken before the value was
102+
* defined, which is how the non-weights-cache path in XNNCompiler decides
103+
* what to retain.
104+
*
105+
* @param[in] first_index Index into the unpacked buffer list, from
106+
* get_num_unpacked_data() before the value was defined.
107+
* @param[out] out Receives the buffers. The caller owns them and must keep
108+
* them alive for at least as long as the runtime.
109+
*/
110+
void take_unpacked_data_from(
111+
size_t first_index,
112+
std::vector<FreeableBuffer>& out);
113+
94114
// Taken from XNN_ALLOCATION_ALIGNMENT in xnnpack/common.h
95115
static const size_t kPackedAllocationAlignment = 64;
96116

‎backends/xnnpack/test/runtime/test_xnn_weights_cache.cpp‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,54 @@ TEST_F(XNNWeightsCacheTest, ReusePackedWeights) {
320320
ASSERT_EQ(packed_data_names.size(), 0);
321321
}
322322

323+
TEST_F(XNNWeightsCacheTest, TakenUnpackedDataSurvivesFinalize) {
324+
// XNNPACK does not pack every constant it is given: PReLU slopes, for one,
325+
// stay as the unpacked buffer the subgraph points at. Those buffers are taken
326+
// out of the cache while the graph is built, and finalize_for_runtime must
327+
// leave them alone while still freeing the ones it packed.
328+
XNNWeightsCache cache;
329+
cache.initialize_for_runtime(memory_allocator_.get(), data_map_.get());
330+
331+
Result<const uint8_t*> packed = cache.load_unpacked_data("weight");
332+
ASSERT_EQ(packed.error(), Error::Ok);
333+
// The index the caller would take immediately before defining the value it
334+
// wants to keep, which is what XNNCompiler passes down.
335+
size_t first_index = cache.get_num_unpacked_data();
336+
Result<const uint8_t*> retained_load = cache.load_unpacked_data("bias");
337+
ASSERT_EQ(retained_load.error(), Error::Ok);
338+
ASSERT_EQ(cache.get_num_unpacked_data(), first_index + 1);
339+
340+
std::vector<executorch::runtime::FreeableBuffer> retained;
341+
cache.take_unpacked_data_from(first_index, retained);
342+
ASSERT_EQ(retained.size(), 1u);
343+
ASSERT_EQ(retained[0].size(), static_cast<size_t>(kSegmentSizes[1]));
344+
345+
Result<std::vector<std::string>> names = cache.finalize_for_runtime();
346+
ASSERT_EQ(names.error(), Error::Ok);
347+
348+
// Still readable, and still the "bias" segment, which SetUp fills with 2s.
349+
ASSERT_NE(retained[0].data(), nullptr);
350+
const uint8_t* data = static_cast<const uint8_t*>(retained[0].data());
351+
for (size_t i = 0; i < retained[0].size(); i++) {
352+
ASSERT_EQ(data[i], 2) << "byte " << i << " was freed or overwritten";
353+
}
354+
}
355+
356+
TEST_F(XNNWeightsCacheTest, InitializeForRuntimeClearsLeftoverUnpackedData) {
357+
// A compile that bails out after loading a constant never reaches
358+
// finalize_for_runtime. This instance outlives any one model, so the next
359+
// initialize has to drop what the failed compile left behind.
360+
XNNWeightsCache cache;
361+
cache.initialize_for_runtime(memory_allocator_.get(), data_map_.get());
362+
Result<const uint8_t*> loaded = cache.load_unpacked_data("weight");
363+
ASSERT_EQ(loaded.error(), Error::Ok);
364+
ASSERT_EQ(cache.get_num_unpacked_data(), 1u);
365+
366+
// No finalize_for_runtime(): this is the failed-compile path.
367+
cache.initialize_for_runtime(memory_allocator_.get(), data_map_.get());
368+
ASSERT_EQ(cache.get_num_unpacked_data(), 0u);
369+
}
370+
323371
#ifndef _WIN32
324372
// Verify pack-and-run works when packed weight allocations go to a
325373
// MAP_SHARED file instead of heap. The cache path is unique per test so

0 commit comments

Comments
 (0)