Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fill memory with random data

Is there a way to fill the free RAM on a linux machine with random data?

The reason I'm asking this: I'm working in a group where we do numerical programming in Fortran. Sometimes, people mess up working with double precision reals, so that programs that should give double precision results only give single precision.

If my understanding is correct, one would see random fluctuations of the result after the single precision limit in such a program. That is, if you run the same program with the same input several times, you get randomly different result each time. What you see (the random part) depends on the random values in the free RAM of the machine. But in practice, if you run the program repeatedly on the same machine, the same parts of memory tend to be used repeatedly, which have the same random data in them, leading the same output every time.

My idea is that if you could overwrite the memory with random data, you would actually see the random fluctuations in your program output. That would make it a lot easier to find these bugs.

Is this idea whack, or if not, how do I fill the memory? Can I pipe /dev/random into the RAM, or something?

like image 802
Michael Goerz Avatar asked Aug 11 '09 13:08

Michael Goerz


2 Answers

If you have a recent (>=2.4 it seems) glibc you can use set the environment variable MALLOC_PERTURB_ to make malloc() return memory that is set to some value. See http://udrepper.livejournal.com/11429.html and inside http://people.redhat.com/drepper/defprogramming.pdf

Then the question is if your Fortran program uses the glibc malloc(), I guess it depends on the Fortran compiler.

like image 165
uekstrom Avatar answered Oct 21 '22 06:10

uekstrom


Your understanding is incorrect. You cannot fill a program's memory with random data before it starts executing, and even if you could, it wouldn't solve your problem.

If your Fortran program declares a single precision floating point variable, the compiler will allocate a 32 bit cell in memory to hold the value. Each time your program reads from the variable, the processor will fetch a 32 bit value from the cell. Each time you assign to the variable, the processor will write a 32 bit value to the cell. Under no circumstances should random bits "bleed" into the value from the cells before or after the cell.

While floating point arithmetic is not precise, it is not random either. If you calculate 1.0 / 3.0 + 1.0 / 3.0 + 1.0 / 3.0) one thousand times, you will get 0.99999... each and every time.

The second point is that when a program is executed on Linux, all data memory is carefully preinitialized to zero by the operating system. This is done to avoid your program behaving differently each time you run it: that would be a BAD THING. EDIT: another reason this is done is to prevent leakage of private information from one process to another.

(Commenters: please note that I've deliberately skated over a number of issues to make the explanation simple.)

like image 21
Stephen C Avatar answered Oct 21 '22 05:10

Stephen C