Repository navigation
fix(windows): bypass bmalloc/libpas to fix GC crashes under memory pressure #28230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,287 @@ | ||
| /** | ||
| * Working SystemHeap implementation for Windows using mimalloc. | ||
| * | ||
| * bmalloc's SystemHeap is unimplemented on Windows (all methods crash with | ||
| * RELEASE_BASSERT_NOT_REACHED). This file provides a functional replacement | ||
| * that uses mimalloc for allocations. | ||
| * | ||
| * Combined with setting the "Malloc" environment variable at process startup, | ||
| * this causes bmalloc/libpas to redirect all allocations through this | ||
| * SystemHeap instead of using libpas directly. This avoids GC crashes caused | ||
| * by libpas instability on Windows under memory pressure. | ||
| * | ||
| * See: https://github.com/oven-sh/bun/issues/21569 | ||
| * https://github.com/oven-sh/bun/issues/28097 | ||
| * | ||
| * How it works: | ||
| * 1. Bun sets Malloc=1 env var early in main() (see src/main.zig) | ||
| * 2. bmalloc's Environment reads Malloc=1 and sets | ||
| * shouldBmallocAllocateThroughSystemHeap() = true | ||
| * 3. All bmalloc allocations route through SystemHeap | ||
| * 4. This file provides SystemHeap using mimalloc instead of crashing | ||
| * | ||
| * Link-time override: | ||
| * This object file defines the same symbols as bmalloc.lib's | ||
| * SystemHeap.cpp.o. Since object files are processed before static | ||
| * libraries, the linker uses these definitions and skips bmalloc.lib's | ||
| * unimplemented stubs. | ||
| */ | ||
|
|
||
| #include <bmalloc/BPlatform.h> | ||
|
|
||
| // Only override on Windows where SystemHeap is unimplemented. | ||
| #if BOS(WINDOWS) | ||
|
|
||
| #include <bmalloc/SystemHeap.h> | ||
| #include <bmalloc/Algorithm.h> | ||
| #include <bmalloc/BAssert.h> | ||
| #include <bmalloc/VMAllocate.h> | ||
| #include "mimalloc.h" | ||
|
|
||
| #if BENABLE(LIBPAS) | ||
| #include <bmalloc/pas_system_heap.h> | ||
| #endif | ||
|
|
||
| namespace bmalloc { | ||
|
|
||
| SystemHeap* systemHeapCache { nullptr }; | ||
|
|
||
| BALLOW_DEPRECATED_DECLARATIONS_BEGIN | ||
| DEFINE_STATIC_PER_PROCESS_STORAGE(SystemHeap); | ||
| BALLOW_DEPRECATED_DECLARATIONS_END | ||
|
|
||
| SystemHeap::SystemHeap(const LockHolder&) | ||
| : m_pageSize(vmPageSize()) | ||
| { | ||
| } | ||
|
|
||
| void* SystemHeap::malloc(size_t size, FailureAction action) | ||
| { | ||
| void* result = mi_malloc(size); | ||
| RELEASE_BASSERT(action == FailureAction::ReturnNull || result); | ||
| return result; | ||
| } | ||
|
|
||
| void* SystemHeap::memalign(size_t alignment, size_t size, FailureAction action) | ||
| { | ||
| void* result = mi_malloc_aligned(size, alignment); | ||
| RELEASE_BASSERT(action == FailureAction::ReturnNull || result); | ||
| return result; | ||
| } | ||
|
|
||
| void* SystemHeap::realloc(void* object, size_t size, FailureAction action) | ||
| { | ||
| void* result = mi_realloc(object, size); | ||
| RELEASE_BASSERT(action == FailureAction::ReturnNull || result); | ||
| return result; | ||
| } | ||
|
|
||
| void SystemHeap::free(void* object) | ||
| { | ||
| mi_free(object); | ||
| } | ||
|
|
||
| void SystemHeap::scavenge() | ||
| { | ||
| mi_collect(false); | ||
| } | ||
|
|
||
| void SystemHeap::dump() | ||
| { | ||
| } | ||
|
|
||
| // memalignLarge / freeLarge use virtual memory directly (same as upstream). | ||
| void* SystemHeap::memalignLarge(size_t alignment, size_t size) | ||
| { | ||
| alignment = roundUpToMultipleOf(m_pageSize, alignment); | ||
| size = roundUpToMultipleOf(m_pageSize, size); | ||
| void* result = tryVMAllocate(alignment, size); | ||
| if (!result) | ||
| return nullptr; | ||
| { | ||
| LockHolder locker(mutex()); | ||
| m_sizeMap[result] = size; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| void SystemHeap::freeLarge(void* base) | ||
| { | ||
| if (!base) | ||
| return; | ||
|
|
||
| size_t size; | ||
| { | ||
| LockHolder locker(mutex()); | ||
| size = m_sizeMap[base]; | ||
| size_t numErased = m_sizeMap.erase(base); | ||
| RELEASE_BASSERT(numErased == 1); | ||
|
Comment on lines
+101
to
+118
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use non-inserting map operations for Line 116 inserts a default entry when 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| vmDeallocate(base, size); | ||
| } | ||
|
|
||
| SystemHeap* SystemHeap::tryGetSlow() | ||
| { | ||
| SystemHeap* result; | ||
| if (Environment::get()->isSystemHeapEnabled()) { | ||
| systemHeapCache = SystemHeap::get(); | ||
| result = systemHeapCache; | ||
| } else { | ||
| systemHeapCache = systemHeapDisabled(); | ||
| result = nullptr; | ||
| } | ||
| RELEASE_BASSERT(systemHeapCache); | ||
| return result; | ||
| } | ||
|
|
||
| } // namespace bmalloc | ||
|
|
||
| // libpas integration — same as upstream, but our SystemHeap actually works. | ||
| #if BENABLE(LIBPAS) | ||
|
|
||
| #if BUSE(LIBPAS) | ||
|
|
||
| using namespace bmalloc; | ||
|
|
||
| bool pas_system_heap_is_enabled(pas_heap_config_kind kind) | ||
| { | ||
| switch (kind) { | ||
| case pas_heap_config_kind_bmalloc: | ||
| return !!SystemHeap::tryGet(); | ||
| case pas_heap_config_kind_jit: | ||
| case pas_heap_config_kind_pas_utility: | ||
| return false; | ||
| default: | ||
| BCRASH(); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| bool pas_system_heap_should_supplant_bmalloc(pas_heap_config_kind kind) | ||
| { | ||
| SystemHeap* heap; | ||
| switch (kind) { | ||
| case pas_heap_config_kind_bmalloc: | ||
| heap = SystemHeap::tryGet(); | ||
| if (!heap) | ||
| return false; | ||
| return heap->shouldSupplantBmalloc(); | ||
| case pas_heap_config_kind_jit: | ||
| case pas_heap_config_kind_pas_utility: | ||
| return false; | ||
| default: | ||
| BCRASH(); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| void* pas_system_heap_malloc(size_t size) | ||
| { | ||
| return SystemHeap::getExisting()->malloc(size, FailureAction::ReturnNull); | ||
| } | ||
|
|
||
| void* pas_system_heap_memalign(size_t alignment, size_t size) | ||
| { | ||
| return SystemHeap::getExisting()->memalign(alignment, size, FailureAction::ReturnNull); | ||
| } | ||
|
|
||
| void* pas_system_heap_realloc(void* ptr, size_t size) | ||
| { | ||
| return SystemHeap::getExisting()->realloc(ptr, size, FailureAction::ReturnNull); | ||
| } | ||
|
|
||
| void pas_system_heap_free(void* ptr) | ||
| { | ||
| SystemHeap::getExisting()->free(ptr); | ||
| } | ||
|
|
||
| void* pas_system_heap_malloc_compact(size_t size) | ||
| { | ||
| return SystemHeap::getExisting()->malloc(size, FailureAction::ReturnNull); | ||
| } | ||
|
|
||
| void* pas_system_heap_memalign_compact(size_t alignment, size_t size) | ||
| { | ||
| return SystemHeap::getExisting()->memalign(alignment, size, FailureAction::ReturnNull); | ||
| } | ||
|
|
||
| void* pas_system_heap_realloc_compact(void* ptr, size_t size) | ||
| { | ||
| return SystemHeap::getExisting()->realloc(ptr, size, FailureAction::ReturnNull); | ||
| } | ||
|
|
||
| #else // !BUSE(LIBPAS) | ||
|
|
||
| bool pas_system_heap_is_enabled(pas_heap_config_kind kind) | ||
| { | ||
| BUNUSED_PARAM(kind); | ||
| return false; | ||
| } | ||
|
|
||
| bool pas_system_heap_should_supplant_bmalloc(pas_heap_config_kind kind) | ||
| { | ||
| BUNUSED_PARAM(kind); | ||
| return false; | ||
| } | ||
|
|
||
| void* pas_system_heap_malloc(size_t size) | ||
| { | ||
| BUNUSED_PARAM(size); | ||
| RELEASE_BASSERT_NOT_REACHED(); | ||
| return nullptr; | ||
| } | ||
|
|
||
| void* pas_system_heap_memalign(size_t alignment, size_t size) | ||
| { | ||
| BUNUSED_PARAM(size); | ||
| BUNUSED_PARAM(alignment); | ||
| RELEASE_BASSERT_NOT_REACHED(); | ||
| return nullptr; | ||
| } | ||
|
|
||
| void* pas_system_heap_realloc(void* ptr, size_t size) | ||
| { | ||
| BUNUSED_PARAM(ptr); | ||
| BUNUSED_PARAM(size); | ||
| RELEASE_BASSERT_NOT_REACHED(); | ||
| return nullptr; | ||
| } | ||
|
|
||
| void* pas_system_heap_malloc_compact(size_t size) | ||
| { | ||
| BUNUSED_PARAM(size); | ||
| RELEASE_BASSERT_NOT_REACHED(); | ||
| return nullptr; | ||
| } | ||
|
|
||
| void* pas_system_heap_memalign_compact(size_t alignment, size_t size) | ||
| { | ||
| BUNUSED_PARAM(size); | ||
| BUNUSED_PARAM(alignment); | ||
| RELEASE_BASSERT_NOT_REACHED(); | ||
| return nullptr; | ||
| } | ||
|
|
||
| void* pas_system_heap_realloc_compact(void* ptr, size_t size) | ||
| { | ||
| BUNUSED_PARAM(ptr); | ||
| BUNUSED_PARAM(size); | ||
| RELEASE_BASSERT_NOT_REACHED(); | ||
| return nullptr; | ||
| } | ||
|
|
||
| #pragma clang diagnostic push | ||
| #pragma clang diagnostic ignored "-Wmissing-noreturn" | ||
| void pas_system_heap_free(void* ptr) | ||
| { | ||
| BUNUSED_PARAM(ptr); | ||
| RELEASE_BASSERT_NOT_REACHED(); | ||
| } | ||
| #pragma clang diagnostic pop | ||
|
|
||
| #endif // BUSE(LIBPAS) | ||
|
|
||
| #endif // BENABLE(LIBPAS) | ||
|
|
||
| #endif // BOS(WINDOWS) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,17 @@ pub extern "c" var environ: ?*anyopaque; | |
| pub fn main() void { | ||
| _bun.crash_handler.init(); | ||
|
|
||
| // Force bmalloc to use SystemHeap (mimalloc) instead of libpas on Windows. | ||
| // bmalloc's libpas allocator causes GC crashes under memory pressure on | ||
| // Windows. Setting "Malloc" causes bmalloc::Environment to route all | ||
| // allocations through SystemHeap, which we implement using mimalloc | ||
| // (see BmallocSystemHeapOverride.cpp). Must be set before any bmalloc | ||
| // allocation triggers Environment initialization. | ||
| // See: https://github.com/oven-sh/bun/issues/21569 | ||
| if (Environment.isWindows) { | ||
| _ = _bun.c._putenv("Malloc=1"); | ||
| } | ||
|
Comment on lines
+24
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Make the allocator switch the first statement in Line 22 still runs before this block, so the workaround currently depends on Proposed reorder pub fn main() void {
- _bun.crash_handler.init();
-
// Force bmalloc to use SystemHeap (mimalloc) instead of libpas on Windows.
// bmalloc's libpas allocator causes GC crashes under memory pressure on
// Windows. Setting "Malloc" causes bmalloc::Environment to route all
// allocations through SystemHeap, which we implement using mimalloc
// (see BmallocSystemHeapOverride.cpp). Must be set before any bmalloc
// allocation triggers Environment initialization.
// See: https://github.com/oven-sh/bun/issues/21569
if (Environment.isWindows) {
_ = _bun.c._putenv("Malloc=1");
}
+
+ _bun.crash_handler.init();🤖 Prompt for AI Agents |
||
|
|
||
| if (Environment.isPosix) { | ||
| var act: std.posix.Sigaction = .{ | ||
| .handler = .{ .handler = std.posix.SIG.IGN }, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: oven-sh/bun
Length of output: 390
🏁 Script executed:
Repository: oven-sh/bun
Length of output: 732
🏁 Script executed:
Repository: oven-sh/bun
Length of output: 1236
🏁 Script executed:
Repository: oven-sh/bun
Length of output: 158
BmallocSystemHeapOverride.cppis not being compiled—missinglist(APPEND BUN_CXX_SOURCES ...)directive.The override file is documented in the comment at
cmake/targets/BuildBun.cmake:1270, but it is never added to the build. Add this line after the comment and before theif(DEBUG)block:Without this, the entire override mechanism is non-functional and Windows will continue to use the problematic bmalloc stubs.
🤖 Prompt for AI Agents