Abstract mmap/munmap behind platform guards for WASM and embedded targets#50
Open
lancejpollard wants to merge 1 commit intoHigherOrderCO:mainfrom
Open
Abstract mmap/munmap behind platform guards for WASM and embedded targets#50lancejpollard wants to merge 1 commit intoHigherOrderCO:mainfrom
lancejpollard wants to merge 1 commit intoHigherOrderCO:mainfrom
Conversation
…gets Add #ifdef guards to sys/mmap_anon.c and sys/munmap_anon.c that fall back to calloc/free on platforms without mmap (__EMSCRIPTEN__ or HVM_NO_MMAP). Uses calloc to preserve mmap's zero-initialization guarantee. Existing behavior on POSIX platforms is unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
#ifdefguards tosys/mmap_anon.candsys/munmap_anon.cthat fall back tocalloc/freewhen__EMSCRIPTEN__orHVM_NO_MMAPis defined.Benefits
Why calloc (not malloc)
mmap(MAP_ANONYMOUS)returns zero-initialized pages. Several callers depend on this:uset_inituses the allocation as a bitset (all bits must start clear), and the heap is expected to start zeroed.callocpreserves this guarantee.Note on lazy allocation
mmapwithMAP_NORESERVEallocates virtual address space lazily (physical pages allocated on first access).callocdoes not have this property. On platforms using this fallback,HEAP_CAPshould be reduced to a size that fits in physical memory.Changes
Two files, 29 lines added, 0 removed from existing code paths. The existing mmap code is wrapped in
#elseand is unchanged.clang/sys/mmap_anon.c:calloc(1, bytes)fallbackclang/sys/munmap_anon.c:free(ptr)fallbackRisk
Don't think there are any on existing platforms. Guards only trigger when
__EMSCRIPTEN__orHVM_NO_MMAPis defined. Neither is defined in normal desktop/server builds. All existing tests pass.