Try   HackMD

Thread Sanitizer (TSan)

What is "Thread Sanitizer"?

Thread Sanitizer is a tool for detection race conditions or deadlocks in multi-thread

How to use it ?

  1. set the compiler flag
-fsanitize=thread: enable thread sanitize
    suggestion add "fPIE" and ""-pie"
-g: create racing line number

-ltsan: link all object code with thread sanitizer
    if create object code and linked by user manual, need this option
-m64: support 64bit
  1. running the code
    e.g.
WARNING: ThreadSanitizer: data race (pid=66577)
  Read of size 8 at 0x7bc400000018 by main thread:
    #0 cmap_utilization /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/cmap.c:145 (test-cmap+0x32cf) (BuildId: 3d54b47dc6687ae54d93b6c7e8381d995f470352)
    #1 main /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/test-cmap.c:195 (test-cmap+0x29a9) (BuildId: 3d54b47dc6687ae54d93b6c7e8381d995f470352)

  Previous write of size 8 at 0x7bc400000018 by thread T4:
    #0 cmap_insert__ /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/cmap.c:38 (test-cmap+0x2fa6) (BuildId: 3d54b47dc6687ae54d93b6c7e8381d995f470352)
    #1 cmap_expand_callback /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/cmap.c:74 (test-cmap+0x2fa6)
    #2 rcu_free /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/rcu.c:29 (test-cmap+0x3d5e) (BuildId: 3d54b47dc6687ae54d93b6c7e8381d995f470352)
    #3 rcu_set__ /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/rcu.c:70 (test-cmap+0x4077) (BuildId: 3d54b47dc6687ae54d93b6c7e8381d995f470352)
    #4 cmap_expand /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/cmap.c:109 (test-cmap+0x3689) (BuildId: 3d54b47dc6687ae54d93b6c7e8381d995f470352)
    #5 cmap_insert /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/cmap.c:169 (test-cmap+0x3689)
    #6 insert_value /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/test-cmap.c:41 (test-cmap+0x4591) (BuildId: 3d54b47dc6687ae54d93b6c7e8381d995f470352)
    #7 update_cmap /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/test-cmap.c:116 (test-cmap+0x4623) (BuildId: 3d54b47dc6687ae54d93b6c7e8381d995f470352)

  As if synchronized via sleep:
    #0 usleep ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:375 (libtsan.so.2+0x5defd) (BuildId: 29a32c589768a10ed232cacef24e09c48a66ec41)
    #1 main /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/test-cmap.c:200 (test-cmap+0x2a0d) (BuildId: 3d54b47dc6687ae54d93b6c7e8381d995f470352)

  Location is heap block of size 65672 at 0x7bc400000000 allocated by thread T4:
    #0 malloc ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:681 (libtsan.so.2+0x3c2d7) (BuildId: 29a32c589768a10ed232cacef24e09c48a66ec41)
    #1 xmalloc /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/util.h:145 (test-cmap+0x30af) (BuildId: 3d54b47dc6687ae54d93b6c7e8381d995f470352)
    #2 cmap_impl_init /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/cmap.c:53 (test-cmap+0x30af)
    #3 cmap_expand /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/cmap.c:101 (test-cmap+0x35fe) (BuildId: 3d54b47dc6687ae54d93b6c7e8381d995f470352)
    #4 cmap_insert /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/cmap.c:169 (test-cmap+0x35fe)
    #5 insert_value /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/test-cmap.c:41 (test-cmap+0x4591) (BuildId: 3d54b47dc6687ae54d93b6c7e8381d995f470352)
    #6 update_cmap /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/test-cmap.c:116 (test-cmap+0x4623) (BuildId: 3d54b47dc6687ae54d93b6c7e8381d995f470352)

  Thread T4 (tid=66582, running) created by main thread at:
    #0 pthread_create ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:1036 (libtsan.so.2+0x3d189) (BuildId: 29a32c589768a10ed232cacef24e09c48a66ec41)
    #1 main /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/test-cmap.c:189 (test-cmap+0x28da) (BuildId: 3d54b47dc6687ae54d93b6c7e8381d995f470352)

SUMMARY: ThreadSanitizer: data race /home/roy/src-code/github/roy/linux2023_summer/hw3/cmap/cmap.c:145 in cmap_utilization
==================

racing happen in cmap.c:38

thread Sanitizer