Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(824)

Unified Diff: snapshot/win/pe_image_reader.cc

Issue 1052813002: win: make CrashpadInfo retrievable (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: . Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: snapshot/win/pe_image_reader.cc
diff --git a/snapshot/win/pe_image_reader.cc b/snapshot/win/pe_image_reader.cc
new file mode 100644
index 0000000000000000000000000000000000000000..879d8e87d7362511123eb7320ae813132116627c
--- /dev/null
+++ b/snapshot/win/pe_image_reader.cc
@@ -0,0 +1,159 @@
+// 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.
+
+#include "snapshot/win/pe_image_reader.h"
+
+#include <string.h>
+
+#include "base/logging.h"
+#include "base/strings/stringprintf.h"
+#include "client/crashpad_info.h"
+#include "snapshot/win/process_reader_win.h"
+#include "util/numeric/checked_address_range.h"
+
+namespace crashpad {
+
+PEImageReader::PEImageReader()
+ : process_reader_(nullptr), address_(0), module_name_(), initialized_() {
Mark Mentovai 2015/04/30 20:58:35 size, module_range
scottmg 2015/04/30 22:09:44 Done.
+}
+
+PEImageReader::~PEImageReader() {
+}
+
+bool PEImageReader::Initialize(ProcessReaderWin* process_reader,
+ uintptr_t address,
+ uintptr_t size,
+ const std::string& module_name) {
+ INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
+
+ process_reader_ = process_reader;
+ address_ = address;
+ size_ = size;
+ module_range_.SetRange(process_reader_->Is64Bit(), address_, size_);
Mark Mentovai 2015/04/30 20:58:35 Check the validity of module_range_ once you set i
scottmg 2015/04/30 22:09:44 Done.
+ module_name_ = module_name;
+
+ INITIALIZATION_STATE_SET_VALID(initialized_);
+ return true;
+}
+
+bool PEImageReader::GetCrashpadInfo(
+ process_types::CrashpadInfo* crashpad_info) const {
+ INITIALIZATION_STATE_DCHECK_VALID(initialized_);
+
+ IMAGE_SECTION_HEADER section;
+ if (!GetSectionByName("CPADinfo", &section))
+ return false;
+
+ if (section.Misc.VirtualSize < sizeof(process_types::CrashpadInfo)) {
+ LOG(WARNING) << "small crashpad info section size "
+ << section.Misc.VirtualSize << ", " << module_name_;
+ return false;
+ }
+
+ uintptr_t crashpad_info_address = address_ + section.VirtualAddress;
+ if (!module_range_.ContainsRange(
+ CheckedAddressRange(process_reader_->Is64Bit(),
Mark Mentovai 2015/04/30 20:58:35 I think that you need to check the new CheckedAddr
scottmg 2015/04/30 22:09:43 Done.
+ crashpad_info_address,
+ sizeof(process_types::CrashpadInfo)))) {
Mark Mentovai 2015/04/30 20:58:35 This should be section.Misc.VirtualSize, since you
scottmg 2015/04/30 22:09:43 Done.
+ LOG(WARNING) << "invalid address for crashpad info "
Mark Mentovai 2015/04/30 20:58:35 This message is a little misleading, because it ch
scottmg 2015/04/30 22:09:43 Done.
+ << crashpad_info_address;
+ return false;
+ }
+
+ // TODO(scottmg): process_types for cross-bitness.
+ if (!process_reader_->ReadMemory(crashpad_info_address,
+ sizeof(process_types::CrashpadInfo),
+ crashpad_info)) {
+ LOG(WARNING) << "could not read crashpad info " << module_name_;
+ return false;
+ }
+
+ if (crashpad_info->signature != CrashpadInfo::kSignature ||
+ crashpad_info->version < 1) {
+ LOG(WARNING) << "unexpected crashpad info data " << module_name_;
+ return false;
+ }
+
+ return true;
+}
+
+bool PEImageReader::GetSectionByName(const std::string& name,
+ IMAGE_SECTION_HEADER* section) const {
+ if (name.size() > sizeof(section->Name)) {
+ LOG(WARNING) << "supplied section name too long " << name;
+ return false;
+ }
+
+ IMAGE_DOS_HEADER dos_header;
+ if (!CheckedReadMemory(address_, sizeof(IMAGE_DOS_HEADER), &dos_header)) {
+ LOG(WARNING) << "could not read dos header of " << module_name_;
+ return false;
+ }
+
+ if (dos_header.e_magic != IMAGE_DOS_SIGNATURE) {
+ LOG(WARNING) << "invalid e_magic in dos header of " << module_name_;
+ return false;
+ }
+
+ // TODO(scottmg): This is reading a same-bitness sized structure.
+ IMAGE_NT_HEADERS nt_headers;
+ uintptr_t nt_headers_address = address_ + dos_header.e_lfanew;
+ if (!CheckedReadMemory(
+ nt_headers_address, sizeof(IMAGE_NT_HEADERS), &nt_headers)) {
+ LOG(WARNING) << "could not read nt headers of " << module_name_;
+ return false;
+ }
+
+ if (nt_headers.Signature != IMAGE_NT_SIGNATURE) {
+ LOG(WARNING) << "invalid signature in nt headers of " << module_name_;
+ return false;
+ }
+
+ uintptr_t first_section_address =
+ nt_headers_address + offsetof(IMAGE_NT_HEADERS, OptionalHeader) +
+ nt_headers.FileHeader.SizeOfOptionalHeader;
+ for (DWORD i = 0; i < nt_headers.FileHeader.NumberOfSections; ++i) {
+ uintptr_t section_address =
+ first_section_address + sizeof(IMAGE_SECTION_HEADER) * i;
+ if (!CheckedReadMemory(
+ section_address, sizeof(IMAGE_SECTION_HEADER), section)) {
+ LOG(WARNING) << "could not read section " << i << " of " << module_name_;
+ return false;
+ }
+ if (strncmp(reinterpret_cast<const char*>(section->Name),
+ name.c_str(),
+ sizeof(section->Name)) == 0) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool PEImageReader::CheckedReadMemory(uintptr_t address,
+ uintptr_t size,
+ void* into) const {
+ if (!module_range_.ContainsRange(
+ CheckedAddressRange(process_reader_->Is64Bit(), address, size))) {
+ LOG(WARNING) << base::StringPrintf(
+ "invalid read range 0x%llx + 0x%llx in %s",
+ address,
+ size,
+ module_name_.c_str());
+ return false;
+ }
+ return process_reader_->ReadMemory(address, size, into);
+}
+
+} // namespace crashpad

Powered by Google App Engine
This is Rietveld 408576698