c - 具有 void 函数的 libffi 段错误

标签 c segmentation-fault ffi libffi

我无法确定这段代码崩溃的原因:

#define MACOSX
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <ffi/ffi.h>

void sum(int64_t *a, int64_t *b, int64_t *c) {
      *c = *a + *b;
}

int main() {
      int64_t ai = 1, bi = 2, ci;

      ffi_cif cif;
      ffi_status status;
      ffi_type *arg_types[] = {
            &ffi_type_pointer,
            &ffi_type_pointer,
            &ffi_type_pointer,
      };
      void *args[] = {&ai, &bi, &ci};

      status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 3, &ffi_type_void, arg_types);
      assert(status == FFI_OK);

      ffi_call(&cif, FFI_FN(sum), NULL, args);

      printf("%lld\n", ci);

      return 0;
}

它因段错误而失败。据我所知,如果使用 ffi_type_void 调用 ffi_prep_cif,则 ffi_call 必须忽略返回值指针。

更新

lldb 输出:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x1)
    frame #0: 0x0000000100000dd4 a.out`sum(a=0x0000000000000001, b=0x0000000000000002, c=0x00007fff5fbffac0) at main2.c:8
   5    #include <ffi/ffi.h>
   6    
   7    void sum(int64_t *a, int64_t *b, int64_t *c) {
-> 8          *c = *a + *b;
   9    }
   10   
   11   int main() {

因此,它在 void 函数内部崩溃(试图取消引用 a?, * before a before underlined)。

最佳答案

您的间接级别有问题。您的 CIF 对于提供的函数是正确的,但您发送给它的实际参数(数组 args 的元素)不是。

参数数组的元素应该是指向参数值的指针。当函数参数是指针类型时,这意味着您必须将指针传递给适当的指针值。相反,您尝试直接传递实际值。因此,函数实现尝试取消引用 a (1) 的整数值,就好像它是一个指针值一样。

对于具有您提供的签名和 CIF 的函数,对其的 FFI 调用可能如下所示:

  int64_t ai = 1, bi = 2, ci;
  int64_t *ap = &ai, *bp = &bi, *cp = &ci;
  void *args[] = {&ap, &bp, &cp};  // <-- pointers to the (pointer) arguments

  // ...

  ffi_call(&cif, FFI_FN(sum), NULL, args);

关于c - 具有 void 函数的 libffi 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44329462/

相关文章:

C: 做 sizeof(((SomeStruct *) 0)->some_member) 的更好方法?

Haskell - FFI 和指针

c - 通过 Linux FrameBuffer 将像素绘制到屏幕上

c++ - 将静态库添加到C或C++项目的通常方法是什么?

c - 当我将标准输出传输到程序时出现段错误

C: initstate_r 崩溃

c - Racket/C FFI 中的变量函数

c - Fopen - C 中没有这样的文件或目录

c - 为什么在这种情况下 SSE 矢量化速度不快?

c++ - 为什么将 'char' 分配给 'char' 会产生段错误? C++