1

I hope someone can help me out with my problem. Currently I'm working with C# and use the library Mono.Cecil to add Instruction Code before a method exits. There is this one case, when a if statement is at the end of a method and I want to add TraceUtil.TraceMethodExit(b); at the end, it ends up in the if statement, but it should be outside at the end. I use exactly the same Instructions which the compiler? produces, when I would manualy would put the TraceUtil.TraceMethodExit(b); at the end. But still mine ends up in the if statement body.

enter image description here

but it should look like this:

enter image description here

Here are what the compiler? produces:

IL_0000: nop
IL_0001: ldarg.1
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: brfalse.s IL_0013
IL_0006: nop
IL_0007: ldstr "TEST"
IL_000c: call System.Void UnityEngine.Debug::Log(System.Object)
IL_0011: nop
IL_0012: nop
IL_0013: ldnull
IL_0014: call System.Void SEE.Utils.TraceUtil::TraceMethodExit(System.Object[])
IL_0019: nop
IL_001a: ret

Here are what will be the output when I add the instructions:

IL_0000: nop
IL_0001: ldarg.1
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: brfalse.s IL_001a
IL_0006: nop
IL_0007: ldstr "TEST"
IL_000c: call System.Void UnityEngine.Debug::Log(System.Object)
IL_0011: nop
IL_0012: nop
IL_0013: ldnull
IL_0014: call System.Void SEE.Utils.TraceUtil::TraceMethodExit(System.Object[])
IL_0019: nop
IL_001a: ret

Here is the Code/Method to add the additional instructions:

private static void TraceZeroParameterMethodExit(ILProcessor iLProc, Instruction instr, MethodReference mRef)
        {
            MethodBodyRocks.SimplifyMacros(iLProc.Body);
            iLProc.InsertBefore(instr, Instruction.Create(OpCodes.Ldnull));
            iLProc.InsertBefore(instr, Instruction.Create(OpCodes.Call, mRef));
            iLProc.InsertBefore(instr, Instruction.Create(OpCodes.Nop));
            MethodBodyRocks.OptimizeMacros(iLProc.Body);
        }

Does anyone have a idea how this could be solved? Maybe it has to do with the Offset? Thanks for any help!

5
  • It's not much reversing but show the Mono.Cecil code. It looks like your brfalse.s is not updated and points out incorrectly. – Paweł Łukasik May 9 at 20:02
  • I've added the method which I use to add the instructions. This works well in all cases, where the method doesn't end with a if statement. @PawełŁukasik – programming_is_bae May 9 at 20:17
  • It probably only works for some simple flows. It might fail also for like try-catch. Just rewrite all those br/br.s opcodes to point to your new opcode. I have a video on my channel where I'm showing how something similar can be done. – Paweł Łukasik May 9 at 20:46
  • So I played around a little bit and now the above problem is solved (by letting the br* point at my first instruction) for if statement, but as you stated , not for try-catch. The funny thing is also, that the instructions what the compiler? produces and the instructions after I modified are exactly the same (same instructions, same labels and even the branches are right), but my method call ends up in the catch block and I get a error. Any idea how this could be solved? – programming_is_bae May 9 at 23:19
  • I think you need to fix also the ExceptionHandlers - check what's inside ilProcessor.Body.ExceptionHandlers – Paweł Łukasik May 10 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.