1 // Copyright 2015 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "minidump/minidump_handle_writer.h"
16 
17 #include <memory>
18 #include <string>
19 #include <utility>
20 
21 #include "base/strings/utf_string_conversions.h"
22 #include "gtest/gtest.h"
23 #include "minidump/minidump_file_writer.h"
24 #include "minidump/test/minidump_file_writer_test_util.h"
25 #include "minidump/test/minidump_string_writer_test_util.h"
26 #include "minidump/test/minidump_writable_test_util.h"
27 #include "util/file/string_file.h"
28 
29 namespace crashpad {
30 namespace test {
31 namespace {
32 
33 // The handle data stream is expected to be the only stream.
GetHandleDataStream(const std::string & file_contents,const MINIDUMP_HANDLE_DATA_STREAM ** handle_data_stream)34 void GetHandleDataStream(
35     const std::string& file_contents,
36     const MINIDUMP_HANDLE_DATA_STREAM** handle_data_stream) {
37   constexpr size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER);
38   constexpr size_t kHandleDataStreamOffset =
39       kDirectoryOffset + sizeof(MINIDUMP_DIRECTORY);
40 
41   const MINIDUMP_DIRECTORY* directory;
42   const MINIDUMP_HEADER* header =
43       MinidumpHeaderAtStart(file_contents, &directory);
44   ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, 0));
45   ASSERT_TRUE(directory);
46 
47   constexpr size_t kDirectoryIndex = 0;
48 
49   ASSERT_EQ(directory[kDirectoryIndex].StreamType,
50             kMinidumpStreamTypeHandleData);
51   EXPECT_EQ(directory[kDirectoryIndex].Location.Rva, kHandleDataStreamOffset);
52 
53   *handle_data_stream =
54       MinidumpWritableAtLocationDescriptor<MINIDUMP_HANDLE_DATA_STREAM>(
55           file_contents, directory[kDirectoryIndex].Location);
56   ASSERT_TRUE(*handle_data_stream);
57 }
58 
TEST(MinidumpHandleDataWriter,Empty)59 TEST(MinidumpHandleDataWriter, Empty) {
60   MinidumpFileWriter minidump_file_writer;
61   auto handle_data_writer = std::make_unique<MinidumpHandleDataWriter>();
62   ASSERT_TRUE(minidump_file_writer.AddStream(std::move(handle_data_writer)));
63 
64   StringFile string_file;
65   ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file));
66 
67   ASSERT_EQ(string_file.string().size(),
68             sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) +
69                 sizeof(MINIDUMP_HANDLE_DATA_STREAM));
70 
71   const MINIDUMP_HANDLE_DATA_STREAM* handle_data_stream = nullptr;
72   ASSERT_NO_FATAL_FAILURE(
73       GetHandleDataStream(string_file.string(), &handle_data_stream));
74 
75   EXPECT_EQ(handle_data_stream->NumberOfDescriptors, 0u);
76 }
77 
TEST(MinidumpHandleDataWriter,OneHandle)78 TEST(MinidumpHandleDataWriter, OneHandle) {
79   MinidumpFileWriter minidump_file_writer;
80   auto handle_data_writer = std::make_unique<MinidumpHandleDataWriter>();
81 
82   HandleSnapshot handle_snapshot;
83   handle_snapshot.handle = 0x1234;
84   handle_snapshot.type_name = "Something";
85   handle_snapshot.attributes = 0x12345678;
86   handle_snapshot.granted_access = 0x9abcdef0;
87   handle_snapshot.pointer_count = 4567;
88   handle_snapshot.handle_count = 9876;
89 
90   std::vector<HandleSnapshot> snapshot;
91   snapshot.push_back(handle_snapshot);
92 
93   handle_data_writer->InitializeFromSnapshot(snapshot);
94 
95   ASSERT_TRUE(minidump_file_writer.AddStream(std::move(handle_data_writer)));
96 
97   StringFile string_file;
98   ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file));
99 
100   const size_t kTypeNameStringDataLength =
101       (handle_snapshot.type_name.size() + 1) * sizeof(base::char16);
102   ASSERT_EQ(string_file.string().size(),
103             sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) +
104                 sizeof(MINIDUMP_HANDLE_DATA_STREAM) +
105                 sizeof(MINIDUMP_HANDLE_DESCRIPTOR) + sizeof(MINIDUMP_STRING) +
106                 kTypeNameStringDataLength);
107 
108   const MINIDUMP_HANDLE_DATA_STREAM* handle_data_stream = nullptr;
109   ASSERT_NO_FATAL_FAILURE(
110       GetHandleDataStream(string_file.string(), &handle_data_stream));
111 
112   EXPECT_EQ(handle_data_stream->NumberOfDescriptors, 1u);
113   const MINIDUMP_HANDLE_DESCRIPTOR* handle_descriptor =
114       reinterpret_cast<const MINIDUMP_HANDLE_DESCRIPTOR*>(
115           &handle_data_stream[1]);
116   EXPECT_EQ(handle_descriptor->Handle, handle_snapshot.handle);
117   EXPECT_EQ(base::UTF16ToUTF8(MinidumpStringAtRVAAsString(
118                 string_file.string(), handle_descriptor->TypeNameRva)),
119             handle_snapshot.type_name);
120   EXPECT_EQ(handle_descriptor->ObjectNameRva, 0u);
121   EXPECT_EQ(handle_descriptor->Attributes, handle_snapshot.attributes);
122   EXPECT_EQ(handle_descriptor->GrantedAccess, handle_snapshot.granted_access);
123   EXPECT_EQ(handle_descriptor->HandleCount, handle_snapshot.handle_count);
124   EXPECT_EQ(handle_descriptor->PointerCount, handle_snapshot.pointer_count);
125 }
126 
TEST(MinidumpHandleDataWriter,RepeatedTypeName)127 TEST(MinidumpHandleDataWriter, RepeatedTypeName) {
128   MinidumpFileWriter minidump_file_writer;
129   auto handle_data_writer = std::make_unique<MinidumpHandleDataWriter>();
130 
131   HandleSnapshot handle_snapshot;
132   handle_snapshot.handle = 0x1234;
133   handle_snapshot.type_name = "Something";
134   handle_snapshot.attributes = 0x12345678;
135   handle_snapshot.granted_access = 0x9abcdef0;
136   handle_snapshot.pointer_count = 4567;
137   handle_snapshot.handle_count = 9876;
138 
139   HandleSnapshot handle_snapshot2;
140   handle_snapshot2.handle = 0x4321;
141   handle_snapshot2.type_name = "Something";  // Note: same as above.
142   handle_snapshot2.attributes = 0x87654321;
143   handle_snapshot2.granted_access = 0x0fedcba9;
144   handle_snapshot2.pointer_count = 7654;
145   handle_snapshot2.handle_count = 6789;
146 
147   std::vector<HandleSnapshot> snapshot;
148   snapshot.push_back(handle_snapshot);
149   snapshot.push_back(handle_snapshot2);
150 
151   handle_data_writer->InitializeFromSnapshot(snapshot);
152 
153   ASSERT_TRUE(minidump_file_writer.AddStream(std::move(handle_data_writer)));
154 
155   StringFile string_file;
156   ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file));
157 
158   const size_t kTypeNameStringDataLength =
159       (handle_snapshot.type_name.size() + 1) * sizeof(base::char16);
160   ASSERT_EQ(string_file.string().size(),
161             sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) +
162                 sizeof(MINIDUMP_HANDLE_DATA_STREAM) +
163                 (sizeof(MINIDUMP_HANDLE_DESCRIPTOR) * 2) +
164                 sizeof(MINIDUMP_STRING) + kTypeNameStringDataLength);
165 
166   const MINIDUMP_HANDLE_DATA_STREAM* handle_data_stream = nullptr;
167   ASSERT_NO_FATAL_FAILURE(
168       GetHandleDataStream(string_file.string(), &handle_data_stream));
169 
170   EXPECT_EQ(handle_data_stream->NumberOfDescriptors, 2u);
171   const MINIDUMP_HANDLE_DESCRIPTOR* handle_descriptor =
172       reinterpret_cast<const MINIDUMP_HANDLE_DESCRIPTOR*>(
173           &handle_data_stream[1]);
174   EXPECT_EQ(handle_descriptor->Handle, handle_snapshot.handle);
175   EXPECT_EQ(base::UTF16ToUTF8(MinidumpStringAtRVAAsString(
176                 string_file.string(), handle_descriptor->TypeNameRva)),
177             handle_snapshot.type_name);
178   EXPECT_EQ(handle_descriptor->ObjectNameRva, 0u);
179   EXPECT_EQ(handle_descriptor->Attributes, handle_snapshot.attributes);
180   EXPECT_EQ(handle_descriptor->GrantedAccess, handle_snapshot.granted_access);
181   EXPECT_EQ(handle_descriptor->HandleCount, handle_snapshot.handle_count);
182   EXPECT_EQ(handle_descriptor->PointerCount, handle_snapshot.pointer_count);
183 
184   const MINIDUMP_HANDLE_DESCRIPTOR* handle_descriptor2 =
185       reinterpret_cast<const MINIDUMP_HANDLE_DESCRIPTOR*>(
186           reinterpret_cast<const unsigned char*>(&handle_data_stream[1]) +
187           sizeof(MINIDUMP_HANDLE_DESCRIPTOR));
188   EXPECT_EQ(handle_descriptor2->Handle, handle_snapshot2.handle);
189   EXPECT_EQ(base::UTF16ToUTF8(MinidumpStringAtRVAAsString(
190                 string_file.string(), handle_descriptor2->TypeNameRva)),
191             handle_snapshot2.type_name);
192   EXPECT_EQ(handle_descriptor2->ObjectNameRva, 0u);
193   EXPECT_EQ(handle_descriptor2->Attributes, handle_snapshot2.attributes);
194   EXPECT_EQ(handle_descriptor2->GrantedAccess, handle_snapshot2.granted_access);
195   EXPECT_EQ(handle_descriptor2->HandleCount, handle_snapshot2.handle_count);
196   EXPECT_EQ(handle_descriptor2->PointerCount, handle_snapshot2.pointer_count);
197 
198   EXPECT_EQ(handle_descriptor2->TypeNameRva, handle_descriptor->TypeNameRva);
199 }
200 
201 }  // namespace
202 }  // namespace test
203 }  // namespace crashpad
204