TypeStreamMerger.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. //===-- TypeStreamMerger.cpp ------------------------------------*- C++ -*-===//
  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. #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
  10. #include "llvm/ADT/SmallString.h"
  11. #include "llvm/ADT/StringExtras.h"
  12. #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
  13. #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
  14. #include "llvm/DebugInfo/CodeView/TypeIndex.h"
  15. #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
  16. #include "llvm/DebugInfo/CodeView/TypeRecord.h"
  17. #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
  18. #include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
  19. #include "llvm/Support/Error.h"
  20. #include "llvm/Support/ScopedPrinter.h"
  21. using namespace llvm;
  22. using namespace llvm::codeview;
  23. namespace {
  24. /// Implementation of CodeView type stream merging.
  25. ///
  26. /// A CodeView type stream is a series of records that reference each other
  27. /// through type indices. A type index is either "simple", meaning it is less
  28. /// than 0x1000 and refers to a builtin type, or it is complex, meaning it
  29. /// refers to a prior type record in the current stream. The type index of a
  30. /// record is equal to the number of records before it in the stream plus
  31. /// 0x1000.
  32. ///
  33. /// Type records are only allowed to use type indices smaller than their own, so
  34. /// a type stream is effectively a topologically sorted DAG. Cycles occuring in
  35. /// the type graph of the source program are resolved with forward declarations
  36. /// of composite types. This class implements the following type stream merging
  37. /// algorithm, which relies on this DAG structure:
  38. ///
  39. /// - Begin with a new empty stream, and a new empty hash table that maps from
  40. /// type record contents to new type index.
  41. /// - For each new type stream, maintain a map from source type index to
  42. /// destination type index.
  43. /// - For each record, copy it and rewrite its type indices to be valid in the
  44. /// destination type stream.
  45. /// - If the new type record is not already present in the destination stream
  46. /// hash table, append it to the destination type stream, assign it the next
  47. /// type index, and update the two hash tables.
  48. /// - If the type record already exists in the destination stream, discard it
  49. /// and update the type index map to forward the source type index to the
  50. /// existing destination type index.
  51. ///
  52. /// As an additional complication, type stream merging actually produces two
  53. /// streams: an item (or IPI) stream and a type stream, as this is what is
  54. /// actually stored in the final PDB. We choose which records go where by
  55. /// looking at the record kind.
  56. class TypeStreamMerger : public TypeVisitorCallbacks {
  57. public:
  58. explicit TypeStreamMerger(SmallVectorImpl<TypeIndex> &SourceToDest,
  59. TypeServerHandler *Handler)
  60. : Handler(Handler), IndexMap(SourceToDest) {
  61. SourceToDest.clear();
  62. }
  63. static const TypeIndex Untranslated;
  64. Error visitTypeBegin(CVType &Record) override;
  65. Error visitTypeEnd(CVType &Record) override;
  66. Error mergeTypesAndIds(TypeTableBuilder &DestIds, TypeTableBuilder &DestTypes,
  67. const CVTypeArray &IdsAndTypes);
  68. Error mergeIdRecords(TypeTableBuilder &Dest,
  69. ArrayRef<TypeIndex> TypeSourceToDest,
  70. const CVTypeArray &Ids);
  71. Error mergeTypeRecords(TypeTableBuilder &Dest, const CVTypeArray &Types);
  72. private:
  73. Error doit(const CVTypeArray &Types);
  74. void addMapping(TypeIndex Idx);
  75. bool remapTypeIndex(TypeIndex &Idx);
  76. bool remapItemIndex(TypeIndex &Idx);
  77. bool remapIndices(RemappedType &Record, ArrayRef<TiReference> Refs) {
  78. auto OriginalData = Record.OriginalRecord.content();
  79. bool Success = true;
  80. for (auto &Ref : Refs) {
  81. uint32_t Offset = Ref.Offset;
  82. ArrayRef<uint8_t> Bytes =
  83. OriginalData.slice(Ref.Offset, sizeof(TypeIndex));
  84. ArrayRef<TypeIndex> TIs(reinterpret_cast<const TypeIndex *>(Bytes.data()),
  85. Ref.Count);
  86. for (auto TI : TIs) {
  87. TypeIndex NewTI = TI;
  88. bool ThisSuccess = (Ref.Kind == TiRefKind::IndexRef)
  89. ? remapItemIndex(NewTI)
  90. : remapTypeIndex(NewTI);
  91. if (ThisSuccess && NewTI != TI)
  92. Record.Mappings.emplace_back(Offset, NewTI);
  93. Offset += sizeof(TypeIndex);
  94. Success &= ThisSuccess;
  95. }
  96. }
  97. return Success;
  98. }
  99. bool remapIndex(TypeIndex &Idx, ArrayRef<TypeIndex> Map);
  100. size_t slotForIndex(TypeIndex Idx) const {
  101. assert(!Idx.isSimple() && "simple type indices have no slots");
  102. return Idx.getIndex() - TypeIndex::FirstNonSimpleIndex;
  103. }
  104. Error errorCorruptRecord() const {
  105. return llvm::make_error<CodeViewError>(cv_error_code::corrupt_record);
  106. }
  107. Error writeRecord(TypeTableBuilder &Dest, const RemappedType &Record,
  108. bool RemapSuccess) {
  109. TypeIndex DestIdx = Untranslated;
  110. if (RemapSuccess)
  111. DestIdx = Dest.writeSerializedRecord(Record);
  112. addMapping(DestIdx);
  113. return Error::success();
  114. }
  115. Error writeTypeRecord(const CVType &Record) {
  116. TypeIndex DestIdx =
  117. DestTypeStream->writeSerializedRecord(Record.RecordData);
  118. addMapping(DestIdx);
  119. return Error::success();
  120. }
  121. Error writeTypeRecord(const RemappedType &Record, bool RemapSuccess) {
  122. return writeRecord(*DestTypeStream, Record, RemapSuccess);
  123. }
  124. Error writeIdRecord(const RemappedType &Record, bool RemapSuccess) {
  125. return writeRecord(*DestIdStream, Record, RemapSuccess);
  126. }
  127. Optional<Error> LastError;
  128. bool IsSecondPass = false;
  129. bool HadUntranslatedMember = false;
  130. unsigned NumBadIndices = 0;
  131. TypeIndex CurIndex{TypeIndex::FirstNonSimpleIndex};
  132. TypeTableBuilder *DestIdStream = nullptr;
  133. TypeTableBuilder *DestTypeStream = nullptr;
  134. TypeServerHandler *Handler = nullptr;
  135. // If we're only mapping id records, this array contains the mapping for
  136. // type records.
  137. ArrayRef<TypeIndex> TypeLookup;
  138. /// Map from source type index to destination type index. Indexed by source
  139. /// type index minus 0x1000.
  140. SmallVectorImpl<TypeIndex> &IndexMap;
  141. };
  142. } // end anonymous namespace
  143. const TypeIndex TypeStreamMerger::Untranslated(SimpleTypeKind::NotTranslated);
  144. Error TypeStreamMerger::visitTypeBegin(CVType &Rec) {
  145. RemappedType R(Rec);
  146. SmallVector<TiReference, 32> Refs;
  147. discoverTypeIndices(Rec.RecordData, Refs);
  148. bool Success = remapIndices(R, Refs);
  149. switch (Rec.kind()) {
  150. case TypeLeafKind::LF_FUNC_ID:
  151. case TypeLeafKind::LF_MFUNC_ID:
  152. case TypeLeafKind::LF_STRING_ID:
  153. case TypeLeafKind::LF_SUBSTR_LIST:
  154. case TypeLeafKind::LF_BUILDINFO:
  155. case TypeLeafKind::LF_UDT_SRC_LINE:
  156. case TypeLeafKind::LF_UDT_MOD_SRC_LINE:
  157. return writeIdRecord(R, Success);
  158. default:
  159. return writeTypeRecord(R, Success);
  160. }
  161. return Error::success();
  162. }
  163. Error TypeStreamMerger::visitTypeEnd(CVType &Rec) {
  164. ++CurIndex;
  165. if (!IsSecondPass)
  166. assert(IndexMap.size() == slotForIndex(CurIndex) &&
  167. "visitKnownRecord should add one index map entry");
  168. return Error::success();
  169. }
  170. void TypeStreamMerger::addMapping(TypeIndex Idx) {
  171. if (!IsSecondPass) {
  172. assert(IndexMap.size() == slotForIndex(CurIndex) &&
  173. "visitKnownRecord should add one index map entry");
  174. IndexMap.push_back(Idx);
  175. } else {
  176. assert(slotForIndex(CurIndex) < IndexMap.size());
  177. IndexMap[slotForIndex(CurIndex)] = Idx;
  178. }
  179. }
  180. bool TypeStreamMerger::remapIndex(TypeIndex &Idx, ArrayRef<TypeIndex> Map) {
  181. // Simple types are unchanged.
  182. if (Idx.isSimple())
  183. return true;
  184. // Check if this type index refers to a record we've already translated
  185. // successfully. If it refers to a type later in the stream or a record we
  186. // had to defer, defer it until later pass.
  187. unsigned MapPos = slotForIndex(Idx);
  188. if (MapPos < Map.size() && Map[MapPos] != Untranslated) {
  189. Idx = Map[MapPos];
  190. return true;
  191. }
  192. // If this is the second pass and this index isn't in the map, then it points
  193. // outside the current type stream, and this is a corrupt record.
  194. if (IsSecondPass && MapPos >= Map.size()) {
  195. // FIXME: Print a more useful error. We can give the current record and the
  196. // index that we think its pointing to.
  197. LastError = joinErrors(std::move(*LastError), errorCorruptRecord());
  198. }
  199. ++NumBadIndices;
  200. // This type index is invalid. Remap this to "not translated by cvpack",
  201. // and return failure.
  202. Idx = Untranslated;
  203. return false;
  204. }
  205. bool TypeStreamMerger::remapTypeIndex(TypeIndex &Idx) {
  206. // If we're mapping a pure index stream, then IndexMap only contains mappings
  207. // from OldIdStream -> NewIdStream, in which case we will need to use the
  208. // special mapping from OldTypeStream -> NewTypeStream which was computed
  209. // externally. Regardless, we use this special map if and only if we are
  210. // doing an id-only mapping.
  211. if (DestTypeStream == nullptr)
  212. return remapIndex(Idx, TypeLookup);
  213. assert(TypeLookup.empty());
  214. return remapIndex(Idx, IndexMap);
  215. }
  216. bool TypeStreamMerger::remapItemIndex(TypeIndex &Idx) {
  217. assert(DestIdStream);
  218. return remapIndex(Idx, IndexMap);
  219. }
  220. Error TypeStreamMerger::mergeTypeRecords(TypeTableBuilder &Dest,
  221. const CVTypeArray &Types) {
  222. DestTypeStream = &Dest;
  223. return doit(Types);
  224. }
  225. Error TypeStreamMerger::mergeIdRecords(TypeTableBuilder &Dest,
  226. ArrayRef<TypeIndex> TypeSourceToDest,
  227. const CVTypeArray &Ids) {
  228. DestIdStream = &Dest;
  229. TypeLookup = TypeSourceToDest;
  230. return doit(Ids);
  231. }
  232. Error TypeStreamMerger::mergeTypesAndIds(TypeTableBuilder &DestIds,
  233. TypeTableBuilder &DestTypes,
  234. const CVTypeArray &IdsAndTypes) {
  235. DestIdStream = &DestIds;
  236. DestTypeStream = &DestTypes;
  237. return doit(IdsAndTypes);
  238. }
  239. Error TypeStreamMerger::doit(const CVTypeArray &Types) {
  240. LastError = Error::success();
  241. // We don't want to deserialize records. I guess this flag is poorly named,
  242. // but it really means "Don't deserialize records before switching on the
  243. // concrete type.
  244. // FIXME: We can probably get even more speed here if we don't use the visitor
  245. // pipeline here, but instead write the switch ourselves. I don't think it
  246. // would buy us much since it's already pretty fast, but it's probably worth
  247. // a few cycles.
  248. if (auto EC =
  249. codeview::visitTypeStream(Types, *this, VDS_BytesExternal, Handler))
  250. return EC;
  251. // If we found bad indices but no other errors, try doing another pass and see
  252. // if we can resolve the indices that weren't in the map on the first pass.
  253. // This may require multiple passes, but we should always make progress. MASM
  254. // is the only known CodeView producer that makes type streams that aren't
  255. // topologically sorted. The standard library contains MASM-produced objects,
  256. // so this is important to handle correctly, but we don't have to be too
  257. // efficient. MASM type streams are usually very small.
  258. while (!*LastError && NumBadIndices > 0) {
  259. unsigned BadIndicesRemaining = NumBadIndices;
  260. IsSecondPass = true;
  261. NumBadIndices = 0;
  262. CurIndex = TypeIndex(TypeIndex::FirstNonSimpleIndex);
  263. if (auto EC =
  264. codeview::visitTypeStream(Types, *this, VDS_BytesExternal, Handler))
  265. return EC;
  266. assert(NumBadIndices <= BadIndicesRemaining &&
  267. "second pass found more bad indices");
  268. if (!*LastError && NumBadIndices == BadIndicesRemaining) {
  269. return llvm::make_error<CodeViewError>(
  270. cv_error_code::corrupt_record, "input type graph contains cycles");
  271. }
  272. }
  273. Error Ret = std::move(*LastError);
  274. LastError.reset();
  275. return Ret;
  276. }
  277. Error llvm::codeview::mergeTypeRecords(TypeTableBuilder &Dest,
  278. SmallVectorImpl<TypeIndex> &SourceToDest,
  279. TypeServerHandler *Handler,
  280. const CVTypeArray &Types) {
  281. TypeStreamMerger M(SourceToDest, Handler);
  282. return M.mergeTypeRecords(Dest, Types);
  283. }
  284. Error llvm::codeview::mergeIdRecords(TypeTableBuilder &Dest,
  285. ArrayRef<TypeIndex> TypeSourceToDest,
  286. SmallVectorImpl<TypeIndex> &SourceToDest,
  287. const CVTypeArray &Ids) {
  288. TypeStreamMerger M(SourceToDest, nullptr);
  289. return M.mergeIdRecords(Dest, TypeSourceToDest, Ids);
  290. }
  291. Error llvm::codeview::mergeTypeAndIdRecords(
  292. TypeTableBuilder &DestIds, TypeTableBuilder &DestTypes,
  293. SmallVectorImpl<TypeIndex> &SourceToDest, TypeServerHandler *Handler,
  294. const CVTypeArray &IdsAndTypes) {
  295. TypeStreamMerger M(SourceToDest, Handler);
  296. return M.mergeTypesAndIds(DestIds, DestTypes, IdsAndTypes);
  297. }