从Lua调用dlopen时出现分段错误

时间:2014-10-07 17:57:24

标签: c lua segmentation-fault valgrind dlopen

我有一个库,可以打开另一个调用dlopen的库,但它会与Seg Fault崩溃。一个感兴趣的评论是它并不是一直发生的,但只有当它从Lua系统调用时我才能维护,如果我从Lua解释器调用它,它就没有错误。

lua电话:

cmdtmpfile = "/home/msv/ericaflr/teste_slurm/echoandsleepjob.sh"

local slurmLibOpen = assert(package.loadlib("/home/msv/ericaflr/sga-slurm/drmaaloader.so", "luaopen_drmaaloader"))
slurmLibOpen()  -- actually open the library
submitJob(cmdtmpfile) --function from drmaa.so

drmaaloader.so源代码:

#include <lua.h>                               
#include <lauxlib.h>                           
#include <lualib.h>

#include <stdio.h>
#include <dlfcn.h>

typedef void Register(lua_State*);

int luaopen_drmaaloader(lua_State *L){
    fprintf(stderr, "In loader C\n");
    void* lib;
    lib = dlopen("/home/msv/ericaflr/sga-slurm/drmaa.so", RTLD_NOW | RTLD_GLOBAL | RTLD_DEEPBIND);
    if(!lib){
        printf("%s\n", dlerror());
        return 1;
    }
    fprintf(stderr, "Loaded library\n");
    Register* loadFunc = (Register*)dlsym(lib, "luaopen_drmaa");
    if(!loadFunc){
        printf("%s\n", dlerror());
        return 1;
    }
    fprintf(stderr, "Got load funtion\n");
    loadFunc(L);
    fprintf(stderr, "Loaded library\n");
    return 0;
}

drmaaloader.so汇编:

gcc -g drmaa_lib_loader.c -shared -fpic -I /usr/include/lua5.1 -I /usr/local/include -L /usr/local/lib -llua5.1 -ldl -o drmaaloader.so

这是valgrind输出:

==28683== Syscall param set_robust_list(head) points to uninitialised byte(s)
==28683==    at 0x65DDBBC: __pthread_initialize_minimal (nptl-init.c:346)
==28683==    by 0x65DC538: ??? (in /lib/x86_64-linux-gnu/libpthread-2.19.so)
==28683==  Address 0x4000bb0 is not stack'd, malloc'd or (recently) free'd
==28683== 
==28683== Jump to the invalid address stated on the next line
==28683==    at 0x0: ???
==28683==    by 0x65DDDD7: __pthread_initialize_minimal (nptl-init.c:433)
==28683==    by 0x65DC538: ??? (in /lib/x86_64-linux-gnu/libpthread-2.19.so)
==28683==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==28683== 
==28683== 
==28683== Process terminating with default action of signal 11 (SIGSEGV)
==28683==  Bad permissions for mapped region at address 0x0
==28683==    at 0x0: ???
==28683==    by 0x65DDDD7: __pthread_initialize_minimal (nptl-init.c:433)
==28683==    by 0x65DC538: ??? (in /lib/x86_64-linux-gnu/libpthread-2.19.so)
==28683== 
==28683== HEAP SUMMARY:
==28683==     in use at exit: 0 bytes in 0 blocks
==28683==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==28683== 
==28683== All heap blocks were freed -- no leaks are possible
==28683== 
==28683== For counts of detected and suppressed errors, rerun with: -v
==28683== Use --track-origins=yes to see where uninitialised values come from
==28683== ERROR SUMMARY: 40067 errors from 136 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

gdb输出:

Warning: couldn't activate thread debugging using libthread_db: Cannot find new threads: generic error
warning: File "/lib/x86_64-linux-gnu/libthread_db-1.0.so" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
    add-auto-load-safe-path /lib/x86_64-linux-gnu/libthread_db-1.0.so
line to your configuration file "/home/msv/ericaflr/.gdbinit".
To completely disable this security protection add
    set auto-load safe-path /
line to your configuration file "/home/msv/ericaflr/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
    info "(gdb)Auto-loading safe path"
warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available.

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) up
#1  0x00007ffff600edd8 in __pthread_initialize_minimal_internal () at nptl-init.c:433
433 nptl-init.c: No such file or directory.
(gdb) up
#2  0x00007ffff600d539 in _init () at ../sysdeps/x86_64/crti.S:72
72  ../sysdeps/x86_64/crti.S: No such file or directory.
(gdb) up
#3  0x0000000000000070 in ?? ()
(gdb) up
#4  0x000000000051d3ab in call_init.part ()
(gdb) up
#5  0x000000000051d599 in _dl_init ()
(gdb) up
#6  0x00000000004e95ca in dl_open_worker ()
(gdb) up
#7  0x00000000004e76c6 in _dl_catch_error ()
(gdb) up
#8  0x00000000004e8e0e in _dl_open ()
(gdb) up
#9  0x00000000004990ec in dlopen_doit ()
(gdb) up
#10 0x00000000004e76c6 in _dl_catch_error ()
(gdb) up
#11 0x0000000000499573 in _dlerror_run ()
(gdb) up
#12 0x000000000049913e in __dlopen ()
(gdb) up
#13 0x00007ffff6eb00da in __dlopen (file=<optimized out>, mode=<optimized out>) at dlopen.c:78
78  dlopen.c: No such file or directory.
(gdb) up
#14 0x00007ffff70b3895 in luaopen_drmaaloader (L=0xb972d0) at drmaa_lib_loader.c:13
13      lib = dlopen("/home/msv/ericaflr/sga-slurm/drmaa.so", RTLD_NOW | RTLD_GLOBAL | RTLD_DEEPBIND);

我想知道dlopen的执行是否会因环境变量而异。有没有人以前经历过或知道可能是什么问题?

提前致谢

- 修改

创建drmaaloader.so解决了链接问题,因为drmaa.so使用了两个相互引用的库。如果lua解释器调用了loadlib,那么在luaopen_drmaa中添加对ldopen的调用来打开这些库也是有效的,但是如果它被上面提到的Lua系统调用它就不起作用,它也会给出Seg Fault,但是有不同的痕迹:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) up
#1  0x00007ffff6210dd8 in __pthread_initialize_minimal_internal () at nptl-init.c:433
433 nptl-init.c: No such file or directory.
(gdb) up
#2  0x00007ffff620f539 in _init () at ../sysdeps/x86_64/crti.S:72
72  ../sysdeps/x86_64/crti.S: No such file or directory.
(gdb) up
#3  0x0000000000000003 in ?? ()
(gdb) up
#4  0x000000000051d3ab in call_init.part ()
(gdb) up
#5  0x000000000051d599 in _dl_init ()
(gdb) up
#6  0x00000000004e95ca in dl_open_worker ()
(gdb) up
#7  0x00000000004e76c6 in _dl_catch_error ()
(gdb) up
#8  0x00000000004e8e0e in _dl_open ()
(gdb) up
#9  0x00000000004990ec in dlopen_doit ()
(gdb) up
#10 0x00000000004e76c6 in _dl_catch_error ()
(gdb) up
#11 0x0000000000499573 in _dlerror_run ()
(gdb) up
#12 0x000000000049913e in __dlopen ()
(gdb) up
#13 0x000000000041fe0d in ll_loadfunc ()
(gdb) up
#14 0x00000000004200d5 in ll_loadlib ()
(gdb) up
#15 0x000000000040c2d4 in luaD_precall ()
(gdb) up
#16 0x0000000000415758 in luaV_execute ()
(gdb) up
#17 0x000000000040b97a in luaD_rawrunprotected ()
(gdb) up
#18 0x000000000040c79a in lua_resume ()
(gdb) up
#19 0x00000000004189ca in auxresume ()
(gdb) up
#20 0x0000000000418bf1 in luaB_coresume ()
(gdb) up
#21 0x000000000040c2d4 in luaD_precall ()
(gdb) up
#22 0x0000000000415758 in luaV_execute ()
(gdb) up
#23 0x000000000040b97a in luaD_rawrunprotected ()
(gdb) up
#24 0x000000000040c79a in lua_resume ()
(gdb) up
#25 0x00000000004189ca in auxresume ()
(gdb) up
#26 0x0000000000418bf1 in luaB_coresume ()
(gdb) up
#27 0x000000000040c2d4 in luaD_precall ()
(gdb) up
#28 0x0000000000415758 in luaV_execute ()
(gdb) up
#29 0x000000000040c6fd in luaD_call ()
(gdb) up
#30 0x000000000040b97a in luaD_rawrunprotected ()
(gdb) up
#31 0x000000000040c8af in luaD_pcall ()
(gdb) up
#32 0x0000000000409ef1 in lua_pcall ()
(gdb) up
#33 0x00000000004015f3 in _dofile(lua_State*, char*) ()
(gdb) up
#34 0x000000000040159e in main ()

0 个答案:

没有答案