add a local variable to methods

Jul 25, 2009 at 3:55 PM

Hi,

I want to add a local variable to existing methods through CCI, can you do me a favor to give me some advice? Thanks a million!

I will appreciate it if you can give me sample code according to the following small example:

public int hello(){

...

Console.WriteLine("hello!");

...

}

and I want to mutate it into :

public int hello(){

bool added=true;

...

Console.WriteLine("hello!");

...

}

 

 

 

Coordinator
Jul 25, 2009 at 6:24 PM

Use the PeToPe sample as a starting point, using your own subclass of CodeMutator as the mutator. Override the method Visit(BlockStatement). Inside that method check that this is the first block of a method and if so, insert a new LocalDeclarationsStatement object as the first statement in the block. You can do the check by looking if this.path(ths.path.Length-2) is an ISourceMethodBody.

Jul 26, 2009 at 1:48 AM
Hi, hermanv~
I tried this, but I got stunted on assigning type and name to the new LocalDefinition object. Can you implement the following code for me? (add a local variable, such as"bool added=true") Thanks very much
LocalDeclarationStatement localDeclare = new LocalDeclarationStatement();

LocalDefinition localDefinition = new LocalDefinition();
localDefinition.Name=?
localDefinition.Type =?
localDeclare.LocalVariable = localDefinition;
CompileTimeConstant initialValue=new CompileTimeConstant();
initialValue.Value=?
initialValue.Type=?
localDeclare.InitialValue=initialValue;
block.Statements.Add(localDeclare);
2009-07-26

zlmdunk

发件人: hermanv
发送时间: 2009-07-26 02:24:53
收件人: zlmdunk@126.com
抄送:
主题: Re: add a local variable to methods [cciast:63517]

From: hermanv

Use the PeToPe sample as a starting point, using your own subclass of CodeMutator as the mutator. Override the method Visit(BlockStatement). Inside that method check that this is the first block of a method and if so, insert a new LocalDeclarationsStatement object as the first statement in the block. You can do the check by looking if this.path(ths.path.Length-2) is an ISourceMethodBody.

Coordinator
Jul 26, 2009 at 6:29 AM

localDefinition.Name = this.host.NameTable.GetNameFor("added");

localDefinition.Type = this.host.PlatformType.SystemBoolean;

initialValue.Value = new CompileTimeConstant() { Value=true, Type=this.host.PlatformType.SystemBoolean };

initialValue.Type = initialValue.Value.Type;

Jul 26, 2009 at 10:17 AM
Thanks very much, I really appreciate your help!
I am Lingming Zhang from Peking University, China. It's nice to know you
2009-07-26

zlmdunk

发件人: hermanv
发送时间: 2009-07-26 14:29:56
收件人: zlmdunk@126.com
抄送:
主题: Re: add a local variable to methods [cciast:63517]

From: hermanv

localDefinition.Name = this.host.NameTable.GetNameFor("added");

localDefinition.Type = this.host.PlatformType.SystemBoolean;

initialValue.Value = new CompileTimeConstant() { Value=true, Type=this.host.PlatformType.SystemBoolean };

initialValue.Type = initialValue.Value.Type;

Jul 29, 2009 at 1:48 PM
Hi, hermanv
I got stunted when I managed to add an exception throw statement into pe files. Can you complete the following code for me? Thanks in advance~
to be added: throw new Exception("exception!!");
the instrumentation code:


MethodReference methodRef =new MethodReference();
TODO
CreateObjectInstance newException = new CreateObjectInstance();
newException.MethodToCall = methodRef;
ThrowStatement throwStatement = new ThrowStatement();
throwStatement.Exception = newException;
Coordinator
Jul 30, 2009 at 12:53 AM

var methodRef = new Microsoft.Cci.MethodReference(this.host, this.host.MyPlatformType.SystemException, CallingConvention.Default, this.host.PlatformType.SystemVoid, this.host.NameTable.Ctor, 0, this.host.PlatformType.SystemString);

where this.host.MyPlatform is a property you need to add to your host class. This property should return an instance of a subclass of Microsoft.Cci.PlatformType. In this subclass add a property called SystemException, using the base class properties as an example.

Jul 30, 2009 at 6:20 AM
Thanks very much, I get it done correctly under your guidance, Thanks a million!!
Sep 12, 2009 at 11:57 AM
Hi, Hermanv
Sorry for bother you again~
I get stunted when trying to fetch a field of a instance of a class, for instance:
assume that the code we want to instrument is: "Class1 c1=new Class1(); c1.field1=...".
the instrumentation code is:
LocalDefinition addedLocal = new LocalDefinition();
addedLocal.Type = Class1;
...//construct a instance for Class1
Then, how can I get the field of the instance (c1.field1)? (suppose that we get the field name)
Thanks very much
2009-09-12

zlmdunk

发件人: hermanv
发送时间: 2009-07-30 08:53:29
收件人: zlmdunk@126.com
抄送:
主题: Re: add a local variable to methods [cciast:63517]

From: hermanv

var methodRef = new Microsoft.Cci.MethodReference(this.host, this.host.MyPlatformType.SystemException, CallingConvention.Default, this.host.PlatformType.SystemVoid, this.host.NameTable.Ctor, 0, this.host.PlatformType.SystemString);

where this.host.MyPlatform is a property you need to add to your host class. This property should return an instance of a subclass of Microsoft.Cci.PlatformType. In this subclass add a property called SystemException, using the base class properties as an example.

Coordinator
Sep 13, 2009 at 5:17 PM

Use a bound expression to bind together the expression for the instance and the definition for the field.

One way to quickly learn how to do this is to run the decompiler against compiled C# code and see what it does. The PeToText sample can help you with that.

Sep 14, 2009 at 1:12 AM
Thanks for your help
2009-09-14

zlmdunk

发件人: hermanv
发送时间: 2009-09-14 01:17:13
收件人: zlmdunk@126.com
抄送:
主题: Re: add a local variable to methods [cciast:63517]

From: hermanv

Use a bound expression to bind together the expression for the instance and the definition for the field.

One way to quickly learn how to do this is to run the decompiler against compiled C# code and see what it does. The PeToText sample can help you with that.