Commit 492ef76
committed
patina_internal_collections: Fix UB memory read in Node fields
Fixes an undefined behavior issue where Cell::set() reads
uninitialized memory during linked list creation in Storage::resize().
Root Cause:
- Cell::set() internally uses mem::replace(), which reads the old value
before writing the new one.
- When Storage::resize() allocates new nodes and calls build_linked_list(),
the Cell fields contain uninitialized memory.
- Reading uninitialized memory is undefined behavior, even if immediately
overwritten. Unwanted compiler "optimizations" could follow.
Impact:
- Any package using patina_internal_collections.
- Potential memory corruption and non-deterministic errors
- Detected by Miri testing (issue #560)
Fix:
- Initialize all Cell fields using ptr::write() before build_linked_list()
- Use addr_of_mut!() to get field pointers without creating references to
uninitialized data
- Verified with Miri
Introduced by:
- PR #1050 (Nov 13, 2025) Replace AtomicPtr with Cell in patina_internal_collections
- AtomicPtr::store() writes without reading the old value, but Cell::set()
uses mem::replace() which reads before writing
Testing:
- Tested with: cargo +nightly-2025-09-19 miri test -p patina_dxe_core.
- 7 tests now pass (previously 0/469 due to this UB issue).
Related to #5601 parent 939804a commit 492ef76
1 file changed
+25
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
204 | 204 | | |
205 | 205 | | |
206 | 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 | + | |
207 | 232 | | |
208 | 233 | | |
209 | 234 | | |
| |||
0 commit comments