Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions llvm/lib/DebugInfo/PDB/Native/NativeSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ Error NativeSession::createFromPdb(std::unique_ptr<MemoryBuffer> Buffer,
return Error::success();
}

static Error validatePdbMagic(StringRef PdbPath) {
file_magic Magic;
if (auto EC = identify_magic(PdbPath, Magic))
return make_error<RawError>(EC);

if (Magic != file_magic::pdb)
return make_error<RawError>(
raw_error_code::invalid_format,
"The input file did not contain the pdb file magic.");

return Error::success();
}

static Expected<std::unique_ptr<PDBFile>>
loadPdbFile(StringRef PdbPath, std::unique_ptr<BumpPtrAllocator> &Allocator) {
ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
Expand All @@ -97,10 +110,8 @@ loadPdbFile(StringRef PdbPath, std::unique_ptr<BumpPtrAllocator> &Allocator) {
std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer);

PdbPath = Buffer->getBufferIdentifier();
file_magic Magic;
auto EC = identify_magic(PdbPath, Magic);
if (EC || Magic != file_magic::pdb)
return make_error<RawError>(EC);
if (auto EC = validatePdbMagic(PdbPath))
return std::move(EC);

auto Stream = std::make_unique<MemoryBufferByteStream>(
std::move(Buffer), llvm::endianness::little);
Expand Down Expand Up @@ -152,10 +163,8 @@ Error NativeSession::createFromExe(StringRef ExePath,
if (!PdbPath)
return PdbPath.takeError();

file_magic Magic;
auto EC = identify_magic(PdbPath.get(), Magic);
if (EC || Magic != file_magic::pdb)
return make_error<RawError>(EC);
if (auto EC = validatePdbMagic(PdbPath.get()))
return EC;

auto Allocator = std::make_unique<BumpPtrAllocator>();
auto File = loadPdbFile(PdbPath.get(), Allocator);
Expand Down
12 changes: 12 additions & 0 deletions llvm/unittests/DebugInfo/PDB/NativeSessionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ TEST(NativeSessionTest, TestCreateFromExe) {
ASSERT_THAT_ERROR(std::move(E), Succeeded());
}

TEST(NativeSessionTest, TestInvalidPdbMagicError) {
SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
llvm::sys::path::append(InputsDir, "SimpleTest.cpp");
std::string CppPath{InputsDir};
std::unique_ptr<IPDBSession> S;

Error E = NativeSession::createFromPdbPath(CppPath, S);
const char *FormatErr = "The record is in an unexpected format. "
"The input file did not contain the pdb file magic.";
ASSERT_THAT_ERROR(std::move(E), FailedWithMessage(FormatErr));
}

TEST(NativeSessionTest, TestSetLoadAddress) {
std::unique_ptr<IPDBSession> S;
Error E = pdb::loadDataForEXE(PDB_ReaderType::Native, getExePath(), S);
Expand Down