OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdint.h> | 5 #include <stdint.h> |
6 #include <windows.h> | 6 #include <windows.h> |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/base_paths.h" | 11 #include "base/base_paths.h" |
| 12 #include "base/command_line.h" |
12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/files/memory_mapped_file.h" | 15 #include "base/files/memory_mapped_file.h" |
15 #include "base/path_service.h" | 16 #include "base/path_service.h" |
16 #include "base/strings/pattern.h" | 17 #include "base/strings/pattern.h" |
17 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/test/launcher/test_launcher.h" |
18 #include "base/win/pe_image.h" | 20 #include "base/win/pe_image.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
23 class ELFImportsTest : public testing::Test { | 25 class ELFImportsTest : public testing::Test { |
24 protected: | 26 protected: |
25 static bool ImportsCallback(const base::win::PEImage &image, | 27 static bool ImportsCallback(const base::win::PEImage &image, |
26 LPCSTR module, | 28 LPCSTR module, |
27 PIMAGE_THUNK_DATA name_table, | 29 PIMAGE_THUNK_DATA name_table, |
(...skipping 28 matching lines...) Expand all Loading... |
56 // depends on a relatively small portion of base. In GYP, this works in debug | 58 // depends on a relatively small portion of base. In GYP, this works in debug |
57 // builds as well because static libraries are used for the sandbox and base | 59 // builds as well because static libraries are used for the sandbox and base |
58 // targets and the files that use e.g. user32.dll happen to not get brought | 60 // targets and the files that use e.g. user32.dll happen to not get brought |
59 // into the build in the first place (due to the way static libraries are | 61 // into the build in the first place (due to the way static libraries are |
60 // linked where only the required .o files are included). But we don't bother | 62 // linked where only the required .o files are included). But we don't bother |
61 // differentiating GYP and GN builds for this purpose. | 63 // differentiating GYP and GN builds for this purpose. |
62 // | 64 // |
63 // If you break this test, you may have changed base or the Windows sandbox | 65 // If you break this test, you may have changed base or the Windows sandbox |
64 // such that more system imports are required to link. | 66 // such that more system imports are required to link. |
65 #if defined(NDEBUG) && !defined(COMPONENT_BUILD) | 67 #if defined(NDEBUG) && !defined(COMPONENT_BUILD) |
| 68 |
66 TEST_F(ELFImportsTest, ChromeElfSanityCheck) { | 69 TEST_F(ELFImportsTest, ChromeElfSanityCheck) { |
67 base::FilePath dll; | 70 base::FilePath dll; |
68 ASSERT_TRUE(PathService::Get(base::DIR_EXE, &dll)); | 71 ASSERT_TRUE(PathService::Get(base::DIR_EXE, &dll)); |
69 dll = dll.Append(L"chrome_elf.dll"); | 72 dll = dll.Append(L"chrome_elf.dll"); |
70 | 73 |
71 std::vector<std::string> elf_imports; | 74 std::vector<std::string> elf_imports; |
72 GetImports(dll, &elf_imports); | 75 GetImports(dll, &elf_imports); |
73 | 76 |
74 // Check that ELF has imports. | 77 // Check that ELF has imports. |
75 ASSERT_LT(0u, elf_imports.size()) << "Ensure the chrome_elf_unittests " | 78 ASSERT_LT(0u, elf_imports.size()) << "Ensure the chrome_elf_unittests " |
(...skipping 20 matching lines...) Expand all Loading... |
96 bool match = false; | 99 bool match = false; |
97 for (const char* kValidFilePattern : kValidFilePatterns) { | 100 for (const char* kValidFilePattern : kValidFilePatterns) { |
98 if (base::MatchPattern(import, kValidFilePattern)) { | 101 if (base::MatchPattern(import, kValidFilePattern)) { |
99 match = true; | 102 match = true; |
100 break; | 103 break; |
101 } | 104 } |
102 } | 105 } |
103 ASSERT_TRUE(match) << "Illegal import in chrome_elf.dll: " << import; | 106 ASSERT_TRUE(match) << "Illegal import in chrome_elf.dll: " << import; |
104 } | 107 } |
105 } | 108 } |
| 109 |
106 TEST_F(ELFImportsTest, ChromeElfLoadSanityTest) { | 110 TEST_F(ELFImportsTest, ChromeElfLoadSanityTest) { |
| 111 // chrome_elf will try to launch crashpad_handler by reinvoking the current |
| 112 // binary with --type=crashpad-handler if not already running that way. To |
| 113 // avoid that, we relaunch and run the real test body manually, adding that |
| 114 // command line argument, as we're only trying to confirm that user32.dll |
| 115 // doesn't get loaded by import table when chrome_elf.dll does. |
| 116 base::CommandLine new_test = |
| 117 base::CommandLine(base::CommandLine::ForCurrentProcess()->GetProgram()); |
| 118 new_test.AppendSwitchASCII( |
| 119 base::kGTestFilterFlag, |
| 120 "ELFImportsTest.DISABLED_ChromeElfLoadSanityTestImpl"); |
| 121 new_test.AppendSwitchASCII("type", "crashpad-handler"); |
| 122 new_test.AppendSwitch("gtest_also_run_disabled_tests"); |
| 123 new_test.AppendSwitch("single-process-tests"); |
| 124 |
| 125 std::string output; |
| 126 ASSERT_TRUE(base::GetAppOutput(new_test, &output)); |
| 127 std::string crash_string = |
| 128 "OK ] ELFImportsTest.DISABLED_ChromeElfLoadSanityTestImpl"; |
| 129 |
| 130 if (output.find(crash_string) == std::string::npos) { |
| 131 GTEST_FAIL() << "Couldn't find\n" << crash_string << "\n in output\n " |
| 132 << output; |
| 133 } |
| 134 } |
| 135 |
| 136 // Note: This test is not actually disabled, it's just tagged disabled so that |
| 137 // the real run (above, in ChromeElfLoadSanityTest) can run it with an argument |
| 138 // added to the command line. |
| 139 TEST_F(ELFImportsTest, DISABLED_ChromeElfLoadSanityTestImpl) { |
107 base::FilePath dll; | 140 base::FilePath dll; |
108 ASSERT_TRUE(PathService::Get(base::DIR_EXE, &dll)); | 141 ASSERT_TRUE(PathService::Get(base::DIR_EXE, &dll)); |
109 dll = dll.Append(L"chrome_elf.dll"); | 142 dll = dll.Append(L"chrome_elf.dll"); |
110 | 143 |
111 // We don't expect user32 to be loaded in chrome_elf_unittests. If this test | 144 // We don't expect user32 to be loaded in chrome_elf_unittests. If this test |
112 // case fails, then it means that a dependency on user32 has crept into the | 145 // case fails, then it means that a dependency on user32 has crept into the |
113 // chrome_elf_unittests executable, which needs to be removed. | 146 // chrome_elf_unittests executable, which needs to be removed. |
114 EXPECT_EQ(nullptr, ::GetModuleHandle(L"user32.dll")); | 147 EXPECT_EQ(nullptr, ::GetModuleHandle(L"user32.dll")); |
115 | 148 |
116 HMODULE chrome_elf_module_handle = ::LoadLibrary(dll.value().c_str()); | 149 HMODULE chrome_elf_module_handle = ::LoadLibrary(dll.value().c_str()); |
117 EXPECT_TRUE(chrome_elf_module_handle != nullptr); | 150 EXPECT_TRUE(chrome_elf_module_handle != nullptr); |
118 // Loading chrome_elf.dll should not load user32.dll | 151 // Loading chrome_elf.dll should not load user32.dll |
119 EXPECT_EQ(nullptr, ::GetModuleHandle(L"user32.dll")); | 152 EXPECT_EQ(nullptr, ::GetModuleHandle(L"user32.dll")); |
120 EXPECT_TRUE(!!::FreeLibrary(chrome_elf_module_handle)); | 153 EXPECT_TRUE(!!::FreeLibrary(chrome_elf_module_handle)); |
121 } | 154 } |
122 #endif // NDEBUG | 155 |
| 156 #endif // NDEBUG && !COMPONENT_BUILD |
123 | 157 |
124 TEST_F(ELFImportsTest, ChromeExeSanityCheck) { | 158 TEST_F(ELFImportsTest, ChromeExeSanityCheck) { |
125 std::vector<std::string> exe_imports; | 159 std::vector<std::string> exe_imports; |
126 | 160 |
127 base::FilePath exe; | 161 base::FilePath exe; |
128 ASSERT_TRUE(PathService::Get(base::DIR_EXE, &exe)); | 162 ASSERT_TRUE(PathService::Get(base::DIR_EXE, &exe)); |
129 exe = exe.Append(L"chrome.exe"); | 163 exe = exe.Append(L"chrome.exe"); |
130 GetImports(exe, &exe_imports); | 164 GetImports(exe, &exe_imports); |
131 | 165 |
132 // Check that chrome.exe has imports. | 166 // Check that chrome.exe has imports. |
133 ASSERT_LT(0u, exe_imports.size()) << "Ensure the chrome_elf_unittests " | 167 ASSERT_LT(0u, exe_imports.size()) << "Ensure the chrome_elf_unittests " |
134 "target was built, instead of chrome_elf_unittests.exe"; | 168 "target was built, instead of chrome_elf_unittests.exe"; |
135 | 169 |
136 // Chrome.exe's first import must be ELF. | 170 // Chrome.exe's first import must be ELF. |
137 EXPECT_EQ("chrome_elf.dll", exe_imports[0]) << | 171 EXPECT_EQ("chrome_elf.dll", exe_imports[0]) << |
138 "Illegal import order in chrome.exe (ensure the chrome_elf_unittest " | 172 "Illegal import order in chrome.exe (ensure the chrome_elf_unittest " |
139 "target was built, instead of just chrome_elf_unittests.exe)"; | 173 "target was built, instead of just chrome_elf_unittests.exe)"; |
140 } | 174 } |
141 | 175 |
142 } // namespace | 176 } // namespace |
OLD | NEW |