Minidump.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //===- Minidump.cpp - Minidump object file implementation -----------------===//
  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. #include "llvm/Object/Minidump.h"
  9. #include "llvm/Object/Error.h"
  10. #include "llvm/Support/ConvertUTF.h"
  11. using namespace llvm;
  12. using namespace llvm::object;
  13. using namespace llvm::minidump;
  14. Optional<ArrayRef<uint8_t>>
  15. MinidumpFile::getRawStream(minidump::StreamType Type) const {
  16. auto It = StreamMap.find(Type);
  17. if (It != StreamMap.end())
  18. return getRawStream(Streams[It->second]);
  19. return None;
  20. }
  21. Expected<std::string> MinidumpFile::getString(size_t Offset) const {
  22. // Minidump strings consist of a 32-bit length field, which gives the size of
  23. // the string in *bytes*. This is followed by the actual string encoded in
  24. // UTF16.
  25. auto ExpectedSize =
  26. getDataSliceAs<support::ulittle32_t>(getData(), Offset, 1);
  27. if (!ExpectedSize)
  28. return ExpectedSize.takeError();
  29. size_t Size = (*ExpectedSize)[0];
  30. if (Size % 2 != 0)
  31. return createError("String size not even");
  32. Size /= 2;
  33. if (Size == 0)
  34. return "";
  35. Offset += sizeof(support::ulittle32_t);
  36. auto ExpectedData =
  37. getDataSliceAs<support::ulittle16_t>(getData(), Offset, Size);
  38. if (!ExpectedData)
  39. return ExpectedData.takeError();
  40. SmallVector<UTF16, 32> WStr(Size);
  41. copy(*ExpectedData, WStr.begin());
  42. std::string Result;
  43. if (!convertUTF16ToUTF8String(WStr, Result))
  44. return createError("String decoding failed");
  45. return Result;
  46. }
  47. Expected<iterator_range<MinidumpFile::MemoryInfoIterator>>
  48. MinidumpFile::getMemoryInfoList() const {
  49. Optional<ArrayRef<uint8_t>> Stream = getRawStream(StreamType::MemoryInfoList);
  50. if (!Stream)
  51. return createError("No such stream");
  52. auto ExpectedHeader =
  53. getDataSliceAs<minidump::MemoryInfoListHeader>(*Stream, 0, 1);
  54. if (!ExpectedHeader)
  55. return ExpectedHeader.takeError();
  56. const minidump::MemoryInfoListHeader &H = ExpectedHeader.get()[0];
  57. Expected<ArrayRef<uint8_t>> Data =
  58. getDataSlice(*Stream, H.SizeOfHeader, H.SizeOfEntry * H.NumberOfEntries);
  59. if (!Data)
  60. return Data.takeError();
  61. return make_range(MemoryInfoIterator(*Data, H.SizeOfEntry),
  62. MemoryInfoIterator({}, H.SizeOfEntry));
  63. }
  64. template <typename T>
  65. Expected<ArrayRef<T>> MinidumpFile::getListStream(StreamType Type) const {
  66. Optional<ArrayRef<uint8_t>> Stream = getRawStream(Type);
  67. if (!Stream)
  68. return createError("No such stream");
  69. auto ExpectedSize = getDataSliceAs<support::ulittle32_t>(*Stream, 0, 1);
  70. if (!ExpectedSize)
  71. return ExpectedSize.takeError();
  72. size_t ListSize = ExpectedSize.get()[0];
  73. size_t ListOffset = 4;
  74. // Some producers insert additional padding bytes to align the list to an
  75. // 8-byte boundary. Check for that by comparing the list size with the overall
  76. // stream size.
  77. if (ListOffset + sizeof(T) * ListSize < Stream->size())
  78. ListOffset = 8;
  79. return getDataSliceAs<T>(*Stream, ListOffset, ListSize);
  80. }
  81. template Expected<ArrayRef<Module>>
  82. MinidumpFile::getListStream(StreamType) const;
  83. template Expected<ArrayRef<Thread>>
  84. MinidumpFile::getListStream(StreamType) const;
  85. template Expected<ArrayRef<MemoryDescriptor>>
  86. MinidumpFile::getListStream(StreamType) const;
  87. Expected<ArrayRef<uint8_t>>
  88. MinidumpFile::getDataSlice(ArrayRef<uint8_t> Data, size_t Offset, size_t Size) {
  89. // Check for overflow.
  90. if (Offset + Size < Offset || Offset + Size < Size ||
  91. Offset + Size > Data.size())
  92. return createEOFError();
  93. return Data.slice(Offset, Size);
  94. }
  95. Expected<std::unique_ptr<MinidumpFile>>
  96. MinidumpFile::create(MemoryBufferRef Source) {
  97. ArrayRef<uint8_t> Data = arrayRefFromStringRef(Source.getBuffer());
  98. auto ExpectedHeader = getDataSliceAs<minidump::Header>(Data, 0, 1);
  99. if (!ExpectedHeader)
  100. return ExpectedHeader.takeError();
  101. const minidump::Header &Hdr = (*ExpectedHeader)[0];
  102. if (Hdr.Signature != Header::MagicSignature)
  103. return createError("Invalid signature");
  104. if ((Hdr.Version & 0xffff) != Header::MagicVersion)
  105. return createError("Invalid version");
  106. auto ExpectedStreams = getDataSliceAs<Directory>(Data, Hdr.StreamDirectoryRVA,
  107. Hdr.NumberOfStreams);
  108. if (!ExpectedStreams)
  109. return ExpectedStreams.takeError();
  110. DenseMap<StreamType, std::size_t> StreamMap;
  111. for (const auto &StreamDescriptor : llvm::enumerate(*ExpectedStreams)) {
  112. StreamType Type = StreamDescriptor.value().Type;
  113. const LocationDescriptor &Loc = StreamDescriptor.value().Location;
  114. Expected<ArrayRef<uint8_t>> Stream =
  115. getDataSlice(Data, Loc.RVA, Loc.DataSize);
  116. if (!Stream)
  117. return Stream.takeError();
  118. if (Type == StreamType::Unused && Loc.DataSize == 0) {
  119. // Ignore dummy streams. This is technically ill-formed, but a number of
  120. // existing minidumps seem to contain such streams.
  121. continue;
  122. }
  123. if (Type == DenseMapInfo<StreamType>::getEmptyKey() ||
  124. Type == DenseMapInfo<StreamType>::getTombstoneKey())
  125. return createError("Cannot handle one of the minidump streams");
  126. // Update the directory map, checking for duplicate stream types.
  127. if (!StreamMap.try_emplace(Type, StreamDescriptor.index()).second)
  128. return createError("Duplicate stream type");
  129. }
  130. return std::unique_ptr<MinidumpFile>(
  131. new MinidumpFile(Source, Hdr, *ExpectedStreams, std::move(StreamMap)));
  132. }