3

I am trying to test the usage of -fsanitize=thread for gcc, and its complaining of unexpected memory mapping, maybe there might have been some change in the kernel, and thats the reason for it. Is there any thing I could do to make it work ?

This is what I am doing ...

mfrw@kp ...fpp/asgn/as2 % 
mfrw@kp ...fpp/asgn/as2 % cat tiny.cpp 
#include <pthread.h>
int global;
void *thread(void *x) {
        global = 42;
        return x;
}
int main() {
        pthread_t t;
        pthread_create(&t, NULL, thread, NULL);
        global = 43;
        pthread_join(t, NULL);
        return global;
}
mfrw@kp ...fpp/asgn/as2 % g++ tiny.cpp -fsanitize=thread -pie -fPIC -g -O1 -o tinyrace -pthread
mfrw@kp ...fpp/asgn/as2 % uname -a
Linux kp 4.4.33-1-MANJARO #1 SMP PREEMPT Fri Nov 18 18:06:44 UTC 2016 x86_64 GNU/Linux
mfrw@kp ...fpp/asgn/as2 % gcc --version
gcc (GCC) 6.2.1 20160830
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

mfrw@kp ...fpp/asgn/as2 % ./tinyrace 
FATAL: ThreadSanitizer: unexpected memory mapping 0x55e38776b000-0x55e38776c000
mfrw@kp ...fpp/asgn/as2 % 
mfrw
  • 99
  • 1
  • 11
  • Got the same trash at the moment. Looking for solution as well. – Alexander Shukaev Mar 02 '17 at 23:45
  • for me, it worked with an older kernel... I had an ancient server with kernel 2.6.32, so maybe its something with the kernel – mfrw Mar 04 '17 at 18:52
  • Same issue: Ubuntu 17.04 ; kernel ver 4.10.0-33-generic ; gcc 6.3.0 . Not using '-pie -fPIC' does not solve the problem. Awaiting an answer.. – kaiwan Sep 02 '17 at 11:48

2 Answers2

1

It is to do with your compilation option: -pie -fPIC.

If I compiled your code (in Ubuntu 16.04, latest update) with:

g++ -fsanitize=thread -pie -fPIC tinyrace.c -g -O1 -o tinyrace -pthread

I will get the same error.

But if changed to:

g++ -fsanitize=thread tinyrace.c -g -O1 -o tinyrace -pthread

Then the race condition alert is printed:

./tinyrace 
==================
WARNING: ThreadSanitizer: data race (pid=12032)
  Write of size 4 at 0x00000060108c by thread T1:
    #0 thread(void*) /home/tteikhua/tinyrace.c:5 (tinyrace+0x000000400a5d)
    #1 <null> <null> (libtsan.so.0+0x0000000230d9)

  Previous write of size 4 at 0x00000060108c by main thread:
    #0 main /home/tteikhua/tinyrace.c:11 (tinyrace+0x000000400ab1)

  Location is global 'global' of size 4 at 0x00000060108c (tinyrace+0x00000060108c)

  Thread T1 (tid=12034, running) created by main thread at:
    #0 pthread_create <null> (libtsan.so.0+0x000000027577)
    #1 main /home/tteikhua/tinyrace.c:10 (tinyrace+0x000000400aa7)

SUMMARY: ThreadSanitizer: data race /home/tteikhua/tinyrace.c:5 thread(void*)
Peter Teoh
  • 6,337
  • 4
  • 42
  • 58
1

Yes, it's due to changes in the kernel and it's not GCC-specific, clang exposes the same behaviour.

There is a corresponding bug in GCC tracker, which references fix in the upstream. Comments mention kernels 4.1+, but I hit this problem on 3.16.

As mentioned in the answer by Peter Teoh, it might work if you omit pie/pic options, but the proper fix is in newer thread sanitizer used by newer compilers (after September 2016, but it's not clear whether GCC 6.x branch got the fix).

Peter Teoh
  • 6,337
  • 4
  • 42
  • 58
xaizek
  • 5,098
  • 1
  • 34
  • 60