How to check if one function calls another function

An application which I’m currently developing has quite complicated authorization system. That is why, we can not use role based authorization, and basically every developer is obliged to call appropriate security check method in every controller action he or she writes. As You probably know it is quite easy to forget about that, therefore I decided to write a test which would check whether all controller’s action invokes this security critical function. After hours of searching for some anchor point, I came across this article in which author creates so called “reflection based ILReader”. ILReader is a class which allows You to iterate over IL instructions of every object of type RuntimeMethodInfo or RuntimeConstructorInfo .One of these instructions is InlineMethodInstruction which indicates method call. That is why, in order to get all methods which are called by given method, we have to simply select all instructions of type InlineMethodInstruction

We can go a bit further and create a recursive version of this function to iterate over entire function execution chain

Please notice, that I limited recursion depth to methods from user defined namespace, otherwise we would end up iterating over .NET’s native functions.
Now it is time to show some results. Here is a sample program which demonstrate possibilities of MethodInvokerCrawler class

calls another function
At this point checking whether function or constructor calls another function is limited to simple LINQ query

Source code for this post can be found here

How to check if one function calls another function

2 thoughts on “How to check if one function calls another function

  1. That's a nice find. I remember when I write some code by hand to get out call chain of the method. This has very limited usages however as we simply get the method implementation and it's not possible to get out of that scope (Well it is possible but the code get's funky as it's pointer magic now). So if you have some simple or complex branching logic then this way will fail fast.

    Another thing is that if you need to force some method call on a developer then why not create a tool that simply generates that code in pre-compile time it's much simpler that way, less error prone and less governing work which is a chore for everyone.

    Bartosz Adamczewski

Comments are closed.