forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix different memory allocation issues (emscripten-core#13442)
* Fix different memory allocation issues: 1. In library.js, my earlier PR gated >>> 0 to CAN_ADDRESS_2GB builds only. That was troublesome, since if one does malloc(-1), it would no longer be caught (malloc(0xFFFFFFFFull) can never succeed). Restored the >>> 0. 2. Fix STACK_OVERFLOW_CHECK=2 mode to work with MINIMAL_RUNTIME. The call to ___set_stack_limits() was misplaced in MINIMAL_RUNTIME postamble. 3. Fix issue with sbrk() not being able to handle signed 32-bit integer overflow in sbrk limit growth, leading to erroneously reporting succeeding massive 2GB heap grow operations. This causes a breaking change to emscripten/heap.h emscripten_get_sbrk_ptr() signature, which will now return a uintptr_t instead of intptr_t. Not worried much since it will lead to clear build error, and there are extremely few users of that function. 4. Fix issues with passing ridiculously large (or small negative) alloc sizes to emmalloc functions (>0xFFFFFFC7u). Add assertions to validate memory overflows in emmalloc. 5. Fix a crash with emmalloc when attempting to malloc() memory when there are absolutely zero bytes available in the heap. (not a single free memory region left) 6. Add a test. 7. Misc comment updates. * flake * Fix validate_alloc_size * Address review. * Add dlmalloc_test_large.c * Remove test line * Separate to new test
- Loading branch information
Showing
15 changed files
with
161 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <emscripten/heap.h> | ||
|
||
uint64_t nextAllocationSize = 16*1024*1024; | ||
bool allocHasFailed = false; | ||
|
||
void grow_memory() | ||
{ | ||
uint8_t *ptr = (uint8_t*)malloc((size_t)nextAllocationSize); | ||
EM_ASM({}, ptr); // Pass ptr out to confuse LLVM that it is used, so it won't optimize it away in -O1 and higher. | ||
size_t heapSize = emscripten_get_heap_size(); | ||
printf("Allocated %zu: %d. Heap size: %zu\n", (size_t)nextAllocationSize, ptr ? 1 : 0, heapSize); | ||
if (ptr) | ||
{ | ||
if (!allocHasFailed) | ||
{ | ||
nextAllocationSize *= 2; | ||
// Make sure we don't overflow, and also exercise malloc(-1) to gracefully return 0 in ABORTING_MALLOC=0 mode. | ||
if (nextAllocationSize > 0xFFFFFFFFULL) | ||
nextAllocationSize = 0xFFFFFFFFULL; | ||
} | ||
} | ||
else | ||
{ | ||
nextAllocationSize /= 2; | ||
allocHasFailed = true; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
// Exhaust all available memory. | ||
for(int i = 0; i < 50; ++i) | ||
grow_memory(); | ||
// If we get this far without crashing on OOM, we are ok! | ||
printf("Test finished!\n"); | ||
#ifdef REPORT_RESULT | ||
REPORT_RESULT(0); | ||
#endif | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
{ | ||
"a.html": 588, | ||
"a.html.gz": 386, | ||
"a.js": 20232, | ||
"a.js.gz": 7961, | ||
"a.js": 20432, | ||
"a.js.gz": 8018, | ||
"a.mem": 3171, | ||
"a.mem.gz": 2715, | ||
"total": 23991, | ||
"total_gz": 11062 | ||
"total": 24191, | ||
"total_gz": 11119 | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
{ | ||
"a.html": 588, | ||
"a.html.gz": 386, | ||
"a.js": 19717, | ||
"a.js.gz": 7812, | ||
"a.js": 19917, | ||
"a.js.gz": 7870, | ||
"a.mem": 3171, | ||
"a.mem.gz": 2715, | ||
"total": 23476, | ||
"total_gz": 10913 | ||
"total": 23676, | ||
"total_gz": 10971 | ||
} |
This file contains 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.