2011-08-31 123 views
2

我正在努力獲得用c寫的ruby擴展,在Mac OSX上使用ruby 1.9.2。由於函數返回的64位指針被截斷爲32位值,並試圖訪問低內存,我一直在收到一段錯誤。Ruby C擴展 - 64位指針被截斷導致段錯誤

下面是我對它的瞭解。 VALUE是一個很長的,在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個字節,但我需要的所有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。

感謝您給我的任何幫助。

+0

'long'的大小是實現定義的,所以不能保證長度爲8字節。 –

回答

3

也許這與在file_a.c編譯單元中不聲明rb_helper_function_foo一樣簡單 - 因此編譯器假定它返回的是int而不是VALUE?

+0

修正了它。謝謝! – keithepley