1 //===--- HeaderMap.cpp - A file that acts like dir of symlinks ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the HeaderMap interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Lex/HeaderMap.h"
15 #include "clang/Basic/CharInfo.h"
16 #include "clang/Basic/FileManager.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/Support/DataTypes.h"
19 #include "llvm/Support/MathExtras.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include <cstdio>
22 #include <memory>
23 using namespace clang;
24 
25 //===----------------------------------------------------------------------===//
26 // Data Structures and Manifest Constants
27 //===----------------------------------------------------------------------===//
28 
29 enum {
30   HMAP_HeaderMagicNumber = ('h' << 24) | ('m' << 16) | ('a' << 8) | 'p',
31   HMAP_HeaderVersion = 1,
32 
33   HMAP_EmptyBucketKey = 0
34 };
35 
36 namespace clang {
37 struct HMapBucket {
38   uint32_t Key;          // Offset (into strings) of key.
39 
40   uint32_t Prefix;     // Offset (into strings) of value prefix.
41   uint32_t Suffix;     // Offset (into strings) of value suffix.
42 };
43 
44 struct HMapHeader {
45   uint32_t Magic;           // Magic word, also indicates byte order.
46   uint16_t Version;         // Version number -- currently 1.
47   uint16_t Reserved;        // Reserved for future use - zero for now.
48   uint32_t StringsOffset;   // Offset to start of string pool.
49   uint32_t NumEntries;      // Number of entries in the string table.
50   uint32_t NumBuckets;      // Number of buckets (always a power of 2).
51   uint32_t MaxValueLength;  // Length of longest result path (excluding nul).
52   // An array of 'NumBuckets' HMapBucket objects follows this header.
53   // Strings follow the buckets, at StringsOffset.
54 };
55 } // end namespace clang.
56 
57 /// HashHMapKey - This is the 'well known' hash function required by the file
58 /// format, used to look up keys in the hash table.  The hash table uses simple
59 /// linear probing based on this function.
60 static inline unsigned HashHMapKey(StringRef Str) {
61   unsigned Result = 0;
62   const char *S = Str.begin(), *End = Str.end();
63 
64   for (; S != End; S++)
65     Result += toLowercase(*S) * 13;
66   return Result;
67 }
68 
69 
70 
71 //===----------------------------------------------------------------------===//
72 // Verification and Construction
73 //===----------------------------------------------------------------------===//
74 
75 /// HeaderMap::Create - This attempts to load the specified file as a header
76 /// map.  If it doesn't look like a HeaderMap, it gives up and returns null.
77 /// If it looks like a HeaderMap but is obviously corrupted, it puts a reason
78 /// into the string error argument and returns null.
79 const HeaderMap *HeaderMap::Create(const FileEntry *FE, FileManager &FM) {
80   // If the file is too small to be a header map, ignore it.
81   unsigned FileSize = FE->getSize();
82   if (FileSize <= sizeof(HMapHeader)) return nullptr;
83 
84   std::unique_ptr<const llvm::MemoryBuffer> FileBuffer =
85       FM.getBufferForFile(FE);
86   if (!FileBuffer) return nullptr;  // Unreadable file?
87   const char *FileStart = FileBuffer->getBufferStart();
88 
89   // We know the file is at least as big as the header, check it now.
90   const HMapHeader *Header = reinterpret_cast<const HMapHeader*>(FileStart);
91 
92   // Sniff it to see if it's a headermap by checking the magic number and
93   // version.
94   bool NeedsByteSwap;
95   if (Header->Magic == HMAP_HeaderMagicNumber &&
96       Header->Version == HMAP_HeaderVersion)
97     NeedsByteSwap = false;
98   else if (Header->Magic == llvm::ByteSwap_32(HMAP_HeaderMagicNumber) &&
99            Header->Version == llvm::ByteSwap_16(HMAP_HeaderVersion))
100     NeedsByteSwap = true;  // Mixed endianness headermap.
101   else
102     return nullptr;  // Not a header map.
103 
104   if (Header->Reserved != 0) return nullptr;
105 
106   // Okay, everything looks good, create the header map.
107   return new HeaderMap(std::move(FileBuffer), NeedsByteSwap);
108 }
109 
110 //===----------------------------------------------------------------------===//
111 //  Utility Methods
112 //===----------------------------------------------------------------------===//
113 
114 
115 /// getFileName - Return the filename of the headermap.
116 const char *HeaderMap::getFileName() const {
117   return FileBuffer->getBufferIdentifier();
118 }
119 
120 unsigned HeaderMap::getEndianAdjustedWord(unsigned X) const {
121   if (!NeedsBSwap) return X;
122   return llvm::ByteSwap_32(X);
123 }
124 
125 /// getHeader - Return a reference to the file header, in unbyte-swapped form.
126 /// This method cannot fail.
127 const HMapHeader &HeaderMap::getHeader() const {
128   // We know the file is at least as big as the header.  Return it.
129   return *reinterpret_cast<const HMapHeader*>(FileBuffer->getBufferStart());
130 }
131 
132 /// getBucket - Return the specified hash table bucket from the header map,
133 /// bswap'ing its fields as appropriate.  If the bucket number is not valid,
134 /// this return a bucket with an empty key (0).
135 HMapBucket HeaderMap::getBucket(unsigned BucketNo) const {
136   HMapBucket Result;
137   Result.Key = HMAP_EmptyBucketKey;
138 
139   const HMapBucket *BucketArray =
140     reinterpret_cast<const HMapBucket*>(FileBuffer->getBufferStart() +
141                                         sizeof(HMapHeader));
142 
143   const HMapBucket *BucketPtr = BucketArray+BucketNo;
144   if ((const char*)(BucketPtr+1) > FileBuffer->getBufferEnd()) {
145     Result.Prefix = 0;
146     Result.Suffix = 0;
147     return Result;  // Invalid buffer, corrupt hmap.
148   }
149 
150   // Otherwise, the bucket is valid.  Load the values, bswapping as needed.
151   Result.Key    = getEndianAdjustedWord(BucketPtr->Key);
152   Result.Prefix = getEndianAdjustedWord(BucketPtr->Prefix);
153   Result.Suffix = getEndianAdjustedWord(BucketPtr->Suffix);
154   return Result;
155 }
156 
157 /// getString - Look up the specified string in the string table.  If the string
158 /// index is not valid, it returns an empty string.
159 const char *HeaderMap::getString(unsigned StrTabIdx) const {
160   // Add the start of the string table to the idx.
161   StrTabIdx += getEndianAdjustedWord(getHeader().StringsOffset);
162 
163   // Check for invalid index.
164   if (StrTabIdx >= FileBuffer->getBufferSize())
165     return nullptr;
166 
167   // Otherwise, we have a valid pointer into the file.  Just return it.  We know
168   // that the "string" can not overrun the end of the file, because the buffer
169   // is nul terminated by virtue of being a MemoryBuffer.
170   return FileBuffer->getBufferStart()+StrTabIdx;
171 }
172 
173 //===----------------------------------------------------------------------===//
174 // The Main Drivers
175 //===----------------------------------------------------------------------===//
176 
177 /// dump - Print the contents of this headermap to stderr.
178 void HeaderMap::dump() const {
179   const HMapHeader &Hdr = getHeader();
180   unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
181 
182   fprintf(stderr, "Header Map %s:\n  %d buckets, %d entries\n",
183           getFileName(), NumBuckets,
184           getEndianAdjustedWord(Hdr.NumEntries));
185 
186   for (unsigned i = 0; i != NumBuckets; ++i) {
187     HMapBucket B = getBucket(i);
188     if (B.Key == HMAP_EmptyBucketKey) continue;
189 
190     const char *Key    = getString(B.Key);
191     const char *Prefix = getString(B.Prefix);
192     const char *Suffix = getString(B.Suffix);
193     fprintf(stderr, "  %d. %s -> '%s' '%s'\n", i, Key, Prefix, Suffix);
194   }
195 }
196 
197 /// LookupFile - Check to see if the specified relative filename is located in
198 /// this HeaderMap.  If so, open it and return its FileEntry.
199 const FileEntry *HeaderMap::LookupFile(
200     StringRef Filename, FileManager &FM) const {
201 
202   SmallString<1024> Path;
203   StringRef Dest = lookupFilename(Filename, Path);
204   if (Dest.empty())
205     return nullptr;
206 
207   return FM.getFile(Dest);
208 }
209 
210 StringRef HeaderMap::lookupFilename(StringRef Filename,
211                                     SmallVectorImpl<char> &DestPath) const {
212   const HMapHeader &Hdr = getHeader();
213   unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
214 
215   // If the number of buckets is not a power of two, the headermap is corrupt.
216   // Don't probe infinitely.
217   if (NumBuckets & (NumBuckets-1))
218     return StringRef();
219 
220   // Linearly probe the hash table.
221   for (unsigned Bucket = HashHMapKey(Filename);; ++Bucket) {
222     HMapBucket B = getBucket(Bucket & (NumBuckets-1));
223     if (B.Key == HMAP_EmptyBucketKey) return StringRef(); // Hash miss.
224 
225     // See if the key matches.  If not, probe on.
226     if (!Filename.equals_lower(getString(B.Key)))
227       continue;
228 
229     // If so, we have a match in the hash table.  Construct the destination
230     // path.
231     StringRef Prefix = getString(B.Prefix);
232     StringRef Suffix = getString(B.Suffix);
233     DestPath.clear();
234     DestPath.append(Prefix.begin(), Prefix.end());
235     DestPath.append(Suffix.begin(), Suffix.end());
236     return StringRef(DestPath.begin(), DestPath.size());
237   }
238 }
239