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