首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >对error()的Python C API调用绑定到libc实现,而不是本地实现

对error()的Python C API调用绑定到libc实现,而不是本地实现
EN

Stack Overflow用户
提问于 2018-08-01 01:37:01
回答 1查看 157关注 0票数 2

已编辑

请参阅帖子末尾的编辑以回应Employed Russian的评论

免责声明:

在继续之前,我知道将函数命名为error通常是不好的做法,因为它可能会与libc中的类似函数冲突,但这是我在使用一些第三方软件时遇到的问题,我对此几乎无法控制。另外,我真的很想知道这个错误是从哪里来的:-)

问题所在

我遇到的问题是,下面的代码在通过Python解释器执行时,实际上是调用errorerror函数(如下面的GDB堆栈跟踪所示),而不是调用libC函数的本地实现。

当在另一个C程序中简单地编译相同的代码时,我不会遇到这样的问题。有人知道这是从哪里来的吗?这与Python加载共享库的方式有关吗?

MCVE

代码语言:javascript
复制
#include <stdio.h>
#include <Python.h>

static PyObject* call_error(PyObject *self, PyObject *args);
static PyMethodDef module_methods[] = {
     {"error", call_error, METH_NOARGS, "call error"},
     {NULL, NULL, 0, NULL}
};

static struct PyModuleDef module_defs = {
     PyModuleDef_HEAD_INIT,
     "Test", "Test", -1, module_methods, NULL, NULL, NULL, NULL};

PyObject* PyInit_Test(void)
{
     PyObject *module = PyModule_Create(&module_defs);
     return module;
}

void error(const char* fmt, ...);

PyObject* call_error(PyObject *self, PyObject *args)
{
     error("Error!");
     Py_RETURN_NONE;
}

void error(const char* fmt, ...)
{
     va_list ap;
     va_start(ap, fmt);
     vprintf(fmt, ap);
     va_end(ap);
}

GDB输出

下面是在GDB中使用python3 -c "import Test; Test.error()"导入并运行上述代码的输出。

代码语言:javascript
复制
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from python3...(no debugging symbols found)...done.
(gdb) r -c 'import Test; Test.error()'
Starting program: /usr/bin/python3 -c 'import Test; Test.error()'
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
/usr/bin/python3:
Program received signal SIGSEGV, Segmentation fault.
__strchrnul_sse2 () at ../sysdeps/x86_64/multiarch/../strchr.S:32
32  ../sysdeps/x86_64/multiarch/../strchr.S: No such file or directory.
(gdb) where
#0  __strchrnul_sse2 () at ../sysdeps/x86_64/multiarch/../strchr.S:32
#1  0x00007ffff6c2c432 in __find_specmb (format=0x4 <error: Cannot access memory at
#     address 0x4>) at printf-parse.h:108
#2  _IO_vfprintf_internal (s=0x7fffffffae60, format=0x4 <error: Cannot access memory at 
#     address 0x4>, ap=0x7fffffffd5b0) at vfprintf.c:1320
#3  0x00007ffff6c2f680 in buffered_vfprintf (s=s@entry=0x7ffff6fbd680 <_IO_2_1_stderr_>,
#     format=format@entry=0x4 <error: Cannot access memory at address 0x4>,
#     args=args@entry=0x7fffffffd5b0) at vfprintf.c:2329
#4  0x00007ffff6c2c726 in _IO_vfprintf_internal (s=0x7ffff6fbd680 <_IO_2_1_stderr_>,
#     format=format@entry=0x4 <error: Cannot access memory at address 0x4>, 
#     ap=ap@entry=0x7fffffffd5b0) at vfprintf.c:1301
#5  0x00007ffff6cef9bb in error_tail (status=status@entry=-161613509, 
#     errnum=errnum@entry=0, message=message@entry=0x4 <error: Cannot access memory at 
#     address 0x4>, args=args@entry=0x7fffffffd5b0) at error.c:271
#6  0x00007ffff6cefb3d in __error (status=-161613509, errnum=0, message=0x4 
#     <error: Cannot access memory at address 0x4>) at error.c:321
#7  0x00007ffff65df82e in call_error (self=0x7ffff67f3548, args=0x0) at test.c:24
#8  0x00000000004c5352 in _PyCFunction_FastCallKeywords ()
#9  0x000000000054ffe4 in ?? ()
#10 0x00000000005546cf in _PyEval_EvalFrameDefault ()
#11 0x000000000054fbe1 in ?? ()
#12 0x0000000000550b93 in PyEval_EvalCode ()
#13 0x000000000042c4ca in PyRun_SimpleStringFlags ()
#14 0x0000000000441918 in Py_Main ()
#15 0x0000000000421ff4 in main ()

编辑

我确实考虑过导入Python模块的dlopen问题,实际上,以下代码可以很好地编译和运行,并打印出来:

代码语言:javascript
复制
> ./main
Hi there

main.c

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <errno.h>
#include <stdarg.h>

typedef void*(*arbitrary)();

extern void error(const char* fmt, ...);

int main(int argc, char **argv)
{
     void *handle;
     arbitrary my_function;

     handle = dlopen("./libtest.so", RTLD_LAZY | RTLD_GLOBAL);
     if (!handle) {
      fprintf(stderr, "%s\n", dlerror());
      exit(EXIT_FAILURE);
     }

     dlerror();    /* Clear any existing error */

     *(void**)(&my_function) = dlsym(handle,"foo");
     (void) my_function();

     // Note: binding using dlsym(handle, "error") works too

     dlclose(handle);
     exit(EXIT_SUCCESS);
}

test.c

代码语言:javascript
复制
#include <stdio.h>
#include <stdarg.h>

extern void error(const char* fmt, ...);
extern void foo(void);

void foo(void)
{
     error("Hi there\n");
}

void error(const char* fmt, ...)
{
     va_list ap;
     va_start(ap, fmt);
     vprintf(fmt, ap);
     va_end(ap);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-02 11:57:46

这是我在一些第三方软件上遇到的问题,我对这些软件几乎无法控制。

如果你有这个third_party软件的源代码,你可以编辑它们,或者使用宏技巧来重命名函数,例如-Derror=foo_error

如果您只有一个存档库,请使用objcopy --redefine-symbol ...

如果你只有一个共享库,我不知道有什么可行的解决方案。

是否与加载共享库的方式有关?

有点。发生的情况是,动态加载器将对error的引用解析为该函数最早导出的定义。

当您将error链接到主a.out中时,该定义是链接器搜索顺序中的第一个,因此它“获胜”。

当您使用dlopen加载包含errorlibfoo.so (这是Python对import所做的事情)时,该库在libc.so.6之后加载,这意味着libc.so.6在加载器搜索顺序中出现得更早,并且它的定义是"wins“。

您不需要使用Python就能看到这一点:编写一个使用dlopen的简单测试,同样的问题也会在其中出现。

更新:

我写了一个小的测试用例

您的测试用例确实证实了我的答案。您可能没有正确地构建它。

代码语言:javascript
复制
$ gcc -fPIC -shared -o libtest.so test.c
$ gcc main.c -ldl 

这里调用了“错误的”error,因为库加载的顺序是:a.outlibc.so.6,然后是libtest.so

代码语言:javascript
复制
$ ./a.out
./a.out: UH��H�=�: Unknown error 640192728

但你可能做的是:

代码语言:javascript
复制
$ gcc main.c ./libtest.so -ldl

这里的库加载顺序是a.outlibtest.so (因为a.out直接依赖于libtest.so),然后是libc.so.6,然后调用“正确的”error

代码语言:javascript
复制
$ ./a.out
Hi there
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51619095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档