| OLD | NEW |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. 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 "snapshot/win/pe_image_reader.h" | 15 #include "snapshot/win/pe_image_reader.h" |
| 16 | 16 |
| 17 #define PSAPI_VERSION 1 | 17 #define PSAPI_VERSION 1 |
| 18 #include <psapi.h> | 18 #include <psapi.h> |
| 19 | 19 |
| 20 #include "base/files/file_path.h" |
| 21 #include "base/strings/utf_string_conversions.h" |
| 20 #include "gtest/gtest.h" | 22 #include "gtest/gtest.h" |
| 21 #include "snapshot/win/process_reader_win.h" | 23 #include "snapshot/win/process_reader_win.h" |
| 24 #include "test/errors.h" |
| 22 #include "util/win/get_function.h" | 25 #include "util/win/get_function.h" |
| 26 #include "util/win/module_version.h" |
| 27 #include "util/win/process_info.h" |
| 23 | 28 |
| 24 extern "C" IMAGE_DOS_HEADER __ImageBase; | 29 extern "C" IMAGE_DOS_HEADER __ImageBase; |
| 25 | 30 |
| 26 namespace crashpad { | 31 namespace crashpad { |
| 27 namespace test { | 32 namespace test { |
| 28 namespace { | 33 namespace { |
| 29 | 34 |
| 30 BOOL CrashpadGetModuleInformation(HANDLE process, | 35 BOOL CrashpadGetModuleInformation(HANDLE process, |
| 31 HMODULE module, | 36 HMODULE module, |
| 32 MODULEINFO* module_info, | 37 MODULEINFO* module_info, |
| 33 DWORD cb) { | 38 DWORD cb) { |
| 34 static const auto get_module_information = | 39 static const auto get_module_information = |
| 35 GET_FUNCTION_REQUIRED(L"psapi.dll", ::GetModuleInformation); | 40 GET_FUNCTION_REQUIRED(L"psapi.dll", ::GetModuleInformation); |
| 36 return get_module_information(process, module, module_info, cb); | 41 return get_module_information(process, module, module_info, cb); |
| 37 } | 42 } |
| 38 | 43 |
| 39 TEST(PEImageReader, DebugDirectory) { | 44 TEST(PEImageReader, DebugDirectory) { |
| 40 PEImageReader pe_image_reader; | 45 PEImageReader pe_image_reader; |
| 41 ProcessReaderWin process_reader; | 46 ProcessReaderWin process_reader; |
| 42 ASSERT_TRUE(process_reader.Initialize(GetCurrentProcess(), | 47 ASSERT_TRUE(process_reader.Initialize(GetCurrentProcess(), |
| 43 ProcessSuspensionState::kRunning)); | 48 ProcessSuspensionState::kRunning)); |
| 44 HMODULE self = reinterpret_cast<HMODULE>(&__ImageBase); | 49 HMODULE self = reinterpret_cast<HMODULE>(&__ImageBase); |
| 45 MODULEINFO module_info; | 50 MODULEINFO module_info; |
| 46 ASSERT_TRUE(CrashpadGetModuleInformation( | 51 ASSERT_TRUE(CrashpadGetModuleInformation( |
| 47 GetCurrentProcess(), self, &module_info, sizeof(module_info))); | 52 GetCurrentProcess(), self, &module_info, sizeof(module_info))) |
| 53 << ErrorMessage("GetModuleInformation"); |
| 48 EXPECT_EQ(self, module_info.lpBaseOfDll); | 54 EXPECT_EQ(self, module_info.lpBaseOfDll); |
| 49 EXPECT_TRUE(pe_image_reader.Initialize(&process_reader, | 55 ASSERT_TRUE(pe_image_reader.Initialize(&process_reader, |
| 50 reinterpret_cast<WinVMAddress>(self), | 56 reinterpret_cast<WinVMAddress>(self), |
| 51 module_info.SizeOfImage, | 57 module_info.SizeOfImage, |
| 52 "self")); | 58 "self")); |
| 53 UUID uuid; | 59 UUID uuid; |
| 54 DWORD age; | 60 DWORD age; |
| 55 std::string pdbname; | 61 std::string pdbname; |
| 56 EXPECT_TRUE(pe_image_reader.DebugDirectoryInformation(&uuid, &age, &pdbname)); | 62 EXPECT_TRUE(pe_image_reader.DebugDirectoryInformation(&uuid, &age, &pdbname)); |
| 57 EXPECT_NE(std::string::npos, pdbname.find("crashpad_snapshot_test")); | 63 EXPECT_NE(std::string::npos, pdbname.find("crashpad_snapshot_test")); |
| 58 const std::string suffix(".pdb"); | 64 const std::string suffix(".pdb"); |
| 59 EXPECT_EQ( | 65 EXPECT_EQ( |
| 60 0, | 66 0, |
| 61 pdbname.compare(pdbname.size() - suffix.size(), suffix.size(), suffix)); | 67 pdbname.compare(pdbname.size() - suffix.size(), suffix.size(), suffix)); |
| 62 } | 68 } |
| 63 | 69 |
| 70 void TestVSFixedFileInfo(ProcessReaderWin* process_reader, |
| 71 const ProcessInfo::Module& module, |
| 72 bool known_dll) { |
| 73 PEImageReader pe_image_reader; |
| 74 ASSERT_TRUE(pe_image_reader.Initialize(process_reader, |
| 75 module.dll_base, |
| 76 module.size, |
| 77 base::UTF16ToUTF8(module.name))); |
| 78 |
| 79 VS_FIXEDFILEINFO observed; |
| 80 const bool observed_rv = pe_image_reader.VSFixedFileInfo(&observed); |
| 81 ASSERT_TRUE(observed_rv || !known_dll); |
| 82 |
| 83 if (observed_rv) { |
| 84 EXPECT_EQ(VS_FFI_SIGNATURE, observed.dwSignature); |
| 85 EXPECT_EQ(VS_FFI_STRUCVERSION, observed.dwStrucVersion); |
| 86 EXPECT_EQ(0, observed.dwFileFlags & ~observed.dwFileFlagsMask); |
| 87 EXPECT_EQ(VOS_NT_WINDOWS32, observed.dwFileOS); |
| 88 if (known_dll) { |
| 89 EXPECT_EQ(VFT_DLL, observed.dwFileType); |
| 90 } else { |
| 91 EXPECT_TRUE(observed.dwFileType == VFT_APP || |
| 92 observed.dwFileType == VFT_DLL); |
| 93 } |
| 94 } |
| 95 |
| 96 base::FilePath module_path(module.name); |
| 97 |
| 98 const DWORD version = GetVersion(); |
| 99 const int major_version = LOBYTE(LOWORD(version)); |
| 100 const int minor_version = HIBYTE(LOWORD(version)); |
| 101 if (major_version > 6 || (major_version == 6 && minor_version >= 2)) { |
| 102 // Windows 8 or later. |
| 103 // |
| 104 // Use BaseName() to ensure that GetModuleVersionAndType() finds the |
| 105 // already-loaded module with the specified name. Otherwise, dwFileVersionMS |
| 106 // may not match. This appears to be related to the changes made in Windows |
| 107 // 8.1 to GetVersion() and GetVersionEx() for non-manifested applications |
| 108 module_path = module_path.BaseName(); |
| 109 } |
| 110 |
| 111 VS_FIXEDFILEINFO expected; |
| 112 const bool expected_rv = GetModuleVersionAndType(module_path, &expected); |
| 113 ASSERT_TRUE(expected_rv || !known_dll); |
| 114 |
| 115 EXPECT_EQ(expected_rv, observed_rv); |
| 116 |
| 117 if (observed_rv && expected_rv) { |
| 118 EXPECT_EQ(expected.dwSignature, observed.dwSignature); |
| 119 EXPECT_EQ(expected.dwStrucVersion, observed.dwStrucVersion); |
| 120 EXPECT_EQ(expected.dwFileVersionMS, observed.dwFileVersionMS); |
| 121 EXPECT_EQ(expected.dwFileVersionLS, observed.dwFileVersionLS); |
| 122 EXPECT_EQ(expected.dwProductVersionMS, observed.dwProductVersionMS); |
| 123 EXPECT_EQ(expected.dwProductVersionLS, observed.dwProductVersionLS); |
| 124 EXPECT_EQ(expected.dwFileFlagsMask, observed.dwFileFlagsMask); |
| 125 EXPECT_EQ(expected.dwFileFlags, observed.dwFileFlags); |
| 126 EXPECT_EQ(expected.dwFileOS, observed.dwFileOS); |
| 127 EXPECT_EQ(expected.dwFileType, observed.dwFileType); |
| 128 EXPECT_EQ(expected.dwFileSubtype, observed.dwFileSubtype); |
| 129 EXPECT_EQ(expected.dwFileDateMS, observed.dwFileDateMS); |
| 130 EXPECT_EQ(expected.dwFileDateLS, observed.dwFileDateLS); |
| 131 } |
| 132 } |
| 133 |
| 134 TEST(PEImageReader, VSFixedFileInfo_OneModule) { |
| 135 ProcessReaderWin process_reader; |
| 136 ASSERT_TRUE(process_reader.Initialize(GetCurrentProcess(), |
| 137 ProcessSuspensionState::kRunning)); |
| 138 |
| 139 const wchar_t kModuleName[] = L"kernel32.dll"; |
| 140 const HMODULE module_handle = GetModuleHandle(kModuleName); |
| 141 ASSERT_TRUE(module_handle) << ErrorMessage("GetModuleHandle"); |
| 142 |
| 143 MODULEINFO module_info; |
| 144 ASSERT_TRUE(CrashpadGetModuleInformation( |
| 145 GetCurrentProcess(), module_handle, &module_info, sizeof(module_info))) |
| 146 << ErrorMessage("GetModuleInformation"); |
| 147 EXPECT_EQ(module_handle, module_info.lpBaseOfDll); |
| 148 |
| 149 ProcessInfo::Module module; |
| 150 module.name = kModuleName; |
| 151 module.dll_base = reinterpret_cast<WinVMAddress>(module_info.lpBaseOfDll); |
| 152 module.size = module_info.SizeOfImage; |
| 153 |
| 154 TestVSFixedFileInfo(&process_reader, module, true); |
| 155 } |
| 156 |
| 157 TEST(PEImageReader, VSFixedFileInfo_AllModules) { |
| 158 ProcessReaderWin process_reader; |
| 159 ASSERT_TRUE(process_reader.Initialize(GetCurrentProcess(), |
| 160 ProcessSuspensionState::kRunning)); |
| 161 |
| 162 const std::vector<ProcessInfo::Module>& modules = process_reader.Modules(); |
| 163 EXPECT_GT(modules.size(), 2u); |
| 164 |
| 165 for (const auto& module : modules) { |
| 166 SCOPED_TRACE(base::UTF16ToUTF8(module.name)); |
| 167 TestVSFixedFileInfo(&process_reader, module, false); |
| 168 } |
| 169 } |
| 170 |
| 64 } // namespace | 171 } // namespace |
| 65 } // namespace test | 172 } // namespace test |
| 66 } // namespace crashpad | 173 } // namespace crashpad |
| OLD | NEW |