Skip to content

Commit 00f4e0b

Browse files
authored
Ensure MAXIMUM_MEMORY is set correctly (#19960)
This avoids issues where MAXIMUM_MEMORY is inconsistent and simplifies check that wants to check for max memory address. Without this change the following check in preable.js was producing the wrong result when INITIAL_MEMORY was set to >4gb but MAXIMUM_MEMORY was not set: ``` #if MEMORY64 && MAXIMUM_MEMORY > FOUR_GB ``` When the user specifies INITIAL_MEMORY that should set a lower bound for MAXIMUM_MEMORY. When memory growth is disallowed MAXIMUM_MEMORY should match INITIAL_MEMORY.
1 parent 308fcdb commit 00f4e0b

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

emcc.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,22 @@ def include_and_export(name):
17781778
settings.EXPORTED_RUNTIME_METHODS += ['ExitStatus']
17791779

17801780

1781+
def set_max_memory():
1782+
# When memory growth is disallowed set MAXIMUM_MEMORY equal to INITIAL_MEMORY
1783+
if not settings.ALLOW_MEMORY_GROWTH:
1784+
if 'MAXIMUM_MEMORY' in user_settings:
1785+
diagnostics.warning('unused-command-line-argument', 'MAXIMUM_MEMORY is only meaningful with ALLOW_MEMORY_GROWTH')
1786+
settings.MAXIMUM_MEMORY = settings.INITIAL_MEMORY
1787+
1788+
# INITIAL_MEMORY sets a lower bound for MAXIMUM_MEMORY
1789+
if 'MAXIMUM_MEMORY' not in user_settings:
1790+
if settings.INITIAL_MEMORY > settings.MAXIMUM_MEMORY:
1791+
settings.MAXIMUM_MEMORY = settings.INITIAL_MEMORY
1792+
1793+
if settings.MAXIMUM_MEMORY < settings.INITIAL_MEMORY:
1794+
exit_with_error('MAXIMUM_MEMORY cannot be less than INITIAL_MEMORY')
1795+
1796+
17811797
@ToolchainProfiler.profile_block('linker_setup')
17821798
def phase_linker_setup(options, state, newargs):
17831799
autoconf = os.environ.get('EMMAKEN_JUST_CONFIGURE') or 'conftest.c' in state.orig_args or 'conftest.cpp' in state.orig_args
@@ -2522,12 +2538,6 @@ def check_memory_setting(setting):
25222538
if settings.MEMORY_GROWTH_LINEAR_STEP != -1:
25232539
check_memory_setting('MEMORY_GROWTH_LINEAR_STEP')
25242540

2525-
if settings.ALLOW_MEMORY_GROWTH and settings.MAXIMUM_MEMORY < settings.INITIAL_MEMORY:
2526-
exit_with_error('MAXIMUM_MEMORY must be larger then INITIAL_MEMORY')
2527-
2528-
if 'MAXIMUM_MEMORY' in user_settings and not settings.ALLOW_MEMORY_GROWTH:
2529-
diagnostics.warning('unused-command-line-argument', 'MAXIMUM_MEMORY is only meaningful with ALLOW_MEMORY_GROWTH')
2530-
25312541
if settings.EXPORT_ES6:
25322542
if not settings.MODULARIZE:
25332543
# EXPORT_ES6 requires output to be a module
@@ -2774,6 +2784,12 @@ def check_memory_setting(setting):
27742784
if sanitize and settings.GENERATE_SOURCE_MAP:
27752785
settings.LOAD_SOURCE_MAP = 1
27762786

2787+
set_max_memory()
2788+
2789+
# check if we can address the 2GB mark and higher.
2790+
if not settings.MEMORY64 and settings.MAXIMUM_MEMORY > 2 * 1024 * 1024 * 1024:
2791+
settings.CAN_ADDRESS_2GB = 1
2792+
27772793
if settings.MINIMAL_RUNTIME:
27782794
if settings.EXIT_RUNTIME:
27792795
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['proc_exit', '$callRuntimeCallbacks']
@@ -2874,14 +2890,6 @@ def get_full_import_name(name):
28742890
'emscripten_stack_get_base',
28752891
'emscripten_stack_get_end']
28762892

2877-
# check if we can address the 2GB mark and higher: either if we start at
2878-
# 2GB, or if we allow growth to either any amount or to 2GB or more.
2879-
if not settings.MEMORY64 and (settings.INITIAL_MEMORY > 2 * 1024 * 1024 * 1024 or
2880-
(settings.ALLOW_MEMORY_GROWTH and
2881-
(settings.MAXIMUM_MEMORY < 0 or
2882-
settings.MAXIMUM_MEMORY > 2 * 1024 * 1024 * 1024))):
2883-
settings.CAN_ADDRESS_2GB = 1
2884-
28852893
settings.EMSCRIPTEN_VERSION = shared.EMSCRIPTEN_VERSION
28862894
settings.SOURCE_MAP_BASE = options.source_map_base or ''
28872895

test/test_core.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9781,9 +9781,7 @@ def setUp(self):
97819781
# Run the wasm64 tests with all memory offsets > 4gb. Be careful running this test
97829782
# suite with any kind of parallelism.
97839783
wasm64_4gb = make_run('wasm64_4gb', emcc_args=['-Wno-experimental', '--profiling-funcs'],
9784-
settings={'MEMORY64': 1, 'INITIAL_MEMORY': '4200mb',
9785-
'MAXIMUM_MEMORY': '4200mb', # TODO(sbc): should not be needed
9786-
'GLOBAL_BASE': '4gb'},
9784+
settings={'MEMORY64': 1, 'INITIAL_MEMORY': '4200mb', 'GLOBAL_BASE': '4gb'},
97879785
require_wasm64=True)
97889786
# MEMORY64=2, or "lowered"
97899787
wasm64l = make_run('wasm64l', emcc_args=['-O1', '-Wno-experimental', '--profiling-funcs'],

tools/building.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ def lld_flags_for_executable(external_symbols):
213213
cmd += [
214214
'-z', 'stack-size=%s' % settings.STACK_SIZE,
215215
'--initial-memory=%d' % settings.INITIAL_MEMORY,
216+
'--max-memory=%d' % settings.MAXIMUM_MEMORY,
216217
]
217218

218219
if settings.STANDALONE_WASM:
@@ -228,10 +229,6 @@ def lld_flags_for_executable(external_symbols):
228229
# `__main_argv_argc`, but we should address that by using a single `_start`
229230
# function like we do in STANDALONE_WASM mode.
230231
cmd += ['--no-entry']
231-
if not settings.ALLOW_MEMORY_GROWTH:
232-
cmd.append('--max-memory=%d' % settings.INITIAL_MEMORY)
233-
elif settings.MAXIMUM_MEMORY != -1:
234-
cmd.append('--max-memory=%d' % settings.MAXIMUM_MEMORY)
235232

236233
if settings.STACK_FIRST:
237234
cmd.append('--stack-first')

0 commit comments

Comments
 (0)