开发者

Ruby 1.8.7: Segfault on calling function inside C extension

I'm building a simple C extension for a Ruby module, and I'm running into trouble with a segfault when I call another C function inside my extension. The basic flow of execution goes like this:

  1. I create a Ruby class and call an instance method on it, which
  2. Calls a C method in my extension, which
  3. Calls another C function, in a separate file but which compiled OK

It's the last jump that seems to break. I've been able to reproduce the issue with almost no functionality but the function calls. I have a standard extconf.rb, compile the thing with straight Make, and it segfaults on the call to encrypt(). On run, I issue the following commands:

$ ruby extconf.rb
$ make clean
$ make
$ ruby -r des -e 'puts DES.new.encrypt!'

The output:

creating Makefile
/usr/bin/gcc-4.2 -I. -I/opt/local/lib/ruby/1.8/i686-darwin10 -I/opt/local/lib/ruby/1.8/i686-darwin10 -I. -I/opt/local/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -fno-common -std=c99 -arch x86_64 -c calc.c
/usr/bin/gcc-4.2 -I. -I/opt/local/lib/ruby/1.8/i686-darwin10 -I/opt/local/lib/ruby/1.8/i686-darwin10 -I. -I/opt/local/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -fno-common -std=c99 -arch x86_64 -c deslib.c
/usr/bin/gcc-4.2 -dynamic -bundle -undefined suppress -flat_namespace -o calc.bundle calc.o deslib.o -L. -L/opt/local/lib -L/opt/local/lib -L. -L/opt/local/lib -arch x86_64  -arch x86_64  -lruby  -lpthread -ldl -lobjc
About to do C encrypt...
./des.rb:42: [BUG] Segmentation fault
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin10]

zsh: abort      ruby -r des -e 'puts DES.new.encrypt!'

The Ruby class:

class D
    def encrypt!
        self.encrypted = Calc.encrypt(0,0,0)
        return self.encrypted
    end
end

The Calc module:

#include "ruby.h"
#include "cryptlib.h"

VALUE Calc = Qnil;
void Init_calc();

VALUE method_encrypt(VALUE self, VALUE m, VALUE k, VALUE n);

void Init_calc() {
    Calc = rb_define_module("Calc");
    rb_define_method(Calc, "encrypt", method_encrypt, 3);
}

VALUE method_encrypt(VALUE self, VALUE m, VALUE k, VALUE n) {
    uint64_t message = NUM2ULONG(m);
    uint64_t key = NUM2ULONG(k);
    int iters = NUM2INT(n);

    printf("About to do C encrypt...\n");
    uint64_t ciphertext = encrypt(message, key, iters);
    printf("Done with C encrypt\n");

    return ULONG2NUM(ciphertext);
}

cryptlib.h:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

uint64_t encrypt(uint64_t message, uint64_t key, int iters);

cryptlib.c:

#include "cryptlib.h"

uint64_t encrypt(uint64_t message, uint64_t key, int iters) {
    return 0;
}

Why is this breaking so badly? I'm running ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin10] on a MacBook Pro, compiled from MacPorts less than an hour ago. My gcc --version: i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build开发者_Go百科 5666) (dot 3)


It turns out the encrypt function name is reserved somewhere higher up in the Ruby C extension libraries. Renamed the function and everything works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