Skip to content
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

su_use_stack redundant? #12

Closed
iangehc opened this issue Jun 20, 2022 · 2 comments
Closed

su_use_stack redundant? #12

iangehc opened this issue Jun 20, 2022 · 2 comments
Assignees

Comments

@iangehc
Copy link
Contributor

iangehc commented Jun 20, 2022

Hi,

su_use_stack() appears to be redundant because the call to memset() is followed by filling the stack byte-by-byte and, in both cases, if a page is not mapped then the OS will catch a page fault and map the requested page.

Was there a particular OS/kernel version that you had in mind when creating su_use_stack()?

Would you accept a patch to remove this function?
Thank for making this very useful tool!

System

i.MX53 (ARM Cortex-A8, ARMv7-A) running custom Yocto (Dunfell, gcc 9.3.0, kernel 5.15) with stack size 100000 and a main thread of 8 MiB.

Debug Patch

diff --git a/src/sumain.c b/src/sumain.c
index 6df2477..9c64fa4 100644
--- a/src/sumain.c
+++ b/src/sumain.c
@@ -288,6 +288,7 @@ static void su_use_stack(char *base, long size)
   memset(arr, rand() % 255, SU_DUMMY_USE);
   if ((labs(&here - base) + SU_DUMMY_USE) < size)
   {
+    printf("labs(%ld - %ld)=%ld + %ld < %ld\n", (intptr_t)&here, base, labs(&here - base), SU_DUMMY_USE, size);
     su_use_stack(base, size);
   }
   else

Debug Output

As we recurse, the address of here grows by only 40 bytes.
(I assume this is due to a compiler optimisation, since arr is 16 KiB.)

Given main thread size of 8386960, then this implies 8386960/40 or > 200K function calls.
(On my system I stopped waiting after many minutes.)

labs(2125405027 - 2125404560)=467 + 16384 < 8386960 
labs(2125404987 - 2125404560)=427 + 16384 < 8386960 
labs(2125404947 - 2125404560)=387 + 16384 < 8386960 
labs(2125404907 - 2125404560)=347 + 16384 < 8386960 
labs(2125404867 - 2125404560)=307 + 16384 < 8386960 
labs(2125404827 - 2125404560)=267 + 16384 < 8386960 
labs(2125404787 - 2125404560)=227 + 16384 < 8386960 
labs(2125404747 - 2125404560)=187 + 16384 < 8386960 
labs(2125404707 - 2125404560)=147 + 16384 < 8386960 
labs(2125404667 - 2125404560)=107 + 16384 < 8386960 
labs(2125404627 - 2125404560)=67 + 16384 < 8386960  
labs(2125404587 - 2125404560)=27 + 16384 < 8386960  
labs(2125404547 - 2125404560)=13 + 16384 < 8386960  
labs(2125404507 - 2125404560)=53 + 16384 < 8386960  
labs(2125404467 - 2125404560)=93 + 16384 < 8386960  
labs(2125404427 - 2125404560)=133 + 16384 < 8386960 
labs(2125404387 - 2125404560)=173 + 16384 < 8386960 
labs(2125404347 - 2125404560)=213 + 16384 < 8386960 
:
@d99kris d99kris self-assigned this Jun 20, 2022
@d99kris
Copy link
Owner

d99kris commented Jun 25, 2022

Hi @iangehc - thanks for suggesting improvements!
The function su_use_stack() was added in 79a0cc1 to fix #1 - to properly use stack in main-thread under Linux around 2017 (I don't remember the exact Linux kernel version - but it was Ubuntu 16.04 on Intel). If you have a proposed fix which works under current Linux kernels I'd be happy to review and accept a patch.

iangehc pushed a commit to iangehc/stackusage that referenced this issue Jun 27, 2022
With optimization enabled, the compiler may elide the stack based array
allocated by su_use_stack, thus causing this function to run for a very
long time.

Tested on Ubuntu 16.04.6 (i386, kernel 4.15.0-45-generic, gcc 5.4.0).
Tested on custom Yocto (i.MX53, kernel 5.15.22, gcc 9.3.0).
Tested on macOS Catalina (Apple clang version clang-1200.0.32.29).

Signed-off-by: Ian Ray <ian.ray@ge.com>
@iangehc
Copy link
Contributor Author

iangehc commented Jun 27, 2022

Thanks for the reference to #1 -- that was useful to re-create the issue on an Ubuntu VM.

The pull request differs from what I initially planned, as the issue seems to be related to optimization of the dummy array.

d99kris added a commit that referenced this issue Jul 3, 2022
Disable optimization in su_use_stack (#12)
@d99kris d99kris closed this as completed in 842d3e3 Jul 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants