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