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