【问题标题】:How to execute a binary file in memory in Ruby如何在 Ruby 中执行内存中的二进制文件
【发布时间】:2010-04-01 15:30:28
【问题描述】:

是否可以读取 Ruby 文件中的二进制文件并直接在内存中执行?

例如,我想做这样的事情:

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

我尝试了system(x),但它返回了:

ArgumentError: string contains null byte

【问题讨论】:

  • 请添加有关您尝试使用此功能的信息?可能有更好的解决方案...
  • 我只想加载程序字节码并从内存而不是硬盘执行。它类似于 system() 和 exec() 但我想通过将加载在内存中的程序传递给系统而不是硬盘来做同样的事情。

标签: ruby memory system


【解决方案1】:

我认为你无法做到这一点。当一个可执行文件启动时,动态链接器需要做很多修复链接。

最简单的解决方案是将可执行文件写入某个临时磁盘文件,然后执行。

【讨论】:

  • 我们不能做一些类似于 eval() 的事情,但是对于字节码或 asm 码?
【解决方案2】:

system()exec() 都将命令字符串传递给操作系统以使其加载并启动外部命令,这不是您要执行的操作。

例如,这是来自system() 文档:

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)

有些东西可能是用 C 编写的,因为这就是 Ruby 的扩展方式。谷歌上有lots of information

【讨论】:

    猜你喜欢
    • 2021-05-09
    • 2011-01-05
    • 2018-04-17
    • 1970-01-01
    • 2014-04-24
    • 2017-11-16
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多