mfrw
mfrw

Reputation: 129

Thread sanitizer complaining of unexpected memory map

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 % 

Upvotes: 3

Views: 2594

Answers (2)

xaizek
xaizek

Reputation: 5252

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).

Upvotes: 1

Peter Teoh
Peter Teoh

Reputation: 6753

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*)

Upvotes: 1

Related Questions