...
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 |
---|
language | cpp |
---|
theme | Emacs |
---|
title | data-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 |
---|
language | bash |
---|
theme | Emacs |
---|
title | Thread 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.
Code Block |
---|
language | cpp |
---|
theme | Emacs |
---|
title | uninit-mem.c |
---|
|
int main() {
int x;
return x;
} |
Code Block |
---|
language | bash |
---|
theme | Emacs |
---|
title | Memory Sanitizer |
---|
|
==247==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x495056 in main (/mnt/c/Users/Pascal/netdef/a.out+0x495056)
#1 0x7f43b7ed10b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
#2 0x41c26d in _start (/mnt/c/Users/Pascal/netdef/a.out+0x41c26d)
SUMMARY: MemorySanitizer: use-of-uninitialized-value (/mnt/c/Users/Pascal/netdef/a.out+0x495056) in main |
...