1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (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
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #ifndef SYZYGY_PE_UNITTEST_UTIL_H_
16 : #define SYZYGY_PE_UNITTEST_UTIL_H_
17 :
18 : #include <windows.h>
19 :
20 : #include "base/file_util.h"
21 : #include "base/files/file_path.h"
22 : #include "gtest/gtest.h"
23 : #include "syzygy/block_graph/block_graph.h"
24 : #include "syzygy/common/unittest_util.h"
25 : #include "syzygy/pe/image_layout.h"
26 : #include "syzygy/pe/pe_file.h"
27 :
28 : namespace testing {
29 :
30 : // Name of the test DLL and PDB. These exist in the output directory, and again
31 : // in the test_data directory.
32 : extern const wchar_t kTestDllName[];
33 : extern const wchar_t kTestDllPdbName[];
34 :
35 : // Name of the test DLL object files and the PDB files where their type
36 : // information has been emitted, relative to the test_data directory.
37 : // The two object files are guaranteed to be in the specified format.
38 : extern const wchar_t kTestDllCoffObjName[];
39 : extern const wchar_t kTestDllLtcgObjName[];
40 : extern const wchar_t kTestDllCoffObjPdbName[];
41 : extern const wchar_t kTestDllLtcgObjPdbName[];
42 :
43 : // Statically checked in object files that explore some of the boundary cases
44 : // of COFF files. These are also relative to the test data directory. These live
45 : // in syzygy\pe\test_data, thus should be referred to using GetSrcRelativePath.
46 : extern const wchar_t kMachineTypeNullCoffName[];
47 :
48 : // Name of the various test DLLs generated by the test_data project.
49 : // These are placed in $(OutputDir)\test_data, so use
50 : // GetExeTestDataRelativePath to build paths to them.
51 : extern const wchar_t kAsanInstrumentedTestDllName[];
52 : extern const wchar_t kAsanInstrumentedTestDllPdbName[];
53 : extern const wchar_t kBBEntryInstrumentedTestDllName[];
54 : extern const wchar_t kBBEntryInstrumentedTestDllPdbName[];
55 : extern const wchar_t kCallTraceInstrumentedTestDllName[];
56 : extern const wchar_t kCallTraceInstrumentedTestDllPdbName[];
57 : extern const wchar_t kCoverageInstrumentedTestDllName[];
58 : extern const wchar_t kCoverageInstrumentedTestDllPdbName[];
59 : extern const wchar_t kProfileInstrumentedTestDllName[];
60 : extern const wchar_t kProfileInstrumentedTestDllPdbName[];
61 : extern const wchar_t kRandomizedTestDllName[];
62 : extern const wchar_t kRandomizedTestDllPdbName[];
63 :
64 : // Name of the various trace files.
65 : extern const wchar_t *kBBEntryTraceFiles[4];
66 : extern const wchar_t *kBranchTraceFiles[4];
67 : extern const wchar_t *kCallTraceTraceFiles[4];
68 : extern const wchar_t *kCoverageTraceFiles[4];
69 : extern const wchar_t *kProfileTraceFiles[4];
70 :
71 : // This class wraps an HMODULE and ensures that ::FreeLibrary is called when it
72 : // goes out of scope.
73 : class ScopedHMODULE {
74 : public:
75 : ScopedHMODULE();
76 : explicit ScopedHMODULE(HMODULE v);
77 : ~ScopedHMODULE();
78 :
79 : void Reset(HMODULE v);
80 : void Release();
81 :
82 E : operator HMODULE() const {
83 E : return value_;
84 E : }
85 :
86 : private:
87 : HMODULE value_;
88 : };
89 :
90 : // Given a block-graph that represents a decomposed PE image this reaches in
91 : // and modifies the GUID and path values in place. This prevents any written
92 : // images from matching the same symbol file. If this is not done it is possible
93 : // for the debugger to cause an exception to be raised while loading a symbol
94 : // file whose signature does match, but whose contents don't match, a
95 : // transformed PE file.
96 : // @param dos_header_block The DOS header block.
97 : // @note Meant to be called from within an ASSERT_NO_FATAL_FAILURE block.
98 : void TwiddlePdbGuidAndPath(block_graph::BlockGraph::Block* dos_header_block);
99 :
100 : class PELibUnitTest : public testing::ApplicationTestBase {
101 : public:
102 : // Decomposes test_dll, populating the given PE file and image layout.
103 : static void DecomposeTestDll(bool use_old_decomposer,
104 : pe::PEFile* pe_file,
105 : pe::ImageLayout* image_layout);
106 :
107 : // Performs a series of assertions on the test DLL's integrity.
108 : static void CheckTestDll(const base::FilePath& path);
109 :
110 : // Loads the test DLL and returns its module handle.
111 : static void LoadTestDll(const base::FilePath& path, ScopedHMODULE* module);
112 : };
113 :
114 : } // namespace testing
115 :
116 : #endif // SYZYGY_PE_UNITTEST_UTIL_H_
|