xref: /freebsd-src/contrib/llvm-project/clang/lib/Serialization/ModuleManager.cpp (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
1 //===- ModuleManager.cpp - Module Manager ---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ModuleManager class, which manages a set of loaded
10 //  modules for the ASTReader.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Serialization/ModuleManager.h"
15 #include "clang/Basic/FileManager.h"
16 #include "clang/Basic/LLVM.h"
17 #include "clang/Lex/HeaderSearch.h"
18 #include "clang/Lex/ModuleMap.h"
19 #include "clang/Serialization/GlobalModuleIndex.h"
20 #include "clang/Serialization/InMemoryModuleCache.h"
21 #include "clang/Serialization/ModuleFile.h"
22 #include "clang/Serialization/PCHContainerOperations.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/ADT/iterator.h"
29 #include "llvm/Support/Chrono.h"
30 #include "llvm/Support/DOTGraphTraits.h"
31 #include "llvm/Support/ErrorOr.h"
32 #include "llvm/Support/GraphWriter.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/VirtualFileSystem.h"
35 #include <algorithm>
36 #include <cassert>
37 #include <memory>
38 #include <string>
39 #include <system_error>
40 
41 using namespace clang;
42 using namespace serialization;
43 
44 ModuleFile *ModuleManager::lookupByFileName(StringRef Name) const {
45   auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false,
46                                /*CacheFailure=*/false);
47   if (Entry)
48     return lookup(*Entry);
49 
50   return nullptr;
51 }
52 
53 ModuleFile *ModuleManager::lookupByModuleName(StringRef Name) const {
54   if (const Module *Mod = HeaderSearchInfo.getModuleMap().findModule(Name))
55     if (const FileEntry *File = Mod->getASTFile())
56       return lookup(File);
57 
58   return nullptr;
59 }
60 
61 ModuleFile *ModuleManager::lookup(const FileEntry *File) const {
62   auto Known = Modules.find(File);
63   if (Known == Modules.end())
64     return nullptr;
65 
66   return Known->second;
67 }
68 
69 std::unique_ptr<llvm::MemoryBuffer>
70 ModuleManager::lookupBuffer(StringRef Name) {
71   auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false,
72                                /*CacheFailure=*/false);
73   if (!Entry)
74     return nullptr;
75   return std::move(InMemoryBuffers[*Entry]);
76 }
77 
78 static bool checkSignature(ASTFileSignature Signature,
79                            ASTFileSignature ExpectedSignature,
80                            std::string &ErrorStr) {
81   if (!ExpectedSignature || Signature == ExpectedSignature)
82     return false;
83 
84   ErrorStr =
85       Signature ? "signature mismatch" : "could not read module signature";
86   return true;
87 }
88 
89 static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy,
90                                 SourceLocation ImportLoc) {
91   if (ImportedBy) {
92     MF.ImportedBy.insert(ImportedBy);
93     ImportedBy->Imports.insert(&MF);
94   } else {
95     if (!MF.DirectlyImported)
96       MF.ImportLoc = ImportLoc;
97 
98     MF.DirectlyImported = true;
99   }
100 }
101 
102 ModuleManager::AddModuleResult
103 ModuleManager::addModule(StringRef FileName, ModuleKind Type,
104                          SourceLocation ImportLoc, ModuleFile *ImportedBy,
105                          unsigned Generation,
106                          off_t ExpectedSize, time_t ExpectedModTime,
107                          ASTFileSignature ExpectedSignature,
108                          ASTFileSignatureReader ReadSignature,
109                          ModuleFile *&Module,
110                          std::string &ErrorStr) {
111   Module = nullptr;
112 
113   // Look for the file entry. This only fails if the expected size or
114   // modification time differ.
115   OptionalFileEntryRefDegradesToFileEntryPtr Entry;
116   if (Type == MK_ExplicitModule || Type == MK_PrebuiltModule) {
117     // If we're not expecting to pull this file out of the module cache, it
118     // might have a different mtime due to being moved across filesystems in
119     // a distributed build. The size must still match, though. (As must the
120     // contents, but we can't check that.)
121     ExpectedModTime = 0;
122   }
123   // Note: ExpectedSize and ExpectedModTime will be 0 for MK_ImplicitModule
124   // when using an ASTFileSignature.
125   if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
126     ErrorStr = "module file out of date";
127     return OutOfDate;
128   }
129 
130   if (!Entry && FileName != "-") {
131     ErrorStr = "module file not found";
132     return Missing;
133   }
134 
135   // The ModuleManager's use of FileEntry nodes as the keys for its map of
136   // loaded modules is less than ideal. Uniqueness for FileEntry nodes is
137   // maintained by FileManager, which in turn uses inode numbers on hosts
138   // that support that. When coupled with the module cache's proclivity for
139   // turning over and deleting stale PCMs, this means entries for different
140   // module files can wind up reusing the same underlying inode. When this
141   // happens, subsequent accesses to the Modules map will disagree on the
142   // ModuleFile associated with a given file. In general, it is not sufficient
143   // to resolve this conundrum with a type like FileEntryRef that stores the
144   // name of the FileEntry node on first access because of path canonicalization
145   // issues. However, the paths constructed for implicit module builds are
146   // fully under Clang's control. We *can*, therefore, rely on their structure
147   // being consistent across operating systems and across subsequent accesses
148   // to the Modules map.
149   auto implicitModuleNamesMatch = [](ModuleKind Kind, const ModuleFile *MF,
150                                      const FileEntry *Entry) -> bool {
151     if (Kind != MK_ImplicitModule)
152       return true;
153     return Entry->getName() == MF->FileName;
154   };
155 
156   // Check whether we already loaded this module, before
157   if (ModuleFile *ModuleEntry = Modules.lookup(Entry)) {
158     if (implicitModuleNamesMatch(Type, ModuleEntry, Entry)) {
159       // Check the stored signature.
160       if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr))
161         return OutOfDate;
162 
163       Module = ModuleEntry;
164       updateModuleImports(*ModuleEntry, ImportedBy, ImportLoc);
165       return AlreadyLoaded;
166     }
167   }
168 
169   // Allocate a new module.
170   auto NewModule = std::make_unique<ModuleFile>(Type, Generation);
171   NewModule->Index = Chain.size();
172   NewModule->FileName = FileName.str();
173   NewModule->File = Entry;
174   NewModule->ImportLoc = ImportLoc;
175   NewModule->InputFilesValidationTimestamp = 0;
176 
177   if (NewModule->Kind == MK_ImplicitModule) {
178     std::string TimestampFilename = NewModule->getTimestampFilename();
179     llvm::vfs::Status Status;
180     // A cached stat value would be fine as well.
181     if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
182       NewModule->InputFilesValidationTimestamp =
183           llvm::sys::toTimeT(Status.getLastModificationTime());
184   }
185 
186   // Load the contents of the module
187   if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
188     // The buffer was already provided for us.
189     NewModule->Buffer = &ModuleCache->addBuiltPCM(FileName, std::move(Buffer));
190     // Since the cached buffer is reused, it is safe to close the file
191     // descriptor that was opened while stat()ing the PCM in
192     // lookupModuleFile() above, it won't be needed any longer.
193     Entry->closeFile();
194   } else if (llvm::MemoryBuffer *Buffer =
195                  getModuleCache().lookupPCM(FileName)) {
196     NewModule->Buffer = Buffer;
197     // As above, the file descriptor is no longer needed.
198     Entry->closeFile();
199   } else if (getModuleCache().shouldBuildPCM(FileName)) {
200     // Report that the module is out of date, since we tried (and failed) to
201     // import it earlier.
202     Entry->closeFile();
203     return OutOfDate;
204   } else {
205     // Open the AST file.
206     llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf((std::error_code()));
207     if (FileName == "-") {
208       Buf = llvm::MemoryBuffer::getSTDIN();
209     } else {
210       // Get a buffer of the file and close the file descriptor when done.
211       // The file is volatile because in a parallel build we expect multiple
212       // compiler processes to use the same module file rebuilding it if needed.
213       //
214       // RequiresNullTerminator is false because module files don't need it, and
215       // this allows the file to still be mmapped.
216       Buf = FileMgr.getBufferForFile(NewModule->File,
217                                      /*IsVolatile=*/true,
218                                      /*RequiresNullTerminator=*/false);
219     }
220 
221     if (!Buf) {
222       ErrorStr = Buf.getError().message();
223       return Missing;
224     }
225 
226     NewModule->Buffer = &getModuleCache().addPCM(FileName, std::move(*Buf));
227   }
228 
229   // Initialize the stream.
230   NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer);
231 
232   // Read the signature eagerly now so that we can check it.  Avoid calling
233   // ReadSignature unless there's something to check though.
234   if (ExpectedSignature && checkSignature(ReadSignature(NewModule->Data),
235                                           ExpectedSignature, ErrorStr))
236     return OutOfDate;
237 
238   // We're keeping this module.  Store it everywhere.
239   Module = Modules[Entry] = NewModule.get();
240 
241   updateModuleImports(*NewModule, ImportedBy, ImportLoc);
242 
243   if (!NewModule->isModule())
244     PCHChain.push_back(NewModule.get());
245   if (!ImportedBy)
246     Roots.push_back(NewModule.get());
247 
248   Chain.push_back(std::move(NewModule));
249   return NewlyLoaded;
250 }
251 
252 void ModuleManager::removeModules(ModuleIterator First, ModuleMap *modMap) {
253   auto Last = end();
254   if (First == Last)
255     return;
256 
257   // Explicitly clear VisitOrder since we might not notice it is stale.
258   VisitOrder.clear();
259 
260   // Collect the set of module file pointers that we'll be removing.
261   llvm::SmallPtrSet<ModuleFile *, 4> victimSet(
262       (llvm::pointer_iterator<ModuleIterator>(First)),
263       (llvm::pointer_iterator<ModuleIterator>(Last)));
264 
265   auto IsVictim = [&](ModuleFile *MF) {
266     return victimSet.count(MF);
267   };
268   // Remove any references to the now-destroyed modules.
269   for (auto I = begin(); I != First; ++I) {
270     I->Imports.remove_if(IsVictim);
271     I->ImportedBy.remove_if(IsVictim);
272   }
273   llvm::erase_if(Roots, IsVictim);
274 
275   // Remove the modules from the PCH chain.
276   for (auto I = First; I != Last; ++I) {
277     if (!I->isModule()) {
278       PCHChain.erase(llvm::find(PCHChain, &*I), PCHChain.end());
279       break;
280     }
281   }
282 
283   // Delete the modules and erase them from the various structures.
284   for (ModuleIterator victim = First; victim != Last; ++victim) {
285     Modules.erase(victim->File);
286 
287     if (modMap) {
288       StringRef ModuleName = victim->ModuleName;
289       if (Module *mod = modMap->findModule(ModuleName)) {
290         mod->setASTFile(None);
291       }
292     }
293   }
294 
295   // Delete the modules.
296   Chain.erase(Chain.begin() + (First - begin()), Chain.end());
297 }
298 
299 void
300 ModuleManager::addInMemoryBuffer(StringRef FileName,
301                                  std::unique_ptr<llvm::MemoryBuffer> Buffer) {
302   const FileEntry *Entry =
303       FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
304   InMemoryBuffers[Entry] = std::move(Buffer);
305 }
306 
307 ModuleManager::VisitState *ModuleManager::allocateVisitState() {
308   // Fast path: if we have a cached state, use it.
309   if (FirstVisitState) {
310     VisitState *Result = FirstVisitState;
311     FirstVisitState = FirstVisitState->NextState;
312     Result->NextState = nullptr;
313     return Result;
314   }
315 
316   // Allocate and return a new state.
317   return new VisitState(size());
318 }
319 
320 void ModuleManager::returnVisitState(VisitState *State) {
321   assert(State->NextState == nullptr && "Visited state is in list?");
322   State->NextState = FirstVisitState;
323   FirstVisitState = State;
324 }
325 
326 void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
327   GlobalIndex = Index;
328   if (!GlobalIndex) {
329     ModulesInCommonWithGlobalIndex.clear();
330     return;
331   }
332 
333   // Notify the global module index about all of the modules we've already
334   // loaded.
335   for (ModuleFile &M : *this)
336     if (!GlobalIndex->loadedModuleFile(&M))
337       ModulesInCommonWithGlobalIndex.push_back(&M);
338 }
339 
340 void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
341   if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
342     return;
343 
344   ModulesInCommonWithGlobalIndex.push_back(MF);
345 }
346 
347 ModuleManager::ModuleManager(FileManager &FileMgr,
348                              InMemoryModuleCache &ModuleCache,
349                              const PCHContainerReader &PCHContainerRdr,
350                              const HeaderSearch &HeaderSearchInfo)
351     : FileMgr(FileMgr), ModuleCache(&ModuleCache),
352       PCHContainerRdr(PCHContainerRdr), HeaderSearchInfo(HeaderSearchInfo) {}
353 
354 ModuleManager::~ModuleManager() { delete FirstVisitState; }
355 
356 void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
357                           llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
358   // If the visitation order vector is the wrong size, recompute the order.
359   if (VisitOrder.size() != Chain.size()) {
360     unsigned N = size();
361     VisitOrder.clear();
362     VisitOrder.reserve(N);
363 
364     // Record the number of incoming edges for each module. When we
365     // encounter a module with no incoming edges, push it into the queue
366     // to seed the queue.
367     SmallVector<ModuleFile *, 4> Queue;
368     Queue.reserve(N);
369     llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
370     UnusedIncomingEdges.resize(size());
371     for (ModuleFile &M : llvm::reverse(*this)) {
372       unsigned Size = M.ImportedBy.size();
373       UnusedIncomingEdges[M.Index] = Size;
374       if (!Size)
375         Queue.push_back(&M);
376     }
377 
378     // Traverse the graph, making sure to visit a module before visiting any
379     // of its dependencies.
380     while (!Queue.empty()) {
381       ModuleFile *CurrentModule = Queue.pop_back_val();
382       VisitOrder.push_back(CurrentModule);
383 
384       // For any module that this module depends on, push it on the
385       // stack (if it hasn't already been marked as visited).
386       for (ModuleFile *M : llvm::reverse(CurrentModule->Imports)) {
387         // Remove our current module as an impediment to visiting the
388         // module we depend on. If we were the last unvisited module
389         // that depends on this particular module, push it into the
390         // queue to be visited.
391         unsigned &NumUnusedEdges = UnusedIncomingEdges[M->Index];
392         if (NumUnusedEdges && (--NumUnusedEdges == 0))
393           Queue.push_back(M);
394       }
395     }
396 
397     assert(VisitOrder.size() == N && "Visitation order is wrong?");
398 
399     delete FirstVisitState;
400     FirstVisitState = nullptr;
401   }
402 
403   VisitState *State = allocateVisitState();
404   unsigned VisitNumber = State->NextVisitNumber++;
405 
406   // If the caller has provided us with a hit-set that came from the global
407   // module index, mark every module file in common with the global module
408   // index that is *not* in that set as 'visited'.
409   if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
410     for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
411     {
412       ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
413       if (!ModuleFilesHit->count(M))
414         State->VisitNumber[M->Index] = VisitNumber;
415     }
416   }
417 
418   for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
419     ModuleFile *CurrentModule = VisitOrder[I];
420     // Should we skip this module file?
421     if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
422       continue;
423 
424     // Visit the module.
425     assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
426     State->VisitNumber[CurrentModule->Index] = VisitNumber;
427     if (!Visitor(*CurrentModule))
428       continue;
429 
430     // The visitor has requested that cut off visitation of any
431     // module that the current module depends on. To indicate this
432     // behavior, we mark all of the reachable modules as having been visited.
433     ModuleFile *NextModule = CurrentModule;
434     do {
435       // For any module that this module depends on, push it on the
436       // stack (if it hasn't already been marked as visited).
437       for (llvm::SetVector<ModuleFile *>::iterator
438              M = NextModule->Imports.begin(),
439              MEnd = NextModule->Imports.end();
440            M != MEnd; ++M) {
441         if (State->VisitNumber[(*M)->Index] != VisitNumber) {
442           State->Stack.push_back(*M);
443           State->VisitNumber[(*M)->Index] = VisitNumber;
444         }
445       }
446 
447       if (State->Stack.empty())
448         break;
449 
450       // Pop the next module off the stack.
451       NextModule = State->Stack.pop_back_val();
452     } while (true);
453   }
454 
455   returnVisitState(State);
456 }
457 
458 bool ModuleManager::lookupModuleFile(StringRef FileName, off_t ExpectedSize,
459                                      time_t ExpectedModTime,
460                                      Optional<FileEntryRef> &File) {
461   File = None;
462   if (FileName == "-")
463     return false;
464 
465   // Open the file immediately to ensure there is no race between stat'ing and
466   // opening the file.
467   Optional<FileEntryRef> FileOrErr =
468       expectedToOptional(FileMgr.getFileRef(FileName, /*OpenFile=*/true,
469                                             /*CacheFailure=*/false));
470   if (!FileOrErr)
471     return false;
472 
473   File = *FileOrErr;
474 
475   if ((ExpectedSize && ExpectedSize != File->getSize()) ||
476       (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
477     // Do not destroy File, as it may be referenced. If we need to rebuild it,
478     // it will be destroyed by removeModules.
479     return true;
480 
481   return false;
482 }
483 
484 #ifndef NDEBUG
485 namespace llvm {
486 
487   template<>
488   struct GraphTraits<ModuleManager> {
489     using NodeRef = ModuleFile *;
490     using ChildIteratorType = llvm::SetVector<ModuleFile *>::const_iterator;
491     using nodes_iterator = pointer_iterator<ModuleManager::ModuleConstIterator>;
492 
493     static ChildIteratorType child_begin(NodeRef Node) {
494       return Node->Imports.begin();
495     }
496 
497     static ChildIteratorType child_end(NodeRef Node) {
498       return Node->Imports.end();
499     }
500 
501     static nodes_iterator nodes_begin(const ModuleManager &Manager) {
502       return nodes_iterator(Manager.begin());
503     }
504 
505     static nodes_iterator nodes_end(const ModuleManager &Manager) {
506       return nodes_iterator(Manager.end());
507     }
508   };
509 
510   template<>
511   struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
512     explicit DOTGraphTraits(bool IsSimple = false)
513         : DefaultDOTGraphTraits(IsSimple) {}
514 
515     static bool renderGraphFromBottomUp() { return true; }
516 
517     std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
518       return M->ModuleName;
519     }
520   };
521 
522 } // namespace llvm
523 
524 void ModuleManager::viewGraph() {
525   llvm::ViewGraph(*this, "Modules");
526 }
527 #endif
528