So you are going to randomly generate code and run it?
The way I see it, your language choices are:
- binary (straight machine instructions)
- any higher level language
If (2) is randomly generated, it probably simply won't compile. Assuming your code isn't truly random and you get it to compile, most likely you'll just end up with exceptions because of all the bad data being passed around.
So that leaves you with just (1). You have a much higher probability (still not 100%) that you'll be able to string together a bunch of "valid-looking" machine instructions that might run. But then again, 99.99% of your randomly generated machine instructions won't even get past 4 or 5 of them.
If you want to prove this for yourself. I'd say forget trying to prevent damage. There won't be any. Just compile any C++ program, put a breakpoint in main function, open disassembly window and pave over memory with random data and just see how far your program gets.
The only protection you need is already provided for you by any modern OS that supports virtual memory. A normal user process (i.e. one that isn't running in kernel mode) cannot do any damage to another process or the OS itself. The worst it can do is kill itself while executing invalid instructions. I think for your experiment, that should be all protection you ever need.