From 076df2b8bb3341d9b20567c851382cb55068c927 Mon Sep 17 00:00:00 2001 From: Eduard Voronkin Date: Mon, 2 Jun 2025 21:38:32 -0700 Subject: [PATCH] do not touch uncommitted memory sometimes, under high memory pressure situation, VirtualAlloc can fail to commit memory. In such cases, we should not touch such memory, but rather return NULL ptr so we try to mi_heap_collect and recover. --- src/arena.c | 5 ++++- src/os.c | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/arena.c b/src/arena.c index 514a0b25..c7390ae2 100644 --- a/src/arena.c +++ b/src/arena.c @@ -677,7 +677,10 @@ static mi_page_t* mi_arenas_page_alloc_fresh(size_t slice_count, size_t block_si commit_size = _mi_align_up(block_start + block_size, MI_PAGE_MIN_COMMIT_SIZE); if (commit_size > page_noguard_size) { commit_size = page_noguard_size; } bool is_zero; - mi_arena_commit( mi_memid_arena(memid), page, commit_size, &is_zero, 0); + if (!mi_arena_commit( mi_memid_arena(memid), page, commit_size, &is_zero, 0)) { + mi_os_prim_free(p, commit_size, commit_size); + return NULL; + } if (!memid.initially_zero && !is_zero) { _mi_memzero_aligned(page, commit_size); } diff --git a/src/os.c b/src/os.c index 420ad14f..850250a0 100644 --- a/src/os.c +++ b/src/os.c @@ -309,7 +309,10 @@ static void* mi_os_prim_alloc_aligned(size_t size, size_t alignment, bool commit // explicitly commit only the aligned part if (commit) { - _mi_os_commit(p, size, NULL); + if (!_mi_os_commit(p, size, NULL)) { + mi_os_prim_free(p, size, size); + return NULL; + } } } else { // mmap can free inside an allocation