-
Notifications
You must be signed in to change notification settings - Fork 62
Fix overflow with optimized WASM code by ensuring that globals are in… #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -276,8 +276,16 @@ bool Wavm::initialize(const std::string& code, absl::string_view name, bool allo | |
|
|
||
| void Wavm::start(Context* context) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jplevyak unless I'm mistaken, this is never called in program, only in tests, which means that we neither initialize globals, nor call the start function (or I think we've lost the call to |
||
| auto f = getStartFunction(moduleInstance_); | ||
| if (!f) | ||
| if (f) { | ||
| CALL_WITH_CONTEXT(invokeFunctionChecked(context_, f, {}), context); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a early return? It's possible that
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, we want to call the generic start function before the global variables are initialized. I am not sure why, but that is what WAVM does and this is WAVM specific code. The "start" function is special and according to the docs should be called (if available) before any exported functions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the explanation! |
||
| } | ||
| if (emscriptenInstance_) { | ||
| Emscripten::initializeGlobals(context_, irModule_, moduleInstance_); | ||
| } | ||
| f = asFunctionNullable(getInstanceExport(moduleInstance_, "main")); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, on a second thought, what if
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT the "start" function is special and according to the docs should be called (if available) before any exported functions e.g. "main" or "_main". In this case it is also called before the Emscripten module globals are initialized because that is what wavm-run does and this is WAVM specific code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, my point was that Anyway, we can deal with it if that becomes an issue with other languages. |
||
| if (!f) { | ||
| f = asFunctionNullable(getInstanceExport(moduleInstance_, "_main")); | ||
| } | ||
| if (f) { | ||
| CALL_WITH_CONTEXT(invokeFunctionChecked(context_, f, {}), context); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,7 @@ API=../../../../../../api/wasm/cpp | |
|
|
||
| EMSCRIPTEN_OPT=-s WASM=1 -s EMIT_EMSCRIPTEN_METADATA=1 | ||
|
|
||
| # Note that optimizations are disabled, because optimized WASM module | ||
| # throws wavm.integerDivideByZeroOrOverflow, possibly due to an issue | ||
| # with getFunctionWavm() templates and/or their usage. | ||
| #CXXFLAGS=--std=c++14 -O3 -g3 | ||
| CXXFLAGS=--std=c++14 -g3 | ||
| CXXFLAGS=--std=c++14 -O3 -g3 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -g3: TIL. Thanks! |
||
|
|
||
| all: headers.wasm async_call.wasm | ||
|
|
||
|
|
||
Large diffs are not rendered by default.
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any test to cover the behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ensuring that globals are initialized. I get it. Thanks!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds -O3 to a WASM file for a test that used to cause a crash with -O3, so that is the test.