Skip to content

Commit 3c3078c

Browse files
authored
chore: harden checks in _mi_page_malloc_zero (#5427)
Make sure we do not return corrupt blocks which should help us with investigating a very rare memory corruption bug. Signed-off-by: Roman Gershman <[email protected]>
1 parent a9ecff6 commit 3c3078c

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

patches/mimalloc-v2.2.4.patch

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,32 @@ index 5ce084f6..00eba70c 100644
4949
include(GNUInstallDirs)
5050
include("cmake/mimalloc-config-version.cmake")
5151
diff --git a/src/alloc.c b/src/alloc.c
52-
index 0fed5e75..870f8d10 100644
52+
index 0fed5e75..893f3094 100644
5353
--- a/src/alloc.c
5454
+++ b/src/alloc.c
55-
@@ -670,6 +670,24 @@ mi_decl_restrict void* _mi_heap_malloc_guarded(mi_heap_t* heap, size_t size, boo
55+
@@ -25,6 +25,12 @@ terms of the MIT license. A copy of the license can be found in the file
56+
// Allocation
57+
// ------------------------------------------------------
58+
59+
+static void _mi_assert_local(const char* assertion, const char* fname, unsigned line) {
60+
+ _mi_fprintf(NULL, NULL, "mimalloc: assertion failed: at \"%s\":%u, assertion: \"%s\"\n", fname, line, assertion);
61+
+ abort();
62+
+}
63+
+#define mi_assert_local(expr) ((expr) ? (void)0 : _mi_assert_local(#expr,__FILE__,__LINE__))
64+
+
65+
// Fast allocation in a page: just pop from the free list.
66+
// Fall back to generic allocation only if the list is empty.
67+
// Note: in release mode the (inlined) routine is about 7 instructions with a single test.
68+
@@ -43,7 +49,7 @@ extern inline void* _mi_page_malloc_zero(mi_heap_t* heap, mi_page_t* page, size_
69+
// pop from the free list
70+
page->free = mi_block_next(page, block);
71+
page->used++;
72+
- mi_assert_internal(page->free == NULL || _mi_ptr_page(page->free) == page);
73+
+ mi_assert_local(page->free == NULL || _mi_ptr_page(page->free) == page);
74+
mi_assert_internal(page->block_size < MI_MAX_ALIGN_SIZE || _mi_is_aligned(block, MI_MAX_ALIGN_SIZE));
75+
76+
#if MI_DEBUG>3
77+
@@ -670,6 +676,24 @@ mi_decl_restrict void* _mi_heap_malloc_guarded(mi_heap_t* heap, size_t size, boo
5678
}
5779
#endif
5880

0 commit comments

Comments
 (0)