Index: compiler-rt/lib/profile/InstrProfilingUtil.c =================================================================== --- compiler-rt/lib/profile/InstrProfilingUtil.c +++ compiler-rt/lib/profile/InstrProfilingUtil.c @@ -207,8 +207,9 @@ f = fdopen(fd, "r+b"); #elif defined(_WIN32) // FIXME: Use the wide variants to handle Unicode filenames. - HANDLE h = CreateFileA(ProfileName, GENERIC_READ | GENERIC_WRITE, 0, 0, - OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); + HANDLE h = CreateFileA(ProfileName, GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_ALWAYS, + FILE_ATTRIBUTE_NORMAL, 0); if (h == INVALID_HANDLE_VALUE) return NULL; @@ -218,6 +219,10 @@ return NULL; } + if (lprofLockFd(fd) != 0) + PROF_WARN("Data may be corrupted during profile merging : %s\n", + "Fail to obtain file lock due to system limit."); + f = _fdopen(fd, "r+b"); if (f == 0) { CloseHandle(h); Index: compiler-rt/test/profile/Windows/Inputs/instrprof-file-merge.c =================================================================== --- /dev/null +++ compiler-rt/test/profile/Windows/Inputs/instrprof-file-merge.c @@ -0,0 +1,110 @@ +/* This is a test case where the parent process forks 10 + * children which contend to write to the same file. With + * file locking support, the data from each child should not + * be lost. + */ +#include +#include +#include +#include +#include +#include + +void spawn_child(char **argv, PROCESS_INFORMATION *pi, int child_num) { + // Set an environment variable to tell the child process to interrupt + // itself. + wchar_t child_str[10]; + _itow(child_num, child_str, 10); + if (!SetEnvironmentVariableW(L"CHILD_NUM", child_str)) { + printf("SetEnvironmentVariableW failed (0x%8lx).\n", GetLastError()); + fflush(stdout); + exit(1); + } + + STARTUPINFOW si; + memset(&si, 0, sizeof(si)); + si.cb = sizeof(si); + + memset(pi, 0, sizeof(PROCESS_INFORMATION)); + + if (!CreateProcessW(NULL, // No module name (use command line) + GetCommandLineW(), // Command line + NULL, // Process handle not inheritable + NULL, // Thread handle not inheritable + TRUE, // Set handle inheritance to TRUE + 0, // No flags + NULL, // Use parent's environment block + NULL, // Use parent's starting directory + &si, &pi)) { + printf("CreateProcess failed (0x%08lx).\n", GetLastError()); + fflush(stdout); + exit(1); + } + return pi; +} + +int wait_child(PROCESS_INFORMATION *pi) { + WaitForSingleObject(pi->hProcess, INFINITE); + + DWORD exit_code; + if (!GetExitCodeProcess(pi->hProcess, &exit_code)) { + printf("GetExitCodeProcess failed (0x%08lx).\n", GetLastError()); + fflush(stdout); + exit(1); + } + + CloseHandle(pi->hProcess); + CloseHandle(pi->hThread); + + return exit_code; +} + +extern FILE *lprofOpenFileEx(const char *); +int main(int argc, char *argv[]) { + FILE *F; + const char *FN; + + if (argc < 2) { + fprintf(stderr, "Requires one argument \n"); + exit(1); + } + FN = argv[1]; + + char *child_num = getenv("CHILD_NUM"); + if (!child_num) { + PROCESS_INFORMATION child[10]; + + // In parent + int fh; + if (_sopen_s(&fh, FN, _O_RDWR | _O_CREAT, _SH_DENYNO, + _S_IREAD | _S_IWRITE) == 0) { + _chsize(fh, 0); + _close(fh); + } + for (int i = 0; i < 10; i++) { + spawn_child(argv, &child[i], i); + } + for (int i = 0; i < 10; i++) { + wait_child(&child[i]); + } + F = lprofOpenFileEx(FN); + if (!F) { + fprintf(stderr, "Can not open file %s from parent\n", FN); + exit(1); + } + fseek(F, 0, SEEK_END); + fprintf(F, "Dump from parent %d\n", i); + return 0; + } else { + // In child + FILE *F = lprofOpenFileEx(FN); + if (!F) { + fprintf(stderr, "Can not open file %s from child\n", FN); + exit(1); + } + fseek(F, 0, SEEK_END); + fprintf(F, "Dump from Child %s\n", child_num); + fclose(F); + return 0; + } +} Index: compiler-rt/test/profile/Windows/instrprof-file-merge.test =================================================================== --- /dev/null +++ compiler-rt/test/profile/Windows/instrprof-file-merge.test @@ -0,0 +1,17 @@ +RUN: mkdir -p %t.d +RUN: %clang_profgen %S/Inputs/instrprof-file-merge.c -o %t +RUN: rm -f %t.d/run.dump +RUN: %run %t %t.d/run.dump +RUN: sort %t.d/run.dump | FileCheck %s + +CHECK: Dump from Child 0 +CHECK-NEXT: Dump from Child 1 +CHECK-NEXT: Dump from Child 2 +CHECK-NEXT: Dump from Child 3 +CHECK-NEXT: Dump from Child 4 +CHECK-NEXT: Dump from Child 5 +CHECK-NEXT: Dump from Child 6 +CHECK-NEXT: Dump from Child 7 +CHECK-NEXT: Dump from Child 8 +CHECK-NEXT: Dump from Child 9 +CHECK-NEXT: Dump from parent 10 Index: compiler-rt/test/profile/Windows/lit.local.cfg.py =================================================================== --- /dev/null +++ compiler-rt/test/profile/Windows/lit.local.cfg.py @@ -0,0 +1,40 @@ +import subprocess + +def getRoot(config): + if not config.parent: + return config + return getRoot(config.parent) + + +def is_gold_linker_available(): + + if not config.gold_executable: + return False + try: + ld_cmd = subprocess.Popen([config.gold_executable, '--help'], stdout = subprocess.PIPE) + ld_out = ld_cmd.stdout.read().decode() + ld_cmd.wait() + except: + return False + + if not '-plugin' in ld_out: + return False + + # config.clang is not guaranteed to be just the executable! + clang_cmd = subprocess.Popen(" ".join([config.clang, '-fuse-ld=gold', '-xc', '-']), + shell=True, + universal_newlines = True, + stdin = subprocess.PIPE, + stdout = subprocess.PIPE, + stderr = subprocess.PIPE) + clang_err = clang_cmd.communicate('int main() { return 0; }')[1] + + if not 'invalid linker' in clang_err: + return True + + return False + +root = getRoot(config) + +if root.host_os not in ['Windows'] or not is_gold_linker_available(): + config.unsupported = True