Ruby C 扩展 - 64 位指针被截断导致段错误

发布于 2024-12-02 21:17:04 字数 1102 浏览 1 评论 0原文

我正在尝试使用 c 语言编写 ruby​​ 扩展,在 Mac OSX 上使用 ruby​​ 1.9.2。我不断收到由函数返回的 64 位指针被截断为 32 位值并尝试访问低内存引起的段错误。

这就是我的想法。 VALUE 是一个 long,在 ruby​​ 源代码中定义,除非 sizeof(long) 为 8 个字节,否则代码将无法编译。

在 file_b.c 中:

VALUE
rb_helper_function_foo(VALUE file)
{
  VALUE blah;

  ...

  //Using gdb, blah right here is a 64bit address, ex: 0x1 009d 62a0
  return blah;
}

在 file_a.c 中

VALUE
func_do_stuff(int argc, VALUE *argv, VALUE obj)
{
  VALUE filename, file_path;

  ...

  // But here, the size of what is returned is 4 bytes
  // file_path has a size of 8 bytes though
  file_path = rb_helper_function_foo(filename);

  //This statement will segfault :-(
  if(!RTEST(file_path))
  {
    ...
  }
  ...
}

在函数调用结束时,返回值具有正确的地址,但仅返回低 4 个字节:0x009d52a0 而不是 0x1009d52a0。访问低内存地址会导致段错误。

在返回期间调用的汇编代码中,有一条指令

movslq %eax, %r12

仅复制较低的 4 个字节,但我需要复制 %rax 的所有 8 个字节。

我使用的是 ruby​​ 1.9.2-p290(尽管它与 p180 相同),Mac OSX 10.6.8,使用 xcode 4.0.1(带有 gcc 4.2.1),但我也尝试升级到 gcc 4.6.1。之前也尝试过xcode 3.2。

感谢您能给我的任何帮助。

I'm working on trying to get a ruby extension written in c, working with ruby 1.9.2 on Mac OSX. I keep getting a segfault caused by a 64-bit pointer returned from a function being truncated to a 32-bit value and trying to access low memory.

Here's what I've figured out about it. VALUE is a long, defined in the ruby source code, and the code won't compile unless sizeof(long) is 8 bytes.

In file_b.c:

VALUE
rb_helper_function_foo(VALUE file)
{
  VALUE blah;

  ...

  //Using gdb, blah right here is a 64bit address, ex: 0x1 009d 62a0
  return blah;
}

In file_a.c

VALUE
func_do_stuff(int argc, VALUE *argv, VALUE obj)
{
  VALUE filename, file_path;

  ...

  // But here, the size of what is returned is 4 bytes
  // file_path has a size of 8 bytes though
  file_path = rb_helper_function_foo(filename);

  //This statement will segfault :-(
  if(!RTEST(file_path))
  {
    ...
  }
  ...
}

At the end of the function call, the return value has the correct address, but only the lower 4 bytes get returned: 0x009d52a0 instead of 0x1009d52a0. Accessing the low memory address is causing a segfault.

In the assembly code that gets called during the return, there's this instruction

movslq %eax, %r12

which only copies the lower 4 bytes, but I need all 8 bytes of %rax to be copied.

I'm on ruby 1.9.2-p290 (though it did the same with p180), Mac OSX 10.6.8, using xcode 4.0.1 (with gcc 4.2.1), but I also tried upgrading to gcc 4.6.1. It has also previously tried xcode 3.2.

Thanks for any help you can give me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与他有关 2024-12-09 21:17:04

也许这就像在 file_a.c 编译单元中没有声明 rb_helper_function_foo 一样简单 - 所以编译器假设它返回 int 而不是 VALUE?

Maybe this is as simple as rb_helper_function_foo not being declared in the file_a.c compilation unit-- so the compiler is assuming it returns int instead of VALUE?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文