From f26ebf2f8fb1a484a6bbc0455d00d69219e923ce Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Mon, 9 Feb 2026 12:16:02 -0500 Subject: [PATCH] Handle BadImageFormatException in OpenAssociatedSymbolFile Catch BadImageFormatException from PEReader.ReadCodeViewDebugDirectoryData and skip malformed CodeView entries instead of crashing. This is consistent with how other PDB loading failures are handled in the same method. Fixes https://github.com/dotnet/dotnet/issues/4733 --- .../tools/Common/Compiler/CompilerTypeSystemContext.cs | 10 +++++++++- src/coreclr/tools/dotnet-pgo/TraceTypeSystemContext.cs | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/coreclr/tools/Common/Compiler/CompilerTypeSystemContext.cs b/src/coreclr/tools/Common/Compiler/CompilerTypeSystemContext.cs index db8ad465dc2fe3..f7f7baf2c8158d 100644 --- a/src/coreclr/tools/Common/Compiler/CompilerTypeSystemContext.cs +++ b/src/coreclr/tools/Common/Compiler/CompilerTypeSystemContext.cs @@ -328,7 +328,15 @@ private PdbSymbolReader OpenAssociatedSymbolFile(string peFilePath, PEReader peR if (debugEntry.Type != DebugDirectoryEntryType.CodeView) continue; - CodeViewDebugDirectoryData debugDirectoryData = peReader.ReadCodeViewDebugDirectoryData(debugEntry); + CodeViewDebugDirectoryData debugDirectoryData; + try + { + debugDirectoryData = peReader.ReadCodeViewDebugDirectoryData(debugEntry); + } + catch (BadImageFormatException) + { + continue; + } string candidatePath = debugDirectoryData.Path; if (!Path.IsPathRooted(candidatePath) || !File.Exists(candidatePath)) diff --git a/src/coreclr/tools/dotnet-pgo/TraceTypeSystemContext.cs b/src/coreclr/tools/dotnet-pgo/TraceTypeSystemContext.cs index 33e11d92d023b1..3d90930c8aad3f 100644 --- a/src/coreclr/tools/dotnet-pgo/TraceTypeSystemContext.cs +++ b/src/coreclr/tools/dotnet-pgo/TraceTypeSystemContext.cs @@ -369,7 +369,15 @@ private PdbSymbolReader OpenAssociatedSymbolFile(string peFilePath, PEReader peR if (debugEntry.Type != DebugDirectoryEntryType.CodeView) continue; - CodeViewDebugDirectoryData debugDirectoryData = peReader.ReadCodeViewDebugDirectoryData(debugEntry); + CodeViewDebugDirectoryData debugDirectoryData; + try + { + debugDirectoryData = peReader.ReadCodeViewDebugDirectoryData(debugEntry); + } + catch (BadImageFormatException) + { + continue; + } string candidatePath = debugDirectoryData.Path; if (!Path.IsPathRooted(candidatePath) || !File.Exists(candidatePath))