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_set_information_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 : HeapSetInformationEvent::HeapSetInformationEvent(
24 : HANDLE trace_heap,
25 : HEAP_INFORMATION_CLASS info_class,
26 : PVOID info,
27 : SIZE_T info_length,
28 : BOOL trace_succeeded)
29 : : trace_heap_(trace_heap),
30 : info_class_(info_class),
31 : info_(info),
32 : info_length_(info_length),
33 E : trace_succeeded_(trace_succeeded) {
34 E : }
35 :
36 : bool HeapSetInformationEvent::Save(const EventInterface* const event,
37 E : core::OutArchive* out_archive) {
38 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), event);
39 E : DCHECK_NE(static_cast<core::OutArchive*>(nullptr), out_archive);
40 :
41 : const HeapSetInformationEvent* derived_event =
42 E : reinterpret_cast<const HeapSetInformationEvent*>(event);
43 :
44 : return out_archive->Save(
45 : reinterpret_cast<uintptr_t>(derived_event->trace_heap_)) &&
46 : out_archive->Save(
47 : static_cast<uint32_t>(derived_event->info_class_)) &&
48 : out_archive->Save(
49 : reinterpret_cast<uintptr_t>(derived_event->info_)) &&
50 : out_archive->Save(derived_event->info_length_) &&
51 E : out_archive->Save(derived_event->trace_succeeded_);
52 E : }
53 :
54 : scoped_ptr<HeapSetInformationEvent> HeapSetInformationEvent::Load(
55 E : core::InArchive* in_archive) {
56 E : DCHECK_NE(static_cast<core::InArchive*>(nullptr), in_archive);
57 :
58 : uintptr_t trace_heap;
59 : uint32_t info_class;
60 : uintptr_t info;
61 : SIZE_T info_length;
62 : BOOL trace_succeeded;
63 : if (in_archive->Load(&trace_heap) &&
64 : in_archive->Load(&info_class) &&
65 : in_archive->Load(&info) &&
66 : in_archive->Load(&info_length) &&
67 E : in_archive->Load(&trace_succeeded)) {
68 : return scoped_ptr<HeapSetInformationEvent>(
69 : new HeapSetInformationEvent(
70 : reinterpret_cast<HANDLE>(trace_heap),
71 : static_cast<HEAP_INFORMATION_CLASS>(info_class),
72 : reinterpret_cast<PVOID>(info),
73 : info_length,
74 E : trace_succeeded));
75 : }
76 i : return nullptr;
77 E : }
78 :
79 E : bool HeapSetInformationEvent::Play(void* backdrop) {
80 E : DCHECK_NE(static_cast<void*>(nullptr), backdrop);
81 :
82 : using bard::backdrops::HeapBackdrop;
83 E : HeapBackdrop* heap_backdrop = reinterpret_cast<HeapBackdrop*>(backdrop);
84 :
85 E : HANDLE live_heap = INVALID_HANDLE_VALUE;
86 :
87 E : if (!heap_backdrop->heap_map().GetLiveFromTrace(trace_heap_, &live_heap))
88 i : return false;
89 :
90 E : uint64_t t0 = ::trace::common::GetTsc();
91 : BOOL live_succeeded = heap_backdrop->HeapSetInformation(
92 E : live_heap, info_class_, info_, info_length_);
93 E : uint64_t t1 = ::trace::common::GetTsc();
94 :
95 E : if (live_succeeded != trace_succeeded_) {
96 E : LOG(ERROR) << "HeapSetInformation "
97 : << (live_succeeded ? "succeeded" : "failed")
98 : << " when it was supposed to "
99 : << (trace_succeeded_ ? "succeed" : "fail") << ".";
100 E : return false;
101 : }
102 :
103 E : heap_backdrop->UpdateStats(type(), t1 - t0);
104 :
105 E : return true;
106 E : }
107 :
108 E : bool HeapSetInformationEvent::Equals(const EventInterface* rhs) const {
109 E : DCHECK_NE(static_cast<EventInterface*>(nullptr), rhs);
110 :
111 E : if (rhs->type() != kHeapSetInformationEvent)
112 i : return false;
113 :
114 E : const auto e = reinterpret_cast<const HeapSetInformationEvent*>(rhs);
115 : if (trace_heap_ != e->trace_heap_ || info_class_ != e->info_class_ ||
116 : info_ != e->info_ || info_length_ != e->info_length_ ||
117 E : trace_succeeded_ != e->trace_succeeded_) {
118 E : return false;
119 : }
120 :
121 E : return true;
122 E : }
123 :
124 : } // namespace events
125 : } // namespace bard
|