-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions for access emscripten stack layout information (#11437)
This change adds emscripten_stack_get_base/free/end/current. This is a cut down version of #11162 which uses wasm assembly rather than relying on binaryen changes. Also remove old emscripten_get_stack_base/emscripten_get_stack_top from src/library.js. emscripten_get_stack_top never worked anyway since its was not marked as asm and therefore not referencing the arm-internal version of STACKTOP.
- Loading branch information
Showing
7 changed files
with
162 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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,37 @@ | ||
/* | ||
* Copyright 2020 The Emscripten Authors. All rights reserved. | ||
* Emscripten is available under two separate licenses, the MIT license and the | ||
* University of Illinois/NCSA Open Source License. Both these licenses can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
#pragma once | ||
|
||
// API that gives access to introspecting the Wasm data stack. Build with | ||
// -lstack.js to use this API. | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// Returns the starting address of the wasm stack. This is the address | ||
// that the stack pointer would point to when no bytes are in use on the stack. | ||
uintptr_t emscripten_stack_get_base(void); | ||
|
||
// Returns the end address of the wasm stack. This is the address that the stack | ||
// pointer would point to when the whole stack is in use. (the address pointed | ||
// to by the end is not part of the stack itself) Note that in fastcomp, the | ||
// stack grows up, whereas in wasm backend, it grows down. So with wasm | ||
// backend, the address returned by emscripten_stack_get_end() is smaller than | ||
// emscripten_stack_get_base(). | ||
uintptr_t emscripten_stack_get_end(void); | ||
|
||
// Returns the current stack pointer. | ||
uintptr_t emscripten_stack_get_current(void); | ||
|
||
// Returns the number of free bytes left on the stack. | ||
size_t emscripten_stack_get_free(void); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains 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 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,47 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <alloca.h> | ||
#include <emscripten.h> | ||
#include <emscripten/stack.h> | ||
#include <assert.h> | ||
#include <string.h> | ||
|
||
void __attribute__((noinline)) DoSomething(char *addr) { | ||
memset(addr, 42, 13); | ||
} | ||
|
||
void TestStackValidity() { | ||
uintptr_t base = emscripten_stack_get_base(); | ||
uintptr_t end = emscripten_stack_get_end(); | ||
|
||
uintptr_t used = abs((intptr_t)base - (intptr_t)emscripten_stack_get_current()); | ||
uintptr_t free = abs((intptr_t)end - (intptr_t)emscripten_stack_get_current()); | ||
uintptr_t free2 = emscripten_stack_get_free(); | ||
uintptr_t total = abs((intptr_t)end - (intptr_t)base); | ||
assert(used + free == total); | ||
assert(free == free2); | ||
} | ||
|
||
int increment = 256 * 1024; | ||
|
||
int main() { | ||
TestStackValidity(); | ||
|
||
uintptr_t origFree = emscripten_stack_get_free(); | ||
uintptr_t prevFree = emscripten_stack_get_free(); | ||
printf("Stack used: %u\n", origFree - emscripten_stack_get_free()); | ||
for(int i = 0; i < 10; ++i) { | ||
int increment_noopt = emscripten_random() >= 0 ? increment : 2; | ||
char *p = alloca(increment_noopt); | ||
DoSomething(p); | ||
uintptr_t free = emscripten_stack_get_free(); | ||
assert(prevFree - free == increment); | ||
prevFree = free; | ||
// Print something from the allocationed region to prevent whole program | ||
// optimizations from elminiating the alloca completely. | ||
printf("Val: %d\n", p[10]); | ||
printf("Stack used: %u\n", origFree - emscripten_stack_get_free()); | ||
TestStackValidity(); | ||
} | ||
return 0; | ||
} |
This file contains 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,21 @@ | ||
Stack used: 0 | ||
Val: 42 | ||
Stack used: 262144 | ||
Val: 42 | ||
Stack used: 524288 | ||
Val: 42 | ||
Stack used: 786432 | ||
Val: 42 | ||
Stack used: 1048576 | ||
Val: 42 | ||
Stack used: 1310720 | ||
Val: 42 | ||
Stack used: 1572864 | ||
Val: 42 | ||
Stack used: 1835008 | ||
Val: 42 | ||
Stack used: 2097152 | ||
Val: 42 | ||
Stack used: 2359296 | ||
Val: 42 | ||
Stack used: 2621440 |
This file contains 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