From d82bfd4092e710e04f7ee569586ef98fba0b4b4f Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 13 May 2021 11:22:35 -0700 Subject: [PATCH] Reduce default stack size from 5Mb to 1Mb See ChangeLog.md for rationale. --- ChangeLog.md | 8 ++++ src/settings.js | 4 +- test/core/test_emmalloc_memory_statistics.cpp | 47 +++++++++---------- test/core/test_emmalloc_memory_statistics.out | 10 ++-- test/core/test_emmalloc_trim.out | 28 +++++------ .../core/test_memorygrowth_geometric_step.out | 2 +- test/test_other.py | 4 +- 7 files changed, 56 insertions(+), 47 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 25ebe0c2546a9..f2437287eb61d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -601,6 +601,14 @@ See docs/process.md for more on how version tagging works. 2.0.21: 05/18/2021 ------------------ +------ +- Default shadow stack size used by emscripten reduced from 5Mb to 1Mb. Anyone + running into issues can restore the old size using `-sTOTAL_STACK=5Mb`. Since + windows has max stack of 1Mb and since WebAssembly stack usage should be less + than on native platforms (since its only used for address taken values) it + seems like 1Mb might still be on the high side here. For reference, llvm + (`wasm-ld`) use 64kb as the default stack size. `DEFAULT_PTHREAD_STACK_SIZE` + was also reduced from 2Mb to 1Mb to match primary stack. - Options such as EXPORTED_FUNCTIONS that can take a response file containing list of symbols can now use a simple one-symbol-per-line format. This new format is much simpler and doesn't require commas between symbols, opening diff --git a/src/settings.js b/src/settings.js index 3b6536569b916..eabdc38856a3d 100644 --- a/src/settings.js +++ b/src/settings.js @@ -110,7 +110,7 @@ var MEM_INIT_METHOD = false; // assertions are on, we will assert on not exceeding this, otherwise, // it will fail silently. // [link] -var TOTAL_STACK = 5*1024*1024; +var TOTAL_STACK = 1*1024*1024; // What malloc()/free() to use, out of // * dlmalloc - a powerful general-purpose malloc @@ -1582,7 +1582,7 @@ var PTHREAD_POOL_DELAY_LOAD = false; // those that have their addresses taken, or ones that are too large to fit as // local vars in wasm code. // [link] -var DEFAULT_PTHREAD_STACK_SIZE = 2*1024*1024; +var DEFAULT_PTHREAD_STACK_SIZE = 1*1024*1024; // True when building with --threadprofiler // [link] diff --git a/test/core/test_emmalloc_memory_statistics.cpp b/test/core/test_emmalloc_memory_statistics.cpp index 3e76af71705e4..42750a3109a87 100644 --- a/test/core/test_emmalloc_memory_statistics.cpp +++ b/test/core/test_emmalloc_memory_statistics.cpp @@ -2,30 +2,29 @@ #include template -T round_to_4k(T val) { - return (T)(((size_t)val + 4095) & ~4095); +T round_to_4k(T val){ + return (T)(((size_t)val + 4095) & ~4095); } -int main() { - void *ptr = malloc(32*1024*1024); - void *ptr2 = malloc(4*1024*1024); - void *ptr3 = malloc(64*1024*1024); - void *ptr4 = malloc(16*1024); - void *ptr5 = malloc(2*1024*1024); - printf("%ld\n", (long)(ptr && ptr2 && ptr3 && ptr4 && ptr5)); - free(ptr2); - free(ptr4); - printf("validate_memory_regions: %d\n", emmalloc_validate_memory_regions()); - printf("dynamic_heap_size: %zu\n", emmalloc_dynamic_heap_size()); - printf("free_dynamic_memory: %zu\n", emmalloc_free_dynamic_memory()); - size_t numFreeMemoryRegions = 0; - size_t freeMemorySizeMap[32]; - numFreeMemoryRegions = emmalloc_compute_free_dynamic_memory_fragmentation_map(freeMemorySizeMap); - printf("numFreeMemoryRegions: %zu\n", numFreeMemoryRegions); - for (int i = 0; i < 32; ++i) { - printf("%zu ", freeMemorySizeMap[i]); - } - printf("\n"); - printf("unclaimed_heap_memory: %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory())); - return 0; +int main() +{ + void *ptr = malloc(32*1024*1024); + void *ptr2 = malloc(4*1024*1024); + void *ptr3 = malloc(64*1024*1024); + void *ptr4 = malloc(16*1024); + void *ptr5 = malloc(2*1024*1024); + printf("valid allocs: %d\n", (int)(ptr && ptr2 && ptr3 && ptr4 && ptr5)); + free(ptr2); + free(ptr4); + printf("emmalloc_validate_memory_regions: %d\n", emmalloc_validate_memory_regions()); + printf("emmalloc_dynamic_heap_size : %zu\n", emmalloc_dynamic_heap_size()); + printf("emmalloc_free_dynamic_memory : %zu\n", emmalloc_free_dynamic_memory()); + size_t numFreeMemoryRegions = 0; + size_t freeMemorySizeMap[32]; + numFreeMemoryRegions = emmalloc_compute_free_dynamic_memory_fragmentation_map(freeMemorySizeMap); + printf("numFreeMemoryRegions: %zu\n", numFreeMemoryRegions); + for(int i = 0; i < 32; ++i) + printf("%zu ", freeMemorySizeMap[i]); + printf("\n"); + printf("emmalloc_unclaimed_heap_memory : %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory())); } diff --git a/test/core/test_emmalloc_memory_statistics.out b/test/core/test_emmalloc_memory_statistics.out index 85b343f92d679..23722c07fe909 100644 --- a/test/core/test_emmalloc_memory_statistics.out +++ b/test/core/test_emmalloc_memory_statistics.out @@ -1,7 +1,7 @@ -1 -validate_memory_regions: 0 -dynamic_heap_size: 106971424 -free_dynamic_memory: 4210892 +valid allocs: 1 +emmalloc_validate_memory_regions: 0 +emmalloc_dynamic_heap_size : 106971424 +emmalloc_free_dynamic_memory : 4210892 numFreeMemoryRegions: 3 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 -unclaimed_heap_memory: 21999616 +emmalloc_unclaimed_heap_memory : 26193920 diff --git a/test/core/test_emmalloc_trim.out b/test/core/test_emmalloc_trim.out index daa3c04959d7d..5bdeb755a6ca6 100644 --- a/test/core/test_emmalloc_trim.out +++ b/test/core/test_emmalloc_trim.out @@ -1,35 +1,35 @@ heap size: 134217728 dynamic heap 0: 4096 free dynamic memory 0: 4096 -unclaimed heap memory 0: 2142171136 -sbrk 0: 0x502000 +unclaimed heap memory 0: 2146365440 +sbrk 0: 0x102000 1 dynamic heap 1: 37752832 free dynamic memory 1: 4096 -unclaimed heap memory 1: 2104422400 -sbrk 1: 0x2902000 +unclaimed heap memory 1: 2108616704 +sbrk 1: 0x2502000 1st trim: 1 dynamic heap 1: 37752832 free dynamic memory 1: 0 -unclaimed heap memory 1: 2104422400 -sbrk 1: 0x2902000 +unclaimed heap memory 1: 2108616704 +sbrk 1: 0x2502000 2nd trim: 0 dynamic heap 2: 37752832 free dynamic memory 2: 0 -unclaimed heap memory 2: 2104422400 -sbrk 2: 0x2902000 +unclaimed heap memory 2: 2108616704 +sbrk 2: 0x2502000 3rd trim: 1 dynamic heap 3: 33656832 free dynamic memory 3: 102400 -unclaimed heap memory 3: 2104422400 -sbrk 3: 0x2902000 +unclaimed heap memory 3: 2108616704 +sbrk 3: 0x2502000 4th trim: 0 dynamic heap 4: 33656832 free dynamic memory 4: 102400 -unclaimed heap memory 4: 2104422400 -sbrk 4: 0x2902000 +unclaimed heap memory 4: 2108616704 +sbrk 4: 0x2502000 5th trim: 1 dynamic heap 5: 33558528 free dynamic memory 5: 0 -unclaimed heap memory 5: 2104422400 -sbrk 5: 0x2902000 \ No newline at end of file +unclaimed heap memory 5: 2108616704 +sbrk 5: 0x2502000 diff --git a/test/core/test_memorygrowth_geometric_step.out b/test/core/test_memorygrowth_geometric_step.out index 4cb463c65374c..589e07e47e08f 100644 --- a/test/core/test_memorygrowth_geometric_step.out +++ b/test/core/test_memorygrowth_geometric_step.out @@ -1,2 +1,2 @@ Heap size before allocation: 16777216 -Ptr: 1, value: 16843009, Heap size now: 101777408 (increase: 85000192 bytes) \ No newline at end of file +Ptr: 1, value: 16843009, Heap size now: 97583104 (increase: 80805888 bytes) diff --git a/test/test_other.py b/test/test_other.py index 0aac2b3f142bc..268a2e9a76860 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -2605,6 +2605,8 @@ def test_embind(self, extra_args): '--pre-js', test_file('embind/test.pre.js'), '--post-js', test_file('embind/test.post.js'), '-sWASM_ASYNC_COMPILATION=0', + # For some reason the default of 1Mb is not enough for these tests. + '-sTOTAL_STACK=2Mb', '-sIN_TEST_HARNESS'] + args) if '-sDYNAMIC_EXECUTION=0' in args: @@ -5748,7 +5750,7 @@ def test_massive_alloc(self, wasm): # just care about message regarding allocating over 1GB of memory output = self.run_js('a.out.js') if not wasm: - self.assertContained('Warning: Enlarging memory arrays, this is not fast! 16777216,1473314816\n', output) + self.assertContained('Warning: Enlarging memory arrays, this is not fast! 16777216,1469120512\n', output) def test_failing_alloc(self): for pre_fail, post_fail, opts in [