Replace std::unique_ptr<T[]> with HeapArray in process_info.cc and

process_info_test.cc

Bug: crashpad: 326459035,326458915,326459055
Change-Id: Ifb91297b6097aa81a9d5c883b2c284e9fdd512a8
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5463361
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
This commit is contained in:
Arthur Wang 2024-05-01 15:17:40 -07:00 committed by Crashpad LUCI CQ
parent 7e0af1d4d4
commit 76badd4c20
3 changed files with 35 additions and 35 deletions

2
DEPS
View File

@ -47,7 +47,7 @@ deps = {
'9719c1e1e676814c456b55f5f070eabad6709d31', '9719c1e1e676814c456b55f5f070eabad6709d31',
'crashpad/third_party/mini_chromium/mini_chromium': 'crashpad/third_party/mini_chromium/mini_chromium':
Var('chromium_git') + '/chromium/mini_chromium@' + Var('chromium_git') + '/chromium/mini_chromium@' +
'dce72d97d1c2e9beb5e206c6a05a702269794ca3', '8b56c7718412ec7d12d05522f7af0cbb787cbb00',
'crashpad/third_party/libfuzzer/src': 'crashpad/third_party/libfuzzer/src':
Var('chromium_git') + '/chromium/llvm-project/compiler-rt/lib/fuzzer.git@' + Var('chromium_git') + '/chromium/llvm-project/compiler-rt/lib/fuzzer.git@' +
'fda403cf93ecb8792cb1d061564d89a6553ca020', 'fda403cf93ecb8792cb1d061564d89a6553ca020',

View File

@ -24,6 +24,7 @@
#include <type_traits> #include <type_traits>
#include "base/check_op.h" #include "base/check_op.h"
#include "base/containers/heap_array.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/free_deleter.h" #include "base/memory/free_deleter.h"
#include "base/process/memory.h" #include "base/process/memory.h"
@ -147,33 +148,34 @@ MEMORY_BASIC_INFORMATION64 MemoryBasicInformationToMemoryBasicInformation64(
// NtQueryObject with a retry for size mismatch as well as a minimum size to // NtQueryObject with a retry for size mismatch as well as a minimum size to
// retrieve (and expect). // retrieve (and expect).
std::unique_ptr<uint8_t[]> QueryObject( base::HeapArray<uint8_t> QueryObject(
HANDLE handle, HANDLE handle,
OBJECT_INFORMATION_CLASS object_information_class, OBJECT_INFORMATION_CLASS object_information_class,
ULONG minimum_size) { ULONG minimum_size) {
ULONG size = minimum_size;
ULONG return_length; ULONG return_length;
std::unique_ptr<uint8_t[]> buffer(new uint8_t[size]); auto buffer = base::HeapArray<uint8_t>::Uninit(minimum_size);
NTSTATUS status = crashpad::NtQueryObject( NTSTATUS status = crashpad::NtQueryObject(handle,
handle, object_information_class, buffer.get(), size, &return_length); object_information_class,
buffer.data(),
(ULONG)buffer.size(),
&return_length);
if (status == STATUS_INFO_LENGTH_MISMATCH) { if (status == STATUS_INFO_LENGTH_MISMATCH) {
DCHECK_GT(return_length, size); DCHECK_GT(return_length, buffer.size());
size = return_length;
// Free the old buffer before attempting to allocate a new one. buffer = base::HeapArray<uint8_t>::Uninit(return_length);
buffer.reset(); status = crashpad::NtQueryObject(handle,
object_information_class,
buffer.reset(new uint8_t[size]); buffer.data(),
status = crashpad::NtQueryObject( (ULONG)buffer.size(),
handle, object_information_class, buffer.get(), size, &return_length); &return_length);
} }
if (!NT_SUCCESS(status)) { if (!NT_SUCCESS(status)) {
NTSTATUS_LOG(ERROR, status) << "NtQueryObject"; NTSTATUS_LOG(ERROR, status) << "NtQueryObject";
return nullptr; return base::HeapArray<uint8_t>();
} }
DCHECK_LE(return_length, size); DCHECK_LE(return_length, buffer.size());
DCHECK_GE(return_length, minimum_size); DCHECK_GE(return_length, minimum_size);
return buffer; return buffer;
} }
@ -413,14 +415,14 @@ std::vector<ProcessInfo::Handle> ProcessInfo::BuildHandleVector(
// information, but include the information that we do have already. // information, but include the information that we do have already.
ScopedKernelHANDLE scoped_dup_handle(dup_handle); ScopedKernelHANDLE scoped_dup_handle(dup_handle);
std::unique_ptr<uint8_t[]> object_basic_information_buffer = auto object_basic_information_buffer =
QueryObject(dup_handle, QueryObject(dup_handle,
ObjectBasicInformation, ObjectBasicInformation,
sizeof(PUBLIC_OBJECT_BASIC_INFORMATION)); sizeof(PUBLIC_OBJECT_BASIC_INFORMATION));
if (object_basic_information_buffer) { if (!object_basic_information_buffer.empty()) {
PUBLIC_OBJECT_BASIC_INFORMATION* object_basic_information = PUBLIC_OBJECT_BASIC_INFORMATION* object_basic_information =
reinterpret_cast<PUBLIC_OBJECT_BASIC_INFORMATION*>( reinterpret_cast<PUBLIC_OBJECT_BASIC_INFORMATION*>(
object_basic_information_buffer.get()); object_basic_information_buffer.data());
// The Attributes and GrantedAccess sometimes differ slightly between // The Attributes and GrantedAccess sometimes differ slightly between
// the data retrieved in SYSTEM_HANDLE_INFORMATION_EX and // the data retrieved in SYSTEM_HANDLE_INFORMATION_EX and
// PUBLIC_OBJECT_TYPE_INFORMATION. We prefer the values in // PUBLIC_OBJECT_TYPE_INFORMATION. We prefer the values in
@ -439,14 +441,14 @@ std::vector<ProcessInfo::Handle> ProcessInfo::BuildHandleVector(
result_handle.handle_count = object_basic_information->HandleCount - 1; result_handle.handle_count = object_basic_information->HandleCount - 1;
} }
std::unique_ptr<uint8_t[]> object_type_information_buffer = auto object_type_information_buffer =
QueryObject(dup_handle, QueryObject(dup_handle,
ObjectTypeInformation, ObjectTypeInformation,
sizeof(PUBLIC_OBJECT_TYPE_INFORMATION)); sizeof(PUBLIC_OBJECT_TYPE_INFORMATION));
if (object_type_information_buffer) { if (!object_type_information_buffer.empty()) {
PUBLIC_OBJECT_TYPE_INFORMATION* object_type_information = PUBLIC_OBJECT_TYPE_INFORMATION* object_type_information =
reinterpret_cast<PUBLIC_OBJECT_TYPE_INFORMATION*>( reinterpret_cast<PUBLIC_OBJECT_TYPE_INFORMATION*>(
object_type_information_buffer.get()); object_type_information_buffer.data());
DCHECK_EQ(object_type_information->TypeName.Length % DCHECK_EQ(object_type_information->TypeName.Length %
sizeof(result_handle.type_name[0]), sizeof(result_handle.type_name[0]),

View File

@ -20,6 +20,7 @@
#include <memory> #include <memory>
#include "base/containers/heap_array.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
@ -507,26 +508,23 @@ TEST(ProcessInfo, ReadableRanges) {
// Also make sure what we think we can read corresponds with what we can // Also make sure what we think we can read corresponds with what we can
// actually read. // actually read.
std::unique_ptr<unsigned char[]> into(new unsigned char[kBlockSize * 6]); auto into = base::HeapArray<unsigned char>::Uninit(kBlockSize * 6);
SIZE_T bytes_read; SIZE_T bytes_read;
EXPECT_TRUE(ReadProcessMemory( EXPECT_TRUE(ReadProcessMemory(
current_process, readable1, into.get(), kBlockSize, &bytes_read)); current_process, readable1, into.data(), kBlockSize, &bytes_read));
EXPECT_EQ(bytes_read, kBlockSize); EXPECT_EQ(bytes_read, kBlockSize);
EXPECT_TRUE(ReadProcessMemory( EXPECT_TRUE(ReadProcessMemory(
current_process, readable2, into.get(), kBlockSize * 2, &bytes_read)); current_process, readable2, into.data(), kBlockSize * 2, &bytes_read));
EXPECT_EQ(bytes_read, kBlockSize * 2); EXPECT_EQ(bytes_read, kBlockSize * 2);
EXPECT_FALSE(ReadProcessMemory( EXPECT_FALSE(ReadProcessMemory(
current_process, no_access, into.get(), kBlockSize, &bytes_read)); current_process, no_access, into.data(), kBlockSize, &bytes_read));
EXPECT_FALSE(ReadProcessMemory( EXPECT_FALSE(ReadProcessMemory(
current_process, reserve_region, into.get(), kBlockSize, &bytes_read)); current_process, reserve_region, into.data(), kBlockSize, &bytes_read));
EXPECT_FALSE(ReadProcessMemory(current_process, EXPECT_FALSE(ReadProcessMemory(
reserve_region, current_process, reserve_region, into.data(), into.size(), &bytes_read));
into.get(),
kBlockSize * 6,
&bytes_read));
} }
TEST(ProcessInfo, Handles) { TEST(ProcessInfo, Handles) {
@ -633,15 +631,15 @@ TEST(ProcessInfo, Handles) {
} }
TEST(ProcessInfo, OutOfRangeCheck) { TEST(ProcessInfo, OutOfRangeCheck) {
constexpr size_t kAllocationSize = 12345; auto safe_memory = base::HeapArray<char>::Uninit(12345);
std::unique_ptr<char[]> safe_memory(new char[kAllocationSize]);
ProcessInfo info; ProcessInfo info;
info.Initialize(GetCurrentProcess()); info.Initialize(GetCurrentProcess());
EXPECT_TRUE( EXPECT_TRUE(
info.LoggingRangeIsFullyReadable(CheckedRange<WinVMAddress, WinVMSize>( info.LoggingRangeIsFullyReadable(CheckedRange<WinVMAddress, WinVMSize>(
FromPointerCast<WinVMAddress>(safe_memory.get()), kAllocationSize))); FromPointerCast<WinVMAddress>(safe_memory.data()),
safe_memory.size())));
EXPECT_FALSE(info.LoggingRangeIsFullyReadable( EXPECT_FALSE(info.LoggingRangeIsFullyReadable(
CheckedRange<WinVMAddress, WinVMSize>(0, 1024))); CheckedRange<WinVMAddress, WinVMSize>(0, 1024)));
} }