| OLD | NEW |
| 1 // Copyright 2015 Google Inc. All Rights Reserved. | 1 // Copyright 2015 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "syzygy/refinery/process_state/process_state_util.h" | 15 #include "syzygy/refinery/process_state/process_state_util.h" |
| 16 | 16 |
| 17 #include <vector> | 17 #include <vector> |
| 18 | 18 |
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "gtest/gtest.h" | 20 #include "gtest/gtest.h" |
| 21 #include "syzygy/refinery/core/address.h" | 21 #include "syzygy/refinery/core/address.h" |
| 22 #include "syzygy/refinery/process_state/layer_data.h" |
| 22 #include "syzygy/refinery/process_state/process_state.h" | 23 #include "syzygy/refinery/process_state/process_state.h" |
| 23 #include "syzygy/refinery/process_state/refinery.pb.h" | 24 #include "syzygy/refinery/process_state/refinery.pb.h" |
| 25 #include "syzygy/refinery/types/type.h" |
| 24 | 26 |
| 25 namespace refinery { | 27 namespace refinery { |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| 29 const Address kAddress = 0x0000CAFE; // Fits 32-bit. | 31 const Address kAddress = 0x0000CAFE; // Fits 32-bit. |
| 30 const Size kSize = 42U; | 32 const Size kSize = 42U; |
| 31 const uint32 kChecksum = 11U; | 33 const uint32 kChecksum = 11U; |
| 32 const uint32 kTimestamp = 22U; | 34 const uint32 kTimestamp = 22U; |
| 33 const wchar_t kPath[] = L"c:\\path\\ModuleName"; | 35 const wchar_t kPath[] = L"c:\\path\\ModuleName"; |
| 34 const char kDataName[] = "data_name"; | 36 const char kDataName[] = "data_name"; |
| 35 const char kTypeName[] = "Type::Name*"; | 37 const ModuleId kModuleId = 100; |
| 38 const TypeId kTypeId = 42; |
| 36 | 39 |
| 37 } // namespace | 40 } // namespace |
| 38 | 41 |
| 39 TEST(ModuleLayerAccessorTest, AddModuleRecord) { | 42 TEST(ModuleLayerAccessorTest, AddModuleRecord) { |
| 40 ProcessState state; | 43 ProcessState state; |
| 41 ModuleLayerAccessor accessor(&state); | 44 ModuleLayerAccessor accessor(&state); |
| 42 accessor.AddModuleRecord(AddressRange(kAddress, kSize), kChecksum, kTimestamp, | 45 accessor.AddModuleRecord(AddressRange(kAddress, kSize), kChecksum, kTimestamp, |
| 43 kPath); | 46 kPath); |
| 44 | 47 |
| 45 // Validate a record was added. | 48 // Validate a record was added. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 60 ASSERT_TRUE(module_layer->data().Find(module.module_id(), &signature)); | 63 ASSERT_TRUE(module_layer->data().Find(module.module_id(), &signature)); |
| 61 ASSERT_EQ(kPath, signature.path); | 64 ASSERT_EQ(kPath, signature.path); |
| 62 ASSERT_EQ(0U, signature.base_address.value()); | 65 ASSERT_EQ(0U, signature.base_address.value()); |
| 63 ASSERT_EQ(kSize, signature.module_size); | 66 ASSERT_EQ(kSize, signature.module_size); |
| 64 ASSERT_EQ(kChecksum, signature.module_checksum); | 67 ASSERT_EQ(kChecksum, signature.module_checksum); |
| 65 ASSERT_EQ(kTimestamp, signature.module_time_date_stamp); | 68 ASSERT_EQ(kTimestamp, signature.module_time_date_stamp); |
| 66 | 69 |
| 67 ASSERT_EQ(module.module_id(), module_layer->data().Find(signature)); | 70 ASSERT_EQ(module.module_id(), module_layer->data().Find(signature)); |
| 68 } | 71 } |
| 69 | 72 |
| 70 TEST(ModuleLayerAccessorTest, GetModuleSignatureTest) { | 73 TEST(ModuleLayerAccessorTest, GetModuleSignatureVATest) { |
| 71 ProcessState state; | 74 ProcessState state; |
| 72 ModuleLayerAccessor accessor(&state); | 75 ModuleLayerAccessor accessor(&state); |
| 73 pe::PEFile::Signature signature; | 76 pe::PEFile::Signature signature; |
| 74 | 77 |
| 75 // Fails when VA doesn't correspond to a module. | 78 // Fails when VA doesn't correspond to a module. |
| 76 ASSERT_FALSE(accessor.GetModuleSignature(kAddress, &signature)); | 79 ASSERT_FALSE(accessor.GetModuleSignature(kAddress, &signature)); |
| 77 | 80 |
| 78 // Add a module. | 81 // Add a module. |
| 79 accessor.AddModuleRecord(AddressRange(kAddress, kSize), kChecksum, kTimestamp, | 82 accessor.AddModuleRecord(AddressRange(kAddress, kSize), kChecksum, kTimestamp, |
| 80 kPath); | 83 kPath); |
| 81 | 84 |
| 82 // Fails outside the module's range. | 85 // Fails outside the module's range. |
| 83 ASSERT_FALSE(accessor.GetModuleSignature(kAddress - 1, &signature)); | 86 ASSERT_FALSE(accessor.GetModuleSignature(kAddress - 1, &signature)); |
| 84 ASSERT_FALSE(accessor.GetModuleSignature(kAddress + kSize, &signature)); | 87 ASSERT_FALSE(accessor.GetModuleSignature(kAddress + kSize, &signature)); |
| 85 | 88 |
| 86 // Succeeds within the module's range. | 89 // Succeeds within the module's range. |
| 87 ASSERT_TRUE(accessor.GetModuleSignature(kAddress, &signature)); | 90 ASSERT_TRUE(accessor.GetModuleSignature(kAddress, &signature)); |
| 88 ASSERT_TRUE(accessor.GetModuleSignature(kAddress + kSize - 1, &signature)); | 91 ASSERT_TRUE(accessor.GetModuleSignature(kAddress + kSize - 1, &signature)); |
| 89 | 92 |
| 90 // Validate signature on the last hit. | 93 // Validate signature on the last hit. |
| 91 ASSERT_EQ(kAddress, signature.base_address.value()); | 94 ASSERT_EQ(kAddress, signature.base_address.value()); |
| 92 ASSERT_EQ(kSize, signature.module_size); | 95 ASSERT_EQ(kSize, signature.module_size); |
| 93 ASSERT_EQ(kChecksum, signature.module_checksum); | 96 ASSERT_EQ(kChecksum, signature.module_checksum); |
| 94 ASSERT_EQ(kTimestamp, signature.module_time_date_stamp); | 97 ASSERT_EQ(kTimestamp, signature.module_time_date_stamp); |
| 95 ASSERT_EQ(kPath, signature.path); | 98 ASSERT_EQ(kPath, signature.path); |
| 96 } | 99 } |
| 97 | 100 |
| 101 TEST(ModuleLayerAccessorTest, GetModuleSignatureIdTest) { |
| 102 ProcessState state; |
| 103 ModuleLayerAccessor accessor(&state); |
| 104 |
| 105 // Add a module and get its id. |
| 106 accessor.AddModuleRecord(AddressRange(kAddress, kSize), kChecksum, kTimestamp, |
| 107 kPath); |
| 108 ModuleId module_id = accessor.GetModuleId(kAddress); |
| 109 |
| 110 // Validate. |
| 111 pe::PEFile::Signature signature; |
| 112 ASSERT_TRUE(accessor.GetModuleSignature(module_id, &signature)); |
| 113 |
| 114 ASSERT_EQ(0U, signature.base_address.value()); |
| 115 ASSERT_EQ(kSize, signature.module_size); |
| 116 ASSERT_EQ(kChecksum, signature.module_checksum); |
| 117 ASSERT_EQ(kTimestamp, signature.module_time_date_stamp); |
| 118 ASSERT_EQ(kPath, signature.path); |
| 119 } |
| 120 |
| 121 TEST(ModuleLayerAccessorTest, GetModuleIdTest) { |
| 122 ProcessState state; |
| 123 ModuleLayerAccessor accessor(&state); |
| 124 |
| 125 // Not hitting a module case. |
| 126 ASSERT_EQ(kNoModuleId, accessor.GetModuleId(kAddress)); |
| 127 |
| 128 // Hitting a module case. |
| 129 accessor.AddModuleRecord(AddressRange(kAddress, kSize), kChecksum, kTimestamp, |
| 130 kPath); |
| 131 ModuleId module_id = accessor.GetModuleId(kAddress); |
| 132 ASSERT_NE(kNoModuleId, module_id); |
| 133 |
| 134 // Consistency check: the signature associated to module_id must be equal to |
| 135 // that associated with va, up to the base address being 0. |
| 136 pe::PEFile::Signature sig_from_va; |
| 137 ASSERT_TRUE(accessor.GetModuleSignature(kAddress, &sig_from_va)); |
| 138 sig_from_va.base_address = core::AbsoluteAddress(0U); |
| 139 |
| 140 pe::PEFile::Signature sig_from_id; |
| 141 ModuleLayerPtr layer; |
| 142 state.FindOrCreateLayer(&layer); |
| 143 ASSERT_TRUE(layer->data().Find(module_id, &sig_from_id)); |
| 144 |
| 145 ASSERT_EQ(sig_from_va, sig_from_id); |
| 146 } |
| 147 |
| 98 TEST(AddTypedBlockRecord, BasicTest) { | 148 TEST(AddTypedBlockRecord, BasicTest) { |
| 99 ProcessState state; | 149 ProcessState state; |
| 100 AddTypedBlockRecord(AddressRange(kAddress, kSize), | 150 AddTypedBlockRecord(AddressRange(kAddress, kSize), |
| 101 base::ASCIIToUTF16(kDataName), | 151 base::ASCIIToUTF16(kDataName), kModuleId, kTypeId, |
| 102 base::ASCIIToUTF16(kTypeName), &state); | 152 &state); |
| 103 | 153 |
| 104 // Validate a record was added. | 154 // Validate a record was added. |
| 105 TypedBlockLayerPtr layer; | 155 TypedBlockLayerPtr layer; |
| 106 ASSERT_TRUE(state.FindLayer(&layer)); | 156 ASSERT_TRUE(state.FindLayer(&layer)); |
| 107 std::vector<TypedBlockRecordPtr> matching_records; | 157 std::vector<TypedBlockRecordPtr> matching_records; |
| 108 layer->GetRecordsAt(kAddress, &matching_records); | 158 layer->GetRecordsAt(kAddress, &matching_records); |
| 109 ASSERT_EQ(1, matching_records.size()); | 159 ASSERT_EQ(1, matching_records.size()); |
| 110 | 160 |
| 111 // Validate range. | 161 // Validate range. |
| 112 TypedBlockRecordPtr record = matching_records[0]; | 162 TypedBlockRecordPtr record = matching_records[0]; |
| 113 ASSERT_EQ(AddressRange(kAddress, kSize), record->range()); | 163 ASSERT_EQ(AddressRange(kAddress, kSize), record->range()); |
| 114 | 164 |
| 115 // Validate TypedBlock proto. | 165 // Validate TypedBlock proto. |
| 116 TypedBlock* proto = record->mutable_data(); | 166 TypedBlock* proto = record->mutable_data(); |
| 117 ASSERT_EQ(kDataName, proto->data_name()); | 167 ASSERT_EQ(kDataName, proto->data_name()); |
| 118 ASSERT_EQ(kTypeName, proto->type_name()); | 168 ASSERT_EQ(kTypeId, proto->type_id()); |
| 169 ASSERT_EQ(kModuleId, proto->module_id()); |
| 119 } | 170 } |
| 120 | 171 |
| 121 } // namespace refinery | 172 } // namespace refinery |
| OLD | NEW |