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/trace/common/clock.h"
19 :
20 : namespace bard {
21 : namespace events {
22 :
23 : HeapReAllocEvent::HeapReAllocEvent(HANDLE trace_heap,
24 : DWORD flags,
25 : LPVOID trace_alloc,
26 : SIZE_T bytes,
27 : LPVOID trace_realloc)
28 : : trace_heap_(trace_heap),
29 : flags_(flags),
30 : trace_alloc_(trace_alloc),
31 : bytes_(bytes),
32 E : trace_realloc_(trace_realloc) {
33 E : }
34 :
35 : bool HeapReAllocEvent::Save(const EventInterface* const event,
36 E : core::OutArchive* out_archive) {
37 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), event);
38 E : DCHECK_NE(static_cast<core::OutArchive*>(nullptr), out_archive);
39 :
40 : const HeapReAllocEvent* derived_event =
41 E : reinterpret_cast<const HeapReAllocEvent*>(event);
42 :
43 : return out_archive->Save(
44 : reinterpret_cast<uintptr_t>(derived_event->trace_heap_)) &&
45 : out_archive->Save(derived_event->flags_) &&
46 : out_archive->Save(
47 : reinterpret_cast<uintptr_t>(derived_event->trace_alloc_)) &&
48 : out_archive->Save(derived_event->bytes_) &&
49 : out_archive->Save(
50 E : reinterpret_cast<uintptr_t>(derived_event->trace_realloc_));
51 E : }
52 :
53 : scoped_ptr<HeapReAllocEvent> HeapReAllocEvent::Load(
54 E : core::InArchive* in_archive) {
55 E : DCHECK_NE(static_cast<core::InArchive*>(nullptr), in_archive);
56 :
57 : uintptr_t trace_heap;
58 : DWORD flags;
59 : uintptr_t trace_alloc;
60 : SIZE_T bytes;
61 : uintptr_t trace_realloc;
62 : if (in_archive->Load(&trace_heap) &&
63 : in_archive->Load(&flags) &&
64 : in_archive->Load(&trace_alloc) &&
65 : in_archive->Load(&bytes) &&
66 E : in_archive->Load(&trace_realloc)) {
67 : return scoped_ptr<HeapReAllocEvent>(
68 : new HeapReAllocEvent(reinterpret_cast<HANDLE>(trace_heap),
69 : flags,
70 : reinterpret_cast<LPVOID>(trace_alloc),
71 : bytes,
72 E : 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 : if (!heap_backdrop->heap_map().GetLiveFromTrace(trace_heap_, &live_heap) ||
87 E : !heap_backdrop->alloc_map().GetLiveFromTrace(trace_alloc_, &live_alloc)) {
88 i : return false;
89 : }
90 :
91 E : uint64_t t0 = ::trace::common::GetTsc();
92 : LPVOID live_realloc =
93 E : heap_backdrop->HeapReAlloc(live_heap, flags_, live_alloc, bytes_);
94 E : uint64_t t1 = ::trace::common::GetTsc();
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 : live_alloc) ||
107 : !heap_backdrop->alloc_map().AddMapping(trace_realloc_,
108 E : live_realloc)) {
109 i : return false;
110 : }
111 : }
112 :
113 E : heap_backdrop->UpdateStats(type(), t1 - t0);
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 (trace_heap_ != e->trace_heap_ || flags_ != e->flags_ ||
126 : trace_alloc_ != e->trace_alloc_ || bytes_ != e->bytes_ ||
127 E : trace_realloc_ != e->trace_realloc_) {
128 E : return false;
129 : }
130 :
131 E : return true;
132 E : }
133 :
134 : } // namespace events
135 : } // namespace bard
|