I expect that due to Address Space Layout Randomization (ALSR) a process forked from another process will have different addresses returned when calling mmap
. But as I found out, that was not the case. I made the following test program for that purpose. All the addresses returned by malloc
are exactly the same for the parent and the child.
Note that the malloc
for cl1, cl2, pl1, pl2 internally uses mmap
because they are large blocks.
So, my question is, why mmap
is not returning different addresses even in the presence of ALSR. Maybe its because the seed for randomization here is the same for the original and forked process. Or is there any other reason?
int main()
{
pid = fork();
if (pid == 0) // child
{
void * c1 = malloc( 4096 );
void * c2 = malloc( 4096 );
void * cl1 = malloc( (long)512e3 ); // internally uses mmap
void * cl2 = malloc( (long)512e3 ); // internally uses mmap
printf( "c1 = %p, c2 = %p, cl1 = %p, cl2 = %p!\n", c1, c2, cl1, cl2 );
}
else
{
void * p1 = malloc( 4096 );
void * p2 = malloc( 4096 );
void * pl1 = malloc( (long)512e3 ); // internally uses mmap
void * pl2 = malloc( (long)512e3 ); // internally uses mmap
printf( "p1 = %p, p2 = %p, pl1 = %p, pl2 = %p!\n", p1, p2, pl1, pl2 );
}
return 0;
}