[compiler-rt] r234036 - profile: Protect .gcda output with flock

Justin Bogner mail at justinbogner.com
Fri Apr 3 11:55:44 PDT 2015


Author: bogner
Date: Fri Apr  3 13:55:44 2015
New Revision: 234036

URL: http://llvm.org/viewvc/llvm-project?rev=234036&view=rev
Log:
profile: Protect .gcda output with flock

This avoids crashing or corrupting data if multiple concurrent
processes write to the same .gcda file. This is hard to test, since
the previous behaviour was a data race that often worked out, and it
ignores errors in flock to fall back to the old racy behaviour so that
it won't degrade the behaviour on filesystems that don't support
flock.

Modified:
    compiler-rt/trunk/lib/profile/GCDAProfiling.c

Modified: compiler-rt/trunk/lib/profile/GCDAProfiling.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/GCDAProfiling.c?rev=234036&r1=234035&r2=234036&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/GCDAProfiling.c (original)
+++ compiler-rt/trunk/lib/profile/GCDAProfiling.c Fri Apr  3 13:55:44 2015
@@ -294,6 +294,11 @@ void llvm_gcda_start_file(const char *or
     }
   }
 
+  /* Try to flock the file to serialize concurrent processes writing out to the
+   * same GCDA. This can fail if the filesystem doesn't support it, but in that
+   * case we'll just carry on with the old racy behaviour and hope for the best.
+   */
+  flock(fd, LOCK_EX);
   output_file = fdopen(fd, mode);
 
   /* Initialize the write buffer. */
@@ -493,6 +498,7 @@ void llvm_gcda_end_file() {
     }
 
     fclose(output_file);
+    flock(fd, LOCK_UN);
     output_file = NULL;
     write_buffer = NULL;
   }





More information about the llvm-commits mailing list