Build/Test Issues

Cecil failure during test project build

Started by brunellus on 3,949 views

Hello, NCrunch fails with the following message in one of our test projects:

System.NotSupportedException: Specified method is not supported.
at _Mono.Cecil.MetadataReader.ReadVariables(MetadataToken local_var_token)
at _Mono.Cecil.Cil.CodeReader.ReadFatMethod()
at _Mono.Cecil.Cil.CodeReader.ReadMethodBody()
at _Mono.Cecil.Cil.CodeReader.ReadMethodBody(MethodDefinition method)
at _Mono.Cecil.MethodDefinition.<>c.<get_Body>b__36_0(MethodDefinition method, MetadataReader reader)
at _Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func`3 read)
at _Mono.Cecil.MethodDefinition.get_Body()
at nCrunch.Reflection.Cecil.CecilMethod.get_HasBody()
at nCrunch.TestExecution.Frameworks.DefaultClassMethodDescriptionLocator.ExtractClassMemberDataFromType(ReflectedType type, Boolean hashAllMethods)
at nCrunch.Compiler.StaticManipulation.BuiltAssembly.(ReflectedAssembly , IDictionary`2 , TestFrameworkDescription[] , Boolean )
at nCrunch.Compiler.StaticManipulation.BuiltAssembly.ExtractClassMethodData(TestFrameworkDescription[] applicableTestFrameworks, IDictionary`2 codeFileIDsByFilePath, Boolean hashMethods)
at nCrunch.Compiler.RemoteBuildRunner..()
at nCrunch.Common.PerformanceTracking.PerfTracker.TrackActivity(String name, Action activity)
at nCrunch.Compiler.RemoteBuildRunner.(ComponentBuildParameters , FilePath , BuildOutput , DirectoryPath[] , FilePath[] )
at nCrunch.Compiler.RemoteBuildRunner..()
at nCrunch.Common.PerformanceTracking.PerfTracker.TrackActivity(String name, Action activity)
at nCrunch.Compiler.RemoteBuildRunner.(ComponentBuildParameters )

I wonder what can we do -- how to locate the exact code that is triggering this error, for example.
Hi, thanks for sharing this issue.

This particular exception is being thrown because of a method variable that has an unexpected (invalid?) signature in the assembly's IL. It's possible that this is due to a method in your assembly having an unusual structure, or otherwise it could be a downstream problem from some kind of IL corruption that Cecil is sensitive to but the JIT is not.

Did this problem start appearing suddenly? Or is this the first time you're using NCrunch on this project?

Which version of Visual Studio and .NET are you using?

Are you performing any kind of post-build manipulation of this assembly? (i.e. mocking frameworks, MSFakes, code contracts, Frody, postsharp, etc).
This is something that started to appear with growth of the test project. For the record, we use VS 2015, the test library is for .NET Framework 4.5.2. There is no IL postprocessing, I think.

I wrote this code to test what may be causing it:

var assembly = AssemblyDefinition.ReadAssembly(@"C:\Source\...\test.dll", new ReaderParameters { ReadSymbols = true });
var module = assembly.MainModule;

foreach (var type in module.Types)
{
foreach (var method in type.Methods)
{
try
{
var body = method.Body;
}
catch (NotSupportedException ex)
{
Console.WriteLine($"NotSupported: {method.FullName}");
}
catch (NotImplementedException ex)
{
Console.WriteLine($"NotImplemented: {method.FullName}");
}
}
}

There are lots of methods that throw NotSupported and few that throws NotImplemented. Interestingly, when I gradually remove code from the test project, at one point all the issues disappear, even ones previously reported in code that is still there. Very strange. But, all this happens when I use latest stable Cecil nuget (0.9.6.4), but no exceptions are thrown when running against unstable 0.10.0-beta1-v2. So this is probably an already fixed issue.

I guess I can only hope that you update your Cecil sooner than later after 0.10 stabilizes. :-) This is not critical for us, it affects only one from many projects.
brunellus wrote:This is something that started to appear with growth of the test project. For the record, we use VS 2015, the test library is for .NET Framework 4.5.2. There is no IL postprocessing, I think.
There are lots of methods that throw NotSupported and few that throws NotImplemented. Interestingly, when I gradually remove code from the test project, at one point all the issues disappear, even ones previously reported in code that is still there. Very strange. But, all this happens when I use latest stable Cecil nuget (0.9.6.4), but no exceptions are thrown when running against unstable 0.10.0-beta1-v2. So this is probably an already fixed issue.

I guess I can only hope that you update your Cecil sooner than later after 0.10 stabilizes. :-) This is not critical for us, it affects only one from many projects.


Thanks for taking the effort to narrow this down. I've had a bit of a run through the latest Cecil changes to try and figure out what may be causing this. Unfortunately, the problem doesn't look to be simple :(

I think there is something unexpected/corrupt in this assembly at a fairly fundamental level. Cecil 0.10 has seen 1.5 years of development over the last stable release, so I don't think there is much chance of finding this with a code review. To have any chance of fixing this problem I'll need to get hold of an assembly that can reproduce it. Let me know if you find an isolated way to reproduce the problem or if you're able to share a sample with me that I can work with.

Post a reply

Log in to reply.