10b57cec5SDimitry Andric //===- Minidump.cpp - Minidump object file implementation -----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/Object/Minidump.h"
100b57cec5SDimitry Andric #include "llvm/Object/Error.h"
110b57cec5SDimitry Andric #include "llvm/Support/ConvertUTF.h"
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric using namespace llvm;
140b57cec5SDimitry Andric using namespace llvm::object;
150b57cec5SDimitry Andric using namespace llvm::minidump;
160b57cec5SDimitry Andric
17bdd1243dSDimitry Andric std::optional<ArrayRef<uint8_t>>
getRawStream(minidump::StreamType Type) const180b57cec5SDimitry Andric MinidumpFile::getRawStream(minidump::StreamType Type) const {
190b57cec5SDimitry Andric auto It = StreamMap.find(Type);
200b57cec5SDimitry Andric if (It != StreamMap.end())
210b57cec5SDimitry Andric return getRawStream(Streams[It->second]);
22bdd1243dSDimitry Andric return std::nullopt;
230b57cec5SDimitry Andric }
240b57cec5SDimitry Andric
getString(size_t Offset) const250b57cec5SDimitry Andric Expected<std::string> MinidumpFile::getString(size_t Offset) const {
260b57cec5SDimitry Andric // Minidump strings consist of a 32-bit length field, which gives the size of
270b57cec5SDimitry Andric // the string in *bytes*. This is followed by the actual string encoded in
280b57cec5SDimitry Andric // UTF16.
290b57cec5SDimitry Andric auto ExpectedSize =
300b57cec5SDimitry Andric getDataSliceAs<support::ulittle32_t>(getData(), Offset, 1);
310b57cec5SDimitry Andric if (!ExpectedSize)
320b57cec5SDimitry Andric return ExpectedSize.takeError();
330b57cec5SDimitry Andric size_t Size = (*ExpectedSize)[0];
340b57cec5SDimitry Andric if (Size % 2 != 0)
350b57cec5SDimitry Andric return createError("String size not even");
360b57cec5SDimitry Andric Size /= 2;
370b57cec5SDimitry Andric if (Size == 0)
380b57cec5SDimitry Andric return "";
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric Offset += sizeof(support::ulittle32_t);
410b57cec5SDimitry Andric auto ExpectedData =
420b57cec5SDimitry Andric getDataSliceAs<support::ulittle16_t>(getData(), Offset, Size);
430b57cec5SDimitry Andric if (!ExpectedData)
440b57cec5SDimitry Andric return ExpectedData.takeError();
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric SmallVector<UTF16, 32> WStr(Size);
470b57cec5SDimitry Andric copy(*ExpectedData, WStr.begin());
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric std::string Result;
500b57cec5SDimitry Andric if (!convertUTF16ToUTF8String(WStr, Result))
510b57cec5SDimitry Andric return createError("String decoding failed");
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric return Result;
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric
568bcb0991SDimitry Andric Expected<iterator_range<MinidumpFile::MemoryInfoIterator>>
getMemoryInfoList() const578bcb0991SDimitry Andric MinidumpFile::getMemoryInfoList() const {
58bdd1243dSDimitry Andric std::optional<ArrayRef<uint8_t>> Stream =
59bdd1243dSDimitry Andric getRawStream(StreamType::MemoryInfoList);
608bcb0991SDimitry Andric if (!Stream)
610b57cec5SDimitry Andric return createError("No such stream");
628bcb0991SDimitry Andric auto ExpectedHeader =
638bcb0991SDimitry Andric getDataSliceAs<minidump::MemoryInfoListHeader>(*Stream, 0, 1);
648bcb0991SDimitry Andric if (!ExpectedHeader)
658bcb0991SDimitry Andric return ExpectedHeader.takeError();
668bcb0991SDimitry Andric const minidump::MemoryInfoListHeader &H = ExpectedHeader.get()[0];
678bcb0991SDimitry Andric Expected<ArrayRef<uint8_t>> Data =
688bcb0991SDimitry Andric getDataSlice(*Stream, H.SizeOfHeader, H.SizeOfEntry * H.NumberOfEntries);
698bcb0991SDimitry Andric if (!Data)
708bcb0991SDimitry Andric return Data.takeError();
718bcb0991SDimitry Andric return make_range(MemoryInfoIterator(*Data, H.SizeOfEntry),
728bcb0991SDimitry Andric MemoryInfoIterator({}, H.SizeOfEntry));
738bcb0991SDimitry Andric }
748bcb0991SDimitry Andric
758bcb0991SDimitry Andric template <typename T>
getListStream(StreamType Type) const768bcb0991SDimitry Andric Expected<ArrayRef<T>> MinidumpFile::getListStream(StreamType Type) const {
77bdd1243dSDimitry Andric std::optional<ArrayRef<uint8_t>> Stream = getRawStream(Type);
788bcb0991SDimitry Andric if (!Stream)
798bcb0991SDimitry Andric return createError("No such stream");
808bcb0991SDimitry Andric auto ExpectedSize = getDataSliceAs<support::ulittle32_t>(*Stream, 0, 1);
810b57cec5SDimitry Andric if (!ExpectedSize)
820b57cec5SDimitry Andric return ExpectedSize.takeError();
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric size_t ListSize = ExpectedSize.get()[0];
850b57cec5SDimitry Andric
860b57cec5SDimitry Andric size_t ListOffset = 4;
870b57cec5SDimitry Andric // Some producers insert additional padding bytes to align the list to an
880b57cec5SDimitry Andric // 8-byte boundary. Check for that by comparing the list size with the overall
890b57cec5SDimitry Andric // stream size.
908bcb0991SDimitry Andric if (ListOffset + sizeof(T) * ListSize < Stream->size())
910b57cec5SDimitry Andric ListOffset = 8;
920b57cec5SDimitry Andric
938bcb0991SDimitry Andric return getDataSliceAs<T>(*Stream, ListOffset, ListSize);
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric template Expected<ArrayRef<Module>>
960b57cec5SDimitry Andric MinidumpFile::getListStream(StreamType) const;
970b57cec5SDimitry Andric template Expected<ArrayRef<Thread>>
980b57cec5SDimitry Andric MinidumpFile::getListStream(StreamType) const;
990b57cec5SDimitry Andric template Expected<ArrayRef<MemoryDescriptor>>
1000b57cec5SDimitry Andric MinidumpFile::getListStream(StreamType) const;
1010b57cec5SDimitry Andric
1020b57cec5SDimitry Andric Expected<ArrayRef<uint8_t>>
getDataSlice(ArrayRef<uint8_t> Data,size_t Offset,size_t Size)1030b57cec5SDimitry Andric MinidumpFile::getDataSlice(ArrayRef<uint8_t> Data, size_t Offset, size_t Size) {
1040b57cec5SDimitry Andric // Check for overflow.
1050b57cec5SDimitry Andric if (Offset + Size < Offset || Offset + Size < Size ||
1060b57cec5SDimitry Andric Offset + Size > Data.size())
1070b57cec5SDimitry Andric return createEOFError();
1080b57cec5SDimitry Andric return Data.slice(Offset, Size);
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric Expected<std::unique_ptr<MinidumpFile>>
create(MemoryBufferRef Source)1120b57cec5SDimitry Andric MinidumpFile::create(MemoryBufferRef Source) {
1130b57cec5SDimitry Andric ArrayRef<uint8_t> Data = arrayRefFromStringRef(Source.getBuffer());
1140b57cec5SDimitry Andric auto ExpectedHeader = getDataSliceAs<minidump::Header>(Data, 0, 1);
1150b57cec5SDimitry Andric if (!ExpectedHeader)
1160b57cec5SDimitry Andric return ExpectedHeader.takeError();
1170b57cec5SDimitry Andric
1180b57cec5SDimitry Andric const minidump::Header &Hdr = (*ExpectedHeader)[0];
1190b57cec5SDimitry Andric if (Hdr.Signature != Header::MagicSignature)
1200b57cec5SDimitry Andric return createError("Invalid signature");
1210b57cec5SDimitry Andric if ((Hdr.Version & 0xffff) != Header::MagicVersion)
1220b57cec5SDimitry Andric return createError("Invalid version");
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric auto ExpectedStreams = getDataSliceAs<Directory>(Data, Hdr.StreamDirectoryRVA,
1250b57cec5SDimitry Andric Hdr.NumberOfStreams);
1260b57cec5SDimitry Andric if (!ExpectedStreams)
1270b57cec5SDimitry Andric return ExpectedStreams.takeError();
1280b57cec5SDimitry Andric
1290b57cec5SDimitry Andric DenseMap<StreamType, std::size_t> StreamMap;
1308bcb0991SDimitry Andric for (const auto &StreamDescriptor : llvm::enumerate(*ExpectedStreams)) {
1318bcb0991SDimitry Andric StreamType Type = StreamDescriptor.value().Type;
1328bcb0991SDimitry Andric const LocationDescriptor &Loc = StreamDescriptor.value().Location;
1330b57cec5SDimitry Andric
1348bcb0991SDimitry Andric Expected<ArrayRef<uint8_t>> Stream =
1358bcb0991SDimitry Andric getDataSlice(Data, Loc.RVA, Loc.DataSize);
1368bcb0991SDimitry Andric if (!Stream)
1378bcb0991SDimitry Andric return Stream.takeError();
1380b57cec5SDimitry Andric
1390b57cec5SDimitry Andric if (Type == StreamType::Unused && Loc.DataSize == 0) {
1400b57cec5SDimitry Andric // Ignore dummy streams. This is technically ill-formed, but a number of
1410b57cec5SDimitry Andric // existing minidumps seem to contain such streams.
1420b57cec5SDimitry Andric continue;
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric if (Type == DenseMapInfo<StreamType>::getEmptyKey() ||
1460b57cec5SDimitry Andric Type == DenseMapInfo<StreamType>::getTombstoneKey())
1470b57cec5SDimitry Andric return createError("Cannot handle one of the minidump streams");
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric // Update the directory map, checking for duplicate stream types.
1508bcb0991SDimitry Andric if (!StreamMap.try_emplace(Type, StreamDescriptor.index()).second)
1510b57cec5SDimitry Andric return createError("Duplicate stream type");
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andric return std::unique_ptr<MinidumpFile>(
1550b57cec5SDimitry Andric new MinidumpFile(Source, Hdr, *ExpectedStreams, std::move(StreamMap)));
1560b57cec5SDimitry Andric }
157