Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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/basictypes.h" | |
| 12 #include "base/compiler_specific.h" | |
| 13 #include "base/scoped_native_library.h" | |
| 14 #include "base/strings/string_util.h" | |
| 15 #include "base/win/pe_image.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 const char kKernel32[] = "KERNEL32.dll"; | |
| 21 const char kMSVC[] = "MSVC*"; | |
| 22 | |
| 23 class ELFImportsTest : public testing::Test { | |
| 24 protected: | |
| 25 void GetImports(wchar_t* module_name, std::vector<std::string>* imports) { | |
| 26 HMODULE module_handle = ::LoadLibrary(module_name); | |
|
Sigurður Ásgeirsson
2013/12/17 14:52:37
You should be able to do this without executing an
Cait (Slow)
2013/12/18 20:59:03
Done.
| |
| 27 base::ScopedNativeLibrary library(module_handle); | |
|
robertshield
2013/12/17 04:02:03
This looks odd, but I guess it's done since SNL's
Cait (Slow)
2013/12/18 20:59:03
Not needed anymore.
| |
| 28 | |
| 29 if (!module_handle || !imports) | |
|
robertshield
2013/12/17 04:02:03
checking imports first would allow you to abort ea
Cait (Slow)
2013/12/18 20:59:03
Done.
| |
| 30 return; | |
| 31 | |
| 32 base::win::PEImage pe_image(module_handle); | |
|
Sigurður Ásgeirsson
2013/12/17 14:52:37
PeImageAsDatafile, given the above.
Cait (Slow)
2013/12/18 20:59:03
Done.
| |
| 33 PIMAGE_IMPORT_DESCRIPTOR image_descriptor = pe_image.GetFirstImportChunk(); | |
| 34 | |
| 35 // The list of Import Address Tables ends with an empty {0} descriptor. | |
| 36 while (image_descriptor->Characteristics) { | |
|
Sigurður Ásgeirsson
2013/12/17 14:52:37
You can also do this by using one of the PEImage e
Cait (Slow)
2013/12/18 20:59:03
Done.
| |
| 37 DWORD name = | |
| 38 reinterpret_cast<DWORD>(module_handle) + image_descriptor->Name; | |
|
Sigurður Ásgeirsson
2013/12/17 14:52:37
This is what RVAToAddr does.
Cait (Slow)
2013/12/18 20:59:03
Not needed anymore with EnumImportChunks
| |
| 39 imports->push_back(reinterpret_cast<char*>(name)); | |
| 40 image_descriptor += sizeof(image_descriptor) / sizeof(uintptr_t); | |
| 41 } | |
| 42 } | |
| 43 }; | |
| 44 | |
| 45 TEST_F(ELFImportsTest, ChromeElfSanityCheck) { | |
| 46 std::vector<std::string> elf_imports; | |
| 47 GetImports(L"chrome_elf.dll", &elf_imports); | |
| 48 | |
| 49 // Check that ELF has imports. | |
| 50 ASSERT_TRUE(elf_imports.size() > 0); | |
|
Sigurður Ásgeirsson
2013/12/17 14:52:37
ASSERT_LE or GE
Cait (Slow)
2013/12/18 20:59:03
Done.
| |
| 51 | |
| 52 std::vector<std::string>::iterator it = elf_imports.begin(); | |
| 53 | |
| 54 // Make sure all of ELF's imports are in are valid imports list. | |
| 55 for (; it != elf_imports.end(); it++) { | |
| 56 if (!MatchPattern(*it, kMSVC) && !MatchPattern(*it, kKernel32)) | |
|
robertshield
2013/12/17 04:02:03
elf is now (or about to) import a few functions fr
Cait (Slow)
2013/12/18 20:59:03
Done.
| |
| 57 FAIL() << "Illegal import in chrome_elf.dll."; | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 TEST_F(ELFImportsTest, ChromeExeSanityCheck) { | |
| 62 std::vector<std::string> exe_imports; | |
| 63 GetImports(L"chrome.exe", &exe_imports); | |
|
Sigurður Ásgeirsson
2013/12/17 14:52:37
oooh, I don't know what happens when you LoadLibra
Cait (Slow)
2013/12/18 20:59:03
Switched to loading the file using CreateFile (as
| |
| 64 | |
| 65 // Check that chrome.exe has imports. | |
| 66 ASSERT_TRUE(exe_imports.size() > 0); | |
|
Sigurður Ásgeirsson
2013/12/17 14:52:37
ASSERT_GE or LE?
Cait (Slow)
2013/12/18 20:59:03
Done.
| |
| 67 | |
| 68 // Chrome.exe's first import must be ELF. | |
| 69 EXPECT_EQ("chrome_elf.dll", exe_imports[0]); | |
| 70 } | |
| 71 | |
| 72 } // namespace | |
| OLD | NEW |