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_destroy_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 : HeapDestroyEvent::HeapDestroyEvent(uint32_t stack_trace_id,
24 : HANDLE trace_heap,
25 : BOOL trace_succeeded)
26 E : : stack_trace_id_(stack_trace_id),
27 E : trace_heap_(trace_heap),
28 E : trace_succeeded_(trace_succeeded) {
29 E : }
30 :
31 : bool HeapDestroyEvent::Save(const EventInterface* const event,
32 E : core::OutArchive* out_archive) {
33 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), event);
34 E : DCHECK_NE(static_cast<core::OutArchive*>(nullptr), out_archive);
35 :
36 : const HeapDestroyEvent* derived_event =
37 E : reinterpret_cast<const HeapDestroyEvent*>(event);
38 :
39 E : return out_archive->Save(derived_event->stack_trace_id_) &&
40 : out_archive->Save(
41 : reinterpret_cast<uintptr_t>(derived_event->trace_heap_)) &&
42 : out_archive->Save(derived_event->trace_succeeded_);
43 E : }
44 :
45 : std::unique_ptr<HeapDestroyEvent> HeapDestroyEvent::Load(
46 E : core::InArchive* in_archive) {
47 E : DCHECK_NE(static_cast<core::InArchive*>(nullptr), in_archive);
48 :
49 E : uint32_t stack_trace_id = 0;
50 E : uintptr_t trace_heap = 0;
51 E : BOOL trace_succeeded = 0;
52 E : if (in_archive->Load(&stack_trace_id) && in_archive->Load(&trace_heap) &&
53 : in_archive->Load(&trace_succeeded)) {
54 E : return std::unique_ptr<HeapDestroyEvent>(new HeapDestroyEvent(
55 : stack_trace_id, reinterpret_cast<HANDLE>(trace_heap), trace_succeeded));
56 : }
57 i : return nullptr;
58 E : }
59 :
60 E : bool HeapDestroyEvent::Play(void* backdrop) {
61 E : DCHECK_NE(static_cast<void*>(nullptr), backdrop);
62 :
63 : using bard::backdrops::HeapBackdrop;
64 E : HeapBackdrop* heap_backdrop = reinterpret_cast<HeapBackdrop*>(backdrop);
65 :
66 E : HANDLE live_heap = INVALID_HANDLE_VALUE;
67 :
68 E : if (!heap_backdrop->heap_map().GetLiveFromTrace(trace_heap_, &live_heap))
69 i : return false;
70 :
71 E : uint64_t timing = 0;
72 : BOOL live_succeeded =
73 E : InvokeOnBackdrop(stack_trace_id_, &timing, heap_backdrop,
74 : &HeapBackdrop::HeapDestroy, live_heap);
75 :
76 E : if (live_succeeded != trace_succeeded_) {
77 E : LOG(ERROR) << "HeapDestroy " << (live_succeeded ? "succeeded" : "failed")
78 : << " when it was supposed to "
79 : << (trace_succeeded_ ? "succeed" : "fail") << ".";
80 E : return false;
81 : }
82 :
83 E : if (live_succeeded &&
84 : !heap_backdrop->heap_map().RemoveMapping(trace_heap_, live_heap)) {
85 i : return false;
86 : }
87 :
88 E : heap_backdrop->UpdateStats(type(), timing);
89 :
90 E : return true;
91 E : }
92 :
93 E : bool HeapDestroyEvent::Equals(const EventInterface* rhs) const {
94 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), rhs);
95 :
96 E : if (rhs->type() != kHeapDestroyEvent)
97 i : return false;
98 :
99 E : const auto e = reinterpret_cast<const HeapDestroyEvent*>(rhs);
100 E : if (stack_trace_id_ != e->stack_trace_id_ || trace_heap_ != e->trace_heap_ ||
101 : trace_succeeded_ != e->trace_succeeded_) {
102 E : return false;
103 : }
104 :
105 E : return true;
106 E : }
107 :
108 : } // namespace events
109 : } // namespace bard
|