It seems when I instrument an assembly using OpenCover, assemblies with the SecurityTransparent attribute (and AllowPartiallyTrustedCallers it seems) will throw a VerificationException. I'd like to know why that is and if there is an alternative solution to recompiling the assembly without those attributes included i.e. conditional compilation, as seen in the downloaded code for MVC3 (but strangely I can't find the same when I browse the repository on codeplex). Note without those assembly attributes the coverage runs without issue.
OpenCover uses a CALLI instruction to send instrumentation data (sequence point identifier) to the profiler. It seems the act of calling this instruction causes the exception to occur; the instrumentation part seems fine and the JIT compiles the new instrumented method without issue. The act of instrumentation doesn't cause issues because if I remove all instrumentation other than make Tiny methods Fat and small branches long the code executes without issue.
Currently I am using Mono.Cecil to examine assemblies that pass the "include-in-coverage" filter and report the issue to the user whilst skipping the instrumentation and then continue, but I'd like to know if there is anything I a can do during the profiling to avoid this issue and avoid recompilation.
So to sum up 2 questions "Why does it happen?" and "Can I avoid it without recompilation?"
NOTE: PartCover also seems to exhibit this issue and it uses a different method to record visit points.
NOTE: I am the developer of OpenCover (an open source code coverage tool) and currently the maintainer of PartCover.
I posted the answer on MSDN forums - Why do assemblies with the SecurityTransparent attribute cause instrumented code via a profiler to throw a VerificationException?
and got an answer that I should look at the COR_PRF_DISABLE_TRANSPARENCY_CHECKS_UNDER_FULL_TRUST flag (.NET4 only it seems)
see also:
http://www.madgnome.fr/2011/09/verificationexception-and-profiling-the-hidden-flag/
https://web.archive.org/web/20190114032309/https://blogs.msdn.microsoft.com/davbr/2010/01/07/clr-v4-stuff-that-may-break-your-profiler
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With