Skip to content

Commit

Permalink
Fix #3247: InvalidOperationException thrown when reading debug metada…
Browse files Browse the repository at this point in the history
…ta files
  • Loading branch information
siegfriedpammer committed Jul 31, 2024
1 parent d1c7a51 commit dbd9632
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions ICSharpCode.Decompiler/Metadata/MetadataFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public enum MetadataFileKind

public virtual int MetadataOffset { get; }
public virtual bool IsEmbedded { get; }
public virtual bool IsMetadataOnly { get; } = true;

public bool IsAssembly => Metadata.IsAssembly;

Expand Down
1 change: 1 addition & 0 deletions ICSharpCode.Decompiler/Metadata/PEFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public PEFile(string fileName, PEReader reader, MetadataReaderOptions metadataOp

public override bool IsEmbedded => false;
public override int MetadataOffset => Reader.PEHeaders.MetadataStartOffset;
public override bool IsMetadataOnly => false;

public void Dispose()
{
Expand Down
1 change: 1 addition & 0 deletions ICSharpCode.Decompiler/Metadata/WebCilFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ static unsafe bool TryReadWebCilSegment(BinaryReader reader, out WebcilHeader we
}

public override int MetadataOffset { get; }
public override bool IsMetadataOnly => false;

private static int GetContainingSectionIndex(IEnumerable<SectionHeader> sections, int rva)
{
Expand Down
4 changes: 2 additions & 2 deletions ICSharpCode.ILSpyX/Analyzers/AnalyzerScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public IEnumerable<MetadataFile> GetAllModules()
{
return assemblyListSnapshot.GetAllAssembliesAsync().GetAwaiter().GetResult()
.Select(asm => asm.GetMetadataFileOrNull())
.Where(x => x != null)!;
.Where(x => x != null && !x.IsMetadataOnly)!;
}

public DecompilerTypeSystem ConstructTypeSystem(MetadataFile module)
Expand Down Expand Up @@ -207,7 +207,7 @@ IEnumerable<MetadataFile> GetModuleAndAnyFriends(ITypeDefinition typeScope, Canc
if (friendAssemblies.Contains(assembly.ShortName))
{
var module = assembly.GetMetadataFileOrNull();
if (module == null)
if (module == null || module.IsMetadataOnly)
continue;
if (ModuleReferencesScopeType(module.Metadata, typeScope.Name, typeScope.Namespace))
yield return module;
Expand Down
4 changes: 2 additions & 2 deletions ICSharpCode.ILSpyX/LoadedAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public async Task<MetadataFile> GetMetadataFileAsync()
{
return LazyInitializer.EnsureInitialized(ref this.typeSystem, () => {
var module = GetMetadataFileOrNull();
if (module == null)
if (module == null || module.IsMetadataOnly)
return null!;
return new SimpleCompilation(
module.WithOptions(TypeSystemOptions.Default | TypeSystemOptions.Uncached | TypeSystemOptions.KeepModifiers),
Expand All @@ -230,7 +230,7 @@ public async Task<MetadataFile> GetMetadataFileAsync()
if (typeSystemWithOptions != null && options == currentTypeSystemOptions)
return typeSystemWithOptions;
var module = GetMetadataFileOrNull();
if (module == null)
if (module == null || module.IsMetadataOnly)
return null;
currentTypeSystemOptions = options;
return typeSystemWithOptions = new SimpleCompilation(
Expand Down
11 changes: 10 additions & 1 deletion ILSpy/TreeNodes/AssemblyTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,16 @@ void HandleException(Exception ex, string message)
var loadResult = LoadedAssembly.GetLoadResultAsync().GetAwaiter().GetResult();
if (loadResult.MetadataFile != null)
{
language.DecompileAssembly(LoadedAssembly, output, options);
switch (loadResult.MetadataFile.Kind)
{
case MetadataFile.MetadataFileKind.ProgramDebugDatabase:
case MetadataFile.MetadataFileKind.Metadata:
output.WriteLine("// " + LoadedAssembly.FileName);
break;
default:
language.DecompileAssembly(LoadedAssembly, output, options);
break;
}
}
else if (loadResult.Package != null)
{
Expand Down

0 comments on commit dbd9632

Please sign in to comment.