views:

185

answers:

2

Is it possible to read binary in ruby file and execute it directly in memory?

for example something like this:

x = IO.read('/bin/ls')
execute(x)

I tried system(x) but it gives: ArgumentError: string contains null byte

+1  A: 

system() and exec() both pass the command string to the OS to have it load and launch an external command, which isn't what you're asking.

Something could be written in C, as that is how Ruby is extended. Lots of information on Google:

http://www.google.com/search?client=safari&rls=en&q=extend+ruby+with+c&ie=UTF-8&oe=UTF-8

Greg
A: 

I don't think you're going to be able to do that. When an executable starts, the dynamic linker needs to do quite a lot of fixing links up.

The simplest solution is to write the executable to a temporary disk file somewhere, and execute that.

Douglas Leeder
can't we do something similar to eval() but for byte code or asm code?
John