1 // Verifies that ThreadSanitizer is able to detect a data race in heap allocated
4 // Test case minimizes the use of the standard library to avoid its ambiguous
5 // status with respect to instrumentation (it could vary depending on whatever
6 // a function call is inlined or not).
8 // The conflicting data access is de-facto synchronized with a special TSAN
9 // barrier, which does not introduce synchronization from TSAN perspective, but
10 // is necessary to make the test robust. Without the barrier data race detection
11 // would occasionally fail, making test flaky.
13 // needs-sanitizer-support
14 // needs-sanitizer-thread
16 // compile-flags: -Z sanitizer=thread -O
19 // error-pattern: WARNING: ThreadSanitizer: data race
20 // error-pattern: Location is heap block of size 4
21 // error-pattern: allocated by main thread
23 #![feature(raw_ref_op)]
24 #![feature(rustc_private)]
30 static mut BARRIER: u64 = 0;
33 fn __tsan_testonly_barrier_init(barrier: *mut u64, count: u32);
34 fn __tsan_testonly_barrier_wait(barrier: *mut u64);
37 extern "C" fn start(c: *mut libc::c_void) -> *mut libc::c_void {
39 let c: *mut u32 = c.cast();
41 __tsan_testonly_barrier_wait(&raw mut BARRIER);
48 __tsan_testonly_barrier_init(&raw mut BARRIER, 2);
49 let c: *mut u32 = Box::into_raw(Box::new(1));
50 let mut t: libc::pthread_t = mem::zeroed();
51 libc::pthread_create(&mut t, ptr::null(), start, c.cast());
52 __tsan_testonly_barrier_wait(&raw mut BARRIER);
54 libc::pthread_join(t, ptr::null_mut());