editor.Replace(ins,
Instruction.Create(OpCodes.Ldstr, decryptedstring)); // replace with
an ldstr Instruction (works!)

                    // Now loop back from the Replaced call
Instruction in inverse order.
                    // Go from the Replaced call backwards as long as
the intruction before is not a call on.
                    // if its not a call one, then nop the instruction...
                    if (ins.Next.OpCode != OpCodes.Call) // Never is
true even if I replace != with == why is that?
                    {
                        Console.WriteLine("Can be nop'ed"); // This
code is never getting executed... ? (but the isntruction before the
call is
                        // definitly not a call instruction, so it
should normally get nop'ed?
                    }

What you are seeing is likely a side effect of the Replace implementation.

Replace(instruction, target)

is implemented at

InsertAfter(instruction, target)
Remove(instruction)

So it's likely that your instruction.next points to target.

The rule of thumb is that the ILProcessor is likely to fixup the
double linked list that are instructions for you. Do not use the
linked list if you removed an instruction from a method.

In your case I would say you should save ins.Next before calling the
replace, and use it in your test.

Jb


On Fri, Nov 28, 2014 at 11:44 PM, BlackSky Blacksky
<[email protected]> wrote:
> Hello,
> I have a nasty problem which already has given me quiet some headache.
> Before I explain my problem please look at this code:
>
> foreach (Instruction ins in MethodDef.Body.Instructions) // Loop from first 
> Instruction to the last instruction
>             {
>
>                 if (string.Format("{0:X}", ins.Offset).ToString() == 
> currentoffset) // Find my call (works!)
>                 {
>                     editor.Replace(ins, Instruction.Create(OpCodes.Ldstr, 
> decryptedstring)); // replace with an ldstr Instruction (works!)
>
>                     // Now loop back from the Replaced call Instruction in 
> inverse order.
>                     // Go from the Replaced call backwards as long as the 
> intruction before is not a call on.
>                     // if its not a call one, then nop the instruction...
>                     if (ins.Next.OpCode != OpCodes.Call) // Never is true 
> even if I replace != with == why is that?
>                     {
>                         Console.WriteLine("Can be nop'ed"); // This code is 
> never getting executed... ? (but the isntruction before the call is
>                         // definitly not a call instruction, so it should 
> normally get nop'ed?
>                     }
>                 }
>             }
>
>
>
> So what I am doing is looping through the Instructions of a method.
> On a specific call which I identify by the Offset I replace the call with an 
> ldstr instruction. So far so good.
> But now what I want to do (and am not able to get it) is this:
>
> When the call has been replaced with the ldstr I want to start going stepwise 
> back through the instructions.
> Meaning: first i 've looped from the first instruction till my call. Now I 
> want to go back stepwise and every time the opcode before the current one is 
> != Opcodes.Call I want to nop it. Then after nopping I go one more step back 
> and do the same check and hop if its not a call opcode. I want to do that 
> until I hit an CallOpcode.
> Then I want to exit the loop.
>
> So to visualize it it could look like that:
> (Before manipulation)
>
> Call some stuff
> LDC.i4
> Ldc.i4
> Add
> LD.i4
> Sub
> Call my specific method i've identified.
>
>
> So first I loop down and replace my call, which will then look like this:
>
> Call some stuff
> LDC.i4
> Ldc.i4
> Add
> LD.i4
> Sub
> Ldstr - replaced call
>
>
> And now I want to go stepwise up (inverse if the first loop) and hop every 
> opcode till I hit a Call OpCode.
> The result would then look like this:
>
> Call - some stuff
> Nop
> Nop
> Nop
> Nop
> Nop
> Nop
> Ldstr - replaced call.
>
>
>
> I am already trying to do this for a long time now already but I just can't 
> get it .
> Would be very helpful if someone can explain why its not working and maybe 
> show me some code sample or correct my code.
> That would really help me out.
> I hope I've explained my situation so you can understand it. If something is 
> unclear from.my explanation please ask me;)
>
> --
> --
> --
> mono-cecil
> ---
> You received this message because you are subscribed to the Google Groups 
> "mono-cecil" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.

-- 
-- 
--
mono-cecil
--- 
You received this message because you are subscribed to the Google Groups 
"mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to