Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testthread isn't thread-safe: non-locked access to global variable 'alive' #10711

Closed
smcv opened this issue Sep 5, 2024 · 0 comments · Fixed by #10713
Closed

testthread isn't thread-safe: non-locked access to global variable 'alive' #10711

smcv opened this issue Sep 5, 2024 · 0 comments · Fixed by #10713

Comments

@smcv
Copy link
Contributor

smcv commented Sep 5, 2024

In testthread, the main thread and a helper thread communicate via a global variable alive, but concurrent non-atomic accesses to variables are not thread-safe (undefined behaviour). ThreadSanitizer diagnoses this:

WARNING: ThreadSanitizer: data race (pid=1486312)
  Read of size 4 at 0x55c924964494 by thread T1:
    #0 ThreadFunc /home/smcv/src/SDL/test/testthread.c:65 (testthread+0x14df8) (BuildId: ff8455198b6ec3b4e9d2d60887c1c34f84865e83)
    #1 SDL_RunThread /home/smcv/src/SDL/src/thread/SDL_thread.c:323 (libSDL3.so.0+0x850252) (BuildId: 2ad90d76e3ecc027c5bab3986c6c2f3310e6312f)
    #2 RunThread /home/smcv/src/SDL/src/thread/pthread/SDL_systhread.c:69 (libSDL3.so.0+0xc89efd) (BuildId: 2ad90d76e3ecc027c5bab3986c6c2f3310e6312f)

  Previous write of size 4 at 0x55c924964494 by main thread:
    #0 main /home/smcv/src/SDL/test/testthread.c:147 (testthread+0x151f6) (BuildId: ff8455198b6ec3b4e9d2d60887c1c34f84865e83)

  As if synchronized via sleep:
    #0 nanosleep ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:397 (libtsan.so.2+0x54d2f) (BuildId: 10df5f7ccae7f4e0cda410a7307bbff5fb61b777)
    #1 SDL_SYS_DelayNS /home/smcv/src/SDL/src/timer/unix/SDL_systimer.c:170 (libSDL3.so.0+0xc92065) (BuildId: 2ad90d76e3ecc027c5bab3986c6c2f3310e6312f)
    #2 SDL_Delay_REAL /home/smcv/src/SDL/src/timer/SDL_timer.c:638 (libSDL3.so.0+0x853caf) (BuildId: 2ad90d76e3ecc027c5bab3986c6c2f3310e6312f)
    #3 SDL_Delay /home/smcv/src/SDL/src/dynapi/SDL_dynapi_procs.h:165 (libSDL3.so.0+0x622d35) (BuildId: 2ad90d76e3ecc027c5bab3986c6c2f3310e6312f)
    #4 ThreadFunc /home/smcv/src/SDL/test/testthread.c:75 (testthread+0x14dec) (BuildId: ff8455198b6ec3b4e9d2d60887c1c34f84865e83)
    #5 SDL_RunThread /home/smcv/src/SDL/src/thread/SDL_thread.c:323 (libSDL3.so.0+0x850252) (BuildId: 2ad90d76e3ecc027c5bab3986c6c2f3310e6312f)
    #6 RunThread /home/smcv/src/SDL/src/thread/pthread/SDL_systhread.c:69 (libSDL3.so.0+0xc89efd) (BuildId: 2ad90d76e3ecc027c5bab3986c6c2f3310e6312f)

  Location is global 'alive' of size 4 at 0x55c924964494 (testthread+0x41494)

  Thread T1 'One' (tid=1486314, running) created by main thread at:
    #0 pthread_create ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:1022 (libtsan.so.2+0x568a6) (BuildId: 10df5f7ccae7f4e0cda410a7307bbff5fb61b777)
    #1 SDL_SYS_CreateThread /home/smcv/src/SDL/src/thread/pthread/SDL_systhread.c:111 (libSDL3.so.0+0xc89fca) (BuildId: 2ad90d76e3ecc027c5bab3986c6c2f3310e6312f)
    #2 SDL_CreateThreadWithPropertiesRuntime_REAL /home/smcv/src/SDL/src/thread/SDL_thread.c:383 (libSDL3.so.0+0x8505dc) (BuildId: 2ad90d76e3ecc027c5bab3986c6c2f3310e6312f)
    #3 SDL_CreateThreadRuntime_REAL /home/smcv/src/SDL/src/thread/SDL_thread.c:403 (libSDL3.so.0+0x8507c7) (BuildId: 2ad90d76e3ecc027c5bab3986c6c2f3310e6312f)
    #4 SDL_CreateThreadRuntime /home/smcv/src/SDL/src/dynapi/SDL_dynapi_procs.h:158 (libSDL3.so.0+0x622b43) (BuildId: 2ad90d76e3ecc027c5bab3986c6c2f3310e6312f)
    #5 main /home/smcv/src/SDL/test/testthread.c:140 (testthread+0x151ad) (BuildId: ff8455198b6ec3b4e9d2d60887c1c34f84865e83)

SUMMARY: ThreadSanitizer: data race /home/smcv/src/SDL/test/testthread.c:65 in ThreadFunc

This should probably be a SDL_AtomicInt?

slouken added a commit to slouken/SDL that referenced this issue Sep 5, 2024
@slouken slouken closed this as completed in a0f36fb Sep 5, 2024
slouken added a commit that referenced this issue Sep 5, 2024
slouken added a commit that referenced this issue Sep 5, 2024
Fixes #10711

(cherry picked from commit a0f36fb)
(cherry picked from commit a75227a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant