[mono-cecil] System.BadImageFormatException thrown form pdb writer

481 views
Skip to first unread message

Jan

unread,
May 17, 2010, 1:17:03 PM5/17/10
to mono-cecil
Hi,

I am stucked with a weird problem. If I write the assembly with my
command line tool everything is fine but if I fire the write
operations as part of my visual studio addin I get this exception:

ClassLibrary11\obj\Debug\ClassLibrary11.dll(-1, 1): error:OpenAccess
Error: System.BadImageFormatException: Index not found. (Exception
from HRESULT: 0x80131124)
at Mono.Cecil.Pdb.ISymUnmanagedWriter2.CloseMethod()
at Mono.Cecil.Pdb.PdbWriter.Write(MethodBody body) in D:\jbevain-
cecil-0.9.2-14-gbb02d3b\jbevain-cecil-bb02d3b\symbols\pdb
\Mono.Cecil.Pdb\PdbWriter.cs:line 64
at
Mono.Cecil.Cil.CodeWriter.WriteResolvedMethodBody(MethodDefinition
method) in D:\jbevain-cecil-0.9.2-14-gbb02d3b\jbevain-cecil-bb02d3b
\Mono.Cecil.Cil\CodeWriter.cs:line 126
at Mono.Cecil.Cil.CodeWriter.WriteMethodBody(MethodDefinition
method) in D:\jbevain-cecil-0.9.2-14-gbb02d3b\jbevain-cecil-bb02d3b
\Mono.Cecil.Cil\CodeWriter.cs:line 68
at Mono.Cecil.MetadataBuilder.AddMethod(MethodDefinition method) in
D:\jbevain-cecil-0.9.2-14-gbb02d3b\jbevain-cecil-bb02d3b\Mono.Cecil
\AssemblyWriter.cs:line 1446
at Mono.Cecil.MetadataBuilder.AddMethods(TypeDefinition type) in D:
\jbevain-cecil-0.9.2-14-gbb02d3b\jbevain-cecil-bb02d3b\Mono.Cecil
\AssemblyWriter.cs:line 1440
at Mono.Cecil.MetadataBuilder.AddType(TypeDefinition type) in D:
\jbevain-cecil-0.9.2-14-gbb02d3b\jbevain-cecil-bb02d3b\Mono.Cecil
\AssemblyWriter.cs:line 1273
at Mono.Cecil.MetadataBuilder.AddTypeDefs() in D:\jbevain-
cecil-0.9.2-14-gbb02d3b\jbevain-cecil-bb02d3b\Mono.Cecil
\AssemblyWriter.cs:line 1246
at Mono.Cecil.MetadataBuilder.BuildTypes() in D:\jbevain-
cecil-0.9.2-14-gbb02d3b\jbevain-cecil-bb02d3b\Mono.Cecil
\AssemblyWriter.cs:line 1103
at Mono.Cecil.MetadataBuilder.BuildModule() in D:\jbevain-
cecil-0.9.2-14-gbb02d3b\jbevain-cecil-bb02d3b\Mono.Cecil
\AssemblyWriter.cs:line 887
at Mono.Cecil.ModuleWriter.<BuildMetadata>b__0(MetadataBuilder
builder, MetadataReader _) in D:\jbevain-cecil-0.9.2-14-gbb02d3b
\jbevain-cecil-bb02d3b\Mono.Cecil\AssemblyWriter.cs:line 132
at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func`3
read) in D:\jbevain-cecil-0.9.2-14-gbb02d3b\jbevain-cecil-bb02d3b
\Mono.Cecil\ModuleDefinition.cs:line 553
at Mono.Cecil.ModuleWriter.BuildMetadata(ModuleDefinition module,
MetadataBuilder metadata) in D:\jbevain-cecil-0.9.2-14-gbb02d3b
\jbevain-cecil-bb02d3b\Mono.Cecil\AssemblyWriter.cs:line 131
at Mono.Cecil.ModuleWriter.WriteModuleTo(ModuleDefinition module,
Stream stream, WriterParameters parameters) in D:\jbevain-
cecil-0.9.2-14-gbb02d3b\jbevain-cecil-bb02d3b\Mono.Cecil
\AssemblyWriter.cs:line 107
at Mono.Cecil.ModuleDefinition.Write(Stream stream,
WriterParameters parameters) in D:\jbevain-cecil-0.9.2-14-gbb02d3b
\jbevain-cecil-bb02d3b\Mono.Cecil\ModuleDefinition.cs:line 720
at Mono.Cecil.ModuleDefinition.Write(String fileName,
WriterParameters parameters) in D:\jbevain-cecil-0.9.2-14-gbb02d3b
\jbevain-cecil-bb02d3b\Mono.Cecil\ModuleDefinition.cs:line 709
at Telerik.OpenAccess.Sdk.Enhancer.CecilEnhancer.SaveAssembly() in
D:\views\tfs\Main\Source\Product\Telerik.OpenAccess.SDK\Enhancer
\CecilEnhancer.cs:line 168
at Telerik.OpenAccess.Sdk.Enhancer.CecilEnhancer.ProcessAssembly()
in D:\views\tfs\Main\Source\Product\Telerik.OpenAccess.SDK\Enhancer
\CecilEnhancer.cs:line 49


More info: VS2010 32 bit .net 4.0 vm, if I configure the same for my
exe it works. Can that be a rights problem?

Jan

--
--
mono-cecil

Jb Evain

unread,
May 18, 2010, 6:35:41 AM5/18/10
to mono-...@googlegroups.com
On Mon, May 17, 2010 at 7:17 PM, Jan <jan.ble...@gmail.com> wrote:
> More info: VS2010 32 bit .net 4.0 vm, if I configure the same for my
> exe it works. Can that be a rights problem?

It's possible. VS sometimes hold locks on files. But the exception
seems to say that the pdb writer is closing an unopen method, or that
something went from when writing a method information.

Do you have a small repro?


--
Jb Evain <j...@nurv.fr>

--
--
mono-cecil

Jan

unread,
May 18, 2010, 10:57:14 AM5/18/10
to mono-cecil


On 18 Mai, 12:35, Jb Evain <j...@nurv.fr> wrote:
> On Mon, May 17, 2010 at 7:17 PM, Jan <jan.blessen...@gmail.com> wrote:
> > More info: VS2010 32 bit .net 4.0 vm, if I configure the same for my
> > exe it works. Can that be a rights problem?
>
> It's possible. VS sometimes hold locks on files. But the exception
> seems to say that the pdb writer is closing an unopen method, or that
> something went from when writing a method information.
>
> Do you have a small repro?

I have no repro because it only happens inside the vm hosted inside
vs2010. I was not able to debug the unmanaged code to get more info.
The file that I am trying to write is not there. And I was able to
write the assembly itself, only problems with the pdb file.

In this error case you are also locking the pdb file on the drive. Can
I get a try..finally like this:

sealed class ModuleWriter {

public static void WriteModuleTo (ModuleDefinition module, Stream
stream, WriterParameters parameters)
{
if ((module.Attributes & ModuleAttributes.ILOnly) == 0)
throw new ArgumentException ();

if (module.HasImage && module.ReadingMode == ReadingMode.Deferred)
ImmediateModuleReader.ReadModule (module);

module.MetadataSystem.Clear ();

var fq_name = stream.GetFullyQualifiedName ();
var symbol_writer_provider = parameters.SymbolWriterProvider;
if (symbol_writer_provider == null && parameters.WriteSymbols)
symbol_writer_provider = SymbolProvider.GetPlatformWriterProvider
();
var symbol_writer = GetSymbolWriter (module, fq_name,
symbol_writer_provider);
#if !SILVERLIGHT && !CF
if (parameters.StrongNameKeyPair != null && module.assembly !=
null) {
var name = module.assembly.Name;
name.PublicKey = parameters.StrongNameKeyPair.PublicKey;
name.HasPublicKey = true;
module.Attributes |= ModuleAttributes.StrongNameSigned;
}
#endif
var metadata = new MetadataBuilder (module, fq_name,
symbol_writer_provider, symbol_writer);

try
{
BuildMetadata(module, metadata);

if (module.SymbolReader != null)
module.SymbolReader.Dispose();

var writer = ImageWriter.CreateWriter(module,
metadata, stream);

writer.WriteImage();

#if !SILVERLIGHT && !CF
if (parameters.StrongNameKeyPair != null)
CryptoService.StrongName(stream, writer,
parameters.StrongNameKeyPair);
#endif
}
finally
{
if (symbol_writer != null)
symbol_writer.Dispose();
}
}


I will try to debug more.

Jan

--
--
mono-cecil

Jan

unread,
May 18, 2010, 12:07:26 PM5/18/10
to mono-cecil
Now I habe a new stack trace, really looks like a security problem:

> mscorlib.dll!System.AccessViolationException.AccessViolationException() Line 25 C#
ntdll.dll!ExecuteHandler2@20() + 0x26 bytes
ntdll.dll!ExecuteHandler@20() + 0x24 bytes
ntdll.dll!_KiUserExceptionDispatcher@8() + 0xf bytes
[Managed to Native Transition]
Mono.Cecil.Pdb.dll!Mono.Cecil.Pdb.SymWriter.CloseMethod() Line 103
C#

Has anybody an idea?

Jan

--
--
mono-cecil

Jb Evain

unread,
May 18, 2010, 12:10:53 PM5/18/10
to mono-...@googlegroups.com
Hey,
Well, VS could very well have a lock on the pdb, and maybe you're
writing over it?

Jan

unread,
May 18, 2010, 12:13:09 PM5/18/10
to mono-cecil


On 18 Mai, 18:10, Jb Evain <j...@nurv.fr> wrote:
> Hey,
>
> On Tue, May 18, 2010 at 6:07 PM, Jan <jan.blessen...@gmail.com> wrote:
> > Now I habe a new stack trace, really looks like a security problem:
>
> >>       mscorlib.dll!System.AccessViolationException.AccessViolationException() Line 25 C#
> >        ntdll.dll!ExecuteHandler2@20()  + 0x26 bytes
> >        ntdll.dll!ExecuteHandler@20()  + 0x24 bytes
> >        ntdll.dll!_KiUserExceptionDispatcher@8()  + 0xf bytes
> >        [Managed to Native Transition]
> >        Mono.Cecil.Pdb.dll!Mono.Cecil.Pdb.SymWriter.CloseMethod() Line 103
> > C#
>
> > Has anybody an idea?
>
> Well, VS could very well have a lock on the pdb, and maybe you're
> writing over it?

No, I am writing a new file, no overwrite. My feeling is that the
managed to native transition is not allowed. Which assembly are you
accessing here?

Jan

--
--
mono-cecil

Jb Evain

unread,
May 18, 2010, 12:16:52 PM5/18/10
to mono-...@googlegroups.com
On Tue, May 18, 2010 at 6:13 PM, Jan <jan.ble...@gmail.com> wrote:
>> >>       mscorlib.dll!System.AccessViolationException.AccessViolationException() Line 25 C#
>> >        ntdll.dll!ExecuteHandler2@20()  + 0x26 bytes
>> >        ntdll.dll!ExecuteHandler@20()  + 0x24 bytes
>> >        ntdll.dll!_KiUserExceptionDispatcher@8()  + 0xf bytes
>> >        [Managed to Native Transition]
>> >        Mono.Cecil.Pdb.dll!Mono.Cecil.Pdb.SymWriter.CloseMethod() Line 103
>> > C#
>>
>> > Has anybody an idea?
>>
>> Well, VS could very well have a lock on the pdb, and maybe you're
>> writing over it?
>
> No, I am writing a new file, no overwrite. My feeling is that the
> managed to native transition is not allowed. Which assembly are you
> accessing here?

No assembly beside Mono.Cecil.Pdb. But CloseMethod is a COM call.

Jan

unread,
May 18, 2010, 12:47:52 PM5/18/10
to mono-cecil
There is a new version of diasymreader.dll, only the Visual Studio is
loading version 10. Maybe they changed the interface that you are
using:

diasymreader.dll C:\Windows\Microsoft.NET\Framework
\v2.0.50727\diasymreader.dll N/A N/A Exports loaded. 63
8.0.50727.4927 (NetFXspW7.050727-4900) 04.06.2009 09:24
05200000-0528D000* [5028] venhance.exe: Native


diasymreader.dll C:\Windows\Microsoft.NET\Framework
\v4.0.30319\diasymreader.dll N/A N/A Exports loaded. 202 10.0.30319.1
built by: RTMRel 18.03.2010 14:10 570D0000-5717A000 [5312] devenv.exe:
Native

Jan

--
--
mono-cecil

Jan

unread,
May 18, 2010, 1:40:43 PM5/18/10
to mono-cecil


On 18 Mai, 18:47, Jan <jan.blessen...@gmail.com> wrote:
> There is a new version of diasymreader.dll, only the Visual Studio is
> loading version 10.

No, also not true, I have replaced the the dll and if VS2010 loads
version 8 it is also not working.

Jan

--
--
mono-cecil

Jan

unread,
May 20, 2010, 8:45:14 AM5/20/10
to mono-cecil
Hi JB,

I was able to make a small example that shows the problem. It occurs
also inside the new msbuild, if I touch an existing instruction. I
have sent you the example via mail.

Jan

--
--
mono-cecil

Jb Evain

unread,
May 20, 2010, 12:03:42 PM5/20/10
to mono-...@googlegroups.com
Hey Jan,

On Thu, May 20, 2010 at 2:45 PM, Jan <jan.ble...@gmail.com> wrote:
> I was able to make a small example that shows the problem. It occurs
> also inside the new msbuild, if I touch an existing instruction. I
> have sent you the example via mail.

Could you try with the latest from github?

Jan

unread,
May 20, 2010, 2:04:22 PM5/20/10
to mono-cecil
Hi JB
> Could you try with the latest from github?

This did not fix my problem. I have just added the default ctor to
class1 in the classlibrary project and I can see the problem again.

Jan

--
--
mono-cecil

Jb Evain

unread,
May 20, 2010, 2:27:42 PM5/20/10
to mono-...@googlegroups.com
On Thu, May 20, 2010 at 8:04 PM, Jan <jan.ble...@gmail.com> wrote:
> This did not fix my problem. I have just added the default ctor to
> class1 in the classlibrary project and I can see the problem again.

At least it happens with or without the body being loaded.

Jan

unread,
May 20, 2010, 2:50:47 PM5/20/10
to mono-cecil


On 20 Mai, 20:27, Jb Evain <j...@nurv.fr> wrote:
> On Thu, May 20, 2010 at 8:04 PM, Jan <jan.blessen...@gmail.com> wrote:
> > This did not fix my problem. I have just added the default ctor to
> > class1 in the classlibrary project and I can see the problem again.
>
> At least it happens with or without the body being loaded.

The body must be loaded, if you comment out line 39 in
CecilProblem2Task.cs the error is not showing up

//var instructions = method.Body.Instructions;

Jan

--
--
mono-cecil

Jb Evain

unread,
May 20, 2010, 2:56:20 PM5/20/10
to mono-...@googlegroups.com
On Thu, May 20, 2010 at 8:50 PM, Jan <jan.ble...@gmail.com> wrote:
> The body must be loaded, if you comment out line 39 in
> CecilProblem2Task.cs the error is not showing up

With your repro, if I add a default constructor to the Class1, the
error shows up regardless of the body being loaded or not.

Jan

unread,
May 20, 2010, 3:00:57 PM5/20/10
to mono-cecil
> With your repro, if I add a default constructor to the Class1, the
> error shows up regardless of the body being loaded or not.

Ups, you are right,

Jan

--
--
mono-cecil

Jb Evain

unread,
May 20, 2010, 3:44:02 PM5/20/10
to mono-...@googlegroups.com
On Thu, May 20, 2010 at 9:00 PM, Jan <jan.ble...@gmail.com> wrote:
>> With your repro, if I add a default constructor to the Class1, the
>> error shows up regardless of the body being loaded or not.

Here's a stripped out version of the repro that doesn't require Cecil,
in case someone has more COM fu than I have.
CecilProblem.zip

Jan

unread,
May 28, 2010, 10:49:01 AM5/28/10
to mono-cecil
Hi JB,

This is already a loooong thread, I am very happy that you have
managed to fix that problem.

Thanks man,

Jan
Reply all
Reply to author
Forward
0 new messages