SIGSEGV从golang运行C代码

时间:2018-03-29 04:20:06

标签: go dlopen cgo dlsym

我正在构建一个主要是golang中的grpc服务器的应用程序。我需要使用一些用C语言实现的库。(我有一个.so)。代码流是这样的: 去功能 - >调用C函数 - > dlopen()+ dlysm() - >执行该功能。

去代码调用createOFATableEntry()。

typedef int (*createTable)(char *, unsigned int, char * unsigned int);
int createOFATableEntry(char *tableName, char *prefix, unsigned int prefixLen, char *nextHop, unsigned int outPort) {
    void *handle = dlopen("<lib>", RTLD_NOW);
    if (handle == NULL) {
        return -1;
    }
    createTable createTable_func;
    createTable_func = dlsym(handle, "intf_table_entry_add");
    if (createTable_func == NULL) {
        return -1;
    }
    int ret = (createTable_func)(prefix, prefixLen, nextHop, outPort);
    return ret;
}

我从我的go代码中调用此代码,如下所示:

tabName := C.CString(tableName)
pfxStr := C.CString(pfx)
nhopStr := C.Cstring(nexthop)
defer C.free(unsafe.Pointer(tabName))
defer C.free(unsafe.Pointer(pfxStr))
defer C.free(unsafe.Pointer(nhopStr))
C.createOFATableEntry(tabName, pfxStr, C.uint(pfxLen), nhopStr, C.uint(dport))

当我运行此代码时,我得到一个SIGSEGV如下:

fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x7f6e8ef3030d]

runtime stack:
runtime.throw(0x8cdb84, 0x2a)
        /usr/lib/go-1.8/src/runtime/panic.go:596 +0x95
runtime.sigpanic()
        /usr/lib/go-1.8/src/runtime/signal_unix.go:274 +0x2db

goroutine 34 [syscall, locked to thread]:
runtime.cgocall(0x7b69c0, 0xc42017d5f8, 0x7b56e3)
        /usr/lib/go-1.8/src/runtime/cgocall.go:131 +0xe2 fp=0xc42017d5b8 sp=0xc42017d578
main._Cfunc_createOFATableEntry(0x7f6e800008c0, 0x7f6e80000a10, 0x7f6e00000018, 0x7f6e80000a30, 0xc, 0x0)
        rtagent/_obj/_cgo_gotypes.go:56 +0x4d fp=0xc42017d5f8 sp=0xc42017d5b8
main.handleTableOperation(0xc42019e2d0, 0x1, 0x0, 0x0)
        /home/saramach/P4Runtime/csco/src/rtagent/rtagent.go:86 +0xb37 fp=0xc42017d830 sp=0xc42017d5f8
main.(*p4RuntimeServer).Write(0xd25ec8, 0x7f6e8f718100, 0xc420198180, 0xc420198210, 0xd25ec8, 0x0, 0x0)
        /home/saramach/P4Runtime/csco/src/rtagent/rtagent.go:108 +0x39e fp=0xc42017d958 sp=0xc42017d830
p4._P4Runtime_Write_Handler(0x858440, 0xd25ec8, 0x7f6e8f718100, 0xc420198180, 0xc4201a0070, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/saramach/P4Runtime/csco/src/p4/p4runtime.pb.go:2662 +0x28d fp=0xc42017d9d0 sp=0xc42017d958
google.golang.org/grpc.(*Server).processUnaryRPC(0xc42001ec60, 0xcdcdc0, 0xc42015e000, 0xc4201a4140, 0xc420015f20, 0xcc9280, 0x0, 0x0, 0x0)
        /home/saramach/P4Runtime/csco/src/google.golang.org/grpc/server.go:920 +0xa83 fp=0xc42017dd88 sp=0xc42017d9d0
google.golang.org/grpc.(*Server).handleStream(0xc42001ec60, 0xcdcdc0, 0xc42015e000, 0xc4201a4140, 0x0)
        /home/saramach/P4Runtime/csco/src/google.golang.org/grpc/server.go:1142 +0x15a6 fp=0xc42017df80 sp=0xc42017dd88
google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc420172000, 0xc42001ec60, 0xcdcdc0, 0xc42015e000, 0xc4201a4140)
        /home/saramach/P4Runtime/csco/src/google.golang.org/grpc/server.go:637 +0xa9 fp=0xc42017dfb8 sp=0xc42017df80
runtime.goexit()
        /usr/lib/go-1.8/src/runtime/asm_amd64.s:2197 +0x1 fp=0xc42017dfc0 sp=0xc42017dfb8
created by google.golang.org/grpc.(*Server).serveStreams.func1
        /home/saramach/P4Runtime/csco/src/google.golang.org/grpc/server.go:638 +0xa1

非常感谢任何帮助!

0 个答案:

没有答案