Skip to content

Commit

Permalink
Prevent SIGSEGV in managed code.
Browse files Browse the repository at this point in the history
  • Loading branch information
viewizard authored and gbalykov committed Sep 26, 2023
1 parent d1af532 commit 0c959e4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/managed/SymbolReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,12 @@ internal static RetCode GetSource(IntPtr symbolReaderHandle, [MarshalAs(Unmanage
if (docPath == fileName)
{
MemoryStream ms = GetEmbeddedSource(mdReader, handle, out docSize);

// PDB loaded, but don't contain source file lines.
// This is not an error, but also no data to return.
if (docSize == 0)
return RetCode.OK;

data = Marshal.AllocCoTaskMem(docSize);
Marshal.Copy(ms.ToArray(), 0, data, docSize);
length = docSize;
Expand Down
7 changes: 6 additions & 1 deletion src/protocols/cliprotocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1628,7 +1628,12 @@ HRESULT CLIProtocol::doCommand<CommandTag::List>(const std::vector<std::string>
{
for (int i = 0; i < lines; i++, line++)
{
char* toPrint = m_sources->getLine(m_sourcePath, line);
const char* errMessage = nullptr;
char* toPrint = m_sources->getLine(m_sourcePath, line, &errMessage);
if (errMessage)
{
printf("Source code file: %s\n%s\n", m_sourcePath.c_str(), errMessage);
}
if (toPrint)
{
if(line == m_stoppedAt)
Expand Down
18 changes: 14 additions & 4 deletions src/protocols/sourcestorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace netcoredbg
}
}

char* SourceStorage::getLine(std::string& file, int linenum)
char* SourceStorage::getLine(std::string& file, int linenum, const char **errMessage)
{
if(files.empty() || files.front()->filePath != file)
{
Expand All @@ -36,7 +36,7 @@ namespace netcoredbg
if (notFound)
{
// file is not in the list -- try to load it from pdb
if (loadFile(file) != S_OK)
if (loadFile(file, errMessage) != S_OK)
return NULL;
}
}
Expand All @@ -47,13 +47,23 @@ namespace netcoredbg
return NULL;
}

HRESULT SourceStorage::loadFile(std::string& file)
HRESULT SourceStorage::loadFile(std::string& file, const char **errMessage)
{
char* fileBuff = NULL;
int fileLen = 0;
HRESULT Status = S_OK;

IfFailRet(m_dbg->GetSourceFile(file, &fileBuff, &fileLen));
if (FAILED(Status = m_dbg->GetSourceFile(file, &fileBuff, &fileLen)))
{
*errMessage = "Debug information (PDB file) cannot be opened. Check that PDB file exists and is correct.";
return Status;
}
if (fileLen == 0)
{
*errMessage = "Debug information (PDB file) doesn't contain source code. Make sure csproj file has <EmbedAllSources>true</EmbedAllSources> line.";
return E_FAIL;
}

SourceFile *sf = new SourceFile();
sf->filePath = file;
sf->size = fileLen;
Expand Down
4 changes: 2 additions & 2 deletions src/protocols/sourcestorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SourceStorage
int totalLen;

private:
HRESULT loadFile(std::string& file);
HRESULT loadFile(std::string& file, const char **errMessage);

public:
SourceStorage(IDebugger* d)
Expand All @@ -35,7 +35,7 @@ class SourceStorage
}
~SourceStorage();

char* getLine(std::string& file, int linenum);
char* getLine(std::string& file, int linenum, const char **errMessage);

}; // class sourcestorage
} // namespace

0 comments on commit 0c959e4

Please sign in to comment.