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.
This commit is contained in:
Eduard Voronkin 2025-06-02 21:38:32 -07:00
parent bfa8982d4b
commit 076df2b8bb
2 changed files with 8 additions and 2 deletions

View file

@ -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); 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; } if (commit_size > page_noguard_size) { commit_size = page_noguard_size; }
bool is_zero; 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) { if (!memid.initially_zero && !is_zero) {
_mi_memzero_aligned(page, commit_size); _mi_memzero_aligned(page, commit_size);
} }

View file

@ -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 // explicitly commit only the aligned part
if (commit) { 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 else { // mmap can free inside an allocation