1 // Copyright 2016 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "snapshot/win/capture_memory_delegate_win.h"
16 
17 #include <utility>
18 
19 #include "base/numerics/safe_conversions.h"
20 #include "snapshot/memory_snapshot_generic.h"
21 
22 namespace crashpad {
23 namespace internal {
24 
CaptureMemoryDelegateWin(ProcessReaderWin * process_reader,const ProcessReaderWin::Thread & thread,std::vector<std::unique_ptr<MemorySnapshotGeneric>> * snapshots,uint32_t * budget_remaining)25 CaptureMemoryDelegateWin::CaptureMemoryDelegateWin(
26     ProcessReaderWin* process_reader,
27     const ProcessReaderWin::Thread& thread,
28     std::vector<std::unique_ptr<MemorySnapshotGeneric>>* snapshots,
29     uint32_t* budget_remaining)
30     : stack_(thread.stack_region_address, thread.stack_region_size),
31       process_reader_(process_reader),
32       snapshots_(snapshots),
33       budget_remaining_(budget_remaining) {}
34 
Is64Bit() const35 bool CaptureMemoryDelegateWin::Is64Bit() const {
36   return process_reader_->Is64Bit();
37 }
38 
ReadMemory(uint64_t at,uint64_t num_bytes,void * into) const39 bool CaptureMemoryDelegateWin::ReadMemory(uint64_t at,
40                                           uint64_t num_bytes,
41                                           void* into) const {
42   return process_reader_->Memory()->Read(
43       at, base::checked_cast<size_t>(num_bytes), into);
44 }
45 
GetReadableRanges(const CheckedRange<uint64_t,uint64_t> & range) const46 std::vector<CheckedRange<uint64_t>> CaptureMemoryDelegateWin::GetReadableRanges(
47     const CheckedRange<uint64_t, uint64_t>& range) const {
48   return process_reader_->GetProcessInfo().GetReadableRanges(range);
49 }
50 
AddNewMemorySnapshot(const CheckedRange<uint64_t,uint64_t> & range)51 void CaptureMemoryDelegateWin::AddNewMemorySnapshot(
52     const CheckedRange<uint64_t, uint64_t>& range) {
53   // Don't bother storing this memory if it points back into the stack.
54   if (stack_.ContainsRange(range))
55     return;
56   if (range.size() == 0)
57     return;
58   if (budget_remaining_ && *budget_remaining_ == 0)
59     return;
60   snapshots_->push_back(std::make_unique<internal::MemorySnapshotGeneric>());
61   internal::MemorySnapshotGeneric* snapshot = snapshots_->back().get();
62   snapshot->Initialize(process_reader_->Memory(), range.base(), range.size());
63   if (budget_remaining_) {
64     if (!base::IsValueInRangeForNumericType<int64_t>(range.size())) {
65       *budget_remaining_ = 0;
66     } else {
67       int64_t temp = *budget_remaining_;
68       temp -= range.size();
69       *budget_remaining_ = base::saturated_cast<uint32_t>(temp);
70     }
71   }
72 }
73 
74 }  // namespace internal
75 }  // namespace crashpad
76