Skip to content

Commit 9814b1b

Browse files
ivdiazsaSeeker186
andauthored
Fix unnecessary failure when Crossgen'ing dll's without proper Debug Directory data (#73824)
* Added a check to allow DLL's with bad debug directory sizes to be used by Crossgen2. Instead of failing unnecessarily, it now emits a warning. * Implemented the suggested PEReader extension method, instead of dealing with exceptions and regional issues. * Removed unnecessary 'using' statement. * Fixed the remaining 'unsafe' Debug Directory reading points within this codebase. * Added a missing 'const' modifier for a constant value. Co-authored-by: Seeker186 <101211595+Seeker186@users.noreply.github.com>
1 parent 62d1d42 commit 9814b1b

File tree

8 files changed

+39
-8
lines changed

8 files changed

+39
-8
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ private EcmaModule AddModule(string filePath, string expectedSimpleName, bool us
200200
throw new NotSupportedException($"Error: C++/CLI is not supported: '{filePath}'");
201201
#endif
202202

203-
pdbReader = PortablePdbSymbolReader.TryOpenEmbedded(peReader, GetMetadataStringDecoder()) ?? OpenAssociatedSymbolFile(filePath, peReader);
203+
pdbReader = PortablePdbSymbolReader.TryOpenEmbedded(peReader, GetMetadataStringDecoder())
204+
?? OpenAssociatedSymbolFile(filePath, peReader);
204205
}
205206
else
206207
{
@@ -327,7 +328,7 @@ private PdbSymbolReader OpenAssociatedSymbolFile(string peFilePath, PEReader peR
327328
string pdbFileName = null;
328329
BlobContentId pdbContentId = default;
329330

330-
foreach (DebugDirectoryEntry debugEntry in peReader.ReadDebugDirectory())
331+
foreach (DebugDirectoryEntry debugEntry in peReader.SafeReadDebugDirectory())
331332
{
332333
if (debugEntry.Type != DebugDirectoryEntryType.CodeView)
333334
continue;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Immutable;
5+
using System.Reflection.PortableExecutable;
6+
7+
namespace Internal.TypeSystem
8+
{
9+
public static class PortableExecutableMethodExtensions
10+
{
11+
public static ImmutableArray<DebugDirectoryEntry> SafeReadDebugDirectory(this PEReader peReader)
12+
{
13+
int actualDbgDirSize = peReader.PEHeaders.PEHeader.DebugTableDirectory.Size;
14+
15+
// This comes from the Size property of the DebugDirectoryEntry class.
16+
const int expectedDbgDirSizeBase = 28;
17+
18+
if (actualDbgDirSize % expectedDbgDirSizeBase != 0)
19+
return ImmutableArray<DebugDirectoryEntry>.Empty;
20+
21+
return peReader.ReadDebugDirectory();
22+
}
23+
}
24+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static PdbSymbolReader TryOpen(string pdbFilename, MetadataStringDecoder
8181

8282
public static PdbSymbolReader TryOpenEmbedded(PEReader peReader, MetadataStringDecoder stringDecoder)
8383
{
84-
foreach (DebugDirectoryEntry debugEntry in peReader.ReadDebugDirectory())
84+
foreach (DebugDirectoryEntry debugEntry in peReader.SafeReadDebugDirectory())
8585
{
8686
if (debugEntry.Type != DebugDirectoryEntryType.EmbeddedPortablePdb)
8787
continue;

src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/DebugDirectoryEntryNode.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Reflection.PortableExecutable;
99

1010
using Internal.Text;
11+
using Internal.TypeSystem;
1112
using Internal.TypeSystem.Ecma;
1213
using System.IO;
1314
using System.Collections.Immutable;
@@ -250,7 +251,7 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
250251
return new ObjectData(Array.Empty<byte>(), Array.Empty<Relocation>(), 1, new ISymbolDefinitionNode[] { this });
251252
}
252253

253-
ImmutableArray<DebugDirectoryEntry> entries = _module.PEReader.ReadDebugDirectory();
254+
ImmutableArray<DebugDirectoryEntry> entries = _module.PEReader.SafeReadDebugDirectory();
254255
Debug.Assert(entries != null && _debugEntryIndex < entries.Length);
255256

256257
DebugDirectoryEntry sourceDebugEntry = entries[_debugEntryIndex];

src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/DebugDirectoryNode.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ int GetNumDebugDirectoryEntriesInModule()
8888
if (_module == null)
8989
return 0;
9090

91-
ImmutableArray<DebugDirectoryEntry> entries = _module.PEReader.ReadDebugDirectory();
91+
ImmutableArray<DebugDirectoryEntry> entries = _module.PEReader.SafeReadDebugDirectory();
9292
return entries == null ? 0 : entries.Length;
9393
}
9494

@@ -99,8 +99,9 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
9999
builder.AddSymbol(this);
100100

101101
ImmutableArray<DebugDirectoryEntry> entries = ImmutableArray<DebugDirectoryEntry>.Empty;
102+
102103
if (_module != null)
103-
entries = _module.PEReader.ReadDebugDirectory();
104+
entries = _module.PEReader.SafeReadDebugDirectory();
104105

105106
int numEntries = GetNumDebugDirectoryEntriesInModule();
106107

src/coreclr/tools/aot/ILCompiler.TypeSystem/ILCompiler.TypeSystem.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<Version>1.4.0</Version>
3232
</PackageReference>
3333
</ItemGroup>
34+
3435
<ItemGroup>
3536
<Compile Include="..\..\Common\TypeSystem\Common\ArrayMethod.Diagnostic.cs">
3637
<Link>TypeSystem\Common\ArrayMethod.Diagnostic.cs</Link>
@@ -359,6 +360,9 @@
359360
<Compile Include="..\..\Common\TypeSystem\Common\RuntimeInterfacesAlgorithm.cs">
360361
<Link>TypeSystem\Common\RuntimeInterfacesAlgorithm.cs</Link>
361362
</Compile>
363+
<Compile Include="..\..\Common\TypeSystem\Common\PortableExecutableMethodExtensions.cs">
364+
<Link>Compiler\PortableExecutableMethodExtensions.cs</Link>
365+
</Compile>
362366
<Compile Include="..\..\Common\TypeSystem\Ecma\CustomAttributeTypeProvider.cs">
363367
<Link>Ecma\CustomAttributeTypeProvider.cs</Link>
364368
</Compile>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ static int InnerProcessTraceFileMain(CommandLineOptions commandLineOptions)
12991299
bool matched = false;
13001300
bool mismatch = false;
13011301
bool mismatchHandled = false;
1302-
foreach (var debugEntry in ecmaModule.PEReader.ReadDebugDirectory())
1302+
foreach (DebugDirectoryEntry debugEntry in ecmaModule.PEReader.SafeReadDebugDirectory())
13031303
{
13041304
if (debugEntry.Type == DebugDirectoryEntryType.CodeView)
13051305
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ private PdbSymbolReader OpenAssociatedSymbolFile(string peFilePath, PEReader peR
364364
string pdbFileName = null;
365365
BlobContentId pdbContentId = default;
366366

367-
foreach (DebugDirectoryEntry debugEntry in peReader.ReadDebugDirectory())
367+
foreach (DebugDirectoryEntry debugEntry in peReader.SafeReadDebugDirectory())
368368
{
369369
if (debugEntry.Type != DebugDirectoryEntryType.CodeView)
370370
continue;

0 commit comments

Comments
 (0)