Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从Lua调用dlopen时出现分段错误_C_Lua_Segmentation Fault_Valgrind_Dlopen - Fatal编程技术网

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

C 从Lua调用dlopen时出现分段错误,c,lua,segmentation-fault,valgrind,dlopen,C,Lua,Segmentation Fault,Valgrind,Dlopen,我有一个库,它打开了另一个名为dlopen的库,但它因Seg故障而崩溃。一个有趣的评论是,它并非总是发生,但只有在从我维护的Lua系统调用它时,如果我从Lua解释器调用它,它才能正常工作 lua呼叫: cmdtmpfile = "/home/msv/ericaflr/teste_slurm/echoandsleepjob.sh" local slurmLibOpen = assert(package.loadlib("/home/msv/ericaflr/sga-slurm/drmaaload

我有一个库,它打开了另一个名为dlopen的库,但它因Seg故障而崩溃。一个有趣的评论是,它并非总是发生,但只有在从我维护的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;
}
以下是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);

Lua C模块不应该链接到Lua本身。为什么要手动加载模块?为什么不让
require
去做呢?因为drmaa.so使用两个相互引用的库,所以如果使用require,我在操作某些符号时会出现Seg错误(由于相互依赖而未定义符号)。我不明白。如果
drmaaloader.so
链接到这两个库(并可能从每个库中导出符号),那么我认为这应该是可行的。尽管如此,我不确定手动加载模块是如何避免这个问题的。手动加载DrmaLoader中的其他库。因此,我认为这可能是一个解决方法。是的,此解决方案也适用于lua解释器,但如果所述系统使用loadlib,则不适用。我已经编辑了这个问题,以包含此解决方案的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);
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 ()