Skip to content

Commit d82bfd4

Browse files
committed
Reduce default stack size from 5Mb to 1Mb
See ChangeLog.md for rationale.
1 parent 68a9f99 commit d82bfd4

File tree

7 files changed

+56
-47
lines changed

7 files changed

+56
-47
lines changed

ChangeLog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,14 @@ See docs/process.md for more on how version tagging works.
601601

602602
2.0.21: 05/18/2021
603603
------------------
604+
------
605+
- Default shadow stack size used by emscripten reduced from 5Mb to 1Mb. Anyone
606+
running into issues can restore the old size using `-sTOTAL_STACK=5Mb`. Since
607+
windows has max stack of 1Mb and since WebAssembly stack usage should be less
608+
than on native platforms (since its only used for address taken values) it
609+
seems like 1Mb might still be on the high side here. For reference, llvm
610+
(`wasm-ld`) use 64kb as the default stack size. `DEFAULT_PTHREAD_STACK_SIZE`
611+
was also reduced from 2Mb to 1Mb to match primary stack.
604612
- Options such as EXPORTED_FUNCTIONS that can take a response file containing
605613
list of symbols can now use a simple one-symbol-per-line format. This new
606614
format is much simpler and doesn't require commas between symbols, opening

src/settings.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ var MEM_INIT_METHOD = false;
110110
// assertions are on, we will assert on not exceeding this, otherwise,
111111
// it will fail silently.
112112
// [link]
113-
var TOTAL_STACK = 5*1024*1024;
113+
var TOTAL_STACK = 1*1024*1024;
114114

115115
// What malloc()/free() to use, out of
116116
// * dlmalloc - a powerful general-purpose malloc
@@ -1582,7 +1582,7 @@ var PTHREAD_POOL_DELAY_LOAD = false;
15821582
// those that have their addresses taken, or ones that are too large to fit as
15831583
// local vars in wasm code.
15841584
// [link]
1585-
var DEFAULT_PTHREAD_STACK_SIZE = 2*1024*1024;
1585+
var DEFAULT_PTHREAD_STACK_SIZE = 1*1024*1024;
15861586

15871587
// True when building with --threadprofiler
15881588
// [link]

test/core/test_emmalloc_memory_statistics.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22
#include <emscripten/emmalloc.h>
33

