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