How to execute a binary file in memory in Ruby
Is it possible to read binary in a Ruby file and ex开发者_运维问答ecute it directly in memory?
For example, I want to do something like this:
x = IO.read('/bin/ls')
execute(x)
I tried system(x)
but it returned:
ArgumentError: string contains null byte
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.
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 to do.
For instance, this is from the system()
documentation:
Executes command... in a subshell.
command... is one of following forms.
commandline:
command line string which is passed to the standard shell
cmdname, arg1, ...:
command name and one or more arguments (no shell)
[cmdname, argv0], arg1, ...:
command name, argv[0] and zero or more arguments (no shell)
Something could probably be written in C, as that is how Ruby is extended. There is lots of information on Google.
精彩评论