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_size_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 : HeapSizeEvent::HeapSizeEvent(uint32_t stack_trace_id,
24 : HANDLE trace_heap,
25 : DWORD flags,
26 : LPCVOID trace_alloc,
27 : SIZE_T trace_size)
28 E : : stack_trace_id_(stack_trace_id),
29 E : trace_heap_(trace_heap),
30 E : flags_(flags),
31 E : trace_alloc_(trace_alloc),
32 E : trace_size_(trace_size) {
33 E : }
34 :
35 : bool HeapSizeEvent::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 HeapSizeEvent* derived_event =
41 E : reinterpret_cast<const HeapSizeEvent*>(event);
42 :
43 E : return out_archive->Save(derived_event->stack_trace_id_) &&
44 : out_archive->Save(
45 : reinterpret_cast<uintptr_t>(derived_event->trace_heap_)) &&
46 : out_archive->Save(derived_event->flags_) &&
47 : out_archive->Save(
48 : reinterpret_cast<uintptr_t>(derived_event->trace_alloc_)) &&
49 : out_archive->Save(derived_event->trace_size_);
50 E : }
51 :
52 : std::unique_ptr<HeapSizeEvent> HeapSizeEvent::Load(
53 E : core::InArchive* in_archive) {
54 E : DCHECK_NE(static_cast<core::InArchive*>(nullptr), in_archive);
55 :
56 E : uint32_t stack_trace_id = 0;
57 E : uintptr_t trace_heap = 0;
58 E : DWORD flags = 0;
59 E : uintptr_t trace_alloc = 0;
60 E : SIZE_T trace_size = 0;
61 : if (in_archive->Load(&stack_trace_id) && in_archive->Load(&trace_heap) &&
62 E : in_archive->Load(&flags) && in_archive->Load(&trace_alloc) &&
63 : in_archive->Load(&trace_size)) {
64 E : return std::unique_ptr<HeapSizeEvent>(new HeapSizeEvent(
65 : stack_trace_id, reinterpret_cast<HANDLE>(trace_heap), flags,
66 : reinterpret_cast<LPVOID>(trace_alloc), trace_size));
67 : }
68 i : return nullptr;
69 E : }
70 :
71 E : bool HeapSizeEvent::Play(void* backdrop) {
72 E : DCHECK_NE(static_cast<void*>(nullptr), backdrop);
73 :
74 : using bard::backdrops::HeapBackdrop;
75 E : HeapBackdrop* heap_backdrop = reinterpret_cast<HeapBackdrop*>(backdrop);
76 :
77 E : HANDLE live_heap = INVALID_HANDLE_VALUE;
78 E : LPCVOID live_alloc = nullptr;
79 :
80 E : if (!heap_backdrop->heap_map().GetLiveFromTrace(trace_heap_, &live_heap) ||
81 : !heap_backdrop->alloc_map().GetLiveFromTrace(
82 : const_cast<LPVOID>(trace_alloc_), const_cast<LPVOID*>(&live_alloc))) {
83 i : return false;
84 : }
85 :
86 E : uint64_t timing = 0;
87 : SIZE_T live_size =
88 E : InvokeOnBackdrop(stack_trace_id_, &timing, heap_backdrop,
89 : &HeapBackdrop::HeapSize, live_heap, flags_, live_alloc);
90 :
91 E : if (live_size != trace_size_) {
92 E : LOG(ERROR) << "HeapSize returned an unexpected allocation size.";
93 E : return false;
94 : }
95 :
96 E : heap_backdrop->UpdateStats(type(), timing);
97 :
98 E : return true;
99 E : }
100 :
101 E : bool HeapSizeEvent::Equals(const EventInterface* rhs) const {
102 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), rhs);
103 :
104 E : if (rhs->type() != kHeapSizeEvent)
105 i : return false;
106 :
107 E : const auto e = reinterpret_cast<const HeapSizeEvent*>(rhs);
108 : if (stack_trace_id_ != e->stack_trace_id_ || trace_heap_ != e->trace_heap_ ||
109 E : flags_ != e->flags_ || trace_alloc_ != e->trace_alloc_ ||
110 : trace_size_ != e->trace_size_) {
111 E : return false;
112 : }
113 :
114 E : return true;
115 E : }
116 :
117 : } // namespace events
118 : } // namespace bard
|