-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add -s SAFE_STACK to detect stack overflow #9141
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5c80e27
Implement SAFE_STACK
quantum5 5c84ab9
Add SAFE_STACK tests
quantum5 f0e59bf
Add SAFE_STACK test mode
quantum5 c69b547
Add dynamic linking SAFE_STACK test
quantum5 09193b6
flake8
quantum5 0704f7a
Merge remote-tracking branch 'origin/incoming' into safe-stack
quantum5 d3ddf34
Add browser test
quantum5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -242,6 +242,10 @@ var SAFE_HEAP = 0; | |
| // Log out all SAFE_HEAP operations | ||
| var SAFE_HEAP_LOG = 0; | ||
|
|
||
| // Check each stack pointer decrement on WASM backend to ensure that the stack | ||
|
Member
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. nit (not worth a PR): wasm isn't an acronym, so just "wasm" is fine. |
||
| // does not overflow. | ||
| var SAFE_STACK = 0; | ||
|
|
||
| // In asm.js mode, we cannot simply add function pointers to function tables, so | ||
| // we reserve some slots for them. An alternative to this is to use | ||
| // EMULATED_FUNCTION_POINTERS, in which case we don't need to reserve. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #include <string.h> | ||
|
|
||
| int f(int *b) { | ||
| int a[64]; | ||
| memset(b, 0, 64 * sizeof(int)); | ||
| return f(a); | ||
| } | ||
|
|
||
| int main() { | ||
| int a[64]; | ||
| f(a); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #include <alloca.h> | ||
| #include <string.h> | ||
| #include <stdlib.h> | ||
|
|
||
| int f(int *ptr) { | ||
| for (int i = 0; i < 16384; ++i) | ||
| ptr[i] = rand(); | ||
| return ptr[16383]; | ||
| } | ||
|
|
||
| int main() { | ||
| return f((int*) alloca(65536)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Module['onAbort'] = function (what) { | ||
| if (what == 'stack overflow') { | ||
| reportResultToServer(1); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
__handle_stack_overflow->__abort_stack_overflow?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.
I don't know if
__abort_stack_overflowis really a good name for the handler function. Binaryen gives the runtime flexibility to do whatever in face of stack overflow. Hypothetically, a runtime could implement a mechanism similar tosigaltstack(2)to handle stack overflows.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.
Hmm, now that I look closer at this - does the wasm backend directly generate a call to this function? If so, then the name is very generic. How about
__wasm_stack_overflow? Having compiler backend construct a function call with such a name that does not connect to an established API would make the symbol look somewhat floating.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.
The WASM backend does not generate a call to this.
wasm-emscripten-finalizein binaryen inserts a call to this function when passed a certain flag. So the__wasmprefix is inappropriate.