Skip to content

Commit 157fcd4

Browse files
authored
Reduce default stack size from 5Mb to 64Kb (#18191)
See ChangeLog.md for rationale.
1 parent bdbf777 commit 157fcd4

16 files changed

+95
-75
lines changed

ChangeLog.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ See docs/process.md for more on how version tagging works.
2222
-----------------------
2323
- Add support for `-sEXPORT_ES6`/`*.mjs` on Node.js. (#17915)
2424
- Idle workers in a PThread pool no longer prevent Node.js app from exiting. (#18227)
25+
- The default `STACK_SIZE` was reduced from 5MB to 64KB. Projects that use more
26+
than 64Kb of stack will now need specify `-sSTACK_SIZE` at link time. For
27+
example, `-sSTACK_SIZE=5MB` can be used to restore the previous behaviour.
28+
To aid in debugging, as of #18154, we now also place the stack first in memory
29+
in debug builds so that overflows will be immediately detected, and result in
30+
runtime errors. This change brings emscripten into line with `wasm-ld` and
31+
wasi-sdk defaults, and also reduces memory usage by default. In general,
32+
WebAssembly stack usage should be lower than on other platforms since a lot of
33+
state normally stored on the stack is hidden within the runtime and does not
34+
occupy linear memory at all. The default for `DEFAULT_PTHREAD_STACK_SIZE` was
35+
also reduced from 2MB to 64KB to match.
2536

2637
3.1.26 - 11/17/22
2738
-----------------
@@ -48,6 +59,7 @@ See docs/process.md for more on how version tagging works.
4859
overflow will trap rather corrupting global data first). This should not
4960
be a user-visible change (unless your program does something very odd such
5061
depending on the specific location of stack data in memory). (#18154)
62+
- Add support for `-sEXPORT_ES6`/`*.mjs` on Node.js. (#17915)
5163

5264
3.1.25 - 11/08/22
5365
-----------------

src/library.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3139,12 +3139,14 @@ mergeInto(LibraryManager.library, {
31393139
#if STACK_OVERFLOW_CHECK
31403140
// Used by wasm-emscripten-finalize to implement STACK_OVERFLOW_CHECK
31413141
__handle_stack_overflow__sig: 'vp',
3142-
__handle_stack_overflow__deps: ['emscripten_stack_get_base'],
3142+
__handle_stack_overflow__deps: ['emscripten_stack_get_base', 'emscripten_stack_get_end', '$ptrToString'],
31433143
__handle_stack_overflow: function(requested) {
31443144
requested = requested >>> 0;
3145+
var base = _emscripten_stack_get_base();
3146+
var end = _emscripten_stack_get_end();
31453147
abort('stack overflow (Attempt to set SP to ' + ptrToString(requested) +
3146-
', with stack limits [' + ptrToString(_emscripten_stack_get_end()) +
3147-
' - ' + ptrToString(_emscripten_stack_get_base()) + '])');
3148+
', with stack limits [' + ptrToString(end) + ' - ' + ptrToString(base) +
3149+
']). If you require more stack space build with -sSTACK_SIZE=<bytes>');
31483150
},
31493151
#endif
31503152

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 STACK_SIZE = 5*1024*1024;
113+
var STACK_SIZE = 64*1024;
114114

115115
// What malloc()/free() to use, out of
116116
// * dlmalloc - a powerful general-purpose malloc
@@ -1595,7 +1595,7 @@ var PTHREAD_POOL_DELAY_LOAD = false;
15951595
// those that have their addresses taken, or ones that are too large to fit as
15961596
// local vars in wasm code.
15971597
// [link]
1598-
var DEFAULT_PTHREAD_STACK_SIZE = 2*1024*1024;
1598+
var DEFAULT_PTHREAD_STACK_SIZE = 64*1024;
15991599

16001600
// True when building with --threadprofiler
16011601
// [link]

test/browser/test_sdl_create_rgb_surface_from.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,24 @@
1212

1313
#define width 600
1414
#define height 450
15+
uint8_t pixels[width * height * 4];
1516

1617
int main() {
17-
18-
uint8_t pixels[width * height * 4];
1918
uint8_t *end = pixels + width * height * 4;
2019
uint8_t *pixel = pixels;
2120
SDL_Rect rect = {0, 0, width, height};
2221

2322
while (pixel != end) {
24-
*pixel = (pixel - pixels) * 256 / (width * height * 4);
25-
pixel++;
23+
*pixel = (pixel - pixels) * 256 / (width * height * 4);
24+
pixel++;
2625
}
2726

2827
SDL_Init(SDL_INIT_VIDEO);
2928
SDL_Surface *screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE);
30-
SDL_Surface *image = SDL_CreateRGBSurfaceFrom(pixels, width, height, 32, width * 4,
31-
0x000000ff,
32-
0x0000ff00,
33-
0x00ff0000,
29+
SDL_Surface *image = SDL_CreateRGBSurfaceFrom(pixels, width, height, 32, width * 4,
30+
0x000000ff,
31+
0x0000ff00,
32+
0x00ff0000,
3433
0xff000000);
3534

3635
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 255, 0, 0));
@@ -40,6 +39,5 @@ int main() {
4039
printf("There should be a red to white gradient\n");
4140

4241
SDL_Quit();
43-
4442
return 0;
4543
}

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 : 27176960
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: 106971712
4-
free_dynamic_memory: 4211104
1+
valid allocs: 1
2+
emmalloc_validate_memory_regions: 0
3+
emmalloc_dynamic_heap_size : 106971712
4+
emmalloc_free_dynamic_memory : 4211104
55
numFreeMemoryRegions: 3
66
0 0 0 0 0 0 0 0 1 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 : 27176960

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: 2142175232
5-
sbrk 0: 0x501000
4+
unclaimed heap memory 0: 2147352576
5+
sbrk 0: 0x11000
66
1
77
dynamic heap 1: 37752832
88
free dynamic memory 1: 4096
9-
unclaimed heap memory 1: 2104426496
10-
sbrk 1: 0x2901000
9+
unclaimed heap memory 1: 2109603840
10+
sbrk 1: 0x2411000
1111
1st trim: 1
1212
dynamic heap 1: 37752832
1313
free dynamic memory 1: 0
14-
unclaimed heap memory 1: 2104426496
15-
sbrk 1: 0x2901000
14+
unclaimed heap memory 1: 2109603840
15+
sbrk 1: 0x2411000
1616
2nd trim: 0
1717
dynamic heap 2: 37752832
1818
free dynamic memory 2: 0
19-
unclaimed heap memory 2: 2104426496
20-
sbrk 2: 0x2901000
19+
unclaimed heap memory 2: 2109603840
20+
sbrk 2: 0x2411000
2121
3rd trim: 1
2222
dynamic heap 3: 33656832
2323
free dynamic memory 3: 102400
24-
unclaimed heap memory 3: 2104426496
25-
sbrk 3: 0x2901000
24+
unclaimed heap memory 3: 2109603840
25+
sbrk 3: 0x2411000
2626
4th trim: 0
2727
dynamic heap 4: 33656832
2828
free dynamic memory 4: 102400
29-
unclaimed heap memory 4: 2104426496
30-
sbrk 4: 0x2901000
29+
unclaimed heap memory 4: 2109603840
30+
sbrk 4: 0x2411000
3131
5th trim: 1
3232
dynamic heap 5: 33558528
3333
free dynamic memory 5: 0
34-
unclaimed heap memory 5: 2104426496
35-
sbrk 5: 0x2901000
34+
unclaimed heap memory 5: 2109603840
35+
sbrk 5: 0x2411000

test/core/test_memcpy3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
int main() {
2727
#define RUN(type) \
2828
{ \
29-
type buffer[TOTAL]; \
29+
static type buffer[TOTAL]; \
3030
volatile int seed = 123; \
3131
TEST(1, type); \
3232
TEST(2, type); \
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: 96600064 (increase: 79822848 bytes)

0 commit comments

Comments
 (0)