| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <stdint.h> | |
| 6 #include <windows.h> | |
| 7 | |
| 8 #include <algorithm> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/base_paths.h" | |
| 12 #include "base/compiler_specific.h" | |
| 13 #include "base/files/file_path.h" | |
| 14 #include "base/files/memory_mapped_file.h" | |
| 15 #include "base/path_service.h" | |
| 16 #include "base/strings/pattern.h" | |
| 17 #include "base/strings/string_util.h" | |
| 18 #include "base/win/pe_image.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 class ELFImportsTest : public testing::Test { | |
| 24 protected: | |
| 25 static bool ImportsCallback(const base::win::PEImage &image, | |
| 26 LPCSTR module, | |
| 27 PIMAGE_THUNK_DATA name_table, | |
| 28 PIMAGE_THUNK_DATA iat, | |
| 29 PVOID cookie) { | |
| 30 std::vector<std::string>* import_list = | |
| 31 reinterpret_cast<std::vector<std::string>*>(cookie); | |
| 32 import_list->push_back(module); | |
| 33 return true; | |
| 34 } | |
| 35 | |
| 36 void GetImports(const base::FilePath& module_path, | |
| 37 std::vector<std::string>* imports) { | |
| 38 ASSERT_TRUE(imports != NULL); | |
| 39 | |
| 40 base::MemoryMappedFile module_mmap; | |
| 41 | |
| 42 ASSERT_TRUE(module_mmap.Initialize(module_path)); | |
| 43 base::win::PEImageAsData pe_image_data( | |
| 44 reinterpret_cast<HMODULE>(const_cast<uint8_t*>(module_mmap.data()))); | |
| 45 pe_image_data.EnumImportChunks(ELFImportsTest::ImportsCallback, imports); | |
| 46 } | |
| 47 }; | |
| 48 | |
| 49 // Run this test only in Release builds. | |
| 50 // | |
| 51 // This test makes sure that chrome_elf.dll has only certain types of imports. | |
| 52 // However, it directly and indirectly depends on base, which has lots more | |
| 53 // imports than are allowed here. | |
| 54 // | |
| 55 // In release builds, the offending imports are all stripped since this | |
| 56 // 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 | |
| 58 // 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 | |
| 60 // linked where only the required .o files are included). But we don't bother | |
| 61 // differentiating GYP and GN builds for this purpose. | |
| 62 // | |
| 63 // If you break this test, you may have changed base or the Windows sandbox | |
| 64 // such that more system imports are required to link. | |
| 65 #ifdef NDEBUG | |
| 66 TEST_F(ELFImportsTest, ChromeElfSanityCheck) { | |
| 67 base::FilePath dll; | |
| 68 ASSERT_TRUE(PathService::Get(base::DIR_EXE, &dll)); | |
| 69 dll = dll.Append(L"chrome_elf.dll"); | |
| 70 | |
| 71 std::vector<std::string> elf_imports; | |
| 72 GetImports(dll, &elf_imports); | |
| 73 | |
| 74 // Check that ELF has imports. | |
| 75 ASSERT_LT(0u, elf_imports.size()) << "Ensure the chrome_elf_unittests " | |
| 76 "target was built, instead of chrome_elf_unittests.exe"; | |
| 77 | |
| 78 static const char* const kValidFilePatterns[] = { | |
| 79 "KERNEL32.dll", | |
| 80 #if defined(COMPONENT_BUILD) | |
| 81 "MSVC*.dll", | |
| 82 "VCRUNTIME*.dll", | |
| 83 "api-ms-win-crt-*.dll", | |
| 84 #endif | |
| 85 #if defined(SYZYASAN) | |
| 86 "syzyasan_rtl.dll", | |
| 87 #endif | |
| 88 #if defined(ADDRESS_SANITIZER) && defined(COMPONENT_BUILD) | |
| 89 "clang_rt.asan_dynamic-i386.dll", | |
| 90 #endif | |
| 91 "ADVAPI32.dll" | |
| 92 }; | |
| 93 | |
| 94 // Make sure all of ELF's imports are in the valid imports list. | |
| 95 for (const std::string& import : elf_imports) { | |
| 96 bool match = false; | |
| 97 for (const char* kValidFilePattern : kValidFilePatterns) { | |
| 98 if (base::MatchPattern(import, kValidFilePattern)) { | |
| 99 match = true; | |
| 100 break; | |
| 101 } | |
| 102 } | |
| 103 ASSERT_TRUE(match) << "Illegal import in chrome_elf.dll: " << import; | |
| 104 } | |
| 105 } | |
| 106 #endif // NDEBUG | |
| 107 | |
| 108 TEST_F(ELFImportsTest, ChromeExeSanityCheck) { | |
| 109 std::vector<std::string> exe_imports; | |
| 110 | |
| 111 base::FilePath exe; | |
| 112 ASSERT_TRUE(PathService::Get(base::DIR_EXE, &exe)); | |
| 113 exe = exe.Append(L"chrome.exe"); | |
| 114 GetImports(exe, &exe_imports); | |
| 115 | |
| 116 // Check that chrome.exe has imports. | |
| 117 ASSERT_LT(0u, exe_imports.size()) << "Ensure the chrome_elf_unittests " | |
| 118 "target was built, instead of chrome_elf_unittests.exe"; | |
| 119 | |
| 120 // Chrome.exe's first import must be ELF. | |
| 121 EXPECT_EQ("chrome_elf.dll", exe_imports[0]) << | |
| 122 "Illegal import order in chrome.exe (ensure the chrome_elf_unittest " | |
| 123 "target was built, instead of just chrome_elf_unittests.exe)"; | |
| 124 } | |
| 125 | |
| 126 } // namespace | |
| OLD | NEW |