Skip to content

Commit 3fe03cc

Browse files
authored
gh-117755: Fix mimalloc for huge allocation on s390x (#117809)
Fix mimalloc allocator for huge memory allocation (around 8,589,934,592 GiB) on s390x. Abort allocation early in mimalloc if the number of slices doesn't fit into uint32_t, to prevent a integer overflow (cast 64-bit size_t to uint32_t).
1 parent e05d202 commit 3fe03cc

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix mimalloc allocator for huge memory allocation (around 8,589,934,592 GiB) on
2+
s390x. Patch by Victor Stinner.

Objects/mimalloc/segment.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,9 @@ static mi_segment_t* mi_segment_os_alloc( size_t required, size_t page_alignment
814814
const size_t extra = align_offset - info_size;
815815
// recalculate due to potential guard pages
816816
*psegment_slices = mi_segment_calculate_slices(required + extra, ppre_size, pinfo_slices);
817+
818+
// mi_page_t.slice_count type is uint32_t
819+
if (*psegment_slices > (size_t)UINT32_MAX) return NULL;
817820
}
818821

819822
const size_t segment_size = (*psegment_slices) * MI_SEGMENT_SLICE_SIZE;
@@ -865,6 +868,9 @@ static mi_segment_t* mi_segment_alloc(size_t required, size_t page_alignment, mi
865868
size_t pre_size;
866869
size_t segment_slices = mi_segment_calculate_slices(required, &pre_size, &info_slices);
867870

871+
// mi_page_t.slice_count type is uint32_t
872+
if (segment_slices > (size_t)UINT32_MAX) return NULL;
873+
868874
// Commit eagerly only if not the first N lazy segments (to reduce impact of many threads that allocate just a little)
869875
const bool eager_delay = (// !_mi_os_has_overcommit() && // never delay on overcommit systems
870876
_mi_current_thread_count() > 1 && // do not delay for the first N threads

0 commit comments

Comments
 (0)