1 : // Copyright 2016 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 : #include "syzygy/refinery/types/unittest_util.h"
16 :
17 : #include <windows.h>
18 :
19 : #include <vector>
20 :
21 : #include "base/scoped_native_library.h"
22 : #include "base/win/scoped_handle.h"
23 : #include "syzygy/core/unittest_util.h"
24 :
25 m : namespace testing {
26 :
27 m : namespace {
28 :
29 m : typedef bool (*GetExpectedVftableVAsPtr)(unsigned buffer_size,
30 m : unsigned long long* vftable_vas,
31 m : unsigned* count);
32 :
33 m : } // namespace
34 :
35 m : void PdbCrawlerVTableTestBase::PerformGetVFTableRVAsTest(
36 m : const wchar_t* pdb_path_str,
37 m : const wchar_t* dll_path_str) {
38 m : DCHECK(pdb_path_str); DCHECK(dll_path_str);
39 :
40 : // Crawl the pdb for vftable RVAs.
41 m : base::hash_set<refinery::Address> vftable_rvas;
42 m : ASSERT_NO_FATAL_FAILURE(GetVFTableRVAs(pdb_path_str, &vftable_rvas));
43 :
44 : // Get the expectation from the dll.
45 m : base::FilePath dll_path = testing::GetSrcRelativePath(dll_path_str);
46 :
47 m : base::ScopedNativeLibrary module(dll_path);
48 m : ASSERT_TRUE(module.is_valid());
49 :
50 m : GetExpectedVftableVAsPtr get_vas = reinterpret_cast<GetExpectedVftableVAsPtr>(
51 m : module.GetFunctionPointer("GetExpectedVftableVAs"));
52 m : ASSERT_TRUE(get_vas != nullptr);
53 :
54 m : unsigned buffer_size = 10U;
55 m : std::vector<uint64_t> vftable_vas;
56 m : vftable_vas.resize(buffer_size);
57 m : unsigned count = 0U;
58 m : ASSERT_TRUE(get_vas(buffer_size, &vftable_vas.at(0), &count));
59 :
60 : // Validate the expectation.
61 m : ASSERT_LE(count, vftable_rvas.size());
62 :
63 m : for (size_t i = 0; i < count; ++i) {
64 m : refinery::Address expected_rva =
65 m : static_cast<refinery::RelativeAddress>(vftable_vas[i]) -
66 m : reinterpret_cast<refinery::RelativeAddress>(module.get());
67 m : EXPECT_NE(vftable_rvas.end(), vftable_rvas.find(expected_rva));
68 m : }
69 m : }
70 :
71 m : } // namespace testing
|