Skip to content

Commit b1750b7

Browse files
Reject mismatched symbol files (#67027)
`UnmanagedPdbSymbolReader` will already perform a GUID check that rejects mismatched symbol files. Do the same for the portable symbol reader.
1 parent 320f473 commit b1750b7

File tree

3 files changed

+48
-39
lines changed

3 files changed

+48
-39
lines changed

src/coreclr/tools/Common/Compiler/CompilerTypeSystemContext.cs

+21-21
Original file line numberDiff line numberDiff line change
@@ -315,39 +315,39 @@ public MetadataStringDecoder GetMetadataStringDecoder()
315315

316316
private PdbSymbolReader OpenAssociatedSymbolFile(string peFilePath, PEReader peReader)
317317
{
318-
// Assume that the .pdb file is next to the binary
319-
var pdbFilename = Path.ChangeExtension(peFilePath, ".pdb");
320-
string searchPath = "";
318+
string pdbFileName = null;
319+
BlobContentId pdbContentId = default;
321320

322-
if (!File.Exists(pdbFilename))
321+
foreach (DebugDirectoryEntry debugEntry in peReader.ReadDebugDirectory())
323322
{
324-
pdbFilename = null;
323+
if (debugEntry.Type != DebugDirectoryEntryType.CodeView)
324+
continue;
325325

326-
// If the file doesn't exist, try the path specified in the CodeView section of the image
327-
foreach (DebugDirectoryEntry debugEntry in peReader.ReadDebugDirectory())
326+
CodeViewDebugDirectoryData debugDirectoryData = peReader.ReadCodeViewDebugDirectoryData(debugEntry);
327+
328+
string candidatePath = debugDirectoryData.Path;
329+
if (!Path.IsPathRooted(candidatePath) || !File.Exists(candidatePath))
328330
{
329-
if (debugEntry.Type != DebugDirectoryEntryType.CodeView)
331+
// Also check next to the PE file
332+
candidatePath = Path.Combine(Path.GetDirectoryName(peFilePath), Path.GetFileName(candidatePath));
333+
if (!File.Exists(candidatePath))
330334
continue;
331-
332-
string candidateFileName = peReader.ReadCodeViewDebugDirectoryData(debugEntry).Path;
333-
if (Path.IsPathRooted(candidateFileName) && File.Exists(candidateFileName))
334-
{
335-
pdbFilename = candidateFileName;
336-
searchPath = Path.GetDirectoryName(pdbFilename);
337-
break;
338-
}
339335
}
340-
341-
if (pdbFilename == null)
342-
return null;
336+
337+
pdbFileName = candidatePath;
338+
pdbContentId = new BlobContentId(debugDirectoryData.Guid, debugEntry.Stamp);
339+
break;
343340
}
344341

342+
if (pdbFileName == null)
343+
return null;
344+
345345
// Try to open the symbol file as portable pdb first
346-
PdbSymbolReader reader = PortablePdbSymbolReader.TryOpen(pdbFilename, GetMetadataStringDecoder());
346+
PdbSymbolReader reader = PortablePdbSymbolReader.TryOpen(pdbFileName, GetMetadataStringDecoder(), pdbContentId);
347347
if (reader == null)
348348
{
349349
// Fallback to the diasymreader for non-portable pdbs
350-
reader = UnmanagedPdbSymbolReader.TryOpenSymbolReaderForMetadataFile(peFilePath, searchPath);
350+
reader = UnmanagedPdbSymbolReader.TryOpenSymbolReaderForMetadataFile(peFilePath, Path.GetDirectoryName(pdbFileName));
351351
}
352352

353353
return reader;

src/coreclr/tools/Common/TypeSystem/Ecma/SymbolReader/PortablePdbSymbolReader.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,20 @@ private static unsafe MetadataReader TryOpenMetadataFile(string filePath, Metada
6262
}
6363
}
6464

65-
public static PdbSymbolReader TryOpen(string pdbFilename, MetadataStringDecoder stringDecoder)
65+
public static PdbSymbolReader TryOpen(string pdbFilename, MetadataStringDecoder stringDecoder, BlobContentId expectedContentId)
6666
{
6767
MemoryMappedViewAccessor mappedViewAccessor;
6868
MetadataReader reader = TryOpenMetadataFile(pdbFilename, stringDecoder, out mappedViewAccessor);
6969
if (reader == null)
7070
return null;
7171

72+
var foundContentId = new BlobContentId(reader.DebugMetadataHeader.Id);
73+
if (foundContentId != expectedContentId)
74+
{
75+
mappedViewAccessor.Dispose();
76+
return null;
77+
}
78+
7279
return new PortablePdbSymbolReader(reader, mappedViewAccessor);
7380
}
7481

src/coreclr/tools/dotnet-pgo/TraceTypeSystemContext.cs

+19-17
Original file line numberDiff line numberDiff line change
@@ -356,33 +356,35 @@ private EcmaModule AddModule(string filePath, string expectedSimpleName, byte[]
356356

357357
private PdbSymbolReader OpenAssociatedSymbolFile(string peFilePath, PEReader peReader)
358358
{
359-
// Assume that the .pdb file is next to the binary
360-
var pdbFilename = Path.ChangeExtension(peFilePath, ".pdb");
359+
string pdbFileName = null;
360+
BlobContentId pdbContentId = default;
361361

362-
if (!File.Exists(pdbFilename))
362+
foreach (DebugDirectoryEntry debugEntry in peReader.ReadDebugDirectory())
363363
{
364-
pdbFilename = null;
364+
if (debugEntry.Type != DebugDirectoryEntryType.CodeView)
365+
continue;
365366

366-
// If the file doesn't exist, try the path specified in the CodeView section of the image
367-
foreach (DebugDirectoryEntry debugEntry in peReader.ReadDebugDirectory())
367+
CodeViewDebugDirectoryData debugDirectoryData = peReader.ReadCodeViewDebugDirectoryData(debugEntry);
368+
369+
string candidatePath = debugDirectoryData.Path;
370+
if (!Path.IsPathRooted(candidatePath) || !File.Exists(candidatePath))
368371
{
369-
if (debugEntry.Type != DebugDirectoryEntryType.CodeView)
372+
// Also check next to the PE file
373+
candidatePath = Path.Combine(Path.GetDirectoryName(peFilePath), Path.GetFileName(candidatePath));
374+
if (!File.Exists(candidatePath))
370375
continue;
371-
372-
string candidateFileName = peReader.ReadCodeViewDebugDirectoryData(debugEntry).Path;
373-
if (Path.IsPathRooted(candidateFileName) && File.Exists(candidateFileName))
374-
{
375-
pdbFilename = candidateFileName;
376-
break;
377-
}
378376
}
379377

380-
if (pdbFilename == null)
381-
return null;
378+
pdbFileName = candidatePath;
379+
pdbContentId = new BlobContentId(debugDirectoryData.Guid, debugEntry.Stamp);
380+
break;
382381
}
383382

383+
if (pdbFileName == null)
384+
return null;
385+
384386
// Try to open the symbol file as portable pdb first
385-
PdbSymbolReader reader = PortablePdbSymbolReader.TryOpen(pdbFilename, GetMetadataStringDecoder());
387+
PdbSymbolReader reader = PortablePdbSymbolReader.TryOpen(pdbFileName, GetMetadataStringDecoder(), pdbContentId);
386388

387389
return reader;
388390
}

0 commit comments

Comments
 (0)