Commit 2897bde
Fix alignment calculation in XNNWeightsCache (#15090)
### Summary
We're seeing crashes on Android when running XNNPACK-delegated models. I
tracked it down to a bug in the alignment calculation for weight cache
memory. To make the calculation, it casts the void* to a (signed)
intptr_t. When the address is in the upper half of the address space, it
becomes negative. This causes the modulo to return a negative value and
increment the address too much - leading to out of bounds access.
https://github.com/pytorch/executorch/blob/cc6cb837d6ac92f52a2d30a405900caf115f0556/backends/xnnpack/runtime/XNNWeightsCache.cpp#L166-L168
Walking through the numbers I captured in
#14831:
* The raw (unaligned) address of the data buffer is 0xb40000763d4bfa90.
* The target alignment is 64 bytes.
* Casting the address to intptr_t gives -5476376639047992688.
* Mod 64 is -48.
* The total offset applied is 64 - (-48) = 112.
* Since the allocation size is N + 64, increasing the start by 112 means
the new region extends 48 bytes past the end of the allocation.
To resolve this, I replaced the alignment code with a call to
std::align. Casing to uintptr_t also resolves it, but using the standard
implementation seems less error prone.
### Test plan
I've validated that the repro in
#14831 does not crash with
this change.
Co-authored-by: Gregory Comer <[email protected]>1 parent a728efa commit 2897bde
1 file changed
+36
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
14 | 17 | | |
15 | 18 | | |
16 | 19 | | |
| |||
155 | 158 | | |
156 | 159 | | |
157 | 160 | | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
158 | 166 | | |
159 | 167 | | |
160 | 168 | | |
161 | 169 | | |
162 | 170 | | |
163 | 171 | | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
| 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 | + | |
173 | 200 | | |
174 | 201 | | |
175 | 202 | | |
| |||
0 commit comments