0

My question is based on this tiny stkaddr.cpp program:

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("&argc = %p\n", &argc);
}

Using Visual Studio 2019, I know that I can use linker option /DYNAMICBASE:NO to have invariant(constant) local variable address across each run. May be we call it "no ASLR".

VC2019 no ASLR compile and run

With /DYNAMICBASE:NO, each run of the exe we see the same address for &argc.

Then I'd like to ask how can I achieve this for a gcc compiled Linux program.

On Ubuntu Linux 20.04, with gcc 9.4.0, I tried -no-pie, but no effect.

GCC variant local var address.

5
  • Does this work: stackoverflow.com/a/2340455/5754656 Commented Jan 4, 2024 at 16:57
  • Thank you. Yes, root user executing echo 0 > /proc/sys/kernel/randomize_va_space makes &argc constant, even not compiling with -no-pie. Then I'm wondering must we do that system-wide? I hope it should be at least user-wide, or better, session-wide. Commented Jan 4, 2024 at 17:14
  • @JimmChen The assumption you're making - that it is possible/dependable on - is IMHO misguided. Even with /DYNAMICBASE:NO, you can get different address after a reboot, updates, change in moon phase, etc. None of this is guaranteed. When a process starts up, be it on Windows or on Linux, there is very little in the way of assumptions that one can make about the available address space. &stkadr is what you should use for the stack address. Don't assume it's constant across executions, whether ASLR is enabled or not. Commented Jan 4, 2024 at 20:13
  • 1
    Why would you even care what the exact address of the stack is to start with?? I smell an XY problem. Commented Jan 4, 2024 at 20:15
  • For pedagogical purpose. I need to run two similar programs several times to record their behavior. I will record screen shots during my experiments. So, my screenshot should reflect the "real" difference of the two programs. So the "non-real" difference of stack pointer variation looks quite distracting. Commented Jan 5, 2024 at 1:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.