Skip to content

Commit c339b92

Browse files
committed
Fix crash in disassembler and decompiler when HasPInvokeInfo=true but PInvokeInfo=null (occurs with unmanaged methods in C++/CLI assemblies)
1 parent f2c001a commit c339b92

5 files changed

Lines changed: 8 additions & 3 deletions

File tree

ICSharpCode.Decompiler/Ast/AstBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ void ConvertAttributes(AttributedNode attributedNode, MethodDefinition methodDef
11511151
MethodImplAttributes implAttributes = methodDefinition.ImplAttributes & ~MethodImplAttributes.CodeTypeMask;
11521152

11531153
#region DllImportAttribute
1154-
if (methodDefinition.HasPInvokeInfo) {
1154+
if (methodDefinition.HasPInvokeInfo && methodDefinition.PInvokeInfo != null) {
11551155
PInvokeInfo info = methodDefinition.PInvokeInfo;
11561156
Ast.Attribute dllImport = CreateNonCustomAttribute(typeof(DllImportAttribute));
11571157
dllImport.Arguments.Add(new PrimitiveExpression(info.Module.Name));

ICSharpCode.Decompiler/Ast/NameVariables.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ static string GetNameForArgument(ILExpression parent, int i)
284284

285285
string GetNameByType(TypeReference type)
286286
{
287+
type = TypeAnalysis.UnpackModifiers(type);
288+
287289
GenericInstanceType git = type as GenericInstanceType;
288290
if (git != null && git.ElementType.FullName == "System.Nullable`1" && git.GenericArguments.Count == 1) {
289291
type = ((GenericInstanceType)type).GenericArguments[0];

ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void DisassembleMethodInternal(MethodDefinition method)
117117

118118
if ((method.Attributes & MethodAttributes.PInvokeImpl) == MethodAttributes.PInvokeImpl) {
119119
output.Write("pinvokeimpl");
120-
if (method.HasPInvokeInfo) {
120+
if (method.HasPInvokeInfo && method.PInvokeInfo != null) {
121121
PInvokeInfo info = method.PInvokeInfo;
122122
output.Write("(\"" + NRefactory.CSharp.OutputVisitor.ConvertString(info.Module.Name) + "\"");
123123

ICSharpCode.Decompiler/ILAst/TypeAnalysis.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ static TypeReference UnpackPointer(TypeReference pointerOrManagedReference)
849849
return null;
850850
}
851851

852-
static TypeReference UnpackModifiers(TypeReference type)
852+
internal static TypeReference UnpackModifiers(TypeReference type)
853853
{
854854
while (type is OptionalModifierType || type is RequiredModifierType)
855855
type = ((TypeSpecification)type).ElementType;

ILSpy/Languages/CSharpLanguage.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ public override void DecompileAssembly(LoadedAssembly assembly, ITextOutput outp
251251
output.WriteLine("// Architecture: Itanium-64");
252252
break;
253253
}
254+
if ((mainModule.Attributes & ModuleAttributes.ILOnly) == 0) {
255+
output.WriteLine("// This assembly contains unmanaged code.");
256+
}
254257
switch (mainModule.Runtime) {
255258
case TargetRuntime.Net_1_0:
256259
output.WriteLine("// Runtime: .NET 1.0");

0 commit comments

Comments
 (0)