1

The following code sample is compiled with the subsequent command line input

#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);
}

Command line input:

g++ thread.cpp -fsanitize=thread -fPIE -pie -lpie -g

It compiles fine, but when the code is run there are runtime errors.

FATAL: ThreadSanitizer can not mmap the shadow memory (something is mapped at 0x56167ae3b000 < 0x7cf000000000)
FATAL: Make sure to compile with -fPIE and to link with -pie.

I am running this with a version of g++ that has fSanitize so I am unsure about where the source of the problem is?

g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-28)
coolDude78
  • 11
  • 2
  • You must exchange the lines `printf` and `pthread_join`. Why? Think about it. – 273K Aug 28 '18 at 02:12
  • This code snippet is meant to have a a problem. It is from the readme of the threadsanitizer github page https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual – coolDude78 Aug 28 '18 at 20:25

1 Answers1

0

GCC is too old for Linux kernel used in your RedHat. Due to the mapped address 0x56167ae3b000 I guess the kernel version is 4.1+ (or backported from the kernel version 4.1+) that maps binaries at 0x550000000000. This mapped address is supported by GCC starting from the version 7.1.1. Please try to add the compiler flag -static-libtsan. If it does not help then you need to upgrade your compiler.

273K
  • 29,503
  • 10
  • 41
  • 64