JetBrains / JetBrains/Refasmer

Invalid mock assembly produced

Open
#19 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug help wanted
Dominant language
C#
Stars
94
Forks
14
PR merge metrics
No merged PRs in 30d

Description

I'm generating mock assemblies and then inspecting them with ILSpy. I noticed that in some cases the generated assembly contains methods that can't be processed with ILSpy. I'm unsure if the problem lies in ILSpy or Refasmer. If I generate reference assemblies instead of the mocks, then I'm not facing the issue. Also, not all methods are impacted by the problem in a mock assembly.

The below commands

refasmer /usr/local/share/dotnet/packs/Microsoft.NETCore.App.Ref/7.0.2/ref/net7.0/System.Runtime.dll -v -c -O=out -p -n -m
ilspycmd -o X out/System.Runtime.dll

produce an exception:

 Processing 1 assemblies
 Processing /usr/local/share/dotnet/packs/Microsoft.NETCore.App.Ref/7.0.2/ref/net7.0/System.Runtime.dll
  [System.Runtime.dll] Using custom entity filter
 All done
Error decompiling @06000001 Microsoft.CodeAnalysis.EmbeddedAttribute..ctor
in assembly "out/System.Runtime.dll"
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'methodReference')
   at ICSharpCode.Decompiler.TypeSystem.MetadataModule.ResolveMethod(EntityHandle methodReference, GenericContext context) in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler\TypeSystem\MetadataModule.cs:line 421
   at ICSharpCode.Decompiler.IL.ILReader.DecodeCall(OpCode opCode) in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler\IL\ILReader.cs:line 1517
   at ICSharpCode.Decompiler.IL.ILReader.DecodeInstruction() in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler\IL\ILReader.cs:line 926
   at ICSharpCode.Decompiler.IL.ILReader.ReadInstructions(CancellationToken cancellationToken) in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler\IL\ILReader.cs:line 443
   at ICSharpCode.Decompiler.IL.ILReader.ReadIL(MethodDefinitionHandle method, MethodBodyBlock body, GenericContext genericContext, ILFunctionKind kind, CancellationToken cancellationToken) in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler\IL\ILReader.cs:line 580
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.DecompileBody(IMethod method, EntityDeclaration entityDecl, DecompileRun decompileRun, ITypeResolveContext decompilationContext) in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler\CSharp\CSharpDecompiler.cs:line 1542
-- continuing with outer exception (ICSharpCode.Decompiler.DecompilerException) --
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.DecompileBody(IMethod method, EntityDeclaration entityDecl, DecompileRun decompileRun, ITypeResolveContext decompilationContext) in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler\CSharp\CSharpDecompiler.cs:line 1604
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.DoDecompile(IMethod method, DecompileRun decompileRun, ITypeResolveContext decompilationContext) in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler\CSharp\CSharpDecompiler.cs:line 1478
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.DoDecompile(ITypeDefinition typeDef, DecompileRun decompileRun, ITypeResolveContext decompilationContext) in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler\CSharp\CSharpDecompiler.cs:line 1318
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.DoDecompileTypes(IEnumerable`1 types, DecompileRun decompileRun, ITypeResolveContext decompilationContext, SyntaxTree syntaxTree) in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler\CSharp\CSharpDecompiler.cs:line 574
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.DecompileWholeModuleAsSingleFile(Boolean sortTypes) in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler\CSharp\CSharpDecompiler.cs:line 610
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.DecompileWholeModuleAsSingleFile() in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler\CSharp\CSharpDecompiler.cs:line 584
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.DecompileWholeModuleAsString() in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler\CSharp\CSharpDecompiler.cs:line 866
   at ICSharpCode.Decompiler.Console.ILSpyCmdProgram.Decompile(String assemblyFileName, TextWriter output, String typeName) in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler.Console\IlspyCmdProgram.cs:line 234
   at ICSharpCode.Decompiler.Console.ILSpyCmdProgram.OnExecute(CommandLineApplication app) in D:\GitWorkspace\ILSpy_72\ICSharpCode.Decompiler.Console\IlspyCmdProgram.cs:line 153

From the below two commands, only the latter one is failing, so not all types are impacted:

ilspycmd out/System.Runtime.dll -t System.Security.Cryptography.CryptographicException
ilspycmd out/System.Runtime.dll -t Microsoft.CodeAnalysis.EmbeddedAttribute

Removing the -m works without any problem:

refasmer /usr/local/share/dotnet/packs/Microsoft.NETCore.App.Ref/7.0.2/ref/net7.0/System.Runtime.dll -v -c -O=out -p -n
ilspycmd -o X out/System.Runtime.dll

I'm running the above on MacOS. The input DLL is the dotnet 7 reference assembly System.Runtime.dll.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the refasmer command with and without the -m option, then compare the generated out/System.Runtime.dll files. Use ilspycmd on Microsoft.CodeAnalysis.EmbeddedAttribute and System.Security.Cryptography.CryptographicException to isolate the failing method and verify whether the emitted mock assembly is valid. Done means the -m output can be processed without the reported ILSpy exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.