1

Every process running on a machine is given the illusion that it is the only process running on it. And every process has a text, data and a stack section.

However, I fail to understand why the stack address of every process starts off at the same address(assuming that no kernel patches are installed and Address randomization is disabled).

Could someone point me towards a few reading resources or explain why this happens?

  • 3
    I don't understand what there is not to understand. Why would it start anywhere else? Everything that happens before the process starts, happens the same way every time, and the illusion involves having a separate, newly created address space for the process, so... – Karl Knechtel Dec 31 '10 at 06:25
  • 1
    I agree with Karl. What you're missing is probably the address space notion (and I don't want to steal away the glory, so @Karl Knechtel, you should write the answer). – zneak Dec 31 '10 at 06:41
  • The stack size can be configured, so its starting address can be different for different processes. – ruslik Dec 31 '10 at 15:47

3 Answers3

2

The heap grows up and the stack grows down, so on most operating systems the virtual space looks like:

Program text
Program data/bss
Heap
(dynamically grows up)
...
...
(dynamically growing down)
stack

Thus the location of the heap moves depending on the program size but the stack's starting space is not dependent on anything about the program.

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
  • Thanks, exactly what i was looking for. However, what does the stack's starting address depend on? How is that computed and what is it dependent on? –  Dec 31 '10 at 07:27
1

Because there's an actual address and its, I'm going to use "virtual," address. Like you said it's an illusion. The starting address isn't actually the same.

returneax
  • 709
  • 1
  • 4
  • 18
1

This is not really an asm question, it depends on the operating system. For linux (which is open source, you know) look in fs/exec.c:

/*
 * Place the stack at the largest stack address the architecture
 * supports. Later, we'll move this to an appropriate place. We don't
 * use STACK_TOP because that can depend on attributes which aren't
 * configured yet.
 */
vma->vm_end = STACK_TOP_MAX;

And later:

stack_top = arch_align_stack(stack_top);
stack_top = PAGE_ALIGN(stack_top);

Randomization is done in arch_align_stack.

Jester
  • 56,577
  • 4
  • 81
  • 125