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