crashpad/util/process/process_memory_range.cc
Vlad Tsyrklevich 8f5d83b9e3 Change ProcessMemoryRange to use VMSize
Follow up to https://crrev.com/c/1387756 replace size_t with VMSize.

Bug: crashpad:270
Change-Id: I22ac9e3503ef3e9707b2ad0758ae133c5a746f27
Reviewed-on: https://chromium-review.googlesource.com/c/1389235
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Vlad Tsyrklevich <vtsyrklevich@chromium.org>
2019-01-03 19:00:23 +00:00

94 lines
3.1 KiB
C++

// Copyright 2017 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 "util/process/process_memory_range.h"
#include <algorithm>
#include <limits>
#include "base/logging.h"
namespace crashpad {
ProcessMemoryRange::ProcessMemoryRange()
: memory_(nullptr), range_(), initialized_() {}
ProcessMemoryRange::~ProcessMemoryRange() {}
bool ProcessMemoryRange::Initialize(const ProcessMemory* memory,
bool is_64_bit,
VMAddress base,
VMSize size) {
INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
memory_ = memory;
range_.SetRange(is_64_bit, base, size);
if (!range_.IsValid()) {
LOG(ERROR) << "invalid range";
return false;
}
INITIALIZATION_STATE_SET_VALID(initialized_);
return true;
}
bool ProcessMemoryRange::Initialize(const ProcessMemory* memory,
bool is_64_bit) {
VMSize max = is_64_bit ? std::numeric_limits<uint64_t>::max()
: std::numeric_limits<uint32_t>::max();
return Initialize(memory, is_64_bit, 0, max);
}
bool ProcessMemoryRange::Initialize(const ProcessMemoryRange& other) {
return Initialize(other.memory_,
other.range_.Is64Bit(),
other.range_.Base(),
other.range_.Size());
}
bool ProcessMemoryRange::RestrictRange(VMAddress base, VMSize size) {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
CheckedVMAddressRange new_range(range_.Is64Bit(), base, size);
if (!new_range.IsValid() || !range_.ContainsRange(new_range)) {
LOG(ERROR) << "invalid range";
return false;
}
range_ = new_range;
return true;
}
bool ProcessMemoryRange::Read(VMAddress address,
VMSize size,
void* buffer) const {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
CheckedVMAddressRange read_range(range_.Is64Bit(), address, size);
if (!read_range.IsValid() || !range_.ContainsRange(read_range)) {
LOG(ERROR) << "read out of range";
return false;
}
return memory_->Read(address, size, buffer);
}
bool ProcessMemoryRange::ReadCStringSizeLimited(VMAddress address,
VMSize size,
std::string* string) const {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
if (!range_.ContainsValue(address)) {
LOG(ERROR) << "read out of range";
return false;
}
size = std::min(size, range_.End() - address);
return memory_->ReadCStringSizeLimited(address, size, string);
}
} // namespace crashpad