xref: /llvm-project/llvm/unittests/DebugInfo/PDB/NativeSessionTest.cpp (revision eb4c8608115c1c9af0fc8cb5b1e9f2bc960014ef)
1 //===- llvm/unittest/DebugInfo/PDB/NativeSessionTest.cpp ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
10 #include "llvm/DebugInfo/PDB/ConcreteSymbolEnumerator.h"
11 #include "llvm/DebugInfo/PDB/IPDBSession.h"
12 #include "llvm/DebugInfo/PDB/PDB.h"
13 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
14 #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
15 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
16 #include "llvm/Support/Path.h"
17 
18 #include "llvm/Testing/Support/Error.h"
19 
20 #include "gtest/gtest.h"
21 
22 #include <vector>
23 
24 using namespace llvm;
25 using namespace llvm::pdb;
26 
27 extern const char *TestMainArgv0;
28 
getExePath()29 static std::string getExePath() {
30   SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
31   llvm::sys::path::append(InputsDir, "SimpleTest.exe");
32   return std::string(InputsDir);
33 }
34 
TEST(NativeSessionTest,TestCreateFromExe)35 TEST(NativeSessionTest, TestCreateFromExe) {
36   std::unique_ptr<IPDBSession> S;
37   std::string ExePath = getExePath();
38   Expected<std::string> PdbPath = NativeSession::searchForPdb({ExePath});
39   ASSERT_TRUE((bool)PdbPath);
40 
41   Error E = NativeSession::createFromPdbPath(PdbPath.get(), S);
42   ASSERT_THAT_ERROR(std::move(E), Succeeded());
43 }
44 
TEST(NativeSessionTest,TestSetLoadAddress)45 TEST(NativeSessionTest, TestSetLoadAddress) {
46   std::unique_ptr<IPDBSession> S;
47   Error E = pdb::loadDataForEXE(PDB_ReaderType::Native, getExePath(), S);
48   ASSERT_THAT_ERROR(std::move(E), Succeeded());
49 
50   S->setLoadAddress(123);
51   EXPECT_EQ(S->getLoadAddress(), 123U);
52 }
53 
TEST(NativeSessionTest,TestAddressForVA)54 TEST(NativeSessionTest, TestAddressForVA) {
55   std::unique_ptr<IPDBSession> S;
56   Error E = pdb::loadDataForEXE(PDB_ReaderType::Native, getExePath(), S);
57   ASSERT_THAT_ERROR(std::move(E), Succeeded());
58 
59   uint64_t LoadAddr = S->getLoadAddress();
60   uint32_t Section;
61   uint32_t Offset;
62   ASSERT_TRUE(S->addressForVA(LoadAddr + 5000, Section, Offset));
63   EXPECT_EQ(1U, Section);
64   EXPECT_EQ(904U, Offset);
65 
66   ASSERT_TRUE(S->addressForVA(-1, Section, Offset));
67   EXPECT_EQ(0U, Section);
68   EXPECT_EQ(0U, Offset);
69 
70   ASSERT_TRUE(S->addressForVA(4, Section, Offset));
71   EXPECT_EQ(0U, Section);
72   EXPECT_EQ(4U, Offset);
73 
74   ASSERT_TRUE(S->addressForVA(LoadAddr + 100000, Section, Offset));
75   EXPECT_EQ(3U, Section);
76   EXPECT_EQ(83616U, Offset);
77 }
78 
TEST(NativeSessionTest,TestAddressForRVA)79 TEST(NativeSessionTest, TestAddressForRVA) {
80   std::unique_ptr<IPDBSession> S;
81   Error E = pdb::loadDataForEXE(PDB_ReaderType::Native, getExePath(), S);
82   ASSERT_THAT_ERROR(std::move(E), Succeeded());
83 
84   uint32_t Section;
85   uint32_t Offset;
86   ASSERT_TRUE(S->addressForVA(5000, Section, Offset));
87   EXPECT_EQ(1U, Section);
88   EXPECT_EQ(904U, Offset);
89 
90   ASSERT_TRUE(S->addressForVA(-1, Section, Offset));
91   EXPECT_EQ(0U, Section);
92   EXPECT_EQ(0U, Offset);
93 
94   ASSERT_TRUE(S->addressForVA(4, Section, Offset));
95   EXPECT_EQ(0U, Section);
96   EXPECT_EQ(4U, Offset);
97 
98   ASSERT_TRUE(S->addressForVA(100000, Section, Offset));
99   EXPECT_EQ(3U, Section);
100   EXPECT_EQ(83616U, Offset);
101 }
102