emscripten-core / emscripten-core/emscripten
Multiple main function definitions can cause emscripten to pass argc of 0 and argv of NULL, which can crash some programs
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
edit: the issue is when defining one main function with signature `int main(void)`, and another with `int main(char* argv[], int argc)`. This seems to cause emscripten to pass `argc` of 0 and `argv` of NULL, which in my case happened to cause the Lua interpreter main function crash.
---
If I use emscripten to build the Lua interpreter (includes a main function) to wasm as a static library, and then build my own source files containing a second main function, and link to this Lua static library, I don't get any build time error. At runtime, I get `Uncaught (in promise) RuntimeError: memory access out of bounds` in chrome. And, more importantly, when trying to load the page as an electron app, it crashes the renderer process, which can be quite tricky to debug.
See https://alexbarry.github.io/misc/emscripten_multiple_mains/ for a demo of the issue, and the [source is available here](https://github.com/alexbarry/misc/blob/main/emscripten_multiple_mains/).
It looks like clang and gcc also don't give a build error in this case, but they don't give a runtime error either: they seem to just pick a main function and run it like normal.
I think the runtime error is new, for a while I had suppressed the Lua main function by defining my own, and I only started noticing the error recently (perhaps when upgrading emscripten). My assumption is that wasm apps don't tend to use a main function as often, in my own projects I've defined a separate entry point that I call at a certain point.
I tried to reproduce this in a more minimal example, where I defined my own static library which contained only a main function. I didn't see the error in this case. I'm not sure what it is about the Lua code that causes this.
Note that crashing the renderer process of an electron app can be tricky to debug because it detaches dev tools (meaning no visible error), and I wouldn't have had any idea what was happening without knowing to add `ELECTRON_ENABLE_LOGGING=1` (log errors to stdout) before calling `electron .` .
Here is the runtime error:
```
my_exec.wasm:0x8ba55 Uncaught (in promise) RuntimeError: memory access out of bounds
at my_exec.wasm:0x8ba55
at my_exec.wasm:0x8b617
at my_exec.wasm:0x44872
at my_exec.wasm:0x44d60
at my_exec.wasm:0x451cc
at my_exec.wasm:0x452a0
at my_exec.wasm:0x6471d
at invoke_vii (my_exec.js:4980:29)
at my_exec.wasm:0x425c8
at my_exec.wasm:0x46148
$func1350 @ my_exec.wasm:0x8ba55
$func1348 @ my_exec.wasm:0x8b617
$func510 @ my_exec.wasm:0x44872
$func513 @ my_exec.wasm:0x44d60
$func515 @ my_exec.wasm:0x451cc
$func516 @ my_exec.wasm:0x452a0
$func850 @ my_exec.wasm:0x6471d
invoke_vii @ my_exec.js:4980
$func496 @ my_exec.wasm:0x425c8
$func527 @ my_exec.wasm:0x46148
$func849 @ my_exec.wasm:0x64487
$__main_argc_argv @ my_exec.wasm:0x8b3fe
(anonymous) @ my_exec.js:691
callMain @ my_exec.js:5295
doRun @ my_exec.js:5345
run @ my_exec.js:5360
runCaller @ my_exec.js:5280
removeRunDependency @ my_exec.js:629
receiveInstance @ my_exec.js:825
receiveInstantiationResult @ my_exec.js:843
```
## Template
Please include the following in your bug report:
**Version of emscripten/emsdk:**
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.55-git (165133b1cc977f0b9a277e42ef809b823157189c)
clang version 19.0.0git (/startdir/llvm-project 6c7805d5d186a6d1263f90b8033ad85e2d2633d7)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /opt/emscripten-llvm/bin
```
**Failing command line in full:**
The command isn't failing. [Link to build script](https://github.com/alexbarry/misc/blob/main/emscripten_multiple_mains/build_raw_lua.sh), also attached:
[build_raw_lua.sh.txt](https://github.com/emscripten-core/emscripten/files/14856325/build_raw_lua.sh.txt)
**Full link command and output with `-v` appended:**
[Link to build output uploaded to github](https://github.com/alexbarry/misc/blob/main/emscripten_multiple_mains/output.log), also attached ([output.log](https://github.com/emscripten-core/emscripten/files/14856126/output.log)).
I tried embedding the raw text in the bug report, but I hit the limit of 65536 characters.
Contributor guide
Assessment
This issue has not been assessed yet.