From 958fb74f57c659928a728567fc9cdddadb27feed Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Mon, 29 Jan 2018 22:24:29 +0100 Subject: [PATCH] Fix #1048: Incorrect decompilation of code that creates multi-dimensional generic arrays. --- .../TestCases/Pretty/Generics.cs | 10 +++++ .../TestCases/Pretty/Generics.il | 45 ++++++++++++++++--- .../TestCases/Pretty/Generics.opt.il | 33 +++++++++++--- .../TestCases/Pretty/Generics.opt.roslyn.il | 29 ++++++++++-- .../TestCases/Pretty/Generics.roslyn.il | 41 +++++++++++++++-- .../Implementation/DefaultResolvedMethod.cs | 2 + 6 files changed, 144 insertions(+), 16 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.cs index e4da79407..b8b3be8d3 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.cs @@ -50,5 +50,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty { return t == null; } + + public T[] NewArray(int size) + { + return new T[size]; + } + + public T[,] NewArray(int size1, int size2) + { + return new T[size1, size2]; + } } } diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.il index 35cd026a6..ec77c5fe2 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.il +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.il @@ -1,5 +1,5 @@ -// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.17929 +// Microsoft (R) .NET Framework IL Disassembler. Version 4.6.1055.0 // Copyright (c) Microsoft Corporation. All rights reserved. @@ -10,7 +10,7 @@ .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. .ver 4:0:0:0 } -.assembly lb3mdocq +.assembly xjme13tr { .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx @@ -20,15 +20,15 @@ .hash algorithm 0x00008004 .ver 0:0:0:0 } -.module lb3mdocq.dll -// MVID: {4412C112-CBEB-40EB-BC42-5C82526C8657} +.module xjme13tr.dll +// MVID: {46E33A68-F4FB-4A9A-BF39-6043E66E8EF6} .custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) .imagebase 0x10000000 .file alignment 0x00000200 .stackreserve 0x00100000 .subsystem 0x0003 // WINDOWS_CUI .corflags 0x00000001 // ILONLY -// Image base: 0x03060000 +// Image base: 0x04990000 // =============== CLASS MEMBERS DECLARATION =================== @@ -153,6 +153,41 @@ IL_000e: ret } // end of method Generics::IsNull + .method public hidebysig instance !!T[] + NewArray(int32 size) cil managed + { + // Code size 12 (0xc) + .maxstack 1 + .locals init (!!T[] V_0) + IL_0000: nop + IL_0001: ldarg.1 + IL_0002: newarr !!T + IL_0007: stloc.0 + IL_0008: br.s IL_000a + + IL_000a: ldloc.0 + IL_000b: ret + } // end of method Generics::NewArray + + .method public hidebysig instance !!T[0...,0...] + NewArray(int32 size1, + int32 size2) cil managed + { + // Code size 13 (0xd) + .maxstack 2 + .locals init (!!T[0...,0...] V_0) + IL_0000: nop + IL_0001: ldarg.1 + IL_0002: ldarg.2 + IL_0003: newobj instance void !!T[0...,0...]::.ctor(int32, + int32) + IL_0008: stloc.0 + IL_0009: br.s IL_000b + + IL_000b: ldloc.0 + IL_000c: ret + } // end of method Generics::NewArray + .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.opt.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.opt.il index 9f5537892..e743e9945 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.opt.il +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.opt.il @@ -1,5 +1,5 @@ -// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.17929 +// Microsoft (R) .NET Framework IL Disassembler. Version 4.6.1055.0 // Copyright (c) Microsoft Corporation. All rights reserved. @@ -10,7 +10,7 @@ .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. .ver 4:0:0:0 } -.assembly sxsfxc4c +.assembly '43fwrtdk' { .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx @@ -20,15 +20,15 @@ .hash algorithm 0x00008004 .ver 0:0:0:0 } -.module sxsfxc4c.dll -// MVID: {4BDFEFB1-623B-4D1C-B489-AE8EC4D00CF2} +.module '43fwrtdk.dll' +// MVID: {9D3BB32A-C2A7-4153-BBA3-FDE1FA41BC2E} .custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) .imagebase 0x10000000 .file alignment 0x00000200 .stackreserve 0x00100000 .subsystem 0x0003 // WINDOWS_CUI .corflags 0x00000001 // ILONLY -// Image base: 0x02FA0000 +// Image base: 0x03A50000 // =============== CLASS MEMBERS DECLARATION =================== @@ -134,6 +134,29 @@ IL_0009: ret } // end of method Generics::IsNull + .method public hidebysig instance !!T[] + NewArray(int32 size) cil managed + { + // Code size 7 (0x7) + .maxstack 8 + IL_0000: ldarg.1 + IL_0001: newarr !!T + IL_0006: ret + } // end of method Generics::NewArray + + .method public hidebysig instance !!T[0...,0...] + NewArray(int32 size1, + int32 size2) cil managed + { + // Code size 8 (0x8) + .maxstack 8 + IL_0000: ldarg.1 + IL_0001: ldarg.2 + IL_0002: newobj instance void !!T[0...,0...]::.ctor(int32, + int32) + IL_0007: ret + } // end of method Generics::NewArray + .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.opt.roslyn.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.opt.roslyn.il index e52e38966..6691faba4 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.opt.roslyn.il +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.opt.roslyn.il @@ -1,5 +1,5 @@ -// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.17929 +// Microsoft (R) .NET Framework IL Disassembler. Version 4.6.1055.0 // Copyright (c) Microsoft Corporation. All rights reserved. @@ -25,14 +25,14 @@ .ver 0:0:0:0 } .module Generics.dll -// MVID: {6515FC2D-ED0B-41CF-9FD2-8CD5192CF5A1} +// MVID: {3BDC6EEC-5C6F-4665-B558-CFF1997F2B0C} .custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) .imagebase 0x10000000 .file alignment 0x00000200 .stackreserve 0x00100000 .subsystem 0x0003 // WINDOWS_CUI .corflags 0x00000001 // ILONLY -// Image base: 0x01880000 +// Image base: 0x03340000 // =============== CLASS MEMBERS DECLARATION =================== @@ -125,6 +125,29 @@ IL_0009: ret } // end of method Generics::IsNull + .method public hidebysig instance !!T[] + NewArray(int32 size) cil managed + { + // Code size 7 (0x7) + .maxstack 8 + IL_0000: ldarg.1 + IL_0001: newarr !!T + IL_0006: ret + } // end of method Generics::NewArray + + .method public hidebysig instance !!T[0...,0...] + NewArray(int32 size1, + int32 size2) cil managed + { + // Code size 8 (0x8) + .maxstack 8 + IL_0000: ldarg.1 + IL_0001: ldarg.2 + IL_0002: newobj instance void !!T[0...,0...]::.ctor(int32, + int32) + IL_0007: ret + } // end of method Generics::NewArray + .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.roslyn.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.roslyn.il index 2fcbc212e..d7b3ad497 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.roslyn.il +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.roslyn.il @@ -1,5 +1,5 @@ -// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.17929 +// Microsoft (R) .NET Framework IL Disassembler. Version 4.6.1055.0 // Copyright (c) Microsoft Corporation. All rights reserved. @@ -25,14 +25,14 @@ .ver 0:0:0:0 } .module Generics.dll -// MVID: {9BC41AF0-27DC-4022-8C9C-4D7039122350} +// MVID: {90883852-759A-4D56-85B7-2BCDAEFC00E9} .custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) .imagebase 0x10000000 .file alignment 0x00000200 .stackreserve 0x00100000 .subsystem 0x0003 // WINDOWS_CUI .corflags 0x00000001 // ILONLY -// Image base: 0x03030000 +// Image base: 0x02FD0000 // =============== CLASS MEMBERS DECLARATION =================== @@ -147,6 +147,41 @@ IL_000e: ret } // end of method Generics::IsNull + .method public hidebysig instance !!T[] + NewArray(int32 size) cil managed + { + // Code size 12 (0xc) + .maxstack 1 + .locals init (!!T[] V_0) + IL_0000: nop + IL_0001: ldarg.1 + IL_0002: newarr !!T + IL_0007: stloc.0 + IL_0008: br.s IL_000a + + IL_000a: ldloc.0 + IL_000b: ret + } // end of method Generics::NewArray + + .method public hidebysig instance !!T[0...,0...] + NewArray(int32 size1, + int32 size2) cil managed + { + // Code size 13 (0xd) + .maxstack 2 + .locals init (!!T[0...,0...] V_0) + IL_0000: nop + IL_0001: ldarg.1 + IL_0002: ldarg.2 + IL_0003: newobj instance void !!T[0...,0...]::.ctor(int32, + int32) + IL_0008: stloc.0 + IL_0009: br.s IL_000b + + IL_000b: ldloc.0 + IL_000c: ret + } // end of method Generics::NewArray + .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { diff --git a/ICSharpCode.Decompiler/TypeSystem/Implementation/DefaultResolvedMethod.cs b/ICSharpCode.Decompiler/TypeSystem/Implementation/DefaultResolvedMethod.cs index a0eb3006e..7fd1426f3 100644 --- a/ICSharpCode.Decompiler/TypeSystem/Implementation/DefaultResolvedMethod.cs +++ b/ICSharpCode.Decompiler/TypeSystem/Implementation/DefaultResolvedMethod.cs @@ -250,6 +250,8 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation { if (TypeParameterSubstitution.Identity.Equals(substitution)) return this; + if (DeclaringType is ArrayType) + return new SpecializedMethod(this, substitution); if (TypeParameters.Count == 0) { if (DeclaringTypeDefinition == null || DeclaringTypeDefinition.TypeParameterCount == 0) return this;