Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

ThreadSanitizer (aka TSan) is a data race detector for C/C++. Data races are one of the most common and hardest to debug types of bugs in concurrent systems. A data race occurs when two threads access the same variable concurrently and at least one of the accesses is write. An example is provided below:

Code Block
languagecpp
themeEmacs
titledata-race.c
#include <pthread.h>
int global = 0;

void *threadfunc(void *x) {
  global = global + 1;
  return x;
}

int main() {
  pthread_t t1;
  pthread_t t2;
  pthread_create(&t1, NULL, threadfunc, NULL);
  pthread_create(&t2, NULL, threadfunc, NULL);
  pthread_join(t1, NULL);
  pthread_join(t2, NULL);
  return global;
}


Code Block
languagebash
themeEmacs
titleThread Sanitizer: data race
WARNING: ThreadSanitizer:DEADLYSIGNAL
==204==ERROR: ThreadSanitizer: SEGV on unknown address 0x000000000000 (pc 0x0000004883e8 bp 0x7f697bfbf2f0 sp 0x7f697bfbf2d8 T205)
==204==The signal is caused by a WRITE memory access.
==204==Hint: address points to the zero page.
ThreadSanitizer:DEADLYSIGNAL
ThreadSanitizer: nested bug in the same thread, aborting. data race (pid=371)
  Write of size 4 at 0x000000f12358 by thread T2:
    #0 threadfunc <null> (a.out+0x4b18ee)

  Previous write of size 4 at 0x000000f12358 by thread T1:
    #0 threadfunc <null> (a.out+0x4b18ee)

  Location is global 'global' of size 4 at 0x000000f12358 (a.out+0x000000f12358)

  Thread T2 (tid=374, running) created by main thread at:
    #0 pthread_create <null> (a.out+0x424a2b)
    #1 main <null> (a.out+0x4b1960)

  Thread T1 (tid=373, finished) created by main thread at:
    #0 pthread_create <null> (a.out+0x424a2b)
    #1 main <null> (a.out+0x4b1941)

SUMMARY: ThreadSanitizer: data race (/mnt/c/Users/Pascal/netdef/a.out+0x4b18ee) in threadfunc
==================
ThreadSanitizer: reported 1 warnings

Memory Sanitizer

MemorySanitizer (MSan) is a detector of uninitialized memory reads in C/C++ programs.

...