[PATCH] D59510: [NFC][TSan][libdispatch] Fix test for dispatch_apply[_f]

Julian Lettner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 18 13:46:06 PDT 2019


yln created this revision.
Herald added subscribers: llvm-commits, Sanitizers, jdoerfert, kubamracek.
Herald added projects: Sanitizers, LLVM.

- Array index out of bounds: 100 iterations, but size of array is 2.
- Unmatched barrier_init (2) with barrier_wait (200)
- Number of iterations must be smaller than the available parallelism for the queue, otherwise we deadlock (since every barrier_wait call blocks the thread).

Scary: All of this worked reliably in gcd-apply.mm (for Darwin)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D59510

Files:
  compiler-rt/test/tsan/libdispatch/apply.c


Index: compiler-rt/test/tsan/libdispatch/apply.c
===================================================================
--- compiler-rt/test/tsan/libdispatch/apply.c
+++ compiler-rt/test/tsan/libdispatch/apply.c
@@ -1,16 +1,13 @@
 // RUN: %clang_tsan %s -o %t
 // RUN: %run %t 2>&1 | FileCheck %s
 
-// TODO(yln): Deadlocks while gcd-apply.mm does not. What's the difference
-//            between C and Obj-C compiler?
-// REQUIRES: disable
-
 #include <dispatch/dispatch.h>
 
 #include "../test.h"
 
+const size_t size = 5;
 long global;
-long array[2];
+long array[size];
 
 void callback(void *context, size_t i) {
   long n = global;
@@ -19,7 +16,6 @@
 }
 
 int main(int argc, const char *argv[]) {
-  barrier_init(&barrier, 2);
   fprintf(stderr, "start\n");
 
   // Warm up GCD (workaround for macOS Sierra where dispatch_apply might run single-threaded).
@@ -29,24 +25,34 @@
 
   global = 42;
 
-  dispatch_apply(100, q, ^(size_t i) {
+  barrier_init(&barrier, size);
+  dispatch_apply(size, q, ^(size_t i) {
     long n = global;
     array[i] = n + i;
     barrier_wait(&barrier);
   });
 
-  for (int i = 0; i < 100; i++) {
-    fprintf(stderr, "array[%d] = %ld\n", i, array[i]);
+  for (size_t i = 0; i < size; i++) {
+    fprintf(stderr, "array[%ld] = %ld\n", i, array[i]);
   }
 
-  global = 43;
+  global = 142;
+
+  barrier_init(&barrier, size);
+  dispatch_apply_f(size, q, NULL, &callback);
 
-  dispatch_apply_f(100, q, NULL, &callback);
+  for (size_t i = 0; i < size; i++) {
+    fprintf(stderr, "array[%ld] = %ld\n", i, array[i]);
+  }
 
   fprintf(stderr, "done\n");
   return 0;
 }
 
 // CHECK: start
+// CHECK: array[0] = 42
+// CHECK: array[4] = 46
+// CHECK: array[0] = 142
+// CHECK: array[4] = 146
 // CHECK: done
 // CHECK-NOT: WARNING: ThreadSanitizer


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59510.191165.patch
Type: text/x-patch
Size: 1780 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190318/7ce291c8/attachment.bin>


More information about the llvm-commits mailing list