Fix for IndexOutOfRangeException in ILReader.ReadOpCode() (#81)

* Allow testing internals

* Add ILReaderTests

* Skip inline operands in IL
This commit is contained in:
Aristarkh Zagorodnikov 2019-01-29 18:09:08 +03:00 committed by Ben Adams
parent cdf53b2655
commit adef3aa266
4 changed files with 97 additions and 1 deletions

View File

@ -27,4 +27,9 @@
</PackageReference>
</ItemGroup>
<ItemGroup Label="TestInternalsVisibleTo">
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>$(AssemblyName).Test, PublicKey=00240000048000009400000006020000002400005253413100040000010001005532489e147c0de0c5872048d20f7acf99d172a599d217950eba8fdbd1f98fa5ac47901b076d2bd7da8d436e6b5d6292694902e9748514bb0c3b17e6a0e0386f22447847c1c5cd9e034f79a8fe1c120a12785f7f79617414e63861cf13d6fd1cbb4211b87202c6a52c1e22962a6bd310413c37ca440fad14ab8422707517fbae</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>

View File

@ -41,6 +41,7 @@ namespace System.Diagnostics.Internal
MemberInfo ReadOperand(OpCode code, MethodBase methodInfo)
{
MetadataToken = 0;
int inlineLength;
switch (code.OperandType)
{
case OperandType.InlineMethod:
@ -64,7 +65,47 @@ namespace System.Diagnostics.Internal
// Can return System.ArgumentException : Token xxx is not a valid MemberInfo token in the scope of module xxx.dll
return null;
}
case OperandType.InlineNone:
inlineLength = 0;
break;
case OperandType.ShortInlineBrTarget:
case OperandType.ShortInlineVar:
inlineLength = 1;
break;
case OperandType.InlineVar:
inlineLength = 2;
break;
case OperandType.InlineBrTarget:
case OperandType.InlineField:
case OperandType.InlineI:
case OperandType.InlineString:
case OperandType.InlineSig:
case OperandType.InlineSwitch:
case OperandType.InlineTok:
case OperandType.InlineType:
case OperandType.ShortInlineR:
inlineLength = 4;
break;
case OperandType.InlineI8:
case OperandType.InlineR:
inlineLength = 8;
break;
default:
// Can return System.ArgumentException : Unexpected operand type xxx
return null;
}
for (var i = 0; i < inlineLength; i++)
{
ReadByte();
}
return null;
}

View File

@ -2,7 +2,8 @@
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\src\Ben.Demystifier\key.snk</AssemblyOriginatorKeyFile>
<IsPackable>false</IsPackable>
</PropertyGroup>

View File

@ -0,0 +1,49 @@
using System.Diagnostics.Internal;
using Xunit;
namespace Ben.Demystifier.Test
{
public class ILReaderTests
{
public static TheoryData<byte[]> InlineCilSamples =>
new TheoryData<byte[]>
{
// https://github.com/benaadams/Ben.Demystifier/issues/56#issuecomment-366490463
{ new byte[] {
2, 123, 209, 5, 0, 4, 20, 254, 1, 114, 193, 103, 1, 112, 40, 160, 22, 0, 6,
2, 115, 183, 10, 0, 10, 125, 210, 5, 0, 4, 2, 123, 212, 5, 0, 4, 2, 123, 211,
5, 0, 4, 40, 221, 15, 0, 6, 44, 68, 2, 123, 212, 5, 0, 4, 111, 103, 17, 0, 6, 2,
111, 222, 9, 0, 6, 2, 40, 184, 10, 0, 10, 2, 254, 6, 249, 15, 0, 6, 115, 185, 10,
0, 10, 2, 123, 210, 5, 0, 4, 111, 186, 10, 0, 10, 22, 40, 101, 6, 0, 10, 111, 221,
0, 0, 43, 40, 188, 10, 0, 10, 125, 209, 5, 0, 4, 42, 2, 123, 212, 5, 0, 4, 111,
103, 17, 0, 6, 111, 216, 9, 0, 6, 2, 123, 211, 5, 0, 4, 111, 166, 14, 0, 6, 111,
125, 16, 0, 6, 254, 1, 22, 254, 1, 114, 235, 103, 1, 112, 40, 160, 22, 0, 6, 114,
160, 104, 1, 112, 40, 210, 0, 0, 10, 114, 194, 5, 0, 112, 40, 221, 0, 0, 10, 44, 51,
2, 40, 184, 10, 0, 10, 2, 254, 6, 250, 15, 0, 6, 115, 185, 10, 0, 10, 2, 123, 210,
5, 0, 4, 111, 186, 10, 0, 10, 22, 40, 196, 21, 0, 6, 111, 221, 0, 0, 43, 40, 188,
10, 0, 10, 125, 209, 5, 0, 4, 42, 2, 40, 184, 10, 0, 10, 2, 254, 6, 251, 15, 0, 6,
115, 185, 10, 0, 10, 2, 123, 210, 5, 0, 4, 111, 186, 10, 0, 10, 24, 40, 101, 6, 0,
10, 111, 221, 0, 0, 43, 40, 188, 10, 0, 10, 125, 209, 5, 0, 4, 42
} },
// https://github.com/benaadams/Ben.Demystifier/issues/56#issuecomment-390654651
{ new byte[] {
115, 31, 5, 0, 6, 37, 2, 125, 94, 1, 0, 4, 37, 3, 125, 91, 1, 0, 4, 37, 4, 125, 92,
1, 0, 4, 37, 5, 125, 93, 1, 0, 4, 37, 123, 91, 1, 0, 4, 40, 61, 0, 0, 10, 44, 16,
40, 160, 4, 0, 6, 114, 253, 15, 0, 112, 115, 90, 0, 0, 10, 122, 254, 6, 32, 5, 0,
6, 115, 137, 2, 0, 10, 115, 61, 2, 0, 6, 42
} },
};
// https://github.com/benaadams/Ben.Demystifier/issues/56
[Theory, MemberData(nameof(InlineCilSamples))]
public void ReadsInlinedOpcodes(byte[] cil)
{
var sut = new ILReader(cil);
while (sut.Read(GetType().GetMethod(nameof(ReadsInlinedOpcodes))))
{
}
}
}
}