为什么Valgrind在我发布新线程时出现段错误

时间:2017-07-08 20:18:52

标签: c++ multithreading segmentation-fault valgrind

我正在用C ++编写一个程序,我发现了一些很奇怪的东西。

当我在Xcode下运行程序时,一切正常,但是当我在Valgrind下执行此操作时,它会在几秒后给我一个segmentation fault

我设法提取了一个非常简单的代码,它给了我这个错误:

#include <thread>

void exec_1() {}

int main(int argc, const char * argv[]) {

    std::thread simulator_thread;
    simulator_thread = std::thread(exec_1);
    simulator_thread.join();

    return 0;
}

我正在做的只是使用这些标志在Xcode下构建我的可执行文件:

CFLAGS:

-I/usr/local/lib/python3.6/site-packages/numpy/core/include
-I/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/include/python3.6m 
-Wno-unused-result -Wsign-compare -Wunreachable-code
-fno-common -dynamic -DNDEBUG -g -fwrapv -Wall -Wstrict-prototypes

LDFLAGS:

-L/usr/local/opt/python3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin
-lpython3.6m -ldl -framework CoreFoundation

然后在Valgrind下运行可执行文件以查找内存泄漏。您会看到我正在调用Python C API,因为我在我的main代码中使用了该代码,但此代码会在不使用它们的情况下向我发送segfault

无论如何,Valgrind以及其他一些东西给了我以下输出:

Thread 2:
==41660== Invalid read of size 4
==41660==    at 0x1016FA899: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
==41660==    by 0x1016FA886: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==41660==    by 0x1016FA08C: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
==41660==  Address 0x18 is not stack'd, malloc'd or (recently) free'd
==41660== 
==41660== 
==41660== Process terminating with default action of signal 11 (SIGSEGV)
==41660==  Access not within mapped region at address 0x18
==41660==    at 0x1016FA899: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
==41660==    by 0x1016FA886: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==41660==    by 0x1016FA08C: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
==41660==  If you believe this happened as a result of a stack
==41660==  overflow in your program's main thread (unlikely but
==41660==  possible), you can try to increase the size of the
==41660==  main thread stack using the --main-stacksize= flag.
==41660==  The main thread stack size used in this run was 8388608.
--41660:0:schedule VG_(sema_down): read returned -4

在Valgrind下生成一个线程是否可能导致错误?

P.S:
我的操作系统为MacOS 10.12.5,我正在使用Xcode 8.3.3Valgrind 3.13.0

2 个答案:

答案 0 :(得分:7)

  

在Valgrind下生成一个线程是否可能导致错误?

在运行使用pthread的二进制文件时,看起来这确实是Mac OS X上Valgrind的一个问题:

不在_pthread_find_thread(OS X 10.11)中的映射区域内访问 https://bugs.kde.org/show_bug.cgi?id=349128

你在Valgrind的失败看起来与此处报道的类似:

std::thread.join() SIGSEGV on Mac OS under Valgrind

答案 1 :(得分:2)

我仍然面临与macOS 10.12.6 + Valgrind 3.13.0相同的问题。

==82699== Process terminating with default action of signal 11 (SIGSEGV)
==82699==  Access not within mapped region at address 0x18
==82699==    at 0x10058E899: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
==82699==    by 0x10058E886: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==82699==    by 0x10058E08C: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
==82699==  If you believe this happened as a result of a stack
==82699==  overflow in your program's main thread (unlikely but
==82699==  possible), you can try to increase the size of the
==82699==  main thread stack using the --main-stacksize= flag.
==82699==  The main thread stack size used in this run was 8388608.
--82699:0:schedule VG_(sema_down): read returned -4
==82699== 
==82699== Events    : Ir
==82699== Collected : 30468496
==82699== 
==82699== I   refs:      30,468,496
Segmentation fault: 11

刚发现还有其他错误跟踪https://bugs.kde.org/show_bug.cgi?id=380269。 这真是一个历史性的错误。希望不再需要2年才能修复。