xref: /llvm-project/llvm/unittests/DebugInfo/PDB/NativeSessionTest.cpp (revision a6d8a055e92eb4853805d1ad1be0b1a6523524ef)
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   Error E = pdb::loadDataForEXE(PDB_ReaderType::Native, getExePath(), S);
34   ASSERT_THAT_ERROR(std::move(E), Succeeded());
35 }
36 
37 TEST(NativeSessionTest, TestSetLoadAddress) {
38   std::unique_ptr<IPDBSession> S;
39   Error E = pdb::loadDataForEXE(PDB_ReaderType::Native, getExePath(), S);
40   ASSERT_THAT_ERROR(std::move(E), Succeeded());
41 
42   S->setLoadAddress(123);
43   EXPECT_EQ(S->getLoadAddress(), 123U);
44 }
45 
46 TEST(NativeSessionTest, TestAddressForVA) {
47   std::unique_ptr<IPDBSession> S;
48   Error E = pdb::loadDataForEXE(PDB_ReaderType::Native, getExePath(), S);
49   ASSERT_THAT_ERROR(std::move(E), Succeeded());
50 
51   uint64_t LoadAddr = S->getLoadAddress();
52   uint32_t Section;
53   uint32_t Offset;
54   ASSERT_TRUE(S->addressForVA(LoadAddr + 5000, Section, Offset));
55   EXPECT_EQ(1U, Section);
56   EXPECT_EQ(904U, Offset);
57 
58   ASSERT_TRUE(S->addressForVA(-1, Section, Offset));
59   EXPECT_EQ(0U, Section);
60   EXPECT_EQ(0U, Offset);
61 
62   ASSERT_TRUE(S->addressForVA(4, Section, Offset));
63   EXPECT_EQ(0U, Section);
64   EXPECT_EQ(4U, Offset);
65 
66   ASSERT_TRUE(S->addressForVA(LoadAddr + 100000, Section, Offset));
67   EXPECT_EQ(3U, Section);
68   EXPECT_EQ(83616U, Offset);
69 }
70 
71 TEST(NativeSessionTest, TestAddressForRVA) {
72   std::unique_ptr<IPDBSession> S;
73   Error E = pdb::loadDataForEXE(PDB_ReaderType::Native, getExePath(), S);
74   ASSERT_THAT_ERROR(std::move(E), Succeeded());
75 
76   uint32_t Section;
77   uint32_t Offset;
78   ASSERT_TRUE(S->addressForVA(5000, Section, Offset));
79   EXPECT_EQ(1U, Section);
80   EXPECT_EQ(904U, Offset);
81 
82   ASSERT_TRUE(S->addressForVA(-1, Section, Offset));
83   EXPECT_EQ(0U, Section);
84   EXPECT_EQ(0U, Offset);
85 
86   ASSERT_TRUE(S->addressForVA(4, Section, Offset));
87   EXPECT_EQ(0U, Section);
88   EXPECT_EQ(4U, Offset);
89 
90   ASSERT_TRUE(S->addressForVA(100000, Section, Offset));
91   EXPECT_EQ(3U, Section);
92   EXPECT_EQ(83616U, Offset);
93 }
94