mirror of
https://github.com/chromium/crashpad.git
synced 2025-01-15 10:07:56 +08:00
0a4ea0b52d
All minidump objects now own their all of their children, rather than having them maintain weak pointers and requiring callers to maintain ownership. The only weak object in the entire tree now is the “extra memory” added to a MinidumpMemoryListWriter by its AddExtraMemory() method. Extra memory aliases objects owned elsewhere in the tree, typically by a MinidumpThreadWriter as stack memory. Non-“extra” memory added to a MinidumpMemoryListWriter by its AddMemory() method is strongly owned. Many objects are now deleted through base pointers, and in those cases, the base classes now have public virtual destructors. The ultimate base, MinidumpWritable, is still protected to guard against direct instantiation and deletion, and thus its destructor does not need to be virtual. This updates mini_chromium to eeb3b6a4f020 specifically for that revision, which includes necessary updates to scoped_ptr. It also picks up: eeb3b6a4f020 Update base/move.h and base/memory/scoped_ptr.h to match 67ad2efafaba More porting to Windows be27a006421e AUTHORS: Fix link post-git migration flag day. 05f5b1503230 Add codereview.settings to mini_chromium. a32c2b199811 Beginnings of Windows support in mini_chromium TEST=minidump_test R=rsesek@chromium.org Review URL: https://codereview.chromium.org/674153002
257 lines
9.3 KiB
C++
257 lines
9.3 KiB
C++
// 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"
|
||
|
||
#include <dbghelp.h>
|
||
|
||
#include <string>
|
||
|
||
#include "base/basictypes.h"
|
||
#include "gtest/gtest.h"
|
||
#include "minidump/minidump_stream_writer.h"
|
||
#include "minidump/minidump_writable.h"
|
||
#include "minidump/test/minidump_file_writer_test_util.h"
|
||
#include "minidump/test/minidump_writable_test_util.h"
|
||
#include "util/file/file_writer.h"
|
||
#include "util/file/string_file_writer.h"
|
||
|
||
namespace crashpad {
|
||
namespace test {
|
||
namespace {
|
||
|
||
TEST(MinidumpFileWriter, Empty) {
|
||
MinidumpFileWriter minidump_file;
|
||
StringFileWriter file_writer;
|
||
ASSERT_TRUE(minidump_file.WriteEverything(&file_writer));
|
||
ASSERT_EQ(sizeof(MINIDUMP_HEADER), file_writer.string().size());
|
||
|
||
const MINIDUMP_DIRECTORY* directory;
|
||
const MINIDUMP_HEADER* header =
|
||
MinidumpHeaderAtStart(file_writer.string(), &directory);
|
||
ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 0, 0));
|
||
EXPECT_FALSE(directory);
|
||
}
|
||
|
||
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) {}
|
||
|
||
~TestStream() override {}
|
||
|
||
// MinidumpStreamWriter:
|
||
MinidumpStreamType StreamType() const override {
|
||
return stream_type_;
|
||
}
|
||
|
||
protected:
|
||
// MinidumpWritable:
|
||
size_t SizeOfObject() override {
|
||
EXPECT_GE(state(), kStateFrozen);
|
||
return stream_data_.size();
|
||
}
|
||
|
||
bool WriteObject(FileWriterInterface* file_writer) override {
|
||
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;
|
||
auto stream =
|
||
make_scoped_ptr(new TestStream(kStreamType, kStreamSize, kStreamValue));
|
||
minidump_file.AddStream(stream.Pass());
|
||
|
||
StringFileWriter file_writer;
|
||
ASSERT_TRUE(minidump_file.WriteEverything(&file_writer));
|
||
|
||
const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER);
|
||
const size_t kStreamOffset = kDirectoryOffset + sizeof(MINIDUMP_DIRECTORY);
|
||
const size_t kFileSize = kStreamOffset + kStreamSize;
|
||
|
||
ASSERT_EQ(kFileSize, file_writer.string().size());
|
||
|
||
const MINIDUMP_DIRECTORY* directory;
|
||
const MINIDUMP_HEADER* header =
|
||
MinidumpHeaderAtStart(file_writer.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>(
|
||
file_writer.string(), directory[0].Location);
|
||
ASSERT_TRUE(stream_data);
|
||
|
||
std::string expected_stream(kStreamSize, kStreamValue);
|
||
EXPECT_EQ(0, memcmp(stream_data, expected_stream.c_str(), kStreamSize));
|
||
}
|
||
|
||
TEST(MinidumpFileWriter, ThreeStreams) {
|
||
MinidumpFileWriter minidump_file;
|
||
const time_t kTimestamp = 0x155d2fb8;
|
||
minidump_file.SetTimestamp(kTimestamp);
|
||
|
||
const size_t kStream0Size = 5;
|
||
const MinidumpStreamType kStream0Type = static_cast<MinidumpStreamType>(0x6d);
|
||
const uint8_t kStream0Value = 0x5a;
|
||
auto stream0 = make_scoped_ptr(
|
||
new TestStream(kStream0Type, kStream0Size, kStream0Value));
|
||
minidump_file.AddStream(stream0.Pass());
|
||
|
||
// 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.
|
||
const size_t kStream1Size = 3;
|
||
const MinidumpStreamType kStream1Type = static_cast<MinidumpStreamType>(0x4d);
|
||
const uint8_t kStream1Value = 0xa5;
|
||
auto stream1 = make_scoped_ptr(
|
||
new TestStream(kStream1Type, kStream1Size, kStream1Value));
|
||
minidump_file.AddStream(stream1.Pass());
|
||
|
||
const size_t kStream2Size = 1;
|
||
const MinidumpStreamType kStream2Type = static_cast<MinidumpStreamType>(0x7e);
|
||
const uint8_t kStream2Value = 0x36;
|
||
auto stream2 = make_scoped_ptr(
|
||
new TestStream(kStream2Type, kStream2Size, kStream2Value));
|
||
minidump_file.AddStream(stream2.Pass());
|
||
|
||
StringFileWriter file_writer;
|
||
ASSERT_TRUE(minidump_file.WriteEverything(&file_writer));
|
||
|
||
const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER);
|
||
const size_t kStream0Offset =
|
||
kDirectoryOffset + 3 * sizeof(MINIDUMP_DIRECTORY);
|
||
const size_t kStream1Padding = 3;
|
||
const size_t kStream1Offset = kStream0Offset + kStream0Size + kStream1Padding;
|
||
const size_t kStream2Padding = 1;
|
||
const size_t kStream2Offset = kStream1Offset + kStream1Size + kStream2Padding;
|
||
const size_t kFileSize = kStream2Offset + kStream2Size;
|
||
|
||
ASSERT_EQ(kFileSize, file_writer.string().size());
|
||
|
||
const MINIDUMP_DIRECTORY* directory;
|
||
const MINIDUMP_HEADER* header =
|
||
MinidumpHeaderAtStart(file_writer.string(), &directory);
|
||
ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 3, kTimestamp));
|
||
ASSERT_TRUE(directory);
|
||
|
||
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>(
|
||
file_writer.string(), directory[0].Location);
|
||
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));
|
||
|
||
const uint8_t* stream1_data = MinidumpWritableAtLocationDescriptor<uint8_t>(
|
||
file_writer.string(), directory[1].Location);
|
||
ASSERT_TRUE(stream1_data);
|
||
|
||
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));
|
||
|
||
const uint8_t* stream2_data = MinidumpWritableAtLocationDescriptor<uint8_t>(
|
||
file_writer.string(), directory[2].Location);
|
||
ASSERT_TRUE(stream2_data);
|
||
|
||
std::string expected_stream2(kStream2Size, kStream2Value);
|
||
EXPECT_EQ(0, memcmp(stream2_data, expected_stream2.c_str(), kStream2Size));
|
||
}
|
||
|
||
TEST(MinidumpFileWriter, ZeroLengthStream) {
|
||
MinidumpFileWriter minidump_file;
|
||
|
||
const size_t kStreamSize = 0;
|
||
const MinidumpStreamType kStreamType = static_cast<MinidumpStreamType>(0x4d);
|
||
auto stream = make_scoped_ptr(new TestStream(kStreamType, kStreamSize, 0));
|
||
minidump_file.AddStream(stream.Pass());
|
||
|
||
StringFileWriter file_writer;
|
||
ASSERT_TRUE(minidump_file.WriteEverything(&file_writer));
|
||
|
||
const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER);
|
||
const size_t kStreamOffset = kDirectoryOffset + sizeof(MINIDUMP_DIRECTORY);
|
||
const size_t kFileSize = kStreamOffset + kStreamSize;
|
||
|
||
ASSERT_EQ(kFileSize, file_writer.string().size());
|
||
|
||
const MINIDUMP_DIRECTORY* directory;
|
||
const MINIDUMP_HEADER* header =
|
||
MinidumpHeaderAtStart(file_writer.string(), &directory);
|
||
ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, 0));
|
||
ASSERT_TRUE(directory);
|
||
|
||
EXPECT_EQ(kStreamType, directory[0].StreamType);
|
||
EXPECT_EQ(kStreamSize, directory[0].Location.DataSize);
|
||
EXPECT_EQ(kStreamOffset, directory[0].Location.Rva);
|
||
}
|
||
|
||
TEST(MinidumpFileWriterDeathTest, SameStreamType) {
|
||
MinidumpFileWriter minidump_file;
|
||
|
||
const size_t kStream0Size = 5;
|
||
const MinidumpStreamType kStream0Type = static_cast<MinidumpStreamType>(0x4d);
|
||
const uint8_t kStream0Value = 0x5a;
|
||
auto stream0 = make_scoped_ptr(
|
||
new TestStream(kStream0Type, kStream0Size, kStream0Value));
|
||
minidump_file.AddStream(stream0.Pass());
|
||
|
||
// It is an error to add a second stream of the same type.
|
||
const size_t kStream1Size = 3;
|
||
const MinidumpStreamType kStream1Type = static_cast<MinidumpStreamType>(0x4d);
|
||
const uint8_t kStream1Value = 0xa5;
|
||
auto stream1 = make_scoped_ptr(
|
||
new TestStream(kStream1Type, kStream1Size, kStream1Value));
|
||
ASSERT_DEATH(minidump_file.AddStream(stream1.Pass()), "already present");
|
||
}
|
||
|
||
} // namespace
|
||
} // namespace test
|
||
} // namespace crashpad
|