Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the stack initialized?

Tags:

c++

c

linux

When a process requests for memory and an operating system is giving some new pages to the process, the kernel should initialize the pages (with zeros for instance) in order to avoid showing potentially confident data that another process used. The same when a process is starting and receives some memory, for example the stack segment.

When I execute the following code in Linux, the result is that the majority of allocated memory is indeed 0, but something about 3-4 kB at the bottom of the stack (the last elements of the array, the highest addresses) contains random numbers.

#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
    int * a = (int*)alloca(sizeof(int)*2000000);
    for(int i = 0; i< 2000000; ++i)
        cout << a[i] << endl;
    return 0;
}
  1. Why isn't it set to zero too?
  2. Could it be because it is being reused by the process?
  3. If yes, could it be the initialization code that had used those 3-4 kB of memory earlier?
like image 495
tichy Avatar asked Mar 10 '11 21:03

tichy


People also ask

How is the stack pointer initialized?

Example stack pointer initialization To set up the stack pointers, enter each mode with interrupts disabled, and assign the appropriate value to the stack pointer. The stack pointer value set up in the reset handler is automatically passed as a parameter to __user_initial_stackheap() by C library initialization code.

How is stack initialized in 8085?

Initialize stack pointer (SP) by 3FFF. Push the content of H and L register into the stack. Decrements SP by 2. Push the content of D and E register into the stack.

Which instruction can be used to initialise stack memory?

To write onto Stack, the instruction is PUSH in 8085 microprocessor instruction set. Here we shall discuss more about the PUSH instruction below. In 8085 Instruction set, PUSH rp instruction stores contents of register pair rp by pushing it into two locations above the top of the stack.


2 Answers

The operating system does not guarantee a zero'ed out memory, just that you own it. It will probably give you pages of memory that were used before (or never used before, but non-zero). If an application stores potentially-sensitive data, it is expected to zero it before free()'ing.

It's not set to zero because that would be performing unnecessary work. If you allocate 20 megabytes to store a texture or a few frames of video, why would the OS write zeroes to all that memory just so you can overwrite them as the very next thing you do.

As a general rule, operating systems don't do anything that they don't have to.

edit: to expand a little bit, when you "allocate" a block of memory, all the OS is doing is re-assigning pages of memory (blocks of 4096 bytes, typically) to your process from a pool of un-allocated pages. You can also have shared memory, in which case the OS 'assigns' them to multiple processes. That's all allocation amounts to.

like image 70
yan Avatar answered Sep 20 '22 10:09

yan


When you get new memory into your process through brk(), sbrk() or mmap() then it is guaranteed to be zeroed out.

But the process stack is already allocated to your process. The alloca() function does not get new stack space, it just returns the current stack pointer and moves the pointer to the end of the new block.

So the memory block returned by alloca() has been previously used by your process. Even if you don't have functions before your alloca() in main, the C libraries and dynamic loader have been using the stack.

like image 31
Zan Lynx Avatar answered Sep 22 '22 10:09

Zan Lynx