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_create_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 : HeapCreateEvent::HeapCreateEvent(uint32_t stack_trace_id,
24 : DWORD options,
25 : SIZE_T initial_size,
26 : SIZE_T maximum_size,
27 : HANDLE trace_heap)
28 E : : stack_trace_id_(stack_trace_id),
29 E : options_(options),
30 E : initial_size_(initial_size),
31 E : maximum_size_(maximum_size),
32 E : trace_heap_(trace_heap) {
33 E : }
34 :
35 : bool HeapCreateEvent::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 HeapCreateEvent* derived_event =
41 E : reinterpret_cast<const HeapCreateEvent*>(event);
42 :
43 E : return out_archive->Save(derived_event->stack_trace_id_) &&
44 : out_archive->Save(derived_event->options_) &&
45 : out_archive->Save(derived_event->initial_size_) &&
46 : out_archive->Save(derived_event->maximum_size_) &&
47 : out_archive->Save(
48 : reinterpret_cast<uintptr_t>(derived_event->trace_heap_));
49 E : }
50 :
51 : std::unique_ptr<HeapCreateEvent> HeapCreateEvent::Load(
52 E : core::InArchive* in_archive) {
53 E : DCHECK_NE(static_cast<core::InArchive*>(nullptr), in_archive);
54 :
55 E : uint32_t stack_trace_id = 0;
56 E : DWORD options = 0;
57 E : SIZE_T initial_size = 0;
58 E : SIZE_T maximum_size = 0;
59 : uintptr_t trace_heap;
60 : if (in_archive->Load(&stack_trace_id) && in_archive->Load(&options) &&
61 E : in_archive->Load(&initial_size) && in_archive->Load(&maximum_size) &&
62 : in_archive->Load(&trace_heap)) {
63 E : return std::unique_ptr<HeapCreateEvent>(
64 : new HeapCreateEvent(stack_trace_id, options, initial_size, maximum_size,
65 : reinterpret_cast<HANDLE>(trace_heap)));
66 : }
67 i : return nullptr;
68 E : }
69 :
70 E : bool HeapCreateEvent::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 : uint64_t timing = 0;
77 E : HANDLE live_heap = InvokeOnBackdrop(stack_trace_id_, &timing, heap_backdrop,
78 : &HeapBackdrop::HeapCreate, options_,
79 : initial_size_, maximum_size_);
80 :
81 E : if (!live_heap && trace_heap_) {
82 E : LOG(ERROR) << "HeapCreate failed to create a new heap.";
83 E : return false;
84 : }
85 :
86 E : if (live_heap) {
87 E : if (!trace_heap_) {
88 : // No need to keep this heap.
89 i : heap_backdrop->HeapDestroy(live_heap);
90 E : } else if (!heap_backdrop->heap_map().AddMapping(trace_heap_, live_heap)) {
91 i : return false;
92 : }
93 : }
94 :
95 E : heap_backdrop->UpdateStats(type(), timing);
96 :
97 E : return true;
98 E : }
99 :
100 E : bool HeapCreateEvent::Equals(const EventInterface* rhs) const {
101 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), rhs);
102 :
103 E : if (rhs->type() != kHeapCreateEvent)
104 i : return false;
105 :
106 E : const auto e = reinterpret_cast<const HeapCreateEvent*>(rhs);
107 : if (stack_trace_id_ != e->stack_trace_id_ || options_ != e->options_ ||
108 E : initial_size_ != e->initial_size_ || maximum_size_ != e->maximum_size_ ||
109 : trace_heap_ != e->trace_heap_) {
110 E : return false;
111 : }
112 :
113 E : return true;
114 E : }
115 :
116 : } // namespace events
117 : } // namespace bard
|