Use best-effort allocation in ProcessInfo::BuildHandleVector.

BUG=crashpad:158

Change-Id: If8666140a7fc5315eeb791d0998226de89a22cc3
Reviewed-on: https://chromium-review.googlesource.com/438791
Reviewed-by: Mark Mentovai <mark@chromium.org>
Reviewed-by: Scott Graham <scottmg@chromium.org>
This commit is contained in:
Sigurdur Asgeirsson 2017-02-08 10:38:09 -05:00 committed by Sigurður Ásgeirsson
parent 88442dd578
commit 6af23a933a
3 changed files with 23 additions and 5 deletions

2
DEPS
View File

@ -38,7 +38,7 @@ deps = {
'crashpad/third_party/mini_chromium/mini_chromium':
Var('chromium_git') + '/chromium/mini_chromium@' +
'4f3cfc8e7c2b7d77f94f41a32c3ec84a6920f05d',
'f65519e442d23498937251e680a3b113927613b0',
}
hooks = [

View File

@ -19,9 +19,12 @@
#include <algorithm>
#include <limits>
#include <memory>
#include <new>
#include <type_traits>
#include "base/logging.h"
#include "base/memory/free_deleter.h"
#include "base/process/memory.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "util/numeric/safe_assignment.h"
@ -36,6 +39,16 @@ namespace crashpad {
namespace {
using UniqueMallocPtr = std::unique_ptr<uint8_t[], base::FreeDeleter>;
UniqueMallocPtr UncheckedAllocate(size_t size) {
void* raw_ptr = nullptr;
if (!base::UncheckedMalloc(size, &raw_ptr))
return UniqueMallocPtr();
return UniqueMallocPtr(new (raw_ptr) uint8_t[size]);
}
NTSTATUS NtQueryInformationProcess(HANDLE process_handle,
PROCESSINFOCLASS process_information_class,
PVOID process_information,
@ -347,14 +360,20 @@ bool ReadMemoryInfo(HANDLE process, bool is_64_bit, ProcessInfo* process_info) {
std::vector<ProcessInfo::Handle> ProcessInfo::BuildHandleVector(
HANDLE process) const {
ULONG buffer_size = 2 * 1024 * 1024;
std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
// Typically if the buffer were too small, STATUS_INFO_LENGTH_MISMATCH would
// return the correct size in the final argument, but it does not for
// SystemExtendedHandleInformation, so we loop and attempt larger sizes.
NTSTATUS status;
ULONG returned_length;
UniqueMallocPtr buffer;
for (int tries = 0; tries < 5; ++tries) {
buffer.reset();
buffer = UncheckedAllocate(buffer_size);
if (!buffer) {
LOG(ERROR) << "UncheckedAllocate";
return std::vector<Handle>();
}
status = crashpad::NtQuerySystemInformation(
static_cast<SYSTEM_INFORMATION_CLASS>(SystemExtendedHandleInformation),
buffer.get(),
@ -364,8 +383,6 @@ std::vector<ProcessInfo::Handle> ProcessInfo::BuildHandleVector(
break;
buffer_size *= 2;
buffer.reset();
buffer.reset(new uint8_t[buffer_size]);
}
if (!NT_SUCCESS(status)) {

View File

@ -170,6 +170,7 @@ class ProcessInfo {
bool is_64_bit,
ProcessInfo* process_info);
// This function is best-effort under low memory conditions.
std::vector<Handle> BuildHandleVector(HANDLE process) const;
pid_t process_id_;