运行时ThreadSanitizer FATAL异常

时间:2014-11-21 09:59:19

标签: c++ multithreading gcc

我一直在努力让ThreadSanitizer与我的gcc版本(4.8.2)一起使用,所以我举了他们的简单示例:

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

typedef std::map<std::string, std::string> map_t;

void *threadfunc(void *p) {
  map_t& m = *(map_t*)p;
  m["foo"] = "bar";
  return 0;
}

int main() {
  map_t m;
  pthread_t t;
  pthread_create(&t, 0, threadfunc, &m);
  printf("foo=%s\n", m["foo"].c_str());
  pthread_join(t, 0);
}

并在没有-fsanitize=thread的情况下编译它,如下所示:

  

g ++ -o testtsan testtsan.cpp -lpthread

这很好,然后我添加了线程消毒剂

  

g ++ -o testtsan testtsan.cpp -lpthread -fsanitize = thread

但当然没有-pie -fPIC

就失败了
  

g ++ -o testtsan testtsan.cpp -lpthread -fsanitize = thread -pie -fPIC

然后编译然后在运行时,我得到:

FATAL: ThreadSanitizer CHECK failed: ../../../../libsanitizer/sanitizer_common/sanitizer_allocator.h:310 "((kSpaceBeg)) == ((reinterpret_cast<uptr>(Mprotect(kSpaceBeg, kSpaceSize))))" (0x7d0000000000, 0xffffffffffffffff)
FATAL: ThreadSanitizer: failed to intercept pthread_mutex_lock

检查strace时,这似乎是因为它尝试mmap 1TB的内存,因此ENOMEM失败。我已经启用了ASLR,现在我对这可能是什么感到茫然 - 所以问题是,有没有人能成功实现这一目标?

在我潜入图书馆代码之前,我希望有人可能已经遇到过这个......

环境: GCC 4.8.2 尝试内核:3.0.10和2.6.32(所有Suse),没有运气......

1 个答案:

答案 0 :(得分:2)

当我使用clang 3.4.2的TSan在SLES11SP3上遇到此故障时,我能够通过首先更改我的shell的ulimit来修复它,这样我就可以创建一个大的映射,然后我需要以超级用户身份执行它。

$ ./a.out
FATAL: ThreadSanitizer CHECK failed: bri/llvm-3.4.2.src/projects/compiler-rt/lib/sanitizer_common/sanitizer_allocator.h:316 "((kSpaceBeg)) == (( reinterpret_cast<uptr>(Mprotect(kSpaceBeg, kSpaceSize))))" (0x7d0000000000, 0xfffffffffffffff4)
FATAL: ThreadSanitizer: failed to intercept pthread_mutex_lock

$ ulimit -v
10588960
$ ulimit -v $((10588960*1024))
$ ulimit -v
10843095040
$ ./a.out
==11348==WARNING: Program is run with limited virtual address space, which wouldn't work with ThreadSanitizer.
==11348==Re-execing with unlimited virtual address space.
==11348==WARNING: Program is run with limited virtual address space, which wouldn't work with ThreadSanitizer.
==11348==Re-execing with unlimited virtual address space.
==11348==WARNING: Program is run with limited virtual address space, which wouldn't work with ThreadSanitizer.
==11348==Re-execing with unlimited virtual address space.
...
# only able to recover w/Ctrl-C...

$ sudo ./a.out
root's password:
==11351==WARNING: Program is run with limited virtual address space, which wouldn't work with ThreadSanitizer.
==11351==Re-execing with unlimited virtual address space.
llvm-symbolizer: Unknown command line argument '--default-arch=x86_64'.  Try: '/usr/bin/llvm-symbolizer -help'
llvm-symbolizer: Did you mean '-demangle=x86_64'?
==11351==WARNING: Can't read from symbolizer at fd 3
llvm-symbolizer: Unknown command line argument '--default-arch=x86_64'.  Try: '/usr/bin/llvm-symbolizer -help'
llvm-symbolizer: Did you mean '-demangle=x86_64'?
==11351==WARNING: external symbolizer didn't start up correctly!
==11351==WARNING: Failed to use and restart external symbolizer!
==================
WARNING: ThreadSanitizer: data race (pid=11351)
  Write of size 4 at 0x7fbca5148c48 by thread T1:
    #0 Thread1 /home/bri/tmp/tsan/tiny_race.c:4 (exe+0x0000000ad64f)
    #1 <null> <null>:0 (a.out+0x000000052af4)

  Previous write of size 4 at 0x7fbca5148c48 by main thread:
    #0 main /home/bri/tmp/tsan/tiny_race.c:11 (exe+0x0000000ad6a3)

  Thread T1 (tid=11354, running) created by main thread at:
    #0 pthread_create bri/tsan/rtl/tsan_interceptors.cc:877 (exe+0x000000052c2b)
    #1 main /home/bri/tmp/tsan/tiny_race.c:10 (exe+0x0000000ad694)

SUMMARY: ThreadSanitizer: data race /home/bri/tmp/tsan/tiny_race.c:4 Thread1
==================
ThreadSanitizer: reported 1 warnings

symbolizer警告可能是与我clang的特定构建相关的失败,只是忽略它们。它可能仅用于解码C ++符号名称。