Chromium Code Reviews| 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..1bfec3e7293e2312ee7840ea6d72b9ddce4b2dfc |
| --- /dev/null |
| +++ b/snapshot/win/pe_image_reader.cc |
| @@ -0,0 +1,186 @@ |
| +// 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" |
| + |
| +namespace crashpad { |
| + |
| +namespace { |
| + |
| +std::string RangeToString(const CheckedWinAddressRange& range) { |
| + return base::StringPrintf("[0x%llx + 0x%llx (%s)]", |
| + range.Base(), |
| + range.Size(), |
| + range.Is64Bit() ? "64" : "32"); |
| +} |
| + |
| +} // namespace |
| + |
| +PEImageReader::PEImageReader() |
| + : process_reader_(nullptr), |
| + address_(0), |
| + size_(0), |
| + module_range_(), |
| + module_name_(), |
| + initialized_() { |
| +} |
| + |
| +PEImageReader::~PEImageReader() { |
| +} |
| + |
| +bool PEImageReader::Initialize(ProcessReaderWin* process_reader, |
| + win_vm_address_t address, |
| + win_vm_size_t size, |
| + const std::string& module_name) { |
| + INITIALIZATION_STATE_SET_INITIALIZING(initialized_); |
| + |
| + process_reader_ = process_reader; |
| + address_ = address; |
|
Mark Mentovai
2015/05/01 19:05:31
Do you need distinct fields for address_ and size_
scottmg
2015/05/01 19:46:31
Done.
|
| + size_ = size; |
| + module_range_.SetRange(process_reader_->Is64Bit(), address_, size_); |
| + if (!module_range_.IsValid()) { |
| + LOG(WARNING) << "invalid module range for " << module_name << ": " |
| + << RangeToString(module_range_); |
| + return false; |
| + } |
| + 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", §ion)) |
| + return false; |
| + |
| + if (section.Misc.VirtualSize < sizeof(process_types::CrashpadInfo)) { |
| + LOG(WARNING) << "small crashpad info section size " |
| + << section.Misc.VirtualSize << ", " << module_name_; |
| + return false; |
| + } |
| + |
| + win_vm_address_t crashpad_info_address = address_ + section.VirtualAddress; |
| + CheckedWinAddressRange crashpad_info_range(process_reader_->Is64Bit(), |
| + crashpad_info_address, |
| + section.Misc.VirtualSize); |
| + if (!crashpad_info_range.IsValid()) { |
| + LOG(WARNING) << "invalid range for crashpad info: " |
| + << RangeToString(crashpad_info_range); |
| + return false; |
| + } |
| + |
| + if (!module_range_.ContainsRange(crashpad_info_range)) { |
| + LOG(WARNING) << "crashpad info does not fall inside module " |
| + << module_name_; |
| + 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; |
| + win_vm_address_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; |
| + } |
| + |
| + win_vm_address_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) { |
| + win_vm_address_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(win_vm_address_t address, |
| + win_vm_size_t size, |
| + void* into) const { |
| + CheckedWinAddressRange read_range(process_reader_->Is64Bit(), address, size); |
| + if (!read_range.IsValid()) { |
| + LOG(WARNING) << "invalid read range: " << RangeToString(read_range); |
| + return false; |
| + } |
| + if (!module_range_.ContainsRange(read_range)) { |
| + LOG(WARNING) << "attempt to read outside of module " << module_name_ |
| + << " at range: " << RangeToString(read_range); |
| + return false; |
| + } |
| + return process_reader_->ReadMemory(address, size, into); |
| +} |
| + |
| +} // namespace crashpad |