Advertisement
eivanov89

TSAN race on vptr for object published via CAS

Oct 23rd, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4.  
  5. #include <pthread.h>
  6.  
  7. //------------------------------------------------------------------------------
  8.  
  9. class IFactory
  10. {
  11. public:
  12.     virtual ~IFactory() { /* */ }
  13.     virtual void bar() { printf("Default bar\n"); };
  14. private:
  15. };
  16.  
  17. class Factory : public IFactory
  18. {
  19. public:
  20.     virtual void bar() { printf("Factory::bar()\n"); }
  21. private:
  22. };
  23.  
  24. //------------------------------------------------------------------------------
  25.  
  26. IFactory * get_factory_instance()
  27. {
  28.     static IFactory * s_factory_instance = NULL;
  29.     if (s_factory_instance == NULL) {
  30.         IFactory * factory = new Factory();
  31.         if (__sync_val_compare_and_swap(&s_factory_instance, NULL, factory) != NULL) {
  32.             delete factory;
  33.         }
  34.     }
  35.     return s_factory_instance;
  36. }
  37.  
  38. //------------------------------------------------------------------------------
  39.  
  40. void * thread(void * x)
  41. {
  42.     IFactory * impl_factory = get_factory_instance();
  43.     impl_factory->bar();
  44.     return NULL;
  45. }
  46.  
  47. //------------------------------------------------------------------------------
  48.  
  49. int main()
  50. {
  51.   pthread_t t[2];
  52.   pthread_create(&t[0], NULL, thread, NULL);
  53.   pthread_create(&t[1], NULL, thread, NULL);
  54.   pthread_join(t[0], NULL);
  55.   pthread_join(t[1], NULL);
  56.  
  57.   return 0;
  58. }
  59.  
  60. /*
  61. Factory::bar()
  62. ==================
  63. WARNING: ThreadSanitizer: data race on vptr (ctor/dtor vs virtual call) (pid=2452)
  64.   Read of size 8 at 0x7d040000eff0 by thread T2:
  65.     #0 thread(void*) /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:43:5 (test_vptr_con+0x00000009eb71)
  66.  
  67.   Previous write of size 8 at 0x7d040000eff0 by thread T1:
  68.     #0 operator new(unsigned long) <null>:0 (test_vptr_con+0x0000000273fd)
  69.     #1 get_factory_instance() /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:30:9 (test_vptr_con+0x00000009ea20)
  70.     #2 thread(void*) /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:42:31 (test_vptr_con+0x00000009eb40)
  71.  
  72.   Location is heap block of size 8 at 0x7d040000eff0 allocated by thread T1:
  73.     #0 operator new(unsigned long) <null>:0 (test_vptr_con+0x0000000273fd)
  74.     #1 get_factory_instance() /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:30:9 (test_vptr_con+0x00000009ea20)
  75.     #2 thread(void*) /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:42:31 (test_vptr_con+0x00000009eb40)
  76.  
  77.   Thread T2 (tid=2464, running) created by main thread at:
  78.     #0 pthread_create <null>:0 (test_vptr_con+0x00000002a6c1)
  79.     #1 main /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:53:3 (test_vptr_con+0x00000009ec13)
  80.  
  81.   Thread T1 (tid=2463, finished) created by main thread at:
  82.     #0 pthread_create <null>:0 (test_vptr_con+0x00000002a6c1)
  83.     #1 main /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:52:3 (test_vptr_con+0x00000009ebe9)
  84.  
  85. SUMMARY: ThreadSanitizer: data race on vptr (ctor/dtor vs virtual call) /home/eiva/git/tb/src/servers/test_vptr_con/src/t.cpp:43 thread(void*)
  86. ==================
  87. Factory::bar()
  88. ThreadSanitizer: reported 1 warnings
  89. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement