-
Notifications
You must be signed in to change notification settings - Fork 28
test(multitude): drive alignment guards through an injectable cap #704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Adomas Bekeras (AdomasBekeras)
merged 3 commits into
main
from
u/abekeras/multitude-align-cap
Sep 3, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,246 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //! Every allocation entry point rejects a type aligned at or above the | ||
| //! arena's cap. | ||
| //! | ||
| //! Smart pointers recover their chunk header by masking the value | ||
| //! pointer's offset within its chunk tile. A value aligned at or above | ||
| //! the cap can land outside the first tile, where the mask recovers a | ||
| //! different chunk's header and `Drop` corrupts it. The guards make that | ||
| //! unreachable from safe code, and these tests hold every entry point to | ||
| //! it. | ||
| //! | ||
| //! The tests run against [`capped_arena`], whose caps are lowered so the | ||
| //! boundary is reachable by an alignment every codegen backend accepts. | ||
|
|
||
| use crate::Arena; | ||
| use crate::internal::constants::{CHUNK_ALIGN, max_smart_ptr_align}; | ||
| use crate::tests_support::{ChunkOverAligned, SmartPtrOverAligned, SmartPtrOverAlignedDrop, TEST_CHUNK_ALIGN, capped_arena}; | ||
|
AdomasBekeras marked this conversation as resolved.
|
||
|
|
||
| // The guards read their cap from the arena; `buffer_freezable` still reads | ||
| // `max_smart_ptr_align()` directly. The two must agree, or a `Vec` would | ||
| // freeze in place for an element the smart-pointer path rejects. | ||
| #[test] | ||
| fn default_caps_match_the_constants() { | ||
| let arena = Arena::new(); | ||
| assert_eq!(arena.chunk_align_cap(), CHUNK_ALIGN); | ||
| assert_eq!(arena.smart_ptr_align_cap(), max_smart_ptr_align()); | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic(expected = "the cap may only be lowered")] | ||
| fn set_align_cap_rejects_raising_the_cap() { | ||
| // Must stay a power of two, or the earlier assert fires instead. | ||
| Arena::new().set_align_cap(CHUNK_ALIGN * 2); | ||
|
AdomasBekeras marked this conversation as resolved.
|
||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic(expected = "alignment cap must leave room")] | ||
| fn set_align_cap_rejects_a_degenerate_cap() { | ||
| Arena::new().set_align_cap(1); | ||
| } | ||
|
|
||
| // The guards compare `>=`, so they must reject alignments strictly above the | ||
| // cap as well as those exactly at it. Every fixture is aligned exactly at a | ||
| // cap, so reaching "strictly above" means lowering the cap further rather | ||
| // than raising an alignment. | ||
| #[test] | ||
| fn guards_reject_alignments_above_the_cap() { | ||
| let arena = capped_arena(); | ||
| arena.set_align_cap(TEST_CHUNK_ALIGN / 2); | ||
|
|
||
| assert!(arena.rejects_smart_ptr_align(align_of::<SmartPtrOverAligned>())); | ||
| assert!(arena.rejects_chunk_align(align_of::<ChunkOverAligned>())); | ||
|
|
||
| arena.try_alloc_with(|| SmartPtrOverAligned(0)).unwrap_err(); | ||
|
|
||
| let src = [ChunkOverAligned(0)]; | ||
| arena.try_alloc_slice_copy(&src[..]).unwrap_err(); | ||
| } | ||
|
|
||
| // The counterpart to the above: an alignment strictly below the cap is | ||
| // accepted, so the guards are not simply rejecting everything. | ||
| #[test] | ||
| fn guards_accept_alignments_below_the_cap() { | ||
| let arena = capped_arena(); | ||
|
|
||
| assert!(!arena.rejects_smart_ptr_align(align_of::<u64>())); | ||
| assert!(!arena.rejects_chunk_align(align_of::<SmartPtrOverAligned>())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rejection_reports_alignment_too_large() { | ||
| let arena = capped_arena(); | ||
| let err = arena.try_alloc_with(|| SmartPtrOverAligned(0)).unwrap_err(); | ||
| assert!(err.is_alignment_too_large()); | ||
|
|
||
| let err = arena.try_alloc_arc_with(|| SmartPtrOverAlignedDrop(0)).unwrap_err(); | ||
| assert!(err.is_alignment_too_large()); | ||
|
|
||
| // A local binding, not `&[…][..]`: an over-aligned temporary that gets | ||
| // const-promoted lands in a section the linker rejects. | ||
| let src = [ChunkOverAligned(0)]; | ||
| let err = arena.try_alloc_slice_copy(&src[..]).unwrap_err(); | ||
| assert!(err.is_alignment_too_large()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_alloc_with_rejects_smart_ptr_alignment() { | ||
| let arena = capped_arena(); | ||
| arena.try_alloc_with(|| SmartPtrOverAligned(0)).unwrap_err(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_alloc_arc_with_rejects_smart_ptr_alignment() { | ||
| let arena = capped_arena(); | ||
| arena.try_alloc_arc_with(|| SmartPtrOverAlignedDrop(0)).unwrap_err(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_alloc_box_with_rejects_smart_ptr_alignment() { | ||
| let arena = capped_arena(); | ||
| arena.try_alloc_box_with(|| SmartPtrOverAlignedDrop(0)).unwrap_err(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_alloc_uninit_box_rejects_smart_ptr_alignment() { | ||
| let arena = capped_arena(); | ||
| arena.try_alloc_uninit_box::<SmartPtrOverAlignedDrop>().unwrap_err(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_alloc_uninit_arc_rejects_smart_ptr_alignment() { | ||
| let arena = capped_arena(); | ||
| arena.try_alloc_uninit_arc::<SmartPtrOverAlignedDrop>().unwrap_err(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_alloc_slice_copy_arc_rejects_smart_ptr_alignment() { | ||
| let arena = capped_arena(); | ||
| let src = [SmartPtrOverAligned(0), SmartPtrOverAligned(1)]; | ||
| arena.try_alloc_slice_copy_arc(&src[..]).unwrap_err(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_alloc_arc_with_no_drop_rejects_smart_ptr_alignment() { | ||
| let arena = capped_arena(); | ||
| arena.try_alloc_arc_with(|| SmartPtrOverAligned(0)).unwrap_err(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_alloc_slice_fill_with_arc_rejects_smart_ptr_alignment() { | ||
| let arena = capped_arena(); | ||
| arena.try_alloc_slice_fill_with_arc(1, |_| SmartPtrOverAlignedDrop(0)).unwrap_err(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_alloc_slice_fill_with_arc_no_drop_rejects_smart_ptr_alignment() { | ||
| let arena = capped_arena(); | ||
| arena.try_alloc_slice_fill_with_arc(2, |_| SmartPtrOverAligned(0)).unwrap_err(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_alloc_slice_fill_with_box_rejects_smart_ptr_alignment() { | ||
| let arena = capped_arena(); | ||
| arena.try_alloc_slice_fill_with_box(1, |_| SmartPtrOverAlignedDrop(0)).unwrap_err(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_alloc_uninit_slice_arc_rejects_smart_ptr_alignment() { | ||
| let arena = capped_arena(); | ||
| arena.try_alloc_uninit_slice_arc::<SmartPtrOverAlignedDrop>(1).unwrap_err(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_alloc_uninit_slice_box_rejects_smart_ptr_alignment() { | ||
| let arena = capped_arena(); | ||
| arena.try_alloc_uninit_slice_box::<SmartPtrOverAlignedDrop>(1).unwrap_err(); | ||
| } | ||
|
|
||
| // A pinned box hands out a smart pointer, so it rejects at the smart-pointer | ||
| // cap like the other box entry points. | ||
| #[test] | ||
| fn try_alloc_uninit_box_pin_rejects_smart_ptr_alignment() { | ||
| let arena = capped_arena(); | ||
| arena.try_alloc_uninit_box_pin::<SmartPtrOverAligned>().unwrap_err(); | ||
| } | ||
|
|
||
| // Simple-reference slices hand back a plain `&mut [T]` with no header | ||
| // recovery, so they use the looser chunk cap and only reject at it. | ||
| #[test] | ||
| fn try_alloc_slice_fill_with_rejects_chunk_alignment() { | ||
| let arena = capped_arena(); | ||
| arena.try_alloc_slice_fill_with(1, |_| ChunkOverAligned(0)).unwrap_err(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_alloc_slice_copy_rejects_chunk_alignment() { | ||
| let arena = capped_arena(); | ||
| let src = [ChunkOverAligned(0)]; | ||
| arena.try_alloc_slice_copy(&src[..]).unwrap_err(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn try_alloc_slice_fill_iter_rejects_chunk_alignment() { | ||
| let arena = capped_arena(); | ||
| arena.try_alloc_slice_fill_iter((0..1).map(|_| ChunkOverAligned(0))).unwrap_err(); | ||
| } | ||
|
|
||
| // A type aligned below the chunk cap is accepted by the simple-reference | ||
| // slice paths: the boundary the tighter smart-pointer cap does not apply to. | ||
| #[test] | ||
| fn alloc_slice_ref_accepts_below_chunk_alignment_for_non_drop() { | ||
| let arena = capped_arena(); | ||
| let filled = arena.alloc_slice_fill_with(1, |_| SmartPtrOverAligned(0)); | ||
| assert_eq!(filled.len(), 1); | ||
|
|
||
| let src = [SmartPtrOverAligned(0)]; | ||
| let cloned = arena.alloc_slice_clone(&src[..]); | ||
| assert_eq!(cloned.len(), 1); | ||
| } | ||
|
|
||
| // Panicking entry points route through the same guards and surface the | ||
| // rejection as the arena's allocation-failure panic. | ||
| #[test] | ||
| #[should_panic(expected = "multitude: allocator returned AllocError")] | ||
| fn alloc_with_panics_on_over_alignment() { | ||
| let arena = capped_arena(); | ||
| let _ = arena.alloc_with(|| SmartPtrOverAligned(0)); | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic(expected = "multitude: allocator returned AllocError")] | ||
| fn alloc_arc_with_panics_on_over_alignment() { | ||
| let arena = capped_arena(); | ||
| let _ = arena.alloc_arc_with(|| SmartPtrOverAligned(0)); | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic(expected = "multitude: allocator returned AllocError")] | ||
| fn alloc_box_with_panics_on_over_alignment() { | ||
| let arena = capped_arena(); | ||
| let _ = arena.alloc_box_with(|| SmartPtrOverAligned(0)); | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic(expected = "multitude: allocator returned AllocError")] | ||
| fn alloc_uninit_box_panics_on_over_alignment() { | ||
| let arena = capped_arena(); | ||
| let _ = arena.alloc_uninit_box::<SmartPtrOverAligned>(); | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic(expected = "multitude: allocator returned AllocError")] | ||
| fn alloc_uninit_arc_panics_on_over_alignment() { | ||
| let arena = capped_arena(); | ||
| let _ = arena.alloc_uninit_arc::<SmartPtrOverAligned>(); | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic(expected = "multitude: allocator returned AllocError")] | ||
| fn alloc_slice_copy_panics_on_over_alignment() { | ||
| let arena = capped_arena(); | ||
| let src = [ChunkOverAligned(0)]; | ||
| let _ = arena.alloc_slice_copy(&src[..]); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.