OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 Google Inc. All Rights Reserved. | |
2 // | |
Sigurður Ásgeirsson
2015/11/26 21:45:12
TODO(siggi): Go from here!
manzagop (departed)
2015/11/27 15:20:28
Acknowledged.
| |
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/refinery/analyzers/type_propagator_analyzer.h" | |
16 | |
17 #include <string> | |
18 | |
19 #include "base/memory/ref_counted.h" | |
20 #include "gmock/gmock.h" | |
21 #include "gtest/gtest.h" | |
22 #include "syzygy/minidump/minidump.h" | |
23 #include "syzygy/refinery/process_state/process_state.h" | |
24 #include "syzygy/refinery/process_state/process_state_util.h" | |
25 #include "syzygy/refinery/process_state/refinery.pb.h" | |
26 #include "syzygy/refinery/types/type.h" | |
27 #include "syzygy/refinery/types/type_namer.h" | |
28 #include "syzygy/refinery/types/type_repository.h" | |
29 | |
30 namespace refinery { | |
31 | |
32 using testing::_; | |
33 using testing::DoAll; | |
34 using testing::Return; | |
35 using testing::SetArgPointee; | |
36 | |
37 namespace { | |
38 | |
39 const Address kAddress = 0x0000CAFE; // Fits 32-bit. | |
40 const Size kSize = 42U; | |
41 const uint32 kChecksum = 11U; | |
42 const uint32 kTimestamp = 22U; | |
43 const wchar_t kPath[] = L"c:\\path\\ModuleName"; | |
44 | |
45 class MockSymbolProvider : public SymbolProvider { | |
Sigurður Ásgeirsson
2015/11/27 14:45:31
Yeah - this feels like broken infrastructure again
manzagop (departed)
2015/11/27 15:20:28
Acknowledged.
| |
46 public: | |
47 MOCK_METHOD2(FindOrCreateTypeRepository, | |
48 bool(const pe::PEFile::Signature& signature, | |
49 scoped_refptr<TypeRepository>* type_repo)); | |
50 }; | |
51 | |
52 } // namespace | |
53 | |
54 class TypePropagatorAnalyzerTest : public testing::Test { | |
55 public: | |
56 TypePropagatorAnalyzerTest() | |
57 : variable_(42), | |
58 variable_ptr_(&variable_), | |
59 expected_sig_(kPath, | |
60 core::AbsoluteAddress(0U), | |
61 kSize, | |
62 kChecksum, | |
63 kTimestamp), | |
64 repo_(new TypeRepository(expected_sig_)), | |
65 type_namer_(true) { | |
66 // Create a basic type and a pointer type to it. | |
67 basic_type_ = new BasicType(L"int32_t", sizeof(int32_t)); | |
68 repo_->AddType(basic_type_); | |
69 | |
70 ptr_type_ = new PointerType(basic_type_->size(), PointerType::PTR_MODE_PTR); | |
71 repo_->AddType(ptr_type_); | |
72 ptr_type_->Finalize(kNoTypeFlags, basic_type_->type_id()); | |
73 CHECK(type_namer_.EnsureTypeName(ptr_type_)); | |
74 } | |
75 | |
76 protected: | |
77 // Data for the test. | |
78 int32_t variable_; | |
79 int32_t* variable_ptr_; | |
80 | |
81 TypePtr basic_type_; | |
82 PointerTypePtr ptr_type_; | |
83 | |
84 pe::PEFile::Signature expected_sig_; | |
85 scoped_refptr<TypeRepository> repo_; | |
86 TypeNamer type_namer_; | |
87 }; | |
88 | |
89 TEST_F(TypePropagatorAnalyzerTest, AnalyzeMinidump) { | |
90 ProcessState process_state; | |
91 | |
92 // Populate the bytes layer with the contents of variable_ptr_. | |
93 BytesLayerPtr bytes_layer; | |
94 process_state.FindOrCreateLayer(&bytes_layer); | |
95 | |
96 AddressRange range(reinterpret_cast<Address>(&variable_ptr_), | |
97 sizeof(variable_ptr_)); | |
98 BytesRecordPtr bytes_record; | |
99 bytes_layer->CreateRecord(range, &bytes_record); | |
100 | |
101 Bytes* bytes_proto = bytes_record->mutable_data(); | |
102 std::string* buffer = bytes_proto->mutable_data(); | |
103 buffer->resize(sizeof(variable_ptr_)); | |
104 memcpy(&buffer->at(0), &variable_ptr_, sizeof(variable_ptr_)); | |
Sigurður Ásgeirsson
2015/11/27 14:45:31
WriteInto makes this more concise, I think?
manzagop (departed)
2015/11/27 15:20:28
Done.
| |
105 | |
106 // Populate the module layer with a module and get its id. | |
107 ModuleLayerAccessor accessor(&process_state); | |
108 accessor.AddModuleRecord(AddressRange(kAddress, kSize), kChecksum, kTimestamp, | |
109 kPath); | |
110 ModuleId module_id = accessor.GetModuleId(kAddress); | |
111 ASSERT_NE(kNoModuleId, module_id); | |
112 | |
113 // Populate the typed block layer with knowledge of variable_ptr_. | |
114 ASSERT_TRUE(AddTypedBlockRecord(range, L"variable_ptr_", module_id, | |
115 ptr_type_->type_id(), &process_state)); | |
116 TypedBlockLayerPtr typedblock_layer; | |
117 ASSERT_TRUE(process_state.FindLayer(&typedblock_layer)); | |
118 ASSERT_EQ(1, typedblock_layer->size()); | |
119 | |
120 // Run the analyzer. | |
121 scoped_refptr<MockSymbolProvider> mock_provider(new MockSymbolProvider()); | |
122 EXPECT_CALL(*mock_provider, FindOrCreateTypeRepository(expected_sig_, _)) | |
123 .Times(1) | |
124 .WillOnce(DoAll(SetArgPointee<1>(repo_), Return(true))); | |
125 TypePropagatorAnalyzer analyzer(mock_provider); | |
126 minidump::Minidump dummy_minidump; | |
127 ASSERT_EQ(Analyzer::ANALYSIS_COMPLETE, | |
128 analyzer.Analyze(dummy_minidump, &process_state)); | |
129 | |
130 // Validate the new typed block. | |
131 ASSERT_EQ(2, typedblock_layer->size()); | |
132 | |
133 Address expected_addr = reinterpret_cast<Address>(variable_ptr_); | |
134 TypedBlockRecordPtr typed_record; | |
135 ASSERT_TRUE(process_state.FindSingleRecord(expected_addr, &typed_record)); | |
136 | |
137 ASSERT_EQ(expected_addr, typed_record->range().addr()); | |
138 ASSERT_EQ(basic_type_->size(), typed_record->range().size()); | |
139 const TypedBlock& typedblock = typed_record->data(); | |
140 ASSERT_EQ(module_id, typedblock.module_id()); | |
141 ASSERT_EQ(basic_type_->type_id(), typedblock.type_id()); | |
142 ASSERT_EQ("", typedblock.data_name()); | |
143 } | |
144 | |
145 } // namespace refinery | |
OLD | NEW |