llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

TSan: atomic compare-and-swap under thread sanitizer after multithreaded fork is incorrect

Open llvmbot opened this issue 11 years ago • 2 comments

Bugzilla Link 23176
Resolution FIXED
Resolved on Jun 11, 2018 00:51
Version 3.5
OS Linux
Reporter LLVM Bugzilla Contributor
CC @KernelAddress,@vitalybuka

Extended Description

Many of the atomic compare-and-swap primitives work incorrectly under thread sanitizer after a multithreaded fork. Some of the ones which do not work: __sync_bool_compare_and_swap, __sync_val_compare_and_swap, __tsan_atomic32_compare_exchange_val. They all return a value indicating that the variable did not contain the correct value at the start, even when it does. However, they do change the value of the variable correctly. The variants which return the previous value always return 1.

__tsan_atomic32_compare_exchange_strong does work correctly under the same circumstances.

Here's a simple test program which shows the problem: #include <stdio.h> #include <unistd.h> #include <sanitizer/tsan_interface_atomic.h>

#include

void succeed() { int test = 4; int result = 4; __tsan_atomic32_compare_exchange_strong(&test, &result, 5, __tsan_memory_order_seq_cst, __tsan_memory_order_seq_cst); printf("got %d value is %d\n", result, test); }

void fail2() { int test = 4; int result = __tsan_atomic32_compare_exchange_val( &test, 4, 5, __tsan_memory_order_seq_cst, __tsan_memory_order_seq_cst); printf("got %d value is %d\n", result, test); }

void fail1() { int test = 4; int result = __sync_val_compare_and_swap(&test, 4, 5); printf("got %d value is %d\n", result, test); }

void nop() {}

int main() { ::std::thread thread1(nop); if (fork() == 0) { fail1(); fail2(); succeed(); exit(0); } else { thread1.join(); } }

Here's what it prints for me: got 1 value is 5 got 1 value is 5 got 4 value is 5

Those 1s should be 4s.

llvmbot avatar Apr 09 '15 21:04 llvmbot

I've checked both 4.0.1-10 and clang version 7.0.0 (trunk 326052), both print: got 4 value is 5 got 4 value is 5 got 4 value is 5 Let's consider this fixed. I can't find revision that fixed this.

KernelAddress avatar Jun 11 '18 07:06 KernelAddress

Is this still the issue?

vitalybuka avatar Jun 09 '18 01:06 vitalybuka