SIGSEGV使用SWIG创建java共享库时出错



因此,我正在尝试使用SWIG将C库(libnfc(移植到Java。

我已经到了拥有一个已编译的共享库的地步,并且基本的"nfc_version(("方法调用将起作用。但是,调用"nfc_init(("进行设置会导致SIGSEGV错误。直接调用nfc库是可以的。

我用来生成共享库的命令:

swig -java -I../libnfc/include nfclib.i 
gcc -c -I/usr/lib/jvm/java-7-openjdk-i386/include/ -I/usr/lib/jvm/java-7-openjdk-i386/include/linux nfclib_wrap.c
gcc -shared nfclib_wrap.o ../build/libnfc/libnfc.so libnfc_wrap.so

libnfc.i文件:

%module nfc
%{
#include <nfc/nfc.h>
#include <nfc/nfc-types.h>
#include <nfc/nfc-emulation.h>
%}
%include <nfc/nfc.h>
%include <nfc/nfc-types.h>
%include <nfc/nfc-emulation.h>

也就是说,它应该包括libnfc提供的所有方法。

以下是我得到的错误日志:http://openetherpad.org/AyVDsO4XTg

显然,根据我提供的信息,可能无法找到具体的解决方案。但任何关于尝试的建议都将不胜感激(我在这里的知识已经到了极限(。

要始终自动将同一指针传递给函数,在SWIG中相当简单。例如,给定"头"文件test.h,它捕获了问题的核心部分:

struct context; // only used for pointers
void init_context(struct context **ctx) { *ctx=malloc(1); printf("Init: %pn", *ctx); }
void release_context(struct context *ctx) { printf("Delete: %pn", ctx); free(ctx); }
void foo(struct context *ctx) { printf("foo: %pn", ctx); }

我们可以对其进行包装,并通过执行以下操作自动将全局上下文传递到预期的任何位置:

%module test
%{
#include "test.h"
// this code gets put in the generated C output from SWIG, but not wrapped:
static struct context *get_global_ctx() {
  static struct context *ctx = NULL;
  if (!ctx) 
    init_context(&ctx);
  return ctx;
}
%}
%typemap(in,numinputs=0) struct context *ctx "$1=get_global_ctx();"
%ignore init_context; // redundant since we call it automatically
%include "test.h"

这为struct context *ctx设置了一个类型映射,它不从Java获取输入,而是在匹配的任何地方自动调用get_global_ctx()

这可能足以让Java开发人员使用一个理智的界面,但它并不理想:它迫使上下文是全局的,这意味着任何Java应用程序都不能同时使用多个上下文。

考虑到Java是一种OO语言,一个更好的解决方案是使上下文成为第一类Object。我们也可以让SWIG为我们生成这样一个接口,尽管它有点复杂。我们的SWIG模块文件变为:

%module test
%{
#include "test.h"
%}
// These get called automatically, no need to expose:
%ignore init_context;
%ignore delete_context;
// Fake struct to convince SWIG it should be an object:
struct context {
  %extend {
    context() {
      // Constructor that gets called when this object is created from Java:
      struct context *ret = NULL;
      init_context(&ret); 
      return ret;
    }
    ~context() {
      release_context($self);
    }
  }
};
%include "test.h"

我们可以成功地使用这个代码:

public class run {
  public static void main(String[] argv) {
    System.loadLibrary("test");
    context ctx = new context();
    // You can't count on the finalizer if it exits:
    ctx.delete();
    ctx = null;
    // System.gc() might also do the trick and in a longer
    // running app it would happen at some point probably.
  }
}

给出:

Init: 0xb66dab40
Delete: 0xb66dab40

在动态类型语言中,这将是最困难的部分——我们可以使用一种或另一种形式的元编程来根据需要插入成员函数。因此,我们可以完全按照预期说出类似new context().foo();的内容。Java是静态类型的,所以我们需要更多的东西。我们可以通过多种方式在SWIG中做到这一点:

  1. 接受我们现在可以非常愉快地调用test.foo(new context());的事实——尽管它看起来仍然很像Java中的C,所以我建议,如果你最终编写了很多看起来像C的Java,这可能是一种代码气味。

  2. 使用%extend(手动(将方法添加到上下文类中,test.i中的%extend变为:

    %extend {
        context() {
          // Constructor that gets called when this object is created from Java:
          struct context *ret = NULL;
          init_context(&ret); 
          return ret;
        }
        ~context() {
          release_context($self);
        }
        void foo() {
          foo($self);
        }
      }
    
  3. %extend一样,但是在Java端编写胶水,使用typemap:

    %typemap(javacode) struct context %{
      public void foo() {
        $module.foo(this);
      }
    %}
    

    (注意:这需要在接口文件中足够早才能工作(

请注意,我在任何地方都没有向SWIG显示我的上下文结构的真实定义-对于需要真实定义的任何内容,它总是服从我的"库",因此不透明指针保持完全不透明。


用双指针包装init_context的一个更简单的解决方案是使用%inline提供一个仅在包装中使用的额外函数:

%module test
%{
#include "test.h"
%}
%inline %{
  struct context* make_context() {
    struct context *ctx;
    init_context(&ctx);
    return ctx;
  }
%}
%ignore init_context;
%include "test.h"

足以让我们编写以下Java:

public class run {
  public static void main(String[] argv) {
    System.loadLibrary("test");
    // This object behaves exactly like an opaque pointer in C:
    SWIGTYPE_p_context ctx = test.make_context();
    test.foo(ctx);
    // Important otherwise it will leak, exactly like C
    test.release_context(ctx);
  }
}

可供选择但类似的方法包括使用cpointer.i库:

%module test
%{
#include "test.h"
%}
%include <cpointer.i>
%pointer_functions(struct context *,context_ptr);
%include "test.h"

然后您可以将其用作:

public class run {
  public static void main(String[] argv) {
    System.loadLibrary("test");
    SWIGTYPE_p_p_context ctx_ptr = test.new_context_ptr();
    test.init_context(ctx_ptr);
    SWIGTYPE_p_context ctx = test.context_ptr_value(ctx_ptr);
    // Don't leak the pointer to pointer, the thing it points at is untouched
    test.delete_context_ptr(ctx_ptr);
    test.foo(ctx);
    // Important otherwise it will leak, exactly like C
    test.release_context(ctx);
  }
}

还有一个pointer_class宏,它比这个更OO一点,可能值得使用。不过,关键是您提供了处理不透明指针对象的工具,SWIG使用这些对象来表示它一无所知的指针,但避免了getCPtr()调用,因为这些调用本质上颠覆了类型系统。

因此Flexo的答案是解决此问题的正确方法,但SWIG也提供了"cpointer.i"模块(此处描述:http://www.swig.org/Doc1.3/SWIGDocumentation.html#Library_nn3)这让我能够想出一个快速的解决方案,这样我就可以测试我的基本库是否正常工作。我想我会给出这个答案只是为了完整,并为任何偶然遇到这个问题的人提供一个替代方案。

之所以需要这样做,是因为我认为SWIG产生的不对称性。基类型对象有一个属性swigCPtr,它是该对象的内存地址("自指针"(。然后,要创建指针,只需从基类型中获取swigCptr,并将其传递给swig生成的指针类型(SWIGTYPE_p_X(的构造函数。但是指针类型只存在于Java中,并且只保存swigCptr值。没有保存该指针的"c"内存块。因此,在指针类型中没有swigCPtr的等效项。即在指针类型中没有存储自指针,就像在基类型中存储自指针一样。

因此,您不能创建指向指针(SWIGTYPE_p_X(的指针,因为在构造指针时没有要传递的指针地址。

我的新".i"文件如下:

%module nfc
%{
#include <nfc/nfc.h>
#include <nfc/nfc-types.h>
#include <nfc/nfc-emulation.h>
%}
%include <nfc/nfc.h>
%include <nfc/nfc-types.h>
%include <nfc/nfc-emulation.h>
%include "cpointer.i"
%pointer_functions(nfc_context*, SWIGTYPE_p_p_nfc_context)

最后一个宏所做的是提供4/5个用于生成指针的函数。这些函数都接受并返回swig应该已经生成的类型。Java中使nfc_init和nfc_open命令工作的新用法:

SWIGTYPE_p_p_nfc_context context_p_p = nfc.new_SWIGTYPE_p_p_nfc_context();
nfc.nfc_init(context_p_p);
//get the context pointer after init has set it up (the java doesn't represent what's happening in the c) 
SWIGTYPE_p_nfc_context context_p = nfc.SWIGTYPE_p_p_nfc_context_value(context_p_p);
SWIGTYPE_p_nfc_device pnd = nfc.nfc_open(context_p, null);

注意,init命令完成后,我必须从双指针中获取指针,因为存储在java指针对象中的信息与C的"world"是分开的。因此,检索context_p_p的"value"将为context_p提供其指针的正确值。

最新更新