Skip to content

[mimalloc] Define MI_FREE_USE_PAGEMAP - #27982

Merged
juj merged 2 commits into
emscripten-core:mainfrom
kleisauke:issue-27978
Oct 15, 2026
Merged

juj merged 2 commits into
emscripten-core:mainfrom
kleisauke:issue-27978

Conversation

@kleisauke

Copy link
Copy Markdown
Collaborator

Upstream commit microsoft/mimalloc@0d17826 renamed MI_OPT_FREE to MI_FREE_USE_PAGEMAP, with its meaning reversed.

As we did not previously define MI_OPT_FREE, define MI_FREE_USE_PAGEMAP instead. This also matches the upstream configuration for WASI targets.

Fixes: #27978

Upstream commit microsoft/mimalloc@0d17826 renamed `MI_OPT_FREE` to
`MI_FREE_USE_PAGEMAP`, with its meaning reversed.

As we did not previously define `MI_OPT_FREE`, define
`MI_FREE_USE_PAGEMAP` instead. This also matches the upstream
configuration for WASI targets.

Fixes: emscripten-core#27978
@kleisauke

Copy link
Copy Markdown
Collaborator Author

This also matches the upstream configuration for WASI targets.

See: https://github.com/microsoft/mimalloc/blob/v3.5.4/CMakeLists.txt#L545-L548.

@kleisauke kleisauke changed the title mimalloc: define MI_FREE_USE_PAGEMAP [mimalloc] Define MI_FREE_USE_PAGEMAP Oct 15, 2026
@juj

juj commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

Nice - glad it turned out to be fixable via a config change.

@juj
juj enabled auto-merge (squash) October 15, 2026 11:36
@sbc100

sbc100 commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

Can you add a a little detail in the PR description as to what the crash in #27978 and how this fixes it?

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm % comment

@sbc100

sbc100 commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

Also, can you add wasmfs.test_wrap_malloc_mimalloc to the subset of wasmfs tests we run in circleci?

@juj
juj merged commit 61b8d61 into emscripten-core:main Oct 15, 2026
42 checks passed
@kleisauke

Copy link
Copy Markdown
Collaborator Author

I think this landed as-is due to auto-merge. The commit message is slightly wrong, as I incorrectly assumed that MI_OPT_FREE was available in mimalloc 3.4.1 (i.e. the version we used before commit 9eca018 landed)

Can you add a a little detail in the PR description as to what the crash in #27978 and how this fixes it?

I'm not exactly sure why this fixes #27978 (or why increasing -sINITIAL_MEMORY works around the issue). This change effectively switches us from a 2-level page map to a flat one. I suspect the 2-level map may not work correctly with Emscripten's default INITIAL_HEAP of 16 MiB. For reference, see:

// use a flat page-map or a 2-level one
#ifndef MI_PAGE_MAP_FLAT
#if MI_MAX_VABITS <= 40 && !defined(__APPLE__) && !MI_SECURE && !MI_FREE_IS_CHECKED && MI_FREE_USE_PAGEMAP
#define MI_PAGE_MAP_FLAT 1
#else
#define MI_PAGE_MAP_FLAT 0
#endif
#endif

and:
// flat page-map committed on demand, using one byte per slice (64 KiB).
// single indirection and low commit, but large initial virtual reserve (4 GiB with 48 bit virtual addresses)
// used by default on <= 40 bit virtual address spaces.

versus:
// 2-level page map:
// double indirection, but low commit and low virtual reserve.
//
// the page-map is usually 4 MiB (for 48 bit virtual addresses) and points to sub maps of 64 KiB.
// the page-map is committed on-demand (in 64 KiB parts) (and sub-maps are committed on-demand as well)
// one sub page-map = 64 KiB => covers 2^(16-3) * 2^16 = 2^29 = 512 MiB address space
// the page-map needs 48-(16+13) = 19 bits => 2^19 sub map pointers = 2^22 bytes = 4 MiB reserved size.

(before commit 9eca018, MI_PAGE_MAP_FLAT was also implicitly set to 1)

Feel free to revert and investigate further, though debugging allocator issues can be very tricky.

@kleisauke
kleisauke deleted the issue-27978 branch October 15, 2026 17:54
@sbc100

sbc100 commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

I think just folloing up with adding wasmfs.test_wrap_malloc_mimalloc to circleci is fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wasmfs.test_wrap_malloc_mimalloc regression

3 participants