summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2024-04-16 20:34:12 (GMT)
committerGitHub <noreply@github.com>2024-04-16 20:34:12 (GMT)
commit3fe03ccea61121b05e933013f76aa02f690014c1 (patch)
tree8da922b3d5a618c6324c6ad6d9bf613d639a9d52
parente05d202ebf0904cf0a980fde76f4a38bb8dc7c3b (diff)
downloadcpython-3fe03ccea61121b05e933013f76aa02f690014c1.zip
cpython-3fe03ccea61121b05e933013f76aa02f690014c1.tar.gz
cpython-3fe03ccea61121b05e933013f76aa02f690014c1.tar.bz2
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).
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2024-04-12-12-28-49.gh-issue-117755.6ct8kU.rst2
-rw-r--r--Objects/mimalloc/segment.c6
2 files changed, 8 insertions, 0 deletions
diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-12-12-28-49.gh-issue-117755.6ct8kU.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-12-12-28-49.gh-issue-117755.6ct8kU.rst
new file mode 100644
index 0000000..a65ec43
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-12-12-28-49.gh-issue-117755.6ct8kU.rst
@@ -0,0 +1,2 @@
+Fix mimalloc allocator for huge memory allocation (around 8,589,934,592 GiB) on
+s390x. Patch by Victor Stinner.
diff --git a/Objects/mimalloc/segment.c b/Objects/mimalloc/segment.c
index 08b1564..0b4d3ab 100644
--- a/Objects/mimalloc/segment.c
+++ b/Objects/mimalloc/segment.c
@@ -814,6 +814,9 @@ static mi_segment_t* mi_segment_os_alloc( size_t required, size_t page_alignment
const size_t extra = align_offset - info_size;
// recalculate due to potential guard pages
*psegment_slices = mi_segment_calculate_slices(required + extra, ppre_size, pinfo_slices);
+
+ // mi_page_t.slice_count type is uint32_t
+ if (*psegment_slices > (size_t)UINT32_MAX) return NULL;
}
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
size_t pre_size;
size_t segment_slices = mi_segment_calculate_slices(required, &pre_size, &info_slices);
+ // mi_page_t.slice_count type is uint32_t
+ if (segment_slices > (size_t)UINT32_MAX) return NULL;
+
// Commit eagerly only if not the first N lazy segments (to reduce impact of many threads that allocate just a little)
const bool eager_delay = (// !_mi_os_has_overcommit() && // never delay on overcommit systems
_mi_current_thread_count() > 1 && // do not delay for the first N threads