ErrorProne.NET icon indicating copy to clipboard operation
ErrorProne.NET copied to clipboard

[Feature request] Add a warning wnen GetCallingAssembly() is called from inlineable method.

Open ig-sinicyn opened this issue 9 years ago • 0 comments

Explanation, msdn

If the method that calls the GetCallingAssembly method is expanded inline by the just-in-time (JIT) compiler, or if its caller is expanded inline, the assembly that is returned by GetCallingAssembly may differ unexpectedly. For example, consider the following methods and assemblies:

  • Method M1 in assembly A1 calls GetCallingAssembly.
  • Method M2 in assembly A2 calls M1.
  • Method M3 in assembly A3 calls M2.

When M1 is not inlined, GetCallingAssembly returns A2. When M1 is inlined, GetCallingAssembly returns A3. Similarly, when M2 is not inlined, GetCallingAssembly returns A2. When M2 is inlined, GetCallingAssembly returns A3. This effect also occurs when M1 executes as a tail call from M2, or when M2 executes as a tail call from M3. You can prevent the JIT compiler from inlining the method that calls GetCallingAssembly, by applying the MethodImplAttribute attribute with the MethodImplOptions.NoInlining flag, but there is no similar mechanism for preventing tail calls.

Actually there's undocumented trick in the BCL to prevent the behavior described above. Hovewer, I'd prefer to have a warning for code like this.

As far as I can remember the same error is possible when calling the MethodBase.GetCurrentMethod(), calling the StackTrace() constructor with skipFrames arg set, calling the GetExecutingAssembly() and so on.

As a sidenote: the project should be definitely added into awesome-analysers list.

ig-sinicyn avatar Jul 18 '16 19:07 ig-sinicyn