Repository navigation
[MOD-17680] Fix crashes in CONTAINS and suffix TAG queries on macOS x86_64 - #11909
Conversation
RedisAlloc rounds the size up to the layout's alignment but returns whatever RedisModule_Alloc does, which guarantees only max_align_t (16 bytes). A type needing more, such as memchr's AVX2 substring searcher (32 bytes) boxed in the trie's contains iterator, gets under-aligned memory: undefined behaviour, which assertion-enabled macOS x86_64 builds report as a misaligned-pointer panic on CONTAINS and suffix tag queries. Linux is masked by jemalloc's size classes and aarch64 by a 16-byte NEON searcher. Add an `over_aligned_alloc` crate whose `OverAligned` adapter forwards layouts aligned to at most 16 bytes unchanged and serves larger ones from an inner block padded by the alignment, storing the inner block's address just below the aligned pointer. Register it around RedisAlloc as the global allocator. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #11909 +/- ##
==========================================
+ Coverage 83.61% 83.62% +0.01%
==========================================
Files 681 682 +1
Lines 88225 88328 +103
Branches 11455 11473 +18
==========================================
+ Hits 73770 73866 +96
- Misses 10803 10833 +30
+ Partials 3652 3629 -23
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
A panic must not unwind out of a global allocator, and it would re-enter the allocator to format its message. Replace the `expect` in `dealloc` and the `debug_assert!` in `place` with an abort, and check in debug builds that the inner block has the alignment `place` relies on. Tests now log every call reaching the inner allocator, so they check that alloc, alloc_zeroed, realloc and dealloc all forward small alignments with byte-identical arguments and results. They also cover a failed over-aligned realloc leaving the original block intact. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
| /// | ||
| /// This is `max_align_t` on every platform the module ships on, which is all | ||
| /// `malloc` promises. Layouts asking for more are served by over-allocating. | ||
| pub const MAX_INNER_ALIGN: usize = 16; |
There was a problem hiding this comment.
nitpick: I believe this should be a template parameter of OverAligned. SInce we are making OverAligned a generic over any GlobalAllocator, we should be able to parametrize what the MAX_INNER_ALIGNMENT we need to ensure.
Another option may be to not generalize the implementation.
There was a problem hiding this comment.
MAy be an overcomplication, and it seems that having the specific implementation with RedisAllocator would not work.
There was a problem hiding this comment.
same could apply to HEADER_SIZE
There was a problem hiding this comment.
Good point. I kept it a module constant for now, but I'd like your opinion before settling it.
- Why the adapter is generic at all. The tests need it: they put
OverAlignedover a fake inner allocator whose addresses are always 16-byte but never 32-byte aligned, which makes the bug reproduce deterministically (and under Miri). A version tied toRedisAlloccouldn't be exercised without a live Redis. - Why
MAX_INNER_ALIGNisn't a parameter. The only production inner allocator isRedisAlloc, and what it guarantees ismalloc'smax_align_t: 16 bytes on every platform we ship. The test allocator is built to honor exactly that, so no second value exists to parametrize over today. - Why
HEADER_SIZEisn't one either. It isn't a property of the inner allocator. It's the size of the back-pointer we store below the block,size_of::<*mut u8>(), so it's fixed by what we write.
If you'd prefer the contract in the type anyway, it's a cheap change: OverAligned<A, const INNER_ALIGN: usize = 16>, with the HEADER_SIZE <= INNER_ALIGN check moved into an inline const { assert!(..) }. The #[global_allocator] declaration wouldn't change. Which do you prefer?
There was a problem hiding this comment.
I think this cheap change u propose would be good.
macOS x86_64 validationFlow tests (
|
| /// `min(layout.align(), MAX_INNER_ALIGN)`; nothing else is assumed of it. | ||
| pub struct OverAligned<A>(pub A); | ||
|
|
||
| impl<A> OverAligned<A> { |
There was a problem hiding this comment.
Would be good to have AI write a schema of how the memory is saved.
How offset / header / user returned data is allocated and returned.
| } | ||
|
|
||
| impl Misaligning { | ||
| fn exhausted() -> Self { |
JoanFM
left a comment
There was a problem hiding this comment.
LGTM!
Only 2 suggestions.
- Move INNER_SIZE and check as a parameter and into the type
- Add better visual documentation of what OverAligned provides and how it structures and lays out memory.



Describe the changes in the pull request
misaligned pointer dereference, and release builds are exposed to crashes or silent corruption. Linux is masked by jemalloc's size classes and aarch64 by a 16-byte NEON searcher, by accident rather than by guarantee.Which additional issues this PR fixes
Main objects this PR modified
over_aligned_alloccrate. ItsOverAlignedadapter serves an over-aligned layout from an inner block padded by the alignment, and keeps the inner block's address just below the returned pointer for deallocation.redisearch_rsregistersOverAligned<RedisAlloc>as the global allocator.Review focus: the pointer arithmetic in
OverAligned::place, and thereallocpath for over-aligned blocks, which copies into a new block. The tests drive the adapter over an inner allocator whose addresses are always 16-byte but never 32-byte aligned, so the failure is deterministic on every machine, and they pass under Miri.Memory: an over-aligned allocation costs
alignextra bytes, all of it counted by Redis's allocator. Only a few types are over-aligned (today the AVX2 searcher, one per CONTAINS iterator), so the cost is negligible.Manual validation: PR CI does not run on macOS, so I dispatched macOS x86_64 flow tests by hand. Results will follow in a comment.
Mark if applicable
Release Notes
If a release note is required (bug fix / new feature / enhancement), describe the user impact of this PR in the title.
🤖 Generated with Claude Code