Skip to content

[MOD-17680] Fix crashes in CONTAINS and suffix TAG queries on macOS x86_64 - #11909

Merged
alonre24 merged 2 commits into
masterfrom
alon-mod-17680-overaligned-alloc
Oct 15, 2026
Merged

alonre24 merged 2 commits into
masterfrom
alon-mod-17680-overaligned-alloc

Conversation

@alonre24

@alonre24 alonre24 commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Describe the changes in the pull request

  1. Current: The Rust global allocator returns memory aligned only to 16 bytes, whatever alignment a type asks for. The Redis module allocator has no aligned-allocation call. On macOS x86_64, the AVX2 substring searcher behind CONTAINS and suffix TAG queries needs 32 bytes, so it gets misaligned memory. That is undefined behavior: assertion-enabled builds abort with 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.
  2. Change: Allocations that need more than 16-byte alignment get a correctly aligned block. Allocations needing 16 bytes or less, which is nearly all of them, go to the Redis allocator exactly as before.
  3. Outcome: CONTAINS, suffix and wildcard TAG queries no longer crash on macOS x86_64. A future dependency bump or a new over-aligned type can no longer bring the bug back on other platforms.

Which additional issues this PR fixes

  1. MOD-17680

Main objects this PR modified

  1. New over_aligned_alloc crate. Its OverAligned adapter 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.
  2. redisearch_rs registers OverAligned<RedisAlloc> as the global allocator.

Review focus: the pointer arithmetic in OverAligned::place, and the realloc path 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 align extra 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

  • This PR introduces API changes
  • This PR introduces serialization changes

Release Notes

  • This PR requires release notes
  • This PR does not require 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

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]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-15T14:27:21.821539Z 015414e New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@codecov

codecov Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.11111% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.62%. Comparing base (926aafd) to head (015414e).
⚠️ Report is 13 commits behind head on master.

Files with missing lines Patch % Lines
src/redisearch_rs/over_aligned_alloc/src/lib.rs 86.11% 7 Missing and 3 partials ⚠️
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     
Flag Coverage Δ
flow 72.40% <54.16%> (-0.04%) ⬇️
unit 57.65% <86.11%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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]>
@alonre24
alonre24 requested a review from gdesmott October 15, 2026 14:23
@github-actions github-actions Bot added size:L and removed size:M labels Oct 15, 2026
@alonre24
alonre24 requested a review from JoanFM October 15, 2026 14:41
@sonarqubecloud

Copy link
Copy Markdown

///
/// 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;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAy be an overcomplication, and it seems that having the specific implementation with RedisAllocator would not work.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same could apply to HEADER_SIZE

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 OverAligned over 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 to RedisAlloc couldn't be exercised without a live Redis.
  • Why MAX_INNER_ALIGN isn't a parameter. The only production inner allocator is RedisAlloc, and what it guarantees is malloc's max_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_SIZE isn'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?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this cheap change u propose would be good.

@alonre24

Copy link
Copy Markdown
Collaborator Author

macOS x86_64 validation

Flow tests (QUICK=1, standalone + coordinator) on 526ca8680e. The follow-up commit changes only the adapter's abort-on-invariant handling and its tests.

Platform Standalone Coordinator Run
macos-14 x86_64 2658/2658 2647/2647 38743668104
macos-15 x86_64 2658/2658 2647/2647 38743673655
macos-26 x86_64 2658/2658 2646/2647 38743681901
  • Crashes: none on any of the three platforms. On unmodified master, macos-14 x86_64 aborts on panic_misaligned_pointer_dereference (38547960094).
  • Tests that crashed before (test_contains:testSanityTags, test_contains:testSuffixTrieFindsShortAsciiContainsTag, test_parser:testTagQueryWithOR_V2, test_multibyte_char_terms:testJsonMultibyteTag): all pass now.
  • The one macos-26 failure is unrelated: test_vecsim:test_hybrid_query_with_text_vamana_batches timed out after 250 s waiting for background vector indexing on the Intel runner.

/// `min(layout.align(), MAX_INNER_ALIGN)`; nothing else is assumed of it.
pub struct OverAligned<A>(pub A);

impl<A> OverAligned<A> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this method used?

@JoanFM JoanFM left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@alonre24
alonre24 added this pull request to the merge queue Oct 15, 2026
Merged via the queue into master with commit 58c8544 Oct 15, 2026
61 checks passed
@alonre24
alonre24 deleted the alon-mod-17680-overaligned-alloc branch October 15, 2026 17:45
@JoanFM JoanFM mentioned this pull request Oct 15, 2026
1 of 4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants