[PATCH] D54599: [Profile] Avoid race condition when dumping GCDA files.

calixte via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 15 13:50:27 PST 2018


calixte created this revision.
calixte added reviewers: marco-c, vsk.
Herald added subscribers: Sanitizers, llvm-commits, delcypher.

Seldom the test instrprof-gcov-fork.test is failing, so:

- add an O_EXCL when trying to create the file to avoid a race condition;
- add an error message when the locking is failing.


Repository:
  rCRT Compiler Runtime

https://reviews.llvm.org/D54599

Files:
  lib/profile/GCDAProfiling.c
  lib/profile/InstrProfilingUtil.c


Index: lib/profile/InstrProfilingUtil.c
===================================================================
--- lib/profile/InstrProfilingUtil.c
+++ lib/profile/InstrProfilingUtil.c
@@ -125,8 +125,7 @@
   }
   return 0;
 #else
-  flock(fd, LOCK_EX);
-  return 0;
+  return flock(fd, LOCK_EX);
 #endif
 }
 
@@ -150,8 +149,7 @@
   }
   return 0;
 #else
-  flock(fd, LOCK_UN);
-  return 0;
+  return flock(fd, LOCK_UN);
 #endif
 }
 
Index: lib/profile/GCDAProfiling.c
===================================================================
--- lib/profile/GCDAProfiling.c
+++ lib/profile/GCDAProfiling.c
@@ -352,26 +352,34 @@
     /* Try opening the file, creating it if necessary. */
     new_file = 1;
     mode = "w+b";
-    fd = open(filename, O_RDWR | O_CREAT | O_BINARY, 0644);
+    fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0644);
     if (fd == -1) {
       /* Try creating the directories first then opening the file. */
       __llvm_profile_recursive_mkdir(filename);
-      fd = open(filename, O_RDWR | O_CREAT | O_BINARY, 0644);
+      fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0644);
       if (fd == -1) {
-        /* Bah! It's hopeless. */
-        int errnum = errno;
-        fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
-                strerror(errnum));
-        return;
+        fd = open(filename, O_RDWR | O_BINARY);
+        if (fd == -1) {
+          /* Bah! It's hopeless. */
+          int errnum = errno;
+          fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
+                  strerror(errnum));
+          return;
+        }
       }
     }
   }
 
   /* 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.
    */
-  lprofLockFd(fd);
+  if (lprofLockFd(fd) != 0) {
+    int errnum = errno;
+    fprintf(stderr, "profiling: %s: cannot lock: %s\n", filename,
+            strerror(errnum));
+    return;
+  }
   output_file = fdopen(fd, mode);
 
   /* Initialize the write buffer. */


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54599.174276.patch
Type: text/x-patch
Size: 2159 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181115/2ef40d84/attachment.bin>


More information about the llvm-commits mailing list