summaryrefslogtreecommitdiff
path: root/lib/profile/GCDAProfiling.c
diff options
context:
space:
mode:
authorJustin Bogner <mail@justinbogner.com>2015-04-03 18:55:44 +0000
committerJustin Bogner <mail@justinbogner.com>2015-04-03 18:55:44 +0000
commit6f222309487f2735e122380ab76f06bf0f3092da (patch)
treeb8441c7e8e9acceb04fd3bfd77c7e210f899e797 /lib/profile/GCDAProfiling.c
parentd44226e57645c75f520d27b9744af47db9984839 (diff)
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. git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@234036 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/profile/GCDAProfiling.c')
-rw-r--r--lib/profile/GCDAProfiling.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/lib/profile/GCDAProfiling.c b/lib/profile/GCDAProfiling.c
index 45fbd07e5..c1a9c3808 100644
--- a/lib/profile/GCDAProfiling.c
+++ b/lib/profile/GCDAProfiling.c
@@ -294,6 +294,11 @@ void llvm_gcda_start_file(const char *orig_filename, const char version[4],
}
}
+ /* 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;
}