1 : // Copyright 2015 Google Inc. 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 "syzygy/bard/events/heap_realloc_event.h"
16 :
17 : #include "syzygy/bard/backdrops/heap_backdrop.h"
18 : #include "syzygy/bard/events/play_util.h"
19 :
20 : namespace bard {
21 : namespace events {
22 :
23 : HeapReAllocEvent::HeapReAllocEvent(uint32_t stack_trace_id,
24 : HANDLE trace_heap,
25 : DWORD flags,
26 : LPVOID trace_alloc,
27 : SIZE_T bytes,
28 : LPVOID trace_realloc)
29 E : : stack_trace_id_(stack_trace_id),
30 E : trace_heap_(trace_heap),
31 E : flags_(flags),
32 E : trace_alloc_(trace_alloc),
33 E : bytes_(bytes),
34 E : trace_realloc_(trace_realloc) {
35 E : }
36 :
37 : bool HeapReAllocEvent::Save(const EventInterface* const event,
38 E : core::OutArchive* out_archive) {
39 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), event);
40 E : DCHECK_NE(static_cast<core::OutArchive*>(nullptr), out_archive);
41 :
42 : const HeapReAllocEvent* derived_event =
43 E : reinterpret_cast<const HeapReAllocEvent*>(event);
44 :
45 E : return out_archive->Save(derived_event->stack_trace_id_) &&
46 : out_archive->Save(
47 : reinterpret_cast<uintptr_t>(derived_event->trace_heap_)) &&
48 : out_archive->Save(derived_event->flags_) &&
49 : out_archive->Save(
50 : reinterpret_cast<uintptr_t>(derived_event->trace_alloc_)) &&
51 : out_archive->Save(derived_event->bytes_) &&
52 : out_archive->Save(
53 : reinterpret_cast<uintptr_t>(derived_event->trace_realloc_));
54 E : }
55 :
56 : std::unique_ptr<HeapReAllocEvent> HeapReAllocEvent::Load(
57 E : core::InArchive* in_archive) {
58 E : DCHECK_NE(static_cast<core::InArchive*>(nullptr), in_archive);
59 :
60 E : uint32_t stack_trace_id = 0;
61 E : uintptr_t trace_heap = 0;
62 E : DWORD flags = 0;
63 E : uintptr_t trace_alloc = 0;
64 E : SIZE_T bytes = 0;
65 E : uintptr_t trace_realloc = 0;
66 : if (in_archive->Load(&stack_trace_id) && in_archive->Load(&trace_heap) &&
67 : in_archive->Load(&flags) && in_archive->Load(&trace_alloc) &&
68 E : in_archive->Load(&bytes) && in_archive->Load(&trace_realloc)) {
69 E : return std::unique_ptr<HeapReAllocEvent>(new HeapReAllocEvent(
70 : stack_trace_id, reinterpret_cast<HANDLE>(trace_heap), flags,
71 : reinterpret_cast<LPVOID>(trace_alloc), bytes,
72 : reinterpret_cast<LPVOID>(trace_realloc)));
73 : }
74 i : return nullptr;
75 E : }
76 :
77 E : bool HeapReAllocEvent::Play(void* backdrop) {
78 E : DCHECK_NE(static_cast<void*>(nullptr), backdrop);
79 :
80 : using bard::backdrops::HeapBackdrop;
81 E : HeapBackdrop* heap_backdrop = reinterpret_cast<HeapBackdrop*>(backdrop);
82 :
83 E : HANDLE live_heap = INVALID_HANDLE_VALUE;
84 E : LPVOID live_alloc = nullptr;
85 :
86 E : if (!heap_backdrop->heap_map().GetLiveFromTrace(trace_heap_, &live_heap) ||
87 : !heap_backdrop->alloc_map().GetLiveFromTrace(trace_alloc_, &live_alloc)) {
88 i : return false;
89 : }
90 :
91 E : uint64_t timing = 0;
92 E : LPVOID live_realloc = InvokeOnBackdrop(
93 : stack_trace_id_, &timing, heap_backdrop, &HeapBackdrop::HeapReAlloc,
94 : live_heap, flags_, live_alloc, bytes_);
95 :
96 E : if (!live_realloc && trace_realloc_) {
97 E : LOG(ERROR) << "HeapReAlloc failed to allocate memory.";
98 E : return false;
99 : }
100 :
101 E : if (live_realloc) {
102 E : if (!trace_realloc_) {
103 : // No need to keep the new allocation or remove the previous allocation.
104 i : heap_backdrop->HeapFree(live_heap, flags_, live_realloc);
105 i : } else if (!heap_backdrop->alloc_map().RemoveMapping(trace_alloc_,
106 E : live_alloc) ||
107 : !heap_backdrop->alloc_map().AddMapping(trace_realloc_,
108 : live_realloc)) {
109 i : return false;
110 : }
111 : }
112 :
113 E : heap_backdrop->UpdateStats(type(), timing);
114 :
115 E : return true;
116 E : }
117 :
118 E : bool HeapReAllocEvent::Equals(const EventInterface* rhs) const {
119 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), rhs);
120 :
121 E : if (rhs->type() != kHeapReAllocEvent)
122 i : return false;
123 :
124 E : const auto e = reinterpret_cast<const HeapReAllocEvent*>(rhs);
125 : if (stack_trace_id_ != e->stack_trace_id_ || trace_heap_ != e->trace_heap_ ||
126 : flags_ != e->flags_ || trace_alloc_ != e->trace_alloc_ ||
127 E : bytes_ != e->bytes_ || trace_realloc_ != e->trace_realloc_) {
128 E : return false;
129 : }
130 :
131 E : return true;
132 E : }
133 :
134 : } // namespace events
135 : } // namespace bard
|