Eliminate data race in cons() of test_malloc
authorIvan Maidanski <ivmai@mail.ru>
Tue, 28 Nov 2017 08:06:36 +0000 (11:06 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Thu, 30 Nov 2017 15:51:51 +0000 (18:51 +0300)
(fix commit c058d9d)

The data race has not affected the functionality of the test but this
commit allows to avoid no_sanitize attribute for the function.

* tests/test_malloc.c (cons): Remove AO_ATTR_NO_SANITIZE_THREAD
attribute; Do not reset extra (use "%" operator to get my_extra value).
* tests/test_malloc.c [AO_HAVE_fetch_and_add1] (cons): Change type of
extra to AO_t, add volatile qualifier; use AO_fetch_and_add1 instead of
"++" operator to update extra.

tests/test_malloc.c

index a322099..2735e6b 100644 (file)
@@ -68,19 +68,19 @@ typedef struct list_node {
         int data;
 } ln;
 
-AO_ATTR_NO_SANITIZE_THREAD
 ln *cons(int d, ln *tail)
 {
-  static size_t extra = 0; /* data race in extra is OK */
-  size_t my_extra = extra;
+# ifdef AO_HAVE_fetch_and_add1
+    static volatile AO_t extra = 0;
+    size_t my_extra = (size_t)AO_fetch_and_add1(&extra) % 101;
+# else
+    static size_t extra = 0; /* data race in extra is OK */
+    size_t my_extra = (extra++) % 101;
+# endif
   ln *result;
   int * extras;
   unsigned i;
 
-  if (my_extra > 100)
-    extra = my_extra = 0;
-  else
-    ++extra;
   result = AO_malloc(sizeof(ln) + sizeof(int)*my_extra);
   if (result == 0)
     {