Skip to content

Commit c71a1a1

Browse files
committed
Put stack first when not optimizing for size
Also, when not using santizers (which currently rely on using the start of memory).
1 parent b5c5696 commit c71a1a1

16 files changed

+53
-29
lines changed

emcc.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,14 @@ def phase_linker_setup(options, state, newargs):
18141814
if not settings.AUTO_JS_LIBRARIES:
18151815
default_setting('USE_SDL', 0)
18161816

1817+
if 'GLOBAL_BASE' not in user_settings and not settings.SHRINK_LEVEL and not settings.OPT_LEVEL:
1818+
# When optimizing for size it helps to put static data first before
1819+
# the stack (sincs this makes instructions for accessing this data
1820+
# use a smaller LEB encoding).
1821+
# However, for debugability is better to have the stack come first
1822+
# (becuase stack overflows will trap rather than corrupting data).
1823+
settings.STACK_FIRST = True
1824+
18171825
# Default to TEXTDECODER=2 (always use TextDecoder to decode UTF-8 strings)
18181826
# in -Oz builds, since custom decoder for UTF-8 takes up space.
18191827
# In pthreads enabled builds, TEXTDECODER==2 may not work, see
@@ -2150,7 +2158,7 @@ def phase_linker_setup(options, state, newargs):
21502158
exit_with_error('cannot have both DYNAMIC_EXECUTION=0 and RELOCATABLE enabled at the same time, since RELOCATABLE needs to eval()')
21512159

21522160
if settings.SIDE_MODULE and 'GLOBAL_BASE' in user_settings:
2153-
exit_with_error('Cannot set GLOBAL_BASE when building SIDE_MODULE')
2161+
exit_with_error('GLOBAL_BASE is not compatible with SIDE_MODULE')
21542162

21552163
if options.use_preload_plugins or len(options.preload_files) or len(options.embed_files):
21562164
if settings.NODERAWFS:
@@ -2498,6 +2506,7 @@ def check_memory_setting(setting):
24982506
# We start our global data after the shadow memory.
24992507
# We don't need to worry about alignment here. wasm-ld will take care of that.
25002508
settings.GLOBAL_BASE = shadow_size
2509+
settings.STACK_FIRST = False
25012510

25022511
if not settings.ALLOW_MEMORY_GROWTH:
25032512
settings.INITIAL_MEMORY = total_mem

src/runtime_stack_check.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ function writeStackCookie() {
1414
#if ASSERTIONS
1515
assert((max & 3) == 0);
1616
#endif
17+
// If the stack ends at address zero we write our cookies 4 bytes after the
18+
// prevent interferance with the (separate) address-zero check below.
19+
if (max == 0) {
20+
max += 4;
21+
}
1722
// The stack grow downwards towards _emscripten_stack_get_end.
1823
// We write cookies to the final two words in the stack and detect if they are
1924
// ever overwritten.
@@ -33,6 +38,9 @@ function checkStackCookie() {
3338
#if RUNTIME_DEBUG
3439
dbg('checkStackCookie: ' + max.toString(16));
3540
#endif
41+
if (max == 0) {
42+
max += 4;
43+
}
3644
var cookie1 = {{{ makeGetValue('max', 0, 'u32') }}};
3745
var cookie2 = {{{ makeGetValue('max', 4, 'u32') }}};
3846
if (cookie1 != 0x2135467 || cookie2 != 0x89BACDFE) {

src/settings_internal.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,5 @@ var ALL_INCOMING_MODULE_JS_API = [];
244244
// when weak symbols are undefined. Only applies in the case of dyanmic linking
245245
// (MAIN_MODULE).
246246
var WEAK_IMPORTS = [];
247+
248+
var STACK_FIRST = false;

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: 2142175232
5+
sbrk 0: 0x501000
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: 2104426496
10+
sbrk 1: 0x2901000
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: 2104426496
15+
sbrk 1: 0x2901000
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: 2104426496
20+
sbrk 2: 0x2901000
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: 2104426496
25+
sbrk 3: 0x2901000
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: 2104426496
30+
sbrk 4: 0x2901000
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: 2104426496
35+
sbrk 5: 0x2901000

test/core/test_stack_placement.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ int main(int argc, char* argv[]) {
2222
int* bss_address = &static_bss[BSS_BYTES-1];
2323
int* stack_address = &stack_var;
2424
printf("data: %p bss: %p stack: %p\n", data_address, bss_address, stack_address);
25-
assert(stack_address > bss_address);
25+
// Stack can either come after BSS or before data (In debug builds we link
26+
// with --stack-first).
27+
assert(stack_address > bss_address || stack_address < data_address);
2628
assert(bss_address > data_address);
2729
printf("success.\n");
2830
return 0;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
26256
1+
26310
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
12058
1+
12135
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
22987
1+
23046
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
865
1+
882
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
66886
1+
67108

0 commit comments

Comments
 (0)