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_alloc_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 : HeapAllocEvent::HeapAllocEvent(HANDLE trace_heap,
24 : DWORD flags,
25 : SIZE_T bytes,
26 : LPVOID trace_alloc)
27 : : trace_heap_(trace_heap),
28 : flags_(flags),
29 : bytes_(bytes),
30 E : trace_alloc_(trace_alloc) {
31 E : }
32 :
33 : bool HeapAllocEvent::Save(const EventInterface* const event,
34 E : core::OutArchive* out_archive) {
35 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), event);
36 E : DCHECK_NE(static_cast<core::OutArchive*>(nullptr), out_archive);
37 :
38 : const HeapAllocEvent* derived_event =
39 E : reinterpret_cast<const HeapAllocEvent*>(event);
40 :
41 : return out_archive->Save(
42 : reinterpret_cast<uintptr_t>(derived_event->trace_heap_)) &&
43 : out_archive->Save(derived_event->flags_) &&
44 : out_archive->Save(derived_event->bytes_) &&
45 : out_archive->Save(
46 E : reinterpret_cast<uintptr_t>(derived_event->trace_alloc_));
47 E : }
48 :
49 : scoped_ptr<HeapAllocEvent> HeapAllocEvent::Load(
50 E : core::InArchive* in_archive) {
51 E : DCHECK_NE(static_cast<core::InArchive*>(nullptr), in_archive);
52 :
53 : uintptr_t trace_heap;
54 : DWORD flags;
55 : SIZE_T bytes;
56 : uintptr_t trace_alloc;
57 : if (in_archive->Load(&trace_heap) &&
58 : in_archive->Load(&flags) &&
59 : in_archive->Load(&bytes) &&
60 E : in_archive->Load(&trace_alloc)) {
61 : return scoped_ptr<HeapAllocEvent>(
62 : new HeapAllocEvent(reinterpret_cast<HANDLE>(trace_heap),
63 : flags,
64 : bytes,
65 E : reinterpret_cast<LPVOID>(trace_alloc)));
66 : }
67 i : return nullptr;
68 E : }
69 :
70 E : bool HeapAllocEvent::Play(void* backdrop) {
71 E : DCHECK_NE(static_cast<void*>(nullptr), backdrop);
72 :
73 : using bard::backdrops::HeapBackdrop;
74 E : HeapBackdrop* heap_backdrop = reinterpret_cast<HeapBackdrop*>(backdrop);
75 :
76 E : HANDLE live_heap = INVALID_HANDLE_VALUE;
77 :
78 E : if (!heap_backdrop->heap_map().GetLiveFromTrace(trace_heap_, &live_heap))
79 i : return false;
80 :
81 E : uint64_t t0 = ::trace::common::GetTsc();
82 E : LPVOID live_alloc = heap_backdrop->HeapAlloc(live_heap, flags_, bytes_);
83 E : uint64_t t1 = ::trace::common::GetTsc();
84 :
85 E : if (!live_alloc && trace_alloc_) {
86 E : LOG(ERROR) << "HeapAlloc failed to allocate memory.";
87 E : return false;
88 : }
89 :
90 E : if (live_alloc) {
91 E : if (!trace_alloc_) {
92 : // No need to keep this allocation if it failed in the trace file.
93 i : heap_backdrop->HeapFree(live_heap, flags_, live_alloc);
94 i : } else if (!heap_backdrop->alloc_map().AddMapping(trace_alloc_,
95 E : live_alloc)) {
96 i : return false;
97 : }
98 : }
99 :
100 E : heap_backdrop->UpdateStats(type(), t1 - t0);
101 :
102 E : return true;
103 E : }
104 :
105 E : bool HeapAllocEvent::Equals(const EventInterface* rhs) const {
106 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), rhs);
107 :
108 E : if (rhs->type() != kHeapAllocEvent)
109 i : return false;
110 :
111 E : const auto e = reinterpret_cast<const HeapAllocEvent*>(rhs);
112 : if (trace_heap_ != e->trace_heap_ || flags_ != e->flags_ ||
113 E : bytes_ != e->bytes_ || trace_alloc_ != e->trace_alloc_) {
114 E : return false;
115 : }
116 :
117 E : return true;
118 E : }
119 :
120 : } // namespace events
121 : } // namespace bard
|