Skip to content

Commit 72c3c73

Browse files
committed
Reduce default stack size from 5Mb to 64Kb
See ChangeLog.md for rationale.
1 parent f172b22 commit 72c3c73

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)

test/core/test_memset.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); \

test/fs/test_mmap.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,12 @@ void test_mmap_shared_with_offset() {
180180
assert(fd >= 0);
181181
size_t offset = sysconf(_SC_PAGE_SIZE) * 2;
182182

183-
char buffer[offset + 33];
184-
memset(buffer, 0, offset + 33);
185-
fread(buffer, 1, offset + 32, fd);
183+
char buffer[33];
184+
memset(buffer, 0, 33);
185+
fseek(fd, offset, SEEK_SET);
186+
fread(buffer, 1, 32, fd);
186187
// expect text written from mmap operation to appear at offset in the file
187-
printf("yolo/sharedoffset.txt content=%s %zu\n", buffer + offset, offset);
188+
printf("yolo/sharedoffset.txt content=%s %zu\n", buffer, offset);
188189
fclose(fd);
189190
}
190191
}

test/pthread/test_pthread_create_pthread.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ static void *thread1_start(void *arg)
3232
return NULL;
3333
}
3434

35+
#define DEFAULT_STACK_SIZE (64*1024)
3536
int main()
3637
{
3738
pthread_t thr;
@@ -43,7 +44,7 @@ int main()
4344
void *stack_addr;
4445
pthread_attr_getstack(&attr, &stack_addr, &stack_size);
4546
printf("stack_size: %d, stack_addr: %p\n", (int)stack_size, stack_addr);
46-
if (stack_size != 2*1024*1024 || stack_addr == NULL)
47+
if (stack_size != DEFAULT_STACK_SIZE || stack_addr == NULL)
4748
result = -100; // Report failure.
4849

4950
pthread_join(thr, NULL);

test/test_browser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,9 @@ def test_webgl_context_attributes(self):
12831283
temp_filepath = os.path.basename(filepath)
12841284
shutil.copyfile(filepath, temp_filepath)
12851285

1286+
# testAntiAliasing uses a window-sized buffer on the stack
1287+
self.set_setting('STACK_SIZE', '1MB')
1288+
12861289
# perform tests with attributes activated
12871290
self.btest_exit('test_webgl_context_attributes_glut.c', args=['--js-library', 'check_webgl_attributes_support.js', '-DAA_ACTIVATED', '-DDEPTH_ACTIVATED', '-DSTENCIL_ACTIVATED', '-DALPHA_ACTIVATED', '-lGL', '-lglut', '-lGLEW'])
12881291
self.btest_exit('test_webgl_context_attributes_sdl.c', args=['--js-library', 'check_webgl_attributes_support.js', '-DAA_ACTIVATED', '-DDEPTH_ACTIVATED', '-DSTENCIL_ACTIVATED', '-DALPHA_ACTIVATED', '-lGL', '-lSDL', '-lGLEW'])
@@ -3316,7 +3319,7 @@ def test_async_2(self):
33163319
# Error.stackTraceLimit default to 10 in chrome but this test relies on more
33173320
# than 40 stack frames being reported.
33183321
create_file('pre.js', 'Error.stackTraceLimit = 80;\n')
3319-
self.btest_exit('browser/async_2.cpp', args=['-O3', '--pre-js', 'pre.js', '-sASYNCIFY'])
3322+
self.btest_exit('browser/async_2.cpp', args=['-O3', '--pre-js', 'pre.js', '-sASYNCIFY', '-sSTACK_SIZE=1MB'])
33203323

33213324
def test_async_virtual(self):
33223325
for opts in [0, 3]:

test/test_core.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5333,6 +5333,7 @@ def test_printf(self):
53335333
# needs to flush stdio streams
53345334
self.emcc_args.append('-Wno-format')
53355335
self.set_setting('EXIT_RUNTIME')
5336+
self.set_setting('STACK_SIZE', '1MB')
53365337
self.do_run_in_out_file_test('printf/test.c')
53375338

53385339
def test_printf_2(self):
@@ -6608,7 +6609,7 @@ def test_sse2(self):
66086609
self.run_process([shared.CLANG_CXX, src, '-msse2', '-Wno-argument-outside-range', '-o', 'test_sse2', '-D_CRT_SECURE_NO_WARNINGS=1'] + clang_native.get_clang_native_args(), stdout=PIPE)
66096610
native_result = self.run_process('./test_sse2', stdout=PIPE).stdout
66106611

6611-
self.emcc_args += ['-I' + test_file('sse'), '-msse2', '-Wno-argument-outside-range']
6612+
self.emcc_args += ['-I' + test_file('sse'), '-msse2', '-Wno-argument-outside-range', '-sSTACK_SIZE=1MB']
66126613
self.maybe_closure()
66136614
self.do_runf(src, native_result)
66146615

@@ -6652,7 +6653,7 @@ def test_sse4_1(self):
66526653
self.run_process([shared.CLANG_CXX, src, '-msse4.1', '-Wno-argument-outside-range', '-o', 'test_sse4_1', '-D_CRT_SECURE_NO_WARNINGS=1'] + clang_native.get_clang_native_args(), stdout=PIPE)
66536654
native_result = self.run_process('./test_sse4_1', stdout=PIPE).stdout
66546655

6655-
self.emcc_args += ['-I' + test_file('sse'), '-msse4.1', '-Wno-argument-outside-range']
6656+
self.emcc_args += ['-I' + test_file('sse'), '-msse4.1', '-Wno-argument-outside-range', '-sSTACK_SIZE=1MB']
66566657
self.maybe_closure()
66576658
self.do_runf(src, native_result)
66586659

@@ -6683,7 +6684,7 @@ def test_avx(self):
66836684
self.run_process([shared.CLANG_CXX, src, '-mavx', '-Wno-argument-outside-range', '-o', 'test_avx', '-D_CRT_SECURE_NO_WARNINGS=1'] + clang_native.get_clang_native_args(), stdout=PIPE)
66846685
native_result = self.run_process('./test_avx', stdout=PIPE).stdout
66856686

6686-
self.emcc_args += ['-I' + test_file('sse'), '-mavx', '-Wno-argument-outside-range']
6687+
self.emcc_args += ['-I' + test_file('sse'), '-mavx', '-Wno-argument-outside-range', '-sSTACK_SIZE=1MB']
66876688
self.maybe_closure()
66886689
self.do_runf(src, native_result)
66896690

test/test_other.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,9 @@ def test_embind(self, extra_args):
26522652
'--pre-js', test_file('embind/test.pre.js'),
26532653
'--post-js', test_file('embind/test.post.js'),
26542654
'-sWASM_ASYNC_COMPILATION=0',
2655+
# This test uses a `CustomSmartPtr` class which has 1MB of data embedded in
2656+
# it which means we need more stack space than normal.
2657+
'-sTOTAL_STACK=2MB',
26552658
'-sIN_TEST_HARNESS'] + args)
26562659

26572660
if '-sDYNAMIC_EXECUTION=0' in args:
@@ -5850,7 +5853,7 @@ def test_massive_alloc(self, wasm):
58505853
# just care about message regarding allocating over 1GB of memory
58515854
output = self.run_js('a.out.js')
58525855
if not wasm:
5853-
self.assertContained('Warning: Enlarging memory arrays, this is not fast! 16777216,1473314816\n', output)
5856+
self.assertContained('Warning: Enlarging memory arrays, this is not fast! 16777216,1468137472\n', output)
58545857

58555858
def test_failing_alloc(self):
58565859
for pre_fail, post_fail, opts in [

0 commit comments

Comments
 (0)