2014-08-03 18:47:34 -04:00
|
|
|
|
// Copyright 2014 The Crashpad Authors. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
#include "minidump/minidump_file_writer.h"
|
|
|
|
|
|
2016-01-06 12:22:50 -05:00
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <string.h>
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
|
|
|
|
#include <string>
|
2015-12-09 17:36:32 -05:00
|
|
|
|
#include <utility>
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
2015-02-05 11:30:29 -08:00
|
|
|
|
#include "base/compiler_specific.h"
|
2016-01-06 12:22:50 -05:00
|
|
|
|
#include "base/macros.h"
|
2016-04-25 12:13:07 -07:00
|
|
|
|
#include "base/memory/ptr_util.h"
|
2014-08-03 18:47:34 -04:00
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
#include "minidump/minidump_stream_writer.h"
|
2017-03-23 08:52:46 -04:00
|
|
|
|
#include "minidump/minidump_user_extension_stream_data_source.h"
|
2014-08-03 18:47:34 -04:00
|
|
|
|
#include "minidump/minidump_writable.h"
|
2014-10-20 12:11:14 -04:00
|
|
|
|
#include "minidump/test/minidump_file_writer_test_util.h"
|
2014-10-22 18:35:18 -04:00
|
|
|
|
#include "minidump/test/minidump_writable_test_util.h"
|
2014-11-07 14:47:08 -05:00
|
|
|
|
#include "snapshot/test/test_cpu_context.h"
|
|
|
|
|
#include "snapshot/test/test_exception_snapshot.h"
|
2015-09-25 10:31:02 -07:00
|
|
|
|
#include "snapshot/test/test_memory_snapshot.h"
|
2014-11-07 14:47:08 -05:00
|
|
|
|
#include "snapshot/test/test_module_snapshot.h"
|
|
|
|
|
#include "snapshot/test/test_process_snapshot.h"
|
|
|
|
|
#include "snapshot/test/test_system_snapshot.h"
|
|
|
|
|
#include "snapshot/test/test_thread_snapshot.h"
|
test: Move util/test to its own top-level directory, test.
After 9e79ea1da719, it no longer makes sense for crashpad_util_test_lib
to “hide” in util/util_test.gyp. All of util/test is moved to its own
top-level directory, test, which all other test code is allowed to
depend on. test, too, is allowed to depend on all other non-test code.
In a future change, when crashpad_util_test_lib gains a dependency on
crashpad_client, it won’t look so weird for something in util (even
though it’s in util/test) to depend on something in client, because the
thing that needs to depend on client will live in test, not util.
BUG=crashpad:33
R=scottmg@chromium.org
Review URL: https://codereview.chromium.org/1051533002
2015-03-31 17:44:14 -04:00
|
|
|
|
#include "test/gtest_death_check.h"
|
2015-02-18 14:15:38 -05:00
|
|
|
|
#include "util/file/string_file.h"
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
2014-10-07 17:28:50 -04:00
|
|
|
|
namespace crashpad {
|
|
|
|
|
namespace test {
|
2014-08-03 18:47:34 -04:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
TEST(MinidumpFileWriter, Empty) {
|
|
|
|
|
MinidumpFileWriter minidump_file;
|
2015-02-18 14:15:38 -05:00
|
|
|
|
StringFile string_file;
|
|
|
|
|
ASSERT_TRUE(minidump_file.WriteEverything(&string_file));
|
|
|
|
|
ASSERT_EQ(sizeof(MINIDUMP_HEADER), string_file.string().size());
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
2014-10-21 14:15:07 -04:00
|
|
|
|
const MINIDUMP_DIRECTORY* directory;
|
2014-08-03 18:47:34 -04:00
|
|
|
|
const MINIDUMP_HEADER* header =
|
2015-02-18 14:15:38 -05:00
|
|
|
|
MinidumpHeaderAtStart(string_file.string(), &directory);
|
2014-10-09 15:08:54 -04:00
|
|
|
|
ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 0, 0));
|
2014-10-21 14:15:07 -04:00
|
|
|
|
EXPECT_FALSE(directory);
|
2014-08-03 18:47:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TestStream final : public internal::MinidumpStreamWriter {
|
|
|
|
|
public:
|
|
|
|
|
TestStream(MinidumpStreamType stream_type,
|
|
|
|
|
size_t stream_size,
|
|
|
|
|
uint8_t stream_value)
|
|
|
|
|
: stream_data_(stream_size, stream_value), stream_type_(stream_type) {}
|
|
|
|
|
|
2014-10-27 15:01:39 -04:00
|
|
|
|
~TestStream() override {}
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
|
|
|
|
// MinidumpStreamWriter:
|
2014-10-14 11:11:57 -04:00
|
|
|
|
MinidumpStreamType StreamType() const override {
|
2014-08-03 18:47:34 -04:00
|
|
|
|
return stream_type_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// MinidumpWritable:
|
2014-10-14 11:11:57 -04:00
|
|
|
|
size_t SizeOfObject() override {
|
2014-08-03 18:47:34 -04:00
|
|
|
|
EXPECT_GE(state(), kStateFrozen);
|
|
|
|
|
return stream_data_.size();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-14 11:11:57 -04:00
|
|
|
|
bool WriteObject(FileWriterInterface* file_writer) override {
|
2014-08-03 18:47:34 -04:00
|
|
|
|
EXPECT_EQ(state(), kStateWritable);
|
|
|
|
|
return file_writer->Write(&stream_data_[0], stream_data_.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::string stream_data_;
|
|
|
|
|
MinidumpStreamType stream_type_;
|
|
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TestStream);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST(MinidumpFileWriter, OneStream) {
|
|
|
|
|
MinidumpFileWriter minidump_file;
|
|
|
|
|
const time_t kTimestamp = 0x155d2fb8;
|
|
|
|
|
minidump_file.SetTimestamp(kTimestamp);
|
|
|
|
|
|
|
|
|
|
const size_t kStreamSize = 5;
|
|
|
|
|
const MinidumpStreamType kStreamType = static_cast<MinidumpStreamType>(0x4d);
|
|
|
|
|
const uint8_t kStreamValue = 0x5a;
|
2014-10-27 15:01:39 -04:00
|
|
|
|
auto stream =
|
2016-04-25 12:13:07 -07:00
|
|
|
|
base::WrapUnique(new TestStream(kStreamType, kStreamSize, kStreamValue));
|
2017-03-15 15:35:36 -04:00
|
|
|
|
ASSERT_TRUE(minidump_file.AddStream(std::move(stream)));
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
2015-02-18 14:15:38 -05:00
|
|
|
|
StringFile string_file;
|
|
|
|
|
ASSERT_TRUE(minidump_file.WriteEverything(&string_file));
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
|
|
|
|
const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER);
|
|
|
|
|
const size_t kStreamOffset = kDirectoryOffset + sizeof(MINIDUMP_DIRECTORY);
|
|
|
|
|
const size_t kFileSize = kStreamOffset + kStreamSize;
|
|
|
|
|
|
2015-02-18 14:15:38 -05:00
|
|
|
|
ASSERT_EQ(kFileSize, string_file.string().size());
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
2014-10-21 14:15:07 -04:00
|
|
|
|
const MINIDUMP_DIRECTORY* directory;
|
2014-08-03 18:47:34 -04:00
|
|
|
|
const MINIDUMP_HEADER* header =
|
2015-02-18 14:15:38 -05:00
|
|
|
|
MinidumpHeaderAtStart(string_file.string(), &directory);
|
2014-10-09 15:08:54 -04:00
|
|
|
|
ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, kTimestamp));
|
2014-10-21 14:15:07 -04:00
|
|
|
|
ASSERT_TRUE(directory);
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
2014-10-21 14:15:07 -04:00
|
|
|
|
EXPECT_EQ(kStreamType, directory[0].StreamType);
|
|
|
|
|
EXPECT_EQ(kStreamSize, directory[0].Location.DataSize);
|
|
|
|
|
EXPECT_EQ(kStreamOffset, directory[0].Location.Rva);
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
2014-10-22 18:35:18 -04:00
|
|
|
|
const uint8_t* stream_data = MinidumpWritableAtLocationDescriptor<uint8_t>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[0].Location);
|
2014-10-22 18:35:18 -04:00
|
|
|
|
ASSERT_TRUE(stream_data);
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
|
|
|
|
std::string expected_stream(kStreamSize, kStreamValue);
|
|
|
|
|
EXPECT_EQ(0, memcmp(stream_data, expected_stream.c_str(), kStreamSize));
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 08:52:46 -04:00
|
|
|
|
TEST(MinidumpFileWriter, AddUserExtensionStream) {
|
|
|
|
|
MinidumpFileWriter minidump_file;
|
|
|
|
|
const time_t kTimestamp = 0x155d2fb8;
|
|
|
|
|
minidump_file.SetTimestamp(kTimestamp);
|
|
|
|
|
|
|
|
|
|
static const uint8_t kStreamData[] = "Hello World!";
|
|
|
|
|
const size_t kStreamSize = arraysize(kStreamData);
|
|
|
|
|
const MinidumpStreamType kStreamType = static_cast<MinidumpStreamType>(0x4d);
|
|
|
|
|
|
|
|
|
|
auto stream = base::WrapUnique(new MinidumpUserExtensionStreamDataSource(
|
|
|
|
|
kStreamType, kStreamData, kStreamSize));
|
|
|
|
|
ASSERT_TRUE(minidump_file.AddUserExtensionStream(std::move(stream)));
|
|
|
|
|
|
|
|
|
|
// Adding the same stream type a second time should fail.
|
|
|
|
|
stream = base::WrapUnique(new MinidumpUserExtensionStreamDataSource(
|
|
|
|
|
kStreamType, kStreamData, kStreamSize));
|
|
|
|
|
ASSERT_FALSE(minidump_file.AddUserExtensionStream(std::move(stream)));
|
|
|
|
|
|
|
|
|
|
StringFile string_file;
|
|
|
|
|
ASSERT_TRUE(minidump_file.WriteEverything(&string_file));
|
|
|
|
|
|
|
|
|
|
const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER);
|
|
|
|
|
const size_t kStreamOffset = kDirectoryOffset + sizeof(MINIDUMP_DIRECTORY);
|
|
|
|
|
const size_t kFileSize = kStreamOffset + kStreamSize;
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(kFileSize, string_file.string().size());
|
|
|
|
|
|
|
|
|
|
const MINIDUMP_DIRECTORY* directory;
|
|
|
|
|
const MINIDUMP_HEADER* header =
|
|
|
|
|
MinidumpHeaderAtStart(string_file.string(), &directory);
|
|
|
|
|
ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, kTimestamp));
|
|
|
|
|
ASSERT_TRUE(directory);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kStreamType, directory[0].StreamType);
|
|
|
|
|
EXPECT_EQ(kStreamSize, directory[0].Location.DataSize);
|
|
|
|
|
EXPECT_EQ(kStreamOffset, directory[0].Location.Rva);
|
|
|
|
|
|
|
|
|
|
const uint8_t* stream_data = MinidumpWritableAtLocationDescriptor<uint8_t>(
|
|
|
|
|
string_file.string(), directory[0].Location);
|
|
|
|
|
ASSERT_TRUE(stream_data);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(0, memcmp(stream_data, kStreamData, kStreamSize));
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-03 18:47:34 -04:00
|
|
|
|
TEST(MinidumpFileWriter, ThreeStreams) {
|
|
|
|
|
MinidumpFileWriter minidump_file;
|
|
|
|
|
const time_t kTimestamp = 0x155d2fb8;
|
|
|
|
|
minidump_file.SetTimestamp(kTimestamp);
|
|
|
|
|
|
2014-10-22 18:35:18 -04:00
|
|
|
|
const size_t kStream0Size = 5;
|
|
|
|
|
const MinidumpStreamType kStream0Type = static_cast<MinidumpStreamType>(0x6d);
|
|
|
|
|
const uint8_t kStream0Value = 0x5a;
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto stream0 = base::WrapUnique(
|
2014-10-27 15:01:39 -04:00
|
|
|
|
new TestStream(kStream0Type, kStream0Size, kStream0Value));
|
2017-03-15 15:35:36 -04:00
|
|
|
|
ASSERT_TRUE(minidump_file.AddStream(std::move(stream0)));
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
|
|
|
|
// Make the second stream’s type be a smaller quantity than the first stream’s
|
|
|
|
|
// to test that the streams show up in the order that they were added, not in
|
|
|
|
|
// numeric order.
|
2014-10-22 18:35:18 -04:00
|
|
|
|
const size_t kStream1Size = 3;
|
|
|
|
|
const MinidumpStreamType kStream1Type = static_cast<MinidumpStreamType>(0x4d);
|
|
|
|
|
const uint8_t kStream1Value = 0xa5;
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto stream1 = base::WrapUnique(
|
2014-10-27 15:01:39 -04:00
|
|
|
|
new TestStream(kStream1Type, kStream1Size, kStream1Value));
|
2017-03-15 15:35:36 -04:00
|
|
|
|
ASSERT_TRUE(minidump_file.AddStream(std::move(stream1)));
|
2014-10-22 18:35:18 -04:00
|
|
|
|
|
|
|
|
|
const size_t kStream2Size = 1;
|
|
|
|
|
const MinidumpStreamType kStream2Type = static_cast<MinidumpStreamType>(0x7e);
|
|
|
|
|
const uint8_t kStream2Value = 0x36;
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto stream2 = base::WrapUnique(
|
2014-10-27 15:01:39 -04:00
|
|
|
|
new TestStream(kStream2Type, kStream2Size, kStream2Value));
|
2017-03-15 15:35:36 -04:00
|
|
|
|
ASSERT_TRUE(minidump_file.AddStream(std::move(stream2)));
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
2015-02-18 14:15:38 -05:00
|
|
|
|
StringFile string_file;
|
|
|
|
|
ASSERT_TRUE(minidump_file.WriteEverything(&string_file));
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
|
|
|
|
const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER);
|
2014-10-22 18:35:18 -04:00
|
|
|
|
const size_t kStream0Offset =
|
2014-08-03 18:47:34 -04:00
|
|
|
|
kDirectoryOffset + 3 * sizeof(MINIDUMP_DIRECTORY);
|
2014-10-22 18:35:18 -04:00
|
|
|
|
const size_t kStream1Padding = 3;
|
|
|
|
|
const size_t kStream1Offset = kStream0Offset + kStream0Size + kStream1Padding;
|
|
|
|
|
const size_t kStream2Padding = 1;
|
2014-08-03 18:47:34 -04:00
|
|
|
|
const size_t kStream2Offset = kStream1Offset + kStream1Size + kStream2Padding;
|
2014-10-22 18:35:18 -04:00
|
|
|
|
const size_t kFileSize = kStream2Offset + kStream2Size;
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
2015-02-18 14:15:38 -05:00
|
|
|
|
ASSERT_EQ(kFileSize, string_file.string().size());
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
2014-10-21 14:15:07 -04:00
|
|
|
|
const MINIDUMP_DIRECTORY* directory;
|
2014-08-03 18:47:34 -04:00
|
|
|
|
const MINIDUMP_HEADER* header =
|
2015-02-18 14:15:38 -05:00
|
|
|
|
MinidumpHeaderAtStart(string_file.string(), &directory);
|
2014-10-09 15:08:54 -04:00
|
|
|
|
ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 3, kTimestamp));
|
2014-10-21 14:15:07 -04:00
|
|
|
|
ASSERT_TRUE(directory);
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
2014-10-22 18:35:18 -04:00
|
|
|
|
EXPECT_EQ(kStream0Type, directory[0].StreamType);
|
|
|
|
|
EXPECT_EQ(kStream0Size, directory[0].Location.DataSize);
|
|
|
|
|
EXPECT_EQ(kStream0Offset, directory[0].Location.Rva);
|
|
|
|
|
EXPECT_EQ(kStream1Type, directory[1].StreamType);
|
|
|
|
|
EXPECT_EQ(kStream1Size, directory[1].Location.DataSize);
|
|
|
|
|
EXPECT_EQ(kStream1Offset, directory[1].Location.Rva);
|
|
|
|
|
EXPECT_EQ(kStream2Type, directory[2].StreamType);
|
|
|
|
|
EXPECT_EQ(kStream2Size, directory[2].Location.DataSize);
|
|
|
|
|
EXPECT_EQ(kStream2Offset, directory[2].Location.Rva);
|
|
|
|
|
|
|
|
|
|
const uint8_t* stream0_data = MinidumpWritableAtLocationDescriptor<uint8_t>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[0].Location);
|
2014-10-22 18:35:18 -04:00
|
|
|
|
ASSERT_TRUE(stream0_data);
|
|
|
|
|
|
|
|
|
|
std::string expected_stream0(kStream0Size, kStream0Value);
|
|
|
|
|
EXPECT_EQ(0, memcmp(stream0_data, expected_stream0.c_str(), kStream0Size));
|
|
|
|
|
|
|
|
|
|
const int kZeroes[16] = {};
|
|
|
|
|
ASSERT_GE(sizeof(kZeroes), kStream1Padding);
|
|
|
|
|
EXPECT_EQ(0, memcmp(stream0_data + kStream0Size, kZeroes, kStream1Padding));
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
2014-10-22 18:35:18 -04:00
|
|
|
|
const uint8_t* stream1_data = MinidumpWritableAtLocationDescriptor<uint8_t>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[1].Location);
|
2014-10-22 18:35:18 -04:00
|
|
|
|
ASSERT_TRUE(stream1_data);
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
|
|
|
|
std::string expected_stream1(kStream1Size, kStream1Value);
|
|
|
|
|
EXPECT_EQ(0, memcmp(stream1_data, expected_stream1.c_str(), kStream1Size));
|
|
|
|
|
|
|
|
|
|
ASSERT_GE(sizeof(kZeroes), kStream2Padding);
|
|
|
|
|
EXPECT_EQ(0, memcmp(stream1_data + kStream1Size, kZeroes, kStream2Padding));
|
|
|
|
|
|
2014-10-22 18:35:18 -04:00
|
|
|
|
const uint8_t* stream2_data = MinidumpWritableAtLocationDescriptor<uint8_t>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[2].Location);
|
2014-10-22 18:35:18 -04:00
|
|
|
|
ASSERT_TRUE(stream2_data);
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
|
|
|
|
std::string expected_stream2(kStream2Size, kStream2Value);
|
|
|
|
|
EXPECT_EQ(0, memcmp(stream2_data, expected_stream2.c_str(), kStream2Size));
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-12 10:26:40 -07:00
|
|
|
|
TEST(MinidumpFileWriter, ZeroLengthStream) {
|
|
|
|
|
MinidumpFileWriter minidump_file;
|
|
|
|
|
|
|
|
|
|
const size_t kStreamSize = 0;
|
|
|
|
|
const MinidumpStreamType kStreamType = static_cast<MinidumpStreamType>(0x4d);
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto stream = base::WrapUnique(new TestStream(kStreamType, kStreamSize, 0));
|
2017-03-15 15:35:36 -04:00
|
|
|
|
ASSERT_TRUE(minidump_file.AddStream(std::move(stream)));
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
2015-02-18 14:15:38 -05:00
|
|
|
|
StringFile string_file;
|
|
|
|
|
ASSERT_TRUE(minidump_file.WriteEverything(&string_file));
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
|
|
|
|
const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER);
|
|
|
|
|
const size_t kStreamOffset = kDirectoryOffset + sizeof(MINIDUMP_DIRECTORY);
|
|
|
|
|
const size_t kFileSize = kStreamOffset + kStreamSize;
|
|
|
|
|
|
2015-02-18 14:15:38 -05:00
|
|
|
|
ASSERT_EQ(kFileSize, string_file.string().size());
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
2014-10-21 14:15:07 -04:00
|
|
|
|
const MINIDUMP_DIRECTORY* directory;
|
2014-08-12 10:26:40 -07:00
|
|
|
|
const MINIDUMP_HEADER* header =
|
2015-02-18 14:15:38 -05:00
|
|
|
|
MinidumpHeaderAtStart(string_file.string(), &directory);
|
2014-10-09 15:08:54 -04:00
|
|
|
|
ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, 0));
|
2014-10-21 14:15:07 -04:00
|
|
|
|
ASSERT_TRUE(directory);
|
2014-08-12 10:26:40 -07:00
|
|
|
|
|
2014-10-21 14:15:07 -04:00
|
|
|
|
EXPECT_EQ(kStreamType, directory[0].StreamType);
|
|
|
|
|
EXPECT_EQ(kStreamSize, directory[0].Location.DataSize);
|
|
|
|
|
EXPECT_EQ(kStreamOffset, directory[0].Location.Rva);
|
2014-08-12 10:26:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-07 14:47:08 -05:00
|
|
|
|
TEST(MinidumpFileWriter, InitializeFromSnapshot_Basic) {
|
|
|
|
|
const uint32_t kSnapshotTime = 0x4976043c;
|
2015-02-05 08:43:38 -08:00
|
|
|
|
const timeval kSnapshotTimeval = { static_cast<time_t>(kSnapshotTime), 0 };
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
TestProcessSnapshot process_snapshot;
|
|
|
|
|
process_snapshot.SetSnapshotTime(kSnapshotTimeval);
|
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto system_snapshot = base::WrapUnique(new TestSystemSnapshot());
|
2014-11-07 14:47:08 -05:00
|
|
|
|
system_snapshot->SetCPUArchitecture(kCPUArchitectureX86_64);
|
|
|
|
|
system_snapshot->SetOperatingSystem(SystemSnapshot::kOperatingSystemMacOSX);
|
2015-12-09 17:36:32 -05:00
|
|
|
|
process_snapshot.SetSystem(std::move(system_snapshot));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto peb_snapshot = base::WrapUnique(new TestMemorySnapshot());
|
2015-09-25 10:31:02 -07:00
|
|
|
|
const uint64_t kPebAddress = 0x07f90000;
|
|
|
|
|
peb_snapshot->SetAddress(kPebAddress);
|
|
|
|
|
const size_t kPebSize = 0x280;
|
|
|
|
|
peb_snapshot->SetSize(kPebSize);
|
|
|
|
|
peb_snapshot->SetValue('p');
|
2015-12-09 17:36:32 -05:00
|
|
|
|
process_snapshot.AddExtraMemory(std::move(peb_snapshot));
|
2015-09-25 10:31:02 -07:00
|
|
|
|
|
2014-11-07 14:47:08 -05:00
|
|
|
|
MinidumpFileWriter minidump_file_writer;
|
|
|
|
|
minidump_file_writer.InitializeFromSnapshot(&process_snapshot);
|
|
|
|
|
|
2015-02-18 14:15:38 -05:00
|
|
|
|
StringFile string_file;
|
|
|
|
|
ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
const MINIDUMP_DIRECTORY* directory;
|
|
|
|
|
const MINIDUMP_HEADER* header =
|
2015-02-18 14:15:38 -05:00
|
|
|
|
MinidumpHeaderAtStart(string_file.string(), &directory);
|
2014-11-07 14:47:08 -05:00
|
|
|
|
ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 5, kSnapshotTime));
|
|
|
|
|
ASSERT_TRUE(directory);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeSystemInfo, directory[0].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_SYSTEM_INFO>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[0].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeMiscInfo, directory[1].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MISC_INFO_4>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[1].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeThreadList, directory[2].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_THREAD_LIST>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[2].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeModuleList, directory[3].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MODULE_LIST>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[3].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeMemoryList, directory[4].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MEMORY_LIST>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[4].Location));
|
2015-09-25 10:31:02 -07:00
|
|
|
|
|
|
|
|
|
const MINIDUMP_MEMORY_LIST* memory_list =
|
|
|
|
|
MinidumpWritableAtLocationDescriptor<MINIDUMP_MEMORY_LIST>(
|
|
|
|
|
string_file.string(), directory[4].Location);
|
|
|
|
|
EXPECT_EQ(1u, memory_list->NumberOfMemoryRanges);
|
|
|
|
|
EXPECT_EQ(kPebAddress, memory_list->MemoryRanges[0].StartOfMemoryRange);
|
|
|
|
|
EXPECT_EQ(kPebSize, memory_list->MemoryRanges[0].Memory.DataSize);
|
2014-11-07 14:47:08 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(MinidumpFileWriter, InitializeFromSnapshot_Exception) {
|
|
|
|
|
// In a 32-bit environment, this will give a “timestamp out of range” warning,
|
|
|
|
|
// but the test should complete without failure.
|
|
|
|
|
const uint32_t kSnapshotTime = 0xfd469ab8;
|
2015-02-05 11:30:29 -08:00
|
|
|
|
MSVC_SUPPRESS_WARNING(4309); // Truncation of constant value.
|
2015-09-21 10:51:15 -07:00
|
|
|
|
MSVC_SUPPRESS_WARNING(4838); // Narrowing conversion.
|
2015-02-05 08:43:38 -08:00
|
|
|
|
const timeval kSnapshotTimeval = { static_cast<time_t>(kSnapshotTime), 0 };
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
TestProcessSnapshot process_snapshot;
|
|
|
|
|
process_snapshot.SetSnapshotTime(kSnapshotTimeval);
|
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto system_snapshot = base::WrapUnique(new TestSystemSnapshot());
|
2014-11-07 14:47:08 -05:00
|
|
|
|
system_snapshot->SetCPUArchitecture(kCPUArchitectureX86_64);
|
|
|
|
|
system_snapshot->SetOperatingSystem(SystemSnapshot::kOperatingSystemMacOSX);
|
2015-12-09 17:36:32 -05:00
|
|
|
|
process_snapshot.SetSystem(std::move(system_snapshot));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto thread_snapshot = base::WrapUnique(new TestThreadSnapshot());
|
2014-11-07 14:47:08 -05:00
|
|
|
|
InitializeCPUContextX86_64(thread_snapshot->MutableContext(), 5);
|
2015-12-09 17:36:32 -05:00
|
|
|
|
process_snapshot.AddThread(std::move(thread_snapshot));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto exception_snapshot = base::WrapUnique(new TestExceptionSnapshot());
|
2014-11-07 14:47:08 -05:00
|
|
|
|
InitializeCPUContextX86_64(exception_snapshot->MutableContext(), 11);
|
2015-12-09 17:36:32 -05:00
|
|
|
|
process_snapshot.SetException(std::move(exception_snapshot));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
// The module does not have anything that needs to be represented in a
|
|
|
|
|
// MinidumpModuleCrashpadInfo structure, so no such structure is expected to
|
|
|
|
|
// be present, which will in turn suppress the addition of a
|
|
|
|
|
// MinidumpCrashpadInfo stream.
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto module_snapshot = base::WrapUnique(new TestModuleSnapshot());
|
2015-12-09 17:36:32 -05:00
|
|
|
|
process_snapshot.AddModule(std::move(module_snapshot));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
MinidumpFileWriter minidump_file_writer;
|
|
|
|
|
minidump_file_writer.InitializeFromSnapshot(&process_snapshot);
|
|
|
|
|
|
2015-02-18 14:15:38 -05:00
|
|
|
|
StringFile string_file;
|
|
|
|
|
ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
const MINIDUMP_DIRECTORY* directory;
|
|
|
|
|
const MINIDUMP_HEADER* header =
|
2015-02-18 14:15:38 -05:00
|
|
|
|
MinidumpHeaderAtStart(string_file.string(), &directory);
|
2014-11-07 14:47:08 -05:00
|
|
|
|
ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 6, kSnapshotTime));
|
|
|
|
|
ASSERT_TRUE(directory);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeSystemInfo, directory[0].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_SYSTEM_INFO>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[0].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeMiscInfo, directory[1].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MISC_INFO_4>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[1].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeThreadList, directory[2].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_THREAD_LIST>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[2].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeException, directory[3].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_EXCEPTION_STREAM>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[3].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeModuleList, directory[4].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MODULE_LIST>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[4].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeMemoryList, directory[5].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MEMORY_LIST>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[5].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(MinidumpFileWriter, InitializeFromSnapshot_CrashpadInfo) {
|
|
|
|
|
const uint32_t kSnapshotTime = 0x15393bd3;
|
2015-02-05 08:43:38 -08:00
|
|
|
|
const timeval kSnapshotTimeval = { static_cast<time_t>(kSnapshotTime), 0 };
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
TestProcessSnapshot process_snapshot;
|
|
|
|
|
process_snapshot.SetSnapshotTime(kSnapshotTimeval);
|
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto system_snapshot = base::WrapUnique(new TestSystemSnapshot());
|
2014-11-07 14:47:08 -05:00
|
|
|
|
system_snapshot->SetCPUArchitecture(kCPUArchitectureX86_64);
|
|
|
|
|
system_snapshot->SetOperatingSystem(SystemSnapshot::kOperatingSystemMacOSX);
|
2015-12-09 17:36:32 -05:00
|
|
|
|
process_snapshot.SetSystem(std::move(system_snapshot));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto thread_snapshot = base::WrapUnique(new TestThreadSnapshot());
|
2014-11-07 14:47:08 -05:00
|
|
|
|
InitializeCPUContextX86_64(thread_snapshot->MutableContext(), 5);
|
2015-12-09 17:36:32 -05:00
|
|
|
|
process_snapshot.AddThread(std::move(thread_snapshot));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto exception_snapshot = base::WrapUnique(new TestExceptionSnapshot());
|
2014-11-07 14:47:08 -05:00
|
|
|
|
InitializeCPUContextX86_64(exception_snapshot->MutableContext(), 11);
|
2015-12-09 17:36:32 -05:00
|
|
|
|
process_snapshot.SetException(std::move(exception_snapshot));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
// The module needs an annotation for the MinidumpCrashpadInfo stream to be
|
|
|
|
|
// considered useful and be included.
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto module_snapshot = base::WrapUnique(new TestModuleSnapshot());
|
2014-11-07 14:47:08 -05:00
|
|
|
|
std::vector<std::string> annotations_list(1, std::string("annotation"));
|
|
|
|
|
module_snapshot->SetAnnotationsVector(annotations_list);
|
2015-12-09 17:36:32 -05:00
|
|
|
|
process_snapshot.AddModule(std::move(module_snapshot));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
MinidumpFileWriter minidump_file_writer;
|
|
|
|
|
minidump_file_writer.InitializeFromSnapshot(&process_snapshot);
|
|
|
|
|
|
2015-02-18 14:15:38 -05:00
|
|
|
|
StringFile string_file;
|
|
|
|
|
ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
const MINIDUMP_DIRECTORY* directory;
|
|
|
|
|
const MINIDUMP_HEADER* header =
|
2015-02-18 14:15:38 -05:00
|
|
|
|
MinidumpHeaderAtStart(string_file.string(), &directory);
|
2014-11-07 14:47:08 -05:00
|
|
|
|
ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 7, kSnapshotTime));
|
|
|
|
|
ASSERT_TRUE(directory);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeSystemInfo, directory[0].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_SYSTEM_INFO>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[0].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeMiscInfo, directory[1].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MISC_INFO_4>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[1].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeThreadList, directory[2].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_THREAD_LIST>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[2].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeException, directory[3].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_EXCEPTION_STREAM>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[3].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeModuleList, directory[4].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MODULE_LIST>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[4].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeCrashpadInfo, directory[5].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MinidumpCrashpadInfo>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[5].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kMinidumpStreamTypeMemoryList, directory[6].StreamType);
|
|
|
|
|
EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MEMORY_LIST>(
|
2015-02-18 14:15:38 -05:00
|
|
|
|
string_file.string(), directory[6].Location));
|
2014-11-07 14:47:08 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-15 15:35:36 -04:00
|
|
|
|
TEST(MinidumpFileWriter, SameStreamType) {
|
2014-08-03 18:47:34 -04:00
|
|
|
|
MinidumpFileWriter minidump_file;
|
|
|
|
|
|
2017-03-15 15:35:36 -04:00
|
|
|
|
const size_t kStream0Size = 3;
|
|
|
|
|
const MinidumpStreamType kStreamType = static_cast<MinidumpStreamType>(0x4d);
|
2014-10-22 18:35:18 -04:00
|
|
|
|
const uint8_t kStream0Value = 0x5a;
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto stream0 = base::WrapUnique(
|
2017-03-15 15:35:36 -04:00
|
|
|
|
new TestStream(kStreamType, kStream0Size, kStream0Value));
|
|
|
|
|
ASSERT_TRUE(minidump_file.AddStream(std::move(stream0)));
|
2014-08-03 18:47:34 -04:00
|
|
|
|
|
2017-03-15 15:35:36 -04:00
|
|
|
|
// An attempt to add a second stream of the same type should fail.
|
|
|
|
|
const size_t kStream1Size = 5;
|
2014-10-22 18:35:18 -04:00
|
|
|
|
const uint8_t kStream1Value = 0xa5;
|
2016-04-25 12:13:07 -07:00
|
|
|
|
auto stream1 = base::WrapUnique(
|
2017-03-15 15:35:36 -04:00
|
|
|
|
new TestStream(kStreamType, kStream1Size, kStream1Value));
|
|
|
|
|
ASSERT_FALSE(minidump_file.AddStream(std::move(stream1)));
|
|
|
|
|
|
|
|
|
|
StringFile string_file;
|
|
|
|
|
ASSERT_TRUE(minidump_file.WriteEverything(&string_file));
|
|
|
|
|
|
|
|
|
|
const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER);
|
|
|
|
|
const size_t kStream0Offset = kDirectoryOffset + sizeof(MINIDUMP_DIRECTORY);
|
|
|
|
|
const size_t kFileSize = kStream0Offset + kStream0Size;
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(kFileSize, string_file.string().size());
|
|
|
|
|
|
|
|
|
|
const MINIDUMP_DIRECTORY* directory;
|
|
|
|
|
const MINIDUMP_HEADER* header =
|
|
|
|
|
MinidumpHeaderAtStart(string_file.string(), &directory);
|
|
|
|
|
ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, 0));
|
|
|
|
|
ASSERT_TRUE(directory);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kStreamType, directory[0].StreamType);
|
|
|
|
|
EXPECT_EQ(kStream0Size, directory[0].Location.DataSize);
|
|
|
|
|
EXPECT_EQ(kStream0Offset, directory[0].Location.Rva);
|
|
|
|
|
|
|
|
|
|
const uint8_t* stream_data = MinidumpWritableAtLocationDescriptor<uint8_t>(
|
|
|
|
|
string_file.string(), directory[0].Location);
|
|
|
|
|
ASSERT_TRUE(stream_data);
|
|
|
|
|
|
|
|
|
|
std::string expected_stream(kStream0Size, kStream0Value);
|
|
|
|
|
EXPECT_EQ(0, memcmp(stream_data, expected_stream.c_str(), kStream0Size));
|
2014-08-03 18:47:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
2014-10-07 17:28:50 -04:00
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace crashpad
|