blob: 97fde12c0f8cb47eecb7a075e458abfa02b064e7 [file] [log] [blame]
Pavel Labath581d79a2019-03-21 09:18:59 +00001//===- MinidumpTest.cpp - Tests for Minidump.cpp --------------------------===//
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
9#include "llvm/Object/Minidump.h"
10#include "llvm/Support/MemoryBuffer.h"
11#include "llvm/Testing/Support/Error.h"
12#include "gtest/gtest.h"
13
14using namespace llvm;
15using namespace llvm::object;
16using namespace minidump;
17
18static Expected<std::unique_ptr<MinidumpFile>> create(ArrayRef<uint8_t> Data) {
19 return MinidumpFile::create(
20 MemoryBufferRef(toStringRef(Data), "Test buffer"));
21}
22
23TEST(MinidumpFile, BasicInterface) {
Pavel Labath045b8542019-03-21 10:21:55 +000024 std::vector<uint8_t> Data{ // Header
25 'M', 'D', 'M', 'P', // Signature
26 0x93, 0xa7, 0, 0, // Version
27 1, 0, 0, 0, // NumberOfStreams,
28 0x20, 0, 0, 0, // StreamDirectoryRVA
29 0, 1, 2, 3, 4, 5, 6, 7, // Checksum, TimeDateStamp
30 8, 9, 0, 1, 2, 3, 4, 5, // Flags
Pavel Labath581d79a2019-03-21 09:18:59 +000031 // Stream Directory
Pavel Labath045b8542019-03-21 10:21:55 +000032 3, 0, 0x67, 0x47, 7, 0, 0, 0, // Type, DataSize,
33 0x2c, 0, 0, 0, // RVA
34 // Stream
35 'C', 'P', 'U', 'I', 'N', 'F', 'O'};
36 // A very simple minidump file which contains just a single stream.
37 auto ExpectedFile = create(Data);
Pavel Labath581d79a2019-03-21 09:18:59 +000038 ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
39 const MinidumpFile &File = **ExpectedFile;
40 const Header &H = File.header();
41 EXPECT_EQ(Header::MagicSignature, H.Signature);
42 EXPECT_EQ(Header::MagicVersion, H.Version);
43 EXPECT_EQ(1u, H.NumberOfStreams);
44 EXPECT_EQ(0x20u, H.StreamDirectoryRVA);
45 EXPECT_EQ(0x03020100u, H.Checksum);
46 EXPECT_EQ(0x07060504u, H.TimeDateStamp);
47 EXPECT_EQ(uint64_t(0x0504030201000908), H.Flags);
48
49 ASSERT_EQ(1u, File.streams().size());
50 const Directory &Stream0 = File.streams()[0];
51 EXPECT_EQ(StreamType::LinuxCPUInfo, Stream0.Type);
52 EXPECT_EQ(7u, Stream0.Location.DataSize);
53 EXPECT_EQ(0x2cu, Stream0.Location.RVA);
54
55 EXPECT_EQ("CPUINFO", toStringRef(File.getRawStream(Stream0)));
56 EXPECT_EQ("CPUINFO",
57 toStringRef(*File.getRawStream(StreamType::LinuxCPUInfo)));
58
59 EXPECT_THAT_EXPECTED(File.getSystemInfo(), Failed<BinaryError>());
60}
61
62// Use the input from the previous test, but corrupt it in various ways
63TEST(MinidumpFile, create_ErrorCases) {
Pavel Labath045b8542019-03-21 10:21:55 +000064 std::vector<uint8_t> FileTooShort{'M', 'D', 'M', 'P'};
65 EXPECT_THAT_EXPECTED(create(FileTooShort), Failed<BinaryError>());
Pavel Labath581d79a2019-03-21 09:18:59 +000066
Pavel Labath045b8542019-03-21 10:21:55 +000067 std::vector<uint8_t> WrongSignature{
68 // Header
69 '!', 'D', 'M', 'P', 0x93, 0xa7, 0, 0, // Signature, Version
70 1, 0, 0, 0, // NumberOfStreams,
71 0x20, 0, 0, 0, // StreamDirectoryRVA
72 0, 1, 2, 3, 4, 5, 6, 7, // Checksum, TimeDateStamp
73 8, 9, 0, 1, 2, 3, 4, 5, // Flags
74 // Stream Directory
75 3, 0, 0x67, 0x47, 7, 0, 0, 0, // Type, DataSize,
76 0x2c, 0, 0, 0, // RVA
77 // Stream
78 'C', 'P', 'U', 'I', 'N', 'F', 'O'};
79 EXPECT_THAT_EXPECTED(create(WrongSignature), Failed<BinaryError>());
Pavel Labath581d79a2019-03-21 09:18:59 +000080
Pavel Labath045b8542019-03-21 10:21:55 +000081 std::vector<uint8_t> WrongVersion{
82 // Header
83 'M', 'D', 'M', 'P', 0x39, 0xa7, 0, 0, // Signature, Version
84 1, 0, 0, 0, // NumberOfStreams,
85 0x20, 0, 0, 0, // StreamDirectoryRVA
86 0, 1, 2, 3, 4, 5, 6, 7, // Checksum, TimeDateStamp
87 8, 9, 0, 1, 2, 3, 4, 5, // Flags
88 // Stream Directory
89 3, 0, 0x67, 0x47, 7, 0, 0, 0, // Type, DataSize,
90 0x2c, 0, 0, 0, // RVA
91 // Stream
92 'C', 'P', 'U', 'I', 'N', 'F', 'O'};
93 EXPECT_THAT_EXPECTED(create(WrongVersion), Failed<BinaryError>());
Pavel Labath581d79a2019-03-21 09:18:59 +000094
Pavel Labath045b8542019-03-21 10:21:55 +000095 std::vector<uint8_t> DirectoryAfterEOF{
96 // Header
97 'M', 'D', 'M', 'P', 0x93, 0xa7, 0, 0, // Signature, Version
98 1, 0, 0, 0, // NumberOfStreams,
99 0x20, 1, 0, 0, // StreamDirectoryRVA
100 0, 1, 2, 3, 4, 5, 6, 7, // Checksum, TimeDateStamp
101 8, 9, 0, 1, 2, 3, 4, 5, // Flags
102 // Stream Directory
103 3, 0, 0x67, 0x47, 7, 0, 0, 0, // Type, DataSize,
104 0x2c, 0, 0, 0, // RVA
105 // Stream
106 'C', 'P', 'U', 'I', 'N', 'F', 'O'};
107 EXPECT_THAT_EXPECTED(create(DirectoryAfterEOF), Failed<BinaryError>());
Pavel Labath581d79a2019-03-21 09:18:59 +0000108
Pavel Labath045b8542019-03-21 10:21:55 +0000109 std::vector<uint8_t> TruncatedDirectory{
110 // Header
111 'M', 'D', 'M', 'P', 0x93, 0xa7, 0, 0, // Signature, Version
112 1, 1, 0, 0, // NumberOfStreams,
113 0x20, 0, 0, 0, // StreamDirectoryRVA
114 0, 1, 2, 3, 4, 5, 6, 7, // Checksum, TimeDateStamp
115 8, 9, 0, 1, 2, 3, 4, 5, // Flags
116 // Stream Directory
117 3, 0, 0x67, 0x47, 7, 0, 0, 0, // Type, DataSize,
118 0x2c, 0, 0, 0, // RVA
119 // Stream
120 'C', 'P', 'U', 'I', 'N', 'F', 'O'};
121 EXPECT_THAT_EXPECTED(create(TruncatedDirectory), Failed<BinaryError>());
Pavel Labath581d79a2019-03-21 09:18:59 +0000122
Pavel Labath045b8542019-03-21 10:21:55 +0000123 std::vector<uint8_t> Stream0AfterEOF{
124 // Header
125 'M', 'D', 'M', 'P', 0x93, 0xa7, 0, 0, // Signature, Version
126 1, 0, 0, 0, // NumberOfStreams,
127 0x20, 0, 0, 0, // StreamDirectoryRVA
128 0, 1, 2, 3, 4, 5, 6, 7, // Checksum, TimeDateStamp
129 8, 9, 0, 1, 2, 3, 4, 5, // Flags
130 // Stream Directory
131 3, 0, 0x67, 0x47, 7, 0, 0, 0, // Type, DataSize,
132 0x2c, 1, 0, 0, // RVA
133 // Stream
134 'C', 'P', 'U', 'I', 'N', 'F', 'O'};
135 EXPECT_THAT_EXPECTED(create(Stream0AfterEOF), Failed<BinaryError>());
Pavel Labath581d79a2019-03-21 09:18:59 +0000136
Pavel Labath045b8542019-03-21 10:21:55 +0000137 std::vector<uint8_t> Stream0Truncated{
138 // Header
139 'M', 'D', 'M', 'P', 0x93, 0xa7, 0, 0, // Signature, Version
140 1, 0, 0, 0, // NumberOfStreams,
141 0x20, 0, 0, 0, // StreamDirectoryRVA
142 0, 1, 2, 3, 4, 5, 6, 7, // Checksum, TimeDateStamp
143 8, 9, 0, 1, 2, 3, 4, 5, // Flags
144 // Stream Directory
145 3, 0, 0x67, 0x47, 8, 0, 0, 0, // Type, DataSize,
146 0x2c, 0, 0, 0, // RVA
147 // Stream
148 'C', 'P', 'U', 'I', 'N', 'F', 'O'};
149 EXPECT_THAT_EXPECTED(create(Stream0Truncated), Failed<BinaryError>());
Pavel Labath581d79a2019-03-21 09:18:59 +0000150
Pavel Labath045b8542019-03-21 10:21:55 +0000151 std::vector<uint8_t> DuplicateStream{
152 // Header
153 'M', 'D', 'M', 'P', 0x93, 0xa7, 0, 0, // Signature, Version
154 2, 0, 0, 0, // NumberOfStreams,
155 0x20, 0, 0, 0, // StreamDirectoryRVA
156 0, 1, 2, 3, 4, 5, 6, 7, // Checksum, TimeDateStamp
157 8, 9, 0, 1, 2, 3, 4, 5, // Flags
158 // Stream Directory
159 3, 0, 0x67, 0x47, 7, 0, 0, 0, // Type, DataSize,
160 0x40, 0, 0, 0, // RVA
161 // Stream
162 3, 0, 0x67, 0x47, 7, 0, 0, 0, // Type, DataSize,
163 0x40, 0, 0, 0, // RVA
164 // Stream
165 'C', 'P', 'U', 'I', 'N', 'F', 'O'};
166 EXPECT_THAT_EXPECTED(create(DuplicateStream), Failed<BinaryError>());
Pavel Labath581d79a2019-03-21 09:18:59 +0000167
Pavel Labath045b8542019-03-21 10:21:55 +0000168 std::vector<uint8_t> DenseMapInfoConflict{
169 // Header
170 'M', 'D', 'M', 'P', 0x93, 0xa7, 0, 0, // Signature, Version
171 1, 0, 0, 0, // NumberOfStreams,
172 0x20, 0, 0, 0, // StreamDirectoryRVA
173 0, 1, 2, 3, 4, 5, 6, 7, // Checksum, TimeDateStamp
174 8, 9, 0, 1, 2, 3, 4, 5, // Flags
175 // Stream Directory
176 0xff, 0xff, 0xff, 0xff, 7, 0, 0, 0, // Type, DataSize,
177 0x2c, 0, 0, 0, // RVA
178 // Stream
179 'C', 'P', 'U', 'I', 'N', 'F', 'O'};
180 EXPECT_THAT_EXPECTED(create(DenseMapInfoConflict), Failed<BinaryError>());
Pavel Labath581d79a2019-03-21 09:18:59 +0000181}
182
183TEST(MinidumpFile, IngoresDummyStreams) {
Pavel Labath045b8542019-03-21 10:21:55 +0000184 std::vector<uint8_t> TwoDummyStreams{
Pavel Labath581d79a2019-03-21 09:18:59 +0000185 // Header
186 'M', 'D', 'M', 'P', 0x93, 0xa7, 0, 0, // Signature, Version
187 2, 0, 0, 0, // NumberOfStreams,
188 0x20, 0, 0, 0, // StreamDirectoryRVA
189 0, 1, 2, 3, 4, 5, 6, 7, // Checksum, TimeDateStamp
190 8, 9, 0, 1, 2, 3, 4, 5, // Flags
191 // Stream Directory
192 0, 0, 0, 0, 0, 0, 0, 0, // Type, DataSize,
193 0x20, 0, 0, 0, // RVA
194 0, 0, 0, 0, 0, 0, 0, 0, // Type, DataSize,
195 0x20, 0, 0, 0, // RVA
Pavel Labath045b8542019-03-21 10:21:55 +0000196 };
197 auto ExpectedFile = create(TwoDummyStreams);
Pavel Labath581d79a2019-03-21 09:18:59 +0000198 ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
199 const MinidumpFile &File = **ExpectedFile;
200 ASSERT_EQ(2u, File.streams().size());
201 EXPECT_EQ(StreamType::Unused, File.streams()[0].Type);
202 EXPECT_EQ(StreamType::Unused, File.streams()[1].Type);
203 EXPECT_EQ(None, File.getRawStream(StreamType::Unused));
204}
205
206TEST(MinidumpFile, getSystemInfo) {
Pavel Labath045b8542019-03-21 10:21:55 +0000207 std::vector<uint8_t> Data{
Pavel Labath581d79a2019-03-21 09:18:59 +0000208 // Header
209 'M', 'D', 'M', 'P', 0x93, 0xa7, 0, 0, // Signature, Version
210 1, 0, 0, 0, // NumberOfStreams,
211 0x20, 0, 0, 0, // StreamDirectoryRVA
212 0, 1, 2, 3, 4, 5, 6, 7, // Checksum, TimeDateStamp
213 8, 9, 0, 1, 2, 3, 4, 5, // Flags
214 // Stream Directory
215 7, 0, 0, 0, 56, 0, 0, 0, // Type, DataSize,
216 0x2c, 0, 0, 0, // RVA
217 // SystemInfo
218 0, 0, 1, 2, // ProcessorArch, ProcessorLevel
219 3, 4, 5, 6, // ProcessorRevision, NumberOfProcessors, ProductType
220 7, 8, 9, 0, 1, 2, 3, 4, // MajorVersion, MinorVersion
221 5, 6, 7, 8, 2, 0, 0, 0, // BuildNumber, PlatformId
222 1, 2, 3, 4, 5, 6, 7, 8, // CSDVersionRVA, SuiteMask, Reserved
223 'L', 'L', 'V', 'M', 'L', 'L', 'V', 'M', 'L', 'L', 'V', 'M', // VendorID
224 1, 2, 3, 4, 5, 6, 7, 8, // VersionInfo, FeatureInfo
225 9, 0, 1, 2, // AMDExtendedFeatures
Pavel Labath045b8542019-03-21 10:21:55 +0000226 };
227 auto ExpectedFile = create(Data);
Pavel Labath581d79a2019-03-21 09:18:59 +0000228 ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
229 const MinidumpFile &File = **ExpectedFile;
230
231 auto ExpectedInfo = File.getSystemInfo();
232 ASSERT_THAT_EXPECTED(ExpectedInfo, Succeeded());
233 const SystemInfo &Info = *ExpectedInfo;
234 EXPECT_EQ(ProcessorArchitecture::X86, Info.ProcessorArch);
235 EXPECT_EQ(0x0201, Info.ProcessorLevel);
236 EXPECT_EQ(0x0403, Info.ProcessorRevision);
237 EXPECT_EQ(5, Info.NumberOfProcessors);
238 EXPECT_EQ(6, Info.ProductType);
239 EXPECT_EQ(0x00090807u, Info.MajorVersion);
240 EXPECT_EQ(0x04030201u, Info.MinorVersion);
241 EXPECT_EQ(0x08070605u, Info.BuildNumber);
242 EXPECT_EQ(OSPlatform::Win32NT, Info.PlatformId);
243 EXPECT_EQ(0x04030201u, Info.CSDVersionRVA);
244 EXPECT_EQ(0x0605u, Info.SuiteMask);
245 EXPECT_EQ(0x0807u, Info.Reserved);
246 EXPECT_EQ("LLVMLLVMLLVM", llvm::StringRef(Info.CPU.X86.VendorID,
247 sizeof(Info.CPU.X86.VendorID)));
248 EXPECT_EQ(0x04030201u, Info.CPU.X86.VersionInfo);
249 EXPECT_EQ(0x08070605u, Info.CPU.X86.FeatureInfo);
250 EXPECT_EQ(0x02010009u, Info.CPU.X86.AMDExtendedFeatures);
251}