brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.6 KiB · 20ae253 Raw
112 lines · cpp
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-exception6//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 22using namespace llvm;23using namespace llvm::pdb;24 25extern const char *TestMainArgv0;26 27static 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 33TEST(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 43TEST(NativeSessionTest, TestInvalidPdbMagicError) {44  SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);45  llvm::sys::path::append(InputsDir, "SimpleTest.cpp");46  std::string CppPath{InputsDir};47  std::unique_ptr<IPDBSession> S;48 49  Error E = NativeSession::createFromPdbPath(CppPath, S);50  const char *FormatErr = "The record is in an unexpected format. "51                          "The input file did not contain the pdb file magic.";52  ASSERT_THAT_ERROR(std::move(E), FailedWithMessage(FormatErr));53}54 55TEST(NativeSessionTest, TestSetLoadAddress) {56  std::unique_ptr<IPDBSession> S;57  Error E = pdb::loadDataForEXE(PDB_ReaderType::Native, getExePath(), S);58  ASSERT_THAT_ERROR(std::move(E), Succeeded());59 60  S->setLoadAddress(123);61  EXPECT_EQ(S->getLoadAddress(), 123U);62}63 64TEST(NativeSessionTest, TestAddressForVA) {65  std::unique_ptr<IPDBSession> S;66  Error E = pdb::loadDataForEXE(PDB_ReaderType::Native, getExePath(), S);67  ASSERT_THAT_ERROR(std::move(E), Succeeded());68 69  uint64_t LoadAddr = S->getLoadAddress();70  uint32_t Section;71  uint32_t Offset;72  ASSERT_TRUE(S->addressForVA(LoadAddr + 5000, Section, Offset));73  EXPECT_EQ(1U, Section);74  EXPECT_EQ(904U, Offset);75 76  ASSERT_TRUE(S->addressForVA(-1, Section, Offset));77  EXPECT_EQ(0U, Section);78  EXPECT_EQ(0U, Offset);79 80  ASSERT_TRUE(S->addressForVA(4, Section, Offset));81  EXPECT_EQ(0U, Section);82  EXPECT_EQ(4U, Offset);83 84  ASSERT_TRUE(S->addressForVA(LoadAddr + 100000, Section, Offset));85  EXPECT_EQ(3U, Section);86  EXPECT_EQ(83616U, Offset);87}88 89TEST(NativeSessionTest, TestAddressForRVA) {90  std::unique_ptr<IPDBSession> S;91  Error E = pdb::loadDataForEXE(PDB_ReaderType::Native, getExePath(), S);92  ASSERT_THAT_ERROR(std::move(E), Succeeded());93 94  uint32_t Section;95  uint32_t Offset;96  ASSERT_TRUE(S->addressForVA(5000, Section, Offset));97  EXPECT_EQ(1U, Section);98  EXPECT_EQ(904U, Offset);99 100  ASSERT_TRUE(S->addressForVA(-1, Section, Offset));101  EXPECT_EQ(0U, Section);102  EXPECT_EQ(0U, Offset);103 104  ASSERT_TRUE(S->addressForVA(4, Section, Offset));105  EXPECT_EQ(0U, Section);106  EXPECT_EQ(4U, Offset);107 108  ASSERT_TRUE(S->addressForVA(100000, Section, Offset));109  EXPECT_EQ(3U, Section);110  EXPECT_EQ(83616U, Offset);111}112