Repository navigation
[wasm64] Fix 32-bit size limits in emmalloc
#28035
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
Open
sbc100
wants to merge
1
commit into
emscripten-core:main
Choose a base branch
from
sbc100:fix_emmalloc_wasm64
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,17 +91,21 @@ static_assert(alignof(max_align_t) == 8, "max_align_t must be correct"); | |
|
|
||
| // Dynamic memory is subdivided into regions, in the format | ||
|
|
||
| // <size:uint32_t> ..... <size:uint32_t> | <size:uint32_t> ..... <size:uint32_t> | <size:uint32_t> ..... <size:uint32_t> | ..... | ||
| // <size:size_t> ..... <size:size_t> | <size:size_t> ..... <size:size_t> | <size:size_t> ..... <size:size_t> | ..... | ||
|
|
||
| // That is, at the bottom and top end of each memory region, the size of that region is stored. That allows traversing the | ||
| // memory regions backwards and forwards. Because each allocation must be at least a multiple of 4 bytes, the lowest two bits of | ||
| // each size field is unused. Free regions are distinguished from used regions by having the FREE_REGION_FLAG bit present | ||
| // in the size field. I.e. for free regions, the size field is odd, and for used regions, the size field reads even. | ||
| #define FREE_REGION_FLAG 0x1u | ||
| #define FREE_REGION_FLAG ((size_t)1) | ||
|
|
||
| // Attempts to malloc() more than this many bytes would cause an overflow when calculating the size of a region, | ||
| // therefore allocations larger than this are short-circuited immediately on entry. | ||
| #ifdef __wasm64__ | ||
| #define MAX_ALLOC_SIZE 0xFFFFFFFFFFull | ||
| #else | ||
| #define MAX_ALLOC_SIZE 0xFFFFFFC7u | ||
| #endif | ||
|
|
||
| // A free region has the following structure: | ||
| // <size:size_t> <prevptr> <nextptr> ... <size:size_t> | ||
|
|
@@ -119,7 +123,7 @@ typedef struct Region { | |
| // list of all RootRegions in the program, starting with `listOfAllRegions` | ||
| // below. | ||
| typedef struct RootRegion { | ||
| uint32_t size; | ||
| size_t size; | ||
| struct RootRegion *next; | ||
| uint8_t* endPtr; | ||
| } RootRegion; | ||
|
|
@@ -139,9 +143,9 @@ static volatile uint8_t multithreadingLock = 0; | |
| #endif | ||
|
|
||
| #define IS_POWER_OF_2(val) (((val) & ((val)-1)) == 0) | ||
| #define ALIGN_UP(ptr, alignment) ((uint8_t*)((((uintptr_t)(ptr)) + ((alignment)-1)) & ~((alignment)-1))) | ||
| #define ALIGN_DOWN(ptr, alignment) ((uint8_t*)(((uintptr_t)(ptr)) & ~((alignment)-1))) | ||
| #define HAS_ALIGNMENT(ptr, alignment) ((((uintptr_t)(ptr)) & ((alignment)-1)) == 0) | ||
| #define ALIGN_UP(ptr, alignment) ((uint8_t*)((((uintptr_t)(ptr)) + ((alignment)-1)) & ~((uintptr_t)(alignment)-1))) | ||
| #define ALIGN_DOWN(ptr, alignment) ((uint8_t*)(((uintptr_t)(ptr)) & ~((uintptr_t)(alignment)-1))) | ||
| #define HAS_ALIGNMENT(ptr, alignment) ((((uintptr_t)(ptr)) & ((uintptr_t)(alignment)-1)) == 0) | ||
|
|
||
| static_assert(IS_POWER_OF_2(MALLOC_ALIGNMENT), "MALLOC_ALIGNMENT must be a power of two value!"); | ||
| static_assert(MALLOC_ALIGNMENT >= 4, "Smallest possible MALLOC_ALIGNMENT if 4!"); | ||
|
|
@@ -244,11 +248,12 @@ an allocation size to the bucket index that should be looked at. The buckets are | |
| static_assert(NUM_FREE_BUCKETS == 64, "Following function is tailored specifically for NUM_FREE_BUCKETS == 64 case"); | ||
| static int compute_free_list_bucket(size_t allocSize) { | ||
| if (allocSize < 128) return (allocSize >> 3) - 1; | ||
| if (allocSize >= (1U << 27)) return NUM_FREE_BUCKETS - 1; | ||
| int clz = __builtin_clz(allocSize); | ||
| int bucketIndex = | ||
| (clz > 19) | ||
| ? 110 - (clz<<2) + ((allocSize >> (29-clz)) ^ 4) | ||
| : MIN( 71 - (clz<<1) + ((allocSize >> (30-clz)) ^ 2), NUM_FREE_BUCKETS-1); | ||
| ? 110 - (clz<<2) + ((allocSize >> (29-clz)) ^ 4) | ||
| : 71 - (clz<<1) + ((allocSize >> (30-clz)) ^ 2); | ||
|
Collaborator
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. This looks like code size neutral change, lgtm. (There might be a way to squeeze out the |
||
|
|
||
| assert(bucketIndex >= 0); | ||
| assert(bucketIndex < NUM_FREE_BUCKETS); | ||
|
|
@@ -1406,7 +1411,9 @@ size_t emmalloc_compute_free_dynamic_memory_fragmentation_map(size_t freeMemoryS | |
| freeRegion = freeRegion->next) { | ||
| ++numFreeMemoryRegions; | ||
| size_t freeDynamicMemory = freeRegion->size - REGION_HEADER_SIZE; | ||
| if (freeDynamicMemory > 0) { | ||
| if (freeDynamicMemory >= (1ull << 31)) { | ||
| ++freeMemorySizeMap[31]; | ||
| } else if (freeDynamicMemory > 0) { | ||
| ++freeMemorySizeMap[31-__builtin_clz(freeDynamicMemory)]; | ||
| } else { | ||
| ++freeMemorySizeMap[0]; | ||
|
|
||
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
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.