Fix potential data race in GC_SysVGetDataStart (SPARC)
authorIvan Maidanski <ivmai@mail.ru>
Mon, 5 Dec 2016 21:49:33 +0000 (00:49 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Mon, 5 Dec 2016 21:49:33 +0000 (00:49 +0300)
* os_dep.c [SVR4 || AUX || DGUX || LINUX && SPARC]
(GC_SysVGetDataStart): Use AO_fetch_and_add(p,0) if available instead
of non-atomic read/write in *p = *p statement (thus, to avoid data race
though it is highly unlikely).

os_dep.c

index ae9c338..b78943a 100644 (file)
--- a/os_dep.c
+++ b/os_dep.c
@@ -1872,11 +1872,17 @@ void GC_register_data_segments(void)
     GC_setup_temporary_fault_handler();
     if (SETJMP(GC_jmp_buf) == 0) {
         /* Try writing to the address.  */
-        char v = *result;
-#       if defined(CPPCHECK)
-          GC_noop1((word)&v);
+#       ifdef AO_HAVE_fetch_and_add
+          volatile AO_t zero = 0;
+          (void)AO_fetch_and_add((volatile AO_t *)result, zero);
+#       else
+          /* Fallback to non-atomic fetch-and-store.    */
+          char v = *result;
+#         if defined(CPPCHECK)
+            GC_noop1((word)&v);
+#         endif
+          *result = v;
 #       endif
-        *result = v;
         GC_reset_fault_handler();
     } else {
         GC_reset_fault_handler();