Minidump.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. std::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 std::nullopt;
  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. std::optional<ArrayRef<uint8_t>> Stream =
  50. getRawStream(StreamType::MemoryInfoList);
  51. if (!Stream)
  52. return createError("No such stream");
  53. auto ExpectedHeader =
  54. getDataSliceAs<minidump::MemoryInfoListHeader>(*Stream, 0, 1);
  55. if (!ExpectedHeader)
  56. return ExpectedHeader.takeError();
  57. const minidump::MemoryInfoListHeader &H = ExpectedHeader.get()[0];
  58. Expected<ArrayRef<uint8_t>> Data =
  59. getDataSlice(*Stream, H.SizeOfHeader, H.SizeOfEntry * H.NumberOfEntries);
  60. if (!Data)
  61. return Data.takeError();
  62. return make_range(MemoryInfoIterator(*Data, H.SizeOfEntry),
  63. MemoryInfoIterator({}, H.SizeOfEntry));
  64. }
  65. template <typename T>
  66. Expected<ArrayRef<T>> MinidumpFile::getListStream(StreamType Type) const {
  67. std::optional<ArrayRef<uint8_t>> Stream = getRawStream(Type);
  68. if (!Stream)
  69. return createError("No such stream");
  70. auto ExpectedSize = getDataSliceAs<support::ulittle32_t>(*Stream, 0, 1);
  71. if (!ExpectedSize)
  72. return ExpectedSize.takeError();
  73. size_t ListSize = ExpectedSize.get()[0];
  74. size_t ListOffset = 4;
  75. // Some producers insert additional padding bytes to align the list to an
  76. // 8-byte boundary. Check for that by comparing the list size with the overall
  77. // stream size.
  78. if (ListOffset + sizeof(T) * ListSize < Stream->size())
  79. ListOffset = 8;
  80. return getDataSliceAs<T>(*Stream, ListOffset, ListSize);
  81. }
  82. template Expected<ArrayRef<Module>>
  83. MinidumpFile::getListStream(StreamType) const;
  84. template Expected<ArrayRef<Thread>>
  85. MinidumpFile::getListStream(StreamType) const;
  86. template Expected<ArrayRef<MemoryDescriptor>>
  87. MinidumpFile::getListStream(StreamType) const;
  88. Expected<ArrayRef<uint8_t>>
  89. MinidumpFile::getDataSlice(ArrayRef<uint8_t> Data, size_t Offset, size_t Size) {
  90. // Check for overflow.
  91. if (Offset + Size < Offset || Offset + Size < Size ||
  92. Offset + Size > Data.size())
  93. return createEOFError();
  94. return Data.slice(Offset, Size);
  95. }
  96. Expected<std::unique_ptr<MinidumpFile>>
  97. MinidumpFile::create(MemoryBufferRef Source) {
  98. ArrayRef<uint8_t> Data = arrayRefFromStringRef(Source.getBuffer());
  99. auto ExpectedHeader = getDataSliceAs<minidump::Header>(Data, 0, 1);
  100. if (!ExpectedHeader)
  101. return ExpectedHeader.takeError();
  102. const minidump::Header &Hdr = (*ExpectedHeader)[0];
  103. if (Hdr.Signature != Header::MagicSignature)
  104. return createError("Invalid signature");
  105. if ((Hdr.Version & 0xffff) != Header::MagicVersion)
  106. return createError("Invalid version");
  107. auto ExpectedStreams = getDataSliceAs<Directory>(Data, Hdr.StreamDirectoryRVA,
  108. Hdr.NumberOfStreams);
  109. if (!ExpectedStreams)
  110. return ExpectedStreams.takeError();
  111. DenseMap<StreamType, std::size_t> StreamMap;
  112. for (const auto &StreamDescriptor : llvm::enumerate(*ExpectedStreams)) {
  113. StreamType Type = StreamDescriptor.value().Type;
  114. const LocationDescriptor &Loc = StreamDescriptor.value().Location;
  115. Expected<ArrayRef<uint8_t>> Stream =
  116. getDataSlice(Data, Loc.RVA, Loc.DataSize);
  117. if (!Stream)
  118. return Stream.takeError();
  119. if (Type == StreamType::Unused && Loc.DataSize == 0) {
  120. // Ignore dummy streams. This is technically ill-formed, but a number of
  121. // existing minidumps seem to contain such streams.
  122. continue;
  123. }
  124. if (Type == DenseMapInfo<StreamType>::getEmptyKey() ||
  125. Type == DenseMapInfo<StreamType>::getTombstoneKey())
  126. return createError("Cannot handle one of the minidump streams");
  127. // Update the directory map, checking for duplicate stream types.
  128. if (!StreamMap.try_emplace(Type, StreamDescriptor.index()).second)
  129. return createError("Duplicate stream type");
  130. }
  131. return std::unique_ptr<MinidumpFile>(
  132. new MinidumpFile(Source, Hdr, *ExpectedStreams, std::move(StreamMap)));
  133. }