[gcov] Fix simultaneous .gcda creation/lock
authorKAWASHIMA Takahiro <t-kawashima@fujitsu.com>
Thu, 7 May 2020 07:40:06 +0000 (16:40 +0900)
committerKAWASHIMA Takahiro <t-kawashima@fujitsu.com>
Wed, 13 May 2020 04:03:03 +0000 (13:03 +0900)
commit7d4167430c411ba1441260be9243d3401695ea7a
tree4b3a168c82c28e5d0632bc6c401b83da00d4402e
parent272bc25bc1401d5dc1407ad22a3172ee6914d007
[gcov] Fix simultaneous .gcda creation/lock

Fixes PR45673

The commit 9180c14fe4d (D76206) resolved only a part of the problem
of concurrent .gcda file creation. It ensured that only one process
creates the file but did not ensure that the process locks the
file first. If not, the process which created the file may clobber
the contents written by a process which locked the file first.
This is the cause of PR45673.

This commit prevents the clobbering by revising the assumption
that a process which creates the file locks the file first.
Regardless of file creation, a process which locked the file first
uses fwrite (new_file==1) and other processes use mmap (new_file==0).

I also tried to keep the creation/first-lock process same by using
mkstemp/link/unlink but the code gets long. This commit is more
simple.

Note: You may be confused with other changes which try to resolve
concurrent file access. My understanding is (may not be correct):

D76206:   Resolve race of .gcda file creation (but not lock)
This one: Resolve race of .gcda file creation and lock
D54599:   Same as D76206 but abandoned?
D70910:   Resolve race of multi-threaded counter flushing
D74953:   Resolve counter sharing between parent/children processes
D78477:   Revision of D74953

Differential Revision: https://reviews.llvm.org/D79556
compiler-rt/lib/profile/GCDAProfiling.c
compiler-rt/test/profile/Posix/instrprof-gcov-parallel.test