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

时间:2016-10-26 21:54:37

标签: c unit-testing pthreads function-pointers stub

我试图将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)]
$4 = 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添加了输出。

1 个答案:

答案 0 :(得分:3)

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

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

由于错位的parantheses,此符号的类型是一个返回指针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.h>中添加了mypthreads.c,编译器会为您诊断出这一点,因为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的类型,而不是头文件中的伪造信息