44
template<typename T>
5-
T round_to_4k(T val) {
6-
return (T)(((size_t)val + 4095) & ~4095);
5+
T round_to_4k(T val){
6+
return (T)(((size_t)val + 4095) & ~4095);
77
}
88

9-
int main() {
10-
void *ptr = malloc(32*1024*1024);
11-
void *ptr2 = malloc(4*1024*1024);
12-
void *ptr3 = malloc(64*1024*1024);
13-
void *ptr4 = malloc(16*1024);
14-
void *ptr5 = malloc(2*1024*1024);
15-
printf("%ld\n", (long)(ptr && ptr2 && ptr3 && ptr4 && ptr5));
16-
free(ptr2);
17-
free(ptr4);
18-
printf("validate_memory_regions: %d\n", emmalloc_validate_memory_regions());
19-
printf("dynamic_heap_size: %zu\n", emmalloc_dynamic_heap_size());
20-
printf("free_dynamic_memory: %zu\n", emmalloc_free_dynamic_memory());
21-
size_t numFreeMemoryRegions = 0;
22-
size_t freeMemorySizeMap[32];
23-
numFreeMemoryRegions = emmalloc_compute_free_dynamic_memory_fragmentation_map(freeMemorySizeMap);
24-
printf("numFreeMemoryRegions: %zu\n", numFreeMemoryRegions);
25-
for (int i = 0; i < 32; ++i) {
26-
printf("%zu ", freeMemorySizeMap[i]);
27-
}
28-
printf("\n");
29-
printf("unclaimed_heap_memory: %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory()));
30-
return 0;
9+
int main()
10+
{
11+
void *ptr = malloc(32*1024*1024);
12+
void *ptr2 = malloc(4*1024*1024);
13+
void *ptr3 = malloc(64*1024*1024);
14+
void *ptr4 = malloc(16*1024);
15+
void *ptr5 = malloc(2*1024*1024);
16+
printf("valid allocs: %d\n", (int)(ptr && ptr2 && ptr3 && ptr4 && ptr5));
17+
free(ptr2);
18+
free(ptr4);
19+
printf("emmalloc_validate_memory_regions: %d\n", emmalloc_validate_memory_regions());
20+
printf("emmalloc_dynamic_heap_size : %zu\n", emmalloc_dynamic_heap_size());
21+
printf("emmalloc_free_dynamic_memory : %zu\n", emmalloc_free_dynamic_memory());
22+
size_t numFreeMemoryRegions = 0;
23+
size_t freeMemorySizeMap[32];
24+
numFreeMemoryRegions = emmalloc_compute_free_dynamic_memory_fragmentation_map(freeMemorySizeMap);
25+
printf("numFreeMemoryRegions: %zu\n", numFreeMemoryRegions);
26+
for(int i = 0; i < 32; ++i)
27+
printf("%zu ", freeMemorySizeMap[i]);
28+
printf("\n");
29+
printf("emmalloc_unclaimed_heap_memory : %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory()));
3130
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
1
2-
validate_memory_regions: 0
3-
dynamic_heap_size: 106971424
4-
free_dynamic_memory: 4210892
1+
valid allocs: 1
2+
emmalloc_validate_memory_regions: 0
3+
emmalloc_dynamic_heap_size : 106971424
4+
emmalloc_free_dynamic_memory : 4210892
55
numFreeMemoryRegions: 3
66
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
7-
unclaimed_heap_memory: 21999616
7+
emmalloc_unclaimed_heap_memory : 26193920

test/core/test_emmalloc_trim.out

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
heap size: 134217728
22
dynamic heap 0: 4096
33
free dynamic memory 0: 4096
4-
unclaimed heap memory 0: 2142171136
5-
sbrk 0: 0x502000
4+
unclaimed heap memory 0: 2146365440
5+
sbrk 0: 0x102000
66
1
77
dynamic heap 1: 37752832
88
free dynamic memory 1: 4096
9-
unclaimed heap memory 1: 2104422400
10-
sbrk 1: 0x2902000
9+
unclaimed heap memory 1: 2108616704
10+
sbrk 1: 0x2502000
1111
1st trim: 1
1212
dynamic heap 1: 37752832
1313
free dynamic memory 1: 0
14-
unclaimed heap memory 1: 2104422400
15-
sbrk 1: 0x2902000
14+
unclaimed heap memory 1: 2108616704
15+
sbrk 1: 0x2502000
1616
2nd trim: 0
1717
dynamic heap 2: 37752832
1818
free dynamic memory 2: 0
19-
unclaimed heap memory 2: 2104422400
20-
sbrk 2: 0x2902000
19+
unclaimed heap memory 2: 2108616704
20+
sbrk 2: 0x2502000
2121
3rd trim: 1
2222
dynamic heap 3: 33656832
2323
free dynamic memory 3: 102400
24-
unclaimed heap memory 3: 2104422400
25-
sbrk 3: 0x2902000
24+
unclaimed heap memory 3: 2108616704
25+
sbrk 3: 0x2502000
2626
4th trim: 0
2727
dynamic heap 4: 33656832
2828
free dynamic memory 4: 102400
29-
unclaimed heap memory 4: 2104422400
30-
sbrk 4: 0x2902000
29+
unclaimed heap memory 4: 2108616704
30+
sbrk 4: 0x2502000
3131
5th trim: 1
3232
dynamic heap 5: 33558528
3333
free dynamic memory 5: 0
34-
unclaimed heap memory 5: 2104422400
35-
sbrk 5: 0x2902000
34+
unclaimed heap memory 5: 2108616704
35+
sbrk 5: 0x2502000
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Heap size before allocation: 16777216
2-
Ptr: 1, value: 16843009, Heap size now: 101777408 (increase: 85000192 bytes)
2+
Ptr: 1, value: 16843009, Heap size now: 97583104 (increase: 80805888 bytes)

test/test_other.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2605,6 +2605,8 @@ def test_embind(self, extra_args):
26052605
'--pre-js', test_file('embind/test.pre.js'),
26062606
'--post-js', test_file('embind/test.post.js'),
26072607
'-sWASM_ASYNC_COMPILATION=0',
2608+
# For some reason the default of 1Mb is not enough for these tests.
2609+
'-sTOTAL_STACK=2Mb',
26082610
'-sIN_TEST_HARNESS'] + args)
26092611

26102612
if '-sDYNAMIC_EXECUTION=0' in args:
@@ -5748,7 +5750,7 @@ def test_massive_alloc(self, wasm):
57485750
# just care about message regarding allocating over 1GB of memory
57495751
output = self.run_js('a.out.js')
57505752
if not wasm:
5751-
self.assertContained('Warning: Enlarging memory arrays, this is not fast! 16777216,1473314816\n', output)
5753+
self.assertContained('Warning: Enlarging memory arrays, this is not fast! 16777216,1469120512\n', output)
57525754

57535755
def test_failing_alloc(self):
57545756
for pre_fail, post_fail, opts in [

0 commit comments

Comments
 (0)