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

Unified Diff: util/win/process_info.cc

Issue 1372183002: win: Add memory map range intersection helper (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: . Created 5 years, 3 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: util/win/process_info.cc
diff --git a/util/win/process_info.cc b/util/win/process_info.cc
index ed5262c7f7cc9d7f2d2958d3c7c74a1b69a7b9a6..a5e17acdc63b29968cb236ffebee9f62b385399b 100644
--- a/util/win/process_info.cc
+++ b/util/win/process_info.cc
@@ -16,6 +16,8 @@
#include <winternl.h>
+#include <algorithm>
+
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
@@ -292,6 +294,16 @@ ProcessInfo::Module::Module() : name(), dll_base(0), size(0), timestamp() {
ProcessInfo::Module::~Module() {
}
+ProcessInfo::MemoryInfo::MemoryInfo()
+ : base_address(0),
+ region_size(0),
+ allocation_base(0),
+ state(0),
+ allocation_protect(0),
+ protect(0),
+ type(0) {
+}
+
ProcessInfo::MemoryInfo::MemoryInfo(const MEMORY_BASIC_INFORMATION& mbi)
: base_address(reinterpret_cast<WinVMAddress>(mbi.BaseAddress)),
region_size(mbi.RegionSize),
@@ -415,7 +427,74 @@ bool ProcessInfo::Modules(std::vector<Module>* modules) const {
const std::vector<ProcessInfo::MemoryInfo>& ProcessInfo::MemoryInformation()
const {
+ INITIALIZATION_STATE_DCHECK_VALID(initialized_);
return memory_info_;
}
+std::vector<CheckedRange<WinVMAddress, WinVMSize>>
+GetAccessibleRangesInMemoryMap(
+ WinVMAddress address,
+ WinVMSize size,
+ const std::vector<ProcessInfo::MemoryInfo>& memory_info) {
+ std::vector<ProcessInfo::MemoryInfo> overlapping;
+
+ using Range = CheckedRange<WinVMAddress, WinVMSize>;
+
+ // Find all the ranges that overlap the target range, maintaining their order.
+ for (const auto& mi : memory_info) {
+ if (mi.base_address + mi.region_size <= address)
Mark Mentovai 2015/09/29 21:35:50 Optional, but since you’re using CheckedRange in h
scottmg 2015/09/30 17:40:26 Done. (Sadly std::copy_if is more unwieldy than a
+ continue;
+ if (mi.base_address >= address + size)
+ continue;
+ overlapping.push_back(mi);
+ }
+
+ if (overlapping.empty())
+ return std::vector<Range>();
+
+ // For the first and last, trim to the boundary of the incoming range.
+ ProcessInfo::MemoryInfo& front = overlapping.front();
+ if (front.base_address < address) {
Mark Mentovai 2015/09/29 21:35:50 Everything should be valid without this if, and wi
scottmg 2015/09/30 17:40:26 Done.
+ WinVMAddress original_base = front.base_address;
+ front.base_address = address;
+ front.region_size = (original_base + front.region_size) - address;
+ }
+
+ ProcessInfo::MemoryInfo& back = overlapping.back();
+ if (back.base_address + back.region_size >= address + size) {
+ back.region_size = (address + size) - back.base_address;
+ }
+
+ // Discard all non-accessible.
+ overlapping.erase(std::remove_if(overlapping.begin(),
Mark Mentovai 2015/09/29 21:35:50 I like seeing the standard library being used so n
+ overlapping.end(),
+ [](const ProcessInfo::MemoryInfo& mi) {
+ return mi.state == MEM_FREE;
+ }),
+ overlapping.end());
+
+ if (overlapping.empty())
+ return std::vector<Range>();
+
+ // Convert to return type.
+ std::vector<Range> as_ranges;
+ for (const auto& mi : overlapping) {
+ as_ranges.push_back(Range(mi.base_address, mi.region_size));
Mark Mentovai 2015/09/29 21:35:50 You’re not actually using any of the checking feat
scottmg 2015/09/30 17:40:25 Yeah, it was just a handy representation of a rang
+ }
+
+ // Coalesce remaining regions.
+ std::vector<Range> result;
+ result.push_back(as_ranges[0]);
+ for (size_t i = 1; i < as_ranges.size(); ++i) {
+ if (result.back().end() == as_ranges[i].base()) {
+ result.back().SetRange(result.back().base(),
+ result.back().size() + as_ranges[i].size());
+ } else {
+ result.push_back(as_ranges[i]);
+ }
+ }
+
+ return result;
+}
+
} // namespace crashpad

Powered by Google App Engine
This is Rietveld 408576698