Browse Source

Fix #1211: Handle BadImageFormatException on invalid local signatures.

pull/1213/head
Daniel Grunwald 7 years ago
parent
commit
08e8091336
  1. 8
      ICSharpCode.Decompiler/CSharp/RequiredNamespaceCollector.cs
  2. 20
      ICSharpCode.Decompiler/Disassembler/MethodBodyDisassembler.cs
  3. 11
      ICSharpCode.Decompiler/IL/ILReader.cs
  4. 4
      ICSharpCode.Decompiler/TypeSystem/MetadataModule.cs

8
ICSharpCode.Decompiler/CSharp/RequiredNamespaceCollector.cs

@ -176,7 +176,13 @@ namespace ICSharpCode.Decompiler.CSharp @@ -176,7 +176,13 @@ namespace ICSharpCode.Decompiler.CSharp
var instructions = method.GetILReader();
if (!method.LocalSignature.IsNil) {
var localSignature = module.DecodeLocalSignature(method.LocalSignature, genericContext);
ImmutableArray<IType> localSignature;
try {
localSignature = module.DecodeLocalSignature(method.LocalSignature, genericContext);
} catch (BadImageFormatException) {
// Issue #1211: ignore invalid local signatures
localSignature = ImmutableArray<IType>.Empty;
}
foreach (var type in localSignature)
CollectNamespacesForTypeReference(type, namespaces);
}

20
ICSharpCode.Decompiler/Disassembler/MethodBodyDisassembler.cs

@ -18,6 +18,7 @@ @@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using System.Threading;
@ -124,15 +125,16 @@ namespace ICSharpCode.Decompiler.Disassembler @@ -124,15 +125,16 @@ namespace ICSharpCode.Decompiler.Disassembler
if (body.LocalVariablesInitialized)
output.Write(" init");
var blob = metadata.GetStandaloneSignature(body.LocalSignature);
if (blob.GetKind() != StandaloneSignatureKind.LocalVariables)
return;
var reader = metadata.GetBlobReader(blob.Signature);
if (reader.Length < 2)
return;
reader.Offset = 1;
if (reader.ReadCompressedInteger() == 0)
return;
var signature = blob.DecodeLocalSignature(signatureDecoder, genericContext);
var signature = ImmutableArray<Action<ILNameSyntax>>.Empty;
try {
if (blob.GetKind() == StandaloneSignatureKind.LocalVariables) {
signature = blob.DecodeLocalSignature(signatureDecoder, genericContext);
} else {
output.Write(" /* wrong signature kind */");
}
} catch (BadImageFormatException ex) {
output.Write($" /* {ex.Message} */");
}
output.Write(' ');
output.WriteLine("(");
output.Indent();

11
ICSharpCode.Decompiler/IL/ILReader.cs

@ -154,8 +154,15 @@ namespace ICSharpCode.Decompiler.IL @@ -154,8 +154,15 @@ namespace ICSharpCode.Decompiler.IL
ILVariable[] InitLocalVariables()
{
if (body.LocalSignature.IsNil) return Empty<ILVariable>.Array;
var variableTypes = module.DecodeLocalSignature(body.LocalSignature, genericContext);
if (body.LocalSignature.IsNil)
return Empty<ILVariable>.Array;
ImmutableArray<IType> variableTypes;
try {
variableTypes = module.DecodeLocalSignature(body.LocalSignature, genericContext);
} catch (BadImageFormatException ex) {
Warnings.Add("Error decoding local variables: " + ex.Message);
variableTypes = ImmutableArray<IType>.Empty;
}
var localVariables = new ILVariable[variableTypes.Length];
foreach (var (index, type) in variableTypes.WithIndex()) {
localVariables[index] = CreateILVariable(index, type);

4
ICSharpCode.Decompiler/TypeSystem/MetadataModule.cs

@ -555,7 +555,7 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -555,7 +555,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
{
var standaloneSignature = metadata.GetStandaloneSignature(handle);
if (standaloneSignature.GetKind() != StandaloneSignatureKind.Method)
throw new InvalidOperationException("Expected Method signature");
throw new BadImageFormatException("Expected Method signature");
var sig = standaloneSignature.DecodeMethodSignature(TypeProvider, genericContext);
return new MethodSignature<IType>(
sig.Header,
@ -572,7 +572,7 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -572,7 +572,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
{
var standaloneSignature = metadata.GetStandaloneSignature(handle);
if (standaloneSignature.GetKind() != StandaloneSignatureKind.LocalVariables)
throw new InvalidOperationException("Expected Local signature");
throw new BadImageFormatException("Expected LocalVariables signature");
var types = standaloneSignature.DecodeLocalSignature(TypeProvider, genericContext);
return ImmutableArray.CreateRange(types, IntroduceTupleTypes);
}

Loading…
Cancel
Save