crashpad/minidump/minidump_context_writer_test.cc
Mark Mentovai af594c8deb Heap-allocate MinidumpContextAMD64Writer objects with proper alignment
While making crashpad_minidump_test run in Chromium’s try- and buildbots
(https://crbug.com/779790), crashes in the
MinidumpThreadWriter.OneThread_AMD64_Stack test were observed in 32-bit
x86 Windows builds produced by Clang in the release configuration. These
crashes occurred in crashpad::test::InitializeMinidumpContextAMD64,
which heap-allocates a MinidumpContextAMD64Writer object. These objects
have an alignment requirement of 16, based on the alignment requirement
of their MinidumpContextAMD64 member.

Although this problem was never observed with MSVC, Clang was making use
of the known strict alignment and producing code that depended on it.
This code crashed if the requirement was not met. MSVC had raised a
warning about this usage (C4316), but the warning was disabled as it did
not appear to have any ill effect on code produced by that compiler.

The problem surfaced in test code, but heap-allocated
MinidumpContextAMD64Writer objects are created in non-test code as well.
The impact is limited, because a 32-bit Windows Crashpad handler would
not have a need to allocate one of these objects.

As a fix, MinidumpContextAMD64Writer is given a custom allocation
function (a static “operator new()” member and matching “operator
delete()”) that returns properly aligned memory.

Change-Id: I0cb924da91716eb01b88ec2ae952a69262cc2de6
Reviewed-on: https://chromium-review.googlesource.com/746539
Reviewed-by: Leonard Mosescu <mosescu@chromium.org>
2017-11-01 16:40:58 +00:00

167 lines
5.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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_context_writer.h"
#include <stdint.h>
#include "gtest/gtest.h"
#include "minidump/minidump_context.h"
#include "minidump/test/minidump_context_test_util.h"
#include "minidump/test/minidump_writable_test_util.h"
#include "snapshot/cpu_context.h"
#include "snapshot/test/test_cpu_context.h"
#include "util/file/string_file.h"
namespace crashpad {
namespace test {
namespace {
TEST(MinidumpContextWriter, MinidumpContextX86Writer) {
StringFile string_file;
{
// Make sure that a context writer thats untouched writes a zeroed-out
// context.
SCOPED_TRACE("zero");
MinidumpContextX86Writer context_writer;
EXPECT_TRUE(context_writer.WriteEverything(&string_file));
ASSERT_EQ(string_file.string().size(), sizeof(MinidumpContextX86));
const MinidumpContextX86* observed =
MinidumpWritableAtRVA<MinidumpContextX86>(string_file.string(), 0);
ASSERT_TRUE(observed);
ExpectMinidumpContextX86(0, observed, false);
}
{
SCOPED_TRACE("nonzero");
string_file.Reset();
constexpr uint32_t kSeed = 0x8086;
MinidumpContextX86Writer context_writer;
InitializeMinidumpContextX86(context_writer.context(), kSeed);
EXPECT_TRUE(context_writer.WriteEverything(&string_file));
ASSERT_EQ(string_file.string().size(), sizeof(MinidumpContextX86));
const MinidumpContextX86* observed =
MinidumpWritableAtRVA<MinidumpContextX86>(string_file.string(), 0);
ASSERT_TRUE(observed);
ExpectMinidumpContextX86(kSeed, observed, false);
}
}
TEST(MinidumpContextWriter, MinidumpContextAMD64Writer) {
{
// Make sure that a heap-allocated context writer has the proper alignment,
// because it may be nonstandard.
auto context_writer = std::make_unique<MinidumpContextAMD64Writer>();
EXPECT_EQ(reinterpret_cast<uintptr_t>(context_writer.get()) &
(alignof(MinidumpContextAMD64Writer) - 1),
0u);
}
StringFile string_file;
{
// Make sure that a context writer thats untouched writes a zeroed-out
// context.
SCOPED_TRACE("zero");
MinidumpContextAMD64Writer context_writer;
EXPECT_TRUE(context_writer.WriteEverything(&string_file));
ASSERT_EQ(string_file.string().size(), sizeof(MinidumpContextAMD64));
const MinidumpContextAMD64* observed =
MinidumpWritableAtRVA<MinidumpContextAMD64>(string_file.string(), 0);
ASSERT_TRUE(observed);
ExpectMinidumpContextAMD64(0, observed, false);
}
{
SCOPED_TRACE("nonzero");
string_file.Reset();
constexpr uint32_t kSeed = 0x808664;
MinidumpContextAMD64Writer context_writer;
InitializeMinidumpContextAMD64(context_writer.context(), kSeed);
EXPECT_TRUE(context_writer.WriteEverything(&string_file));
ASSERT_EQ(string_file.string().size(), sizeof(MinidumpContextAMD64));
const MinidumpContextAMD64* observed =
MinidumpWritableAtRVA<MinidumpContextAMD64>(string_file.string(), 0);
ASSERT_TRUE(observed);
ExpectMinidumpContextAMD64(kSeed, observed, false);
}
}
TEST(MinidumpContextWriter, CreateFromSnapshot_X86) {
constexpr uint32_t kSeed = 32;
CPUContextX86 context_snapshot_x86;
CPUContext context_snapshot;
context_snapshot.x86 = &context_snapshot_x86;
InitializeCPUContextX86(&context_snapshot, kSeed);
std::unique_ptr<MinidumpContextWriter> context_writer =
MinidumpContextWriter::CreateFromSnapshot(&context_snapshot);
ASSERT_TRUE(context_writer);
StringFile string_file;
ASSERT_TRUE(context_writer->WriteEverything(&string_file));
const MinidumpContextX86* observed =
MinidumpWritableAtRVA<MinidumpContextX86>(string_file.string(), 0);
ASSERT_TRUE(observed);
ExpectMinidumpContextX86(kSeed, observed, true);
}
TEST(MinidumpContextWriter, CreateFromSnapshot_AMD64) {
constexpr uint32_t kSeed = 64;
CPUContextX86_64 context_snapshot_x86_64;
CPUContext context_snapshot;
context_snapshot.x86_64 = &context_snapshot_x86_64;
InitializeCPUContextX86_64(&context_snapshot, kSeed);
std::unique_ptr<MinidumpContextWriter> context_writer =
MinidumpContextWriter::CreateFromSnapshot(&context_snapshot);
ASSERT_TRUE(context_writer);
StringFile string_file;
ASSERT_TRUE(context_writer->WriteEverything(&string_file));
const MinidumpContextAMD64* observed =
MinidumpWritableAtRVA<MinidumpContextAMD64>(string_file.string(), 0);
ASSERT_TRUE(observed);
ExpectMinidumpContextAMD64(kSeed, observed, true);
}
} // namespace
} // namespace test
} // namespace crashpad