r207735 - Avoid a potential race between stat() and open() of ASTFile

Ben Langmuir blangmuir at apple.com
Wed Apr 30 20:33:36 PDT 2014


Author: benlangmuir
Date: Wed Apr 30 22:33:36 2014
New Revision: 207735

URL: http://llvm.org/viewvc/llvm-project?rev=207735&view=rev
Log:
Avoid a potential race between stat() and open() of ASTFile

We need to open an ASTFile while checking its expected size and
modification time, or another clang instance can modify the file between
the stat() and the open().

Modified:
    cfe/trunk/lib/Serialization/ModuleManager.cpp

Modified: cfe/trunk/lib/Serialization/ModuleManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ModuleManager.cpp?rev=207735&r1=207734&r2=207735&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ModuleManager.cpp (original)
+++ cfe/trunk/lib/Serialization/ModuleManager.cpp Wed Apr 30 22:33:36 2014
@@ -381,7 +381,9 @@ bool ModuleManager::lookupModuleFile(Str
                                      off_t ExpectedSize,
                                      time_t ExpectedModTime,
                                      const FileEntry *&File) {
-  File = FileMgr.getFile(FileName, /*openFile=*/false, /*cacheFailure=*/false);
+  // Open the file immediately to ensure there is no race between stat'ing and
+  // opening the file.
+  File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
 
   if (!File && FileName != "-") {
     return false;
@@ -389,6 +391,8 @@ bool ModuleManager::lookupModuleFile(Str
 
   if ((ExpectedSize && ExpectedSize != File->getSize()) ||
       (ExpectedModTime && ExpectedModTime != File->getModificationTime())) {
+    FileMgr.invalidateCache(File);
+    File = nullptr;
     return true;
   }
 





More information about the cfe-commits mailing list