Don't run ATMAINS unless there is _main function exported (#13901)

Fixes: #13791
This commit is contained in:
Sam Clegg 2021-04-15 11:50:52 -07:00 коммит произвёл GitHub
Родитель d9a7dbb6d4
Коммит 6e794e6b9f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 18 добавлений и 11 удалений

Просмотреть файл

@ -125,6 +125,8 @@ def update_settings_glue(metadata, DEBUG):
if shared.Settings.INITIAL_TABLE == -1: if shared.Settings.INITIAL_TABLE == -1:
shared.Settings.INITIAL_TABLE = metadata['tableSize'] + 1 shared.Settings.INITIAL_TABLE = metadata['tableSize'] + 1
shared.Settings.HAS_MAIN = shared.Settings.MAIN_MODULE or shared.Settings.STANDALONE_WASM or '_main' in shared.Settings.IMPLEMENTED_FUNCTIONS
# When using dynamic linking the main function might be in a side module. # When using dynamic linking the main function might be in a side module.
# To be safe assume they do take input parametes. # To be safe assume they do take input parametes.
shared.Settings.MAIN_READS_PARAMS = metadata['mainReadsParams'] or shared.Settings.MAIN_MODULE shared.Settings.MAIN_READS_PARAMS = metadata['mainReadsParams'] or shared.Settings.MAIN_MODULE
@ -142,7 +144,8 @@ def update_settings_glue(metadata, DEBUG):
def apply_static_code_hooks(forwarded_json, code): def apply_static_code_hooks(forwarded_json, code):
code = shared.do_replace(code, '<<< ATINITS >>>', str(forwarded_json['ATINITS'])) code = shared.do_replace(code, '<<< ATINITS >>>', str(forwarded_json['ATINITS']))
code = shared.do_replace(code, '<<< ATMAINS >>>', str(forwarded_json['ATMAINS'])) if shared.Settings.HAS_MAIN:
code = shared.do_replace(code, '<<< ATMAINS >>>', str(forwarded_json['ATMAINS']))
if shared.Settings.EXIT_RUNTIME: if shared.Settings.EXIT_RUNTIME:
code = shared.do_replace(code, '<<< ATEXITS >>>', str(forwarded_json['ATEXITS'])) code = shared.do_replace(code, '<<< ATEXITS >>>', str(forwarded_json['ATEXITS']))
return code return code

Просмотреть файл

@ -15,14 +15,6 @@ var addedLibraryItems = {};
// Each such proxied function is identified via an ordinal number (this is not the same namespace as function pointers in general). // Each such proxied function is identified via an ordinal number (this is not the same namespace as function pointers in general).
var proxiedFunctionTable = ["null" /* Reserve index 0 for an undefined function*/]; var proxiedFunctionTable = ["null" /* Reserve index 0 for an undefined function*/];
// Used internally. set when there is a main() function.
// Also set when in a linkable module, as the main() function might
// arrive from a dynamically-linked library, and not necessarily
// the current compilation unit.
// Also set for STANDALONE_WASM since the _start function is needed to call
// static ctors, even if there is no user main.
var HAS_MAIN = ('_main' in IMPLEMENTED_FUNCTIONS) || MAIN_MODULE || STANDALONE_WASM;
// Mangles the given C/JS side function name to assembly level function name (adds an underscore) // Mangles the given C/JS side function name to assembly level function name (adds an underscore)
function mangleCSymbolName(f) { function mangleCSymbolName(f) {
return f[0] == '$' ? f.substr(1) : '_' + f; return f[0] == '$' ? f.substr(1) : '_' + f;

Просмотреть файл

@ -319,7 +319,9 @@ function run(args) {
initRuntime(); initRuntime();
#if HAS_MAIN
preMain(); preMain();
#endif
#if MODULARIZE #if MODULARIZE
readyPromiseResolve(Module); readyPromiseResolve(Module);

Просмотреть файл

@ -7,7 +7,7 @@
// === Auto-generated postamble setup entry stuff === // === Auto-generated postamble setup entry stuff ===
{{{ exportRuntime() }}} {{{ exportRuntime() }}}
#if hasExportedFunction('_main') // Only if user is exporting a C main(), we will generate a run() function that can be used to launch main. #if HAS_MAIN // Only if user is exporting a C main(), we will generate a run() function that can be used to launch main.
function run() { function run() {
#if MEMORYPROFILER #if MEMORYPROFILER
emscriptenMemoryProfiler.onPreloadComplete(); emscriptenMemoryProfiler.onPreloadComplete();

Просмотреть файл

@ -395,6 +395,7 @@ function initRuntime() {
callRuntimeCallbacks(__ATINIT__); callRuntimeCallbacks(__ATINIT__);
} }
#if HAS_MAIN
function preMain() { function preMain() {
#if STACK_OVERFLOW_CHECK #if STACK_OVERFLOW_CHECK
checkStackCookie(); checkStackCookie();
@ -405,6 +406,7 @@ function preMain() {
<<< ATMAINS >>> <<< ATMAINS >>>
callRuntimeCallbacks(__ATMAIN__); callRuntimeCallbacks(__ATMAIN__);
} }
#endif
function exitRuntime() { function exitRuntime() {
#if STACK_OVERFLOW_CHECK #if STACK_OVERFLOW_CHECK

Просмотреть файл

@ -185,3 +185,11 @@ var GENERATE_SOURCE_MAP = 0;
var STACK_BASE = 0; var STACK_BASE = 0;
var STACK_MAX = 0; var STACK_MAX = 0;
var HEAP_BASE = 0; var HEAP_BASE = 0;
// Used internally. set when there is a main() function.
// Also set when in a linkable module, as the main() function might
// arrive from a dynamically-linked library, and not necessarily
// the current compilation unit.
// Also set for STANDALONE_WASM since the _start function is needed to call
// static ctors, even if there is no user main.
var HAS_MAIN = 0;

Просмотреть файл

@ -108,7 +108,7 @@ function ready() {
#if MODULARIZE && EXPORT_READY_PROMISE #if MODULARIZE && EXPORT_READY_PROMISE
readyPromiseResolve(Module); readyPromiseResolve(Module);
#endif // MODULARIZE #endif // MODULARIZE
#if INVOKE_RUN && hasExportedFunction('_main') #if INVOKE_RUN && HAS_MAIN
#if USE_PTHREADS #if USE_PTHREADS
if (!ENVIRONMENT_IS_PTHREAD) { if (!ENVIRONMENT_IS_PTHREAD) {
#endif #endif