mirror of
https://github.com/chromium/crashpad.git
synced 2024-12-31 01:43:03 +08:00
af594c8deb
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>
117 lines
3.7 KiB
C++
117 lines
3.7 KiB
C++
// Copyright 2015 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.
|
|
|
|
#ifndef CRASHPAD_UTIL_STDLIB_ALIGNED_ALLOCATOR_H_
|
|
#define CRASHPAD_UTIL_STDLIB_ALIGNED_ALLOCATOR_H_
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <limits>
|
|
#include <memory>
|
|
#include <new>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace crashpad {
|
|
|
|
//! \brief Allocates memory with the specified alignment constraint.
|
|
//!
|
|
//! This function wraps `posix_memalign()` or `_aligned_malloc()`. Memory
|
|
//! allocated by this function must be released by AlignFree().
|
|
void* AlignedAllocate(size_t alignment, size_t size);
|
|
|
|
//! \brief Frees memory allocated by AlignedAllocate().
|
|
//!
|
|
//! This function wraps `free()` or `_aligned_free()`.
|
|
void AlignedFree(void* pointer);
|
|
|
|
//! \brief A standard allocator that aligns its allocations as requested,
|
|
//! suitable for use as an allocator in standard containers.
|
|
//!
|
|
//! This is similar to `std::allocator<T>`, with the addition of an alignment
|
|
//! guarantee. \a Alignment must be a power of 2. If \a Alignment is not
|
|
//! specified, the default alignment for type \a T is used.
|
|
template <class T, size_t Alignment = alignof(T)>
|
|
struct AlignedAllocator {
|
|
public:
|
|
using value_type = T;
|
|
using pointer = T*;
|
|
using const_pointer = const T*;
|
|
using reference = T&;
|
|
using const_reference = const T&;
|
|
using size_type = size_t;
|
|
using difference_type = ptrdiff_t;
|
|
|
|
template <class U>
|
|
struct rebind {
|
|
using other = AlignedAllocator<U, Alignment>;
|
|
};
|
|
|
|
AlignedAllocator() noexcept {}
|
|
AlignedAllocator(const AlignedAllocator& other) noexcept {}
|
|
|
|
template <typename U>
|
|
AlignedAllocator(const AlignedAllocator<U, Alignment>& other) noexcept {}
|
|
|
|
~AlignedAllocator() {}
|
|
|
|
pointer address(reference x) const noexcept { return &x; }
|
|
const_pointer address(const_reference x) const noexcept { return &x; }
|
|
|
|
pointer allocate(size_type n, std::allocator<void>::const_pointer hint = 0) {
|
|
return reinterpret_cast<pointer>(
|
|
AlignedAllocate(Alignment, sizeof(value_type) * n));
|
|
}
|
|
|
|
void deallocate(pointer p, size_type n) { AlignedFree(p); }
|
|
|
|
size_type max_size() const noexcept {
|
|
return std::numeric_limits<size_type>::max() / sizeof(value_type);
|
|
}
|
|
|
|
template <class U, class... Args>
|
|
void construct(U* p, Args&&... args) {
|
|
new (reinterpret_cast<void*>(p)) U(std::forward<Args>(args)...);
|
|
}
|
|
|
|
template <class U>
|
|
void destroy(U* p) {
|
|
p->~U();
|
|
}
|
|
};
|
|
|
|
template <class T1, class T2, size_t Alignment>
|
|
bool operator==(const AlignedAllocator<T1, Alignment>& lhs,
|
|
const AlignedAllocator<T2, Alignment>& rhs) noexcept {
|
|
return true;
|
|
}
|
|
|
|
template <class T1, class T2, size_t Alignment>
|
|
bool operator!=(const AlignedAllocator<T1, Alignment>& lhs,
|
|
const AlignedAllocator<T2, Alignment>& rhs) noexcept {
|
|
return false;
|
|
}
|
|
|
|
//! \brief A `std::vector` using AlignedAllocator.
|
|
//!
|
|
//! This is similar to `std::vector<T>`, with the addition of an alignment
|
|
//! guarantee. \a Alignment must be a power of 2. If \a Alignment is not
|
|
//! specified, the default alignment for type \a T is used.
|
|
template <typename T, size_t Alignment = alignof(T)>
|
|
using AlignedVector = std::vector<T, AlignedAllocator<T, Alignment>>;
|
|
|
|
} // namespace crashpad
|
|
|
|
#endif // CRASHPAD_UTIL_STDLIB_ALIGNED_ALLOCATOR_H_
|