views:

118

answers:

5

Hi,

The following question is a head-scratcher for me. Assuming that I have two platforms with an identical hardware, the same OS and the same compiler on it. If I compile exactly the same application, can I be sure that the memory layout on both machines will exactly be the same? In other words, both applications have exaclty the same virtual address space or is there a high chance that this is not the case.

Thanks for your thoughts about this!

+7  A: 

You can't count on it. As a security feature, some OS's (including Windows) randomize memory layout to some extent.

(Here's a supporting link: http://blogs.msdn.com/b/winsdk/archive/2009/11/30/how-to-disable-address-space-layout-randomization-aslr.aspx)

Steven Sudit
Just for clarity, it is a runtime faeture: (http://en.wikipedia.org/wiki/Address_space_layout_randomization)
Frank Bollack
Even without ASLR it is possible for the layouts to depend on the order in which dynamic libraries are loaded. Many such libraries just use the default "prefered address" and so collisions occur and relocation ensues.
torak
@torak: Good point.
Steven Sudit
A: 

Apart from dynamic question such as stack addresses as Steven points out, there is also the aspect of compile time and static layout.

Already I think that two machines are exact clones of each other is a very particular situation, since you may have tiny differences in CPU version, libraries etc. Then some compilers (perhaps depending on some options) also put the compile time and date in the executable. If e.g your two hostnames have different lengths or this uses a date format that varies in length, not only these strings will be different, but all other static variables might be slightly shifted in address space.

I remember that gcc had difficulties on some architectures with its automatic build since the compiler that was produced in stage 2 was differed of the one build in stage 3 for such stupid reasons.

Jens Gustedt
A: 

The __TIME__ macro expands to (the start of) the compilation time. Furthermore, it's determined independently for each and every .cpp file that you compile, and the linker can eliminate duplicate strings.

As a result, depending on the compile speed, your executables may end up not just with different __TIME__ strings, but even a different number of __TIME__ strings.

If you're working late, you could see the same with __DATE__ strings ;)

MSalters
I don't see the relevance?
John Dibling
@John: if one compilation ends up with a different number of static variables than another, the way those variables are laid out in memory can't possibly be the same.
Dennis Zickefoose
True, but not very deterministic given that the case where the same number of statics tells you nothing.
John Dibling
@John Dibling: the non-determinism is precisely the reason why Robert can't _rely_ on two binaries being identical, even if limited testing suggests otherwise.
MSalters
A: 

Is it possible for them to have the same memory layout? Yes, it is a possibility. Is it probable? Not really.

As others have pointed out, things like address space randomization and __TIME__ macros can cause the address space to differ (whether the changes were made at compile time or run time). In my experiences, many compilers don't produce the same identical output when run twice on the same machine using the exact same input (functions are laid out in memory in different orders, etc).

Is this a rhetorical/intellectual question, or is this causing you to run into some kind of problem with a program you are writing?

bta
+1  A: 

It is highly improbable that an application will be executed in the same address space on the same platform, nonetheless on another computer. Other applications may be running which will affect where the OS loads your application.

Another point to consider is that some applications load run-time libraries (a.k.a. DLLs & shared libraries) on demand. An application may have a few DLLs loaded or not when your application is running.

In non-embedded platforms, the majority of applications don't care about exact physical memory locations, nor is it a concern that they are loaded in the same location each time. Most embedded platforms load their applications in the same place each time, as they don't have enough memory to move it around.

Because of these cases and the situations other people have mentioned, DO NOT CODE CONSTANT MEMORY LOCATION principles into your program. Very bad things will happen, especially difficult to trace and debug.

Thomas Matthews