使用指向 pthread_create 的函数指针会导致段错误

Using a function pointer to pthread_create causes segfault

我正在尝试删除 pthread_create 以便能够对模块进行完整的单元测试。当从测试框架内调用函数指针时,会发生分段错误。如果我使用 'gdb' 调试程序,我可以直接调用函数指针并且它可以正常工作。

我使用 CppUTest 作为单元测试框架,并使用 gcc 编译了我的目标文件。

此函数在将其更改为使用 pthread_create 的函数指针之前已在生产代码中运行,因此我总体上对该函数充满信心。

来自 GDB 的堆栈跟踪

> Starting program:
> /home/lucid/depot/torr_linux_common_dev/main/src/Utilities/tests/testRunner
> [Thread debugging using libthread_db enabled] Using host libthread_db
> library "/lib/i386-linux-gnu/libthread_db.so.1".
> 
> Program received signal SIGSEGV, Segmentation fault. 0x080660c4 in
> sys_pthreads_create () (gdb) backtrace
> #0  0x080660c4 in sys_pthreads_create ()
> #1  0x08049ee4 in th_start_thread_name (thread=0x8049e64 <TestThread>, arg=0x0, opts=0x0, name=0x0) at thr.c:177
> #2  0x08049e47 in test_ThreadTestGroup_ThreadCreateUnnamed_wrapper_c () at thr_test.c:66
> #3  0x08049223 in TEST_ThreadTestGroup_ThreadCreateUnnamed_Test::testBody
> (this=0x806cc90) at testRunner.c:21
> #4  0x0805576a in PlatformSpecificSetJmpImplementation ()
> #5  0x08053ab7 in Utest::run() ()
> #6  0x080550d5 in UtestShell::runOneTestInCurrentProcess(TestPlugin*, TestResult&) ()
> #7  0x08053645 in helperDoRunOneTestInCurrentProcess ()
> #8  0x0805576a in PlatformSpecificSetJmpImplementation ()
> #9  0x08053b8f in UtestShell::runOneTest(TestPlugin*, TestResult&) ()
> #10 0x080530ef in TestRegistry::runAllTests(TestResult&) ()
> #11 0x0804a3ef in CommandLineTestRunner::runAllTests() ()
> #12 0x0804a4e9 in CommandLineTestRunner::runAllTestsMain() ()
> #13 0x0804a628 in CommandLineTestRunner::RunAllTests(int, char const**) ()
> #14 0x08049246 in main (argc=1, argv=0xbffff244) at testRunner.c:25

如果我从 gdb 中调用函数指针,它就可以工作

(gdb) p (*sys_pthreads_create)(&thr, 0, thread, arg)
[New Thread 0xb7c01b40 (LWP 17717)]
 = 0

我正在测试的功能

#include <pthread.h>
#include "mypthreads.h"
long th_start_thread_name(TH_THREAD_FUNC thread, void *arg, th_opts *opts, const char* name)
{
    pthread_t thr;
    int ret, sret;
    //pthread_create(opts ? &opts->thr : &thr, NULL, thread, arg);
    ret = (*sys_pthreads_create)(opts ? &opts->thr : &thr, 0, thread, arg);
    if (ret == 0 && name != NULL)
    {
       extern int pthread_setname_np(pthread_t thr, const char *name);  /* Fix warning from missing prototype. */

       sret = pthread_setname_np(opts ? opts->thr : thr, name);
       /* pthreads says that thread names must not exceed 16, including NULL. */
       if (sret != 0 && strlen(name) > 15)
       {
           ret = -1;
       }
    }
    return (long)ret;
}

mypthreads.h

extern int (*sys_pthreads_create(pthread_t *, const pthread_attr_t *,
                             void *(*) (void*), void *));

mypthreads.c

#include <stdio.h>
#include <pthread.h>

int my_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg)
{
    printf("Did you get the messsage?");
    return pthread_create(thread, attr, start_routine, arg);
}


int (*sys_pthreads_create)(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg) = my_pthread_create;

编辑:在我调用函数指针并成功时添加了 gdb 的输出。

问题是您在 mypthreads.h 中的声明类型错误:

extern int (*sys_pthreads_create(pthread_t *, const pthread_attr_t *, void *(*) (void*), void *));

由于括号放错了地方,这个符号的类型是一个函数,returns 一个指向 int 的指针,但你的实际 sys_pthreads_create 对象是一个指向函数的指针。

这意味着当您调用:

ret = (*sys_pthreads_create)(opts ? &opts->thr : &thr, 0, thread, arg);

sys_pthreads_create 通过隐式获取函数地址转换为指向函数的指针,然后取消引用并调用该地址。但这并不是真正的函数地址——它是指向函数的指针的地址!因此,调用会跳转到 sys_pthreads_create 所在的数据段,并在尝试将函数指针作为代码执行时崩溃(或由于不可执行的映射而崩溃)。

在 gdb 输出中有一条线索:

#0  0x080660c4 in sys_pthreads_create ()

它说它在 sys_pthreads_create 内执行 - 但 sys_pthreads_create 是一个变量,而不是函数。

如果您在 mypthreads.c 中包含 <mypthreads.h>,编译器会为您诊断出这个问题,因为 sys_pthreads_create 的冲突类型对它来说是可见的(这就是为什么您应该始终在定义这些对象的源文件中包含声明对象的头文件。

正确的声明当然是匹配mypthreads.c:

extern int (*sys_pthreads_create)(pthread_t *thread, const pthread_attr_t *attr,
                      void *(*start_routine) (void *), void *arg);

gdb能够成功调用函数指针的原因是gdb使用存储在调试信息中的类型信息来确定sys_pthreads_create的类型,而不是来自的虚假信息头文件。