Problem in PeWriter class in case of a Property in a Type.

May 13, 2009 at 10:41 AM

Hello,

For the following code:

  1. HostEnvironment heHost = new HostEnvironment();
  2. MetadataMutator mutator = new CodeMutator(heHost );
  3. IAssembly vUnit = heHost.LoadUnitFrom(@"C:\...\ClassLibrary1.dll") as IAssembly;
  4. var vMutableAssembly = mutator.Visit(mutator.GetMutableCopy(vUnit));
  5. Stream peStream = File.Create(@"C:\....\ClassLibrary1.dll");
  6. PeWriter.WritePeToStream(vMutableAssembly, heHost , peStream);

 

here ClassLibrary1.dll just has one Class with just one property defined in it.

public int MyProperty { get; set; }

But at step 6 it fails, when I traced I found  that an exception is raised in PeWriter Class in the following method:

private uint GetFieldDefIndex(IFieldDefinition field) {
      return this.fieldDefIndex[field];
    }

saying that "The given key is not present".

I am not sure but something is going wrong n case of the Properties as I am not doing anything tedious just loading one assembly in a simple form and writing it back on a new location, without doing any operations on it. Now if I remove the Property  from my Class and add a field and method instead all is fine, dummy dll is created correctly.

Any Help please..!!!

 

Thanx in advance
Purva.

Coordinator
May 14, 2009 at 5:06 AM

This is most likely a bug in the decompiler. I'll try to find some time tomorrow to see if I can fix it.

May 14, 2009 at 5:55 AM

Hey hi,

Thank you for your quick reply.

 I would like to make some more additions.The above mentioned problem occurs when I use CodeMutator. If MetadataMutator is used instead this problem doesn't occur.

Also could you please help me with some links about the documentation on CCI. This will help me in using it correctly, like in this case I  am using MetadataMutator just because its not giving me that exception. On what basis should I distinguish as to when should I use which mutator and same for other APIs provided. The samples do help but some more of conceptual documentation would be a great help.

 

Thanx in advance.
Purva.

 

 

Coordinator
May 14, 2009 at 7:06 PM
Edited May 14, 2009 at 11:18 PM

The bug is indeed in the CodeMutator. It only manifests when you use the CodeMutator in this slightly unorthodox way. (It is now fixed.)

The main reason for using the CodeMutator is so that you can manipulate the code model, which represents method bodies as statements and expressions, rather than a flat list of IL operations. When you start out with an assembly provided by the the PE reader, you'll need to decompile the IL operations to statements and expressions. The CodeMutator does not have a built-in decompiler, but relies on the constructor to provide it with one. Of course, if you are mutating a code model that has already been decompiled (or that has been provided by compiler and has yet to be compiled to IL), you do not need to provide a decompiler, which is the purpose of the overload that you've selected.

For an example of how to invoke the decompiler, see the PeToPe and PeToText samples.

There isn't much documentation (contributions welcome ;-). What we do have lives in the documentation directory. Look for QuickIntroductionToCCI.docx.