AccessViolationException on Cop.Intercept()

Issue #2 resolved
Peter Anderson created an issue

So after following the Getting Started post an AccessViolationException when executing the following line:

Cop.Intercept();

I am Building the project in Debug|Any Cpu, using Visual Studio 2013 update 5 targeting .Net Framework 4

Exception details:

System.AccessViolationException was unhandled
  HResult=-2147467261
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=CodeCop
  StackTrace:
       at CodeCop.Core.Interop.GetMetadataToken(MethodBase methodTo, MethodBase methodFrom, Type type, Int32 genericParameterPosition)
       at CodeCop.Core.Services.IlWeaverService.Weave(MethodBase method, MethodBase mainBrother)
       at CodeCop.Core.Handlers.WeaveILForInterceptedMethodsCommandHandler.<>c__DisplayClass0_0.<Handle>b__0(InterceptedMethod m)
       at System.Collections.Generic.List`1.ForEach(Action`1 action)
       at CodeCop.Core.Handlers.WeaveILForInterceptedMethodsCommandHandler.Handle(IWeaveILForInterceptedMethodsCommand command)
       at CodeCop.Core.Messaging.InMemoryMessageBus.Send[TCommand](TCommand message)
       at CodeCop.Core.Cop.Run(IEnumerable`1 interceptors)
       at CodeCop.Core.Cop.Intercept()
       at HelloWorld.Program.Main(String[] args) in C:\Users\Peter.Anderson\Documents\Visual Studio 2013\Projects\HelloWorld\HelloWorld\Program.cs:line 16
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Call Stack:

#!
HelloWorld.exe!HelloWorld.Program.Main(string[] args) Line 16   C#
[External Code] 

Comments (6)

  1. Ricardo Barbosa

    Hi Peter,

    This is probably related to the fact that you have .NET 4.6 installed on that machine and that application is running as x64.

    We currently don't support the new jit engine (RyuJIT) because at the time of our development it had serious bugs (and still has).

    https://github.com/dotnet/coreclr/issues/1296

    http://blogs.msdn.com/b/dotnet/archive/2015/07/28/ryujit-bug-advisory-in-the-net-framework-4-6.aspx

    http://nickcraver.com/blog/2015/07/27/why-you-should-wait-on-dotnet-46/

    So on machines that have .NET 4.6 installed you have to disable RyuJIT for CodeCop to work under x64. 3 options are possible, check the below link and see which one suits you best (I personally under my dev machine have the registry option)

    Add a useLegacyJit DWORD with a value of 1 and also change the existing UseRyuJIT DWORD value to 0 under the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework registry key. (case sensitive)

    https://github.com/Microsoft/dotnet/blob/master/docs/testing-with-ryujit.md

    Please tell me if that fixed the issue for you.

    Thanks,

    Ricardo

  2. Peter Anderson reporter

    Ah thanks.

    Yeah the following methods worked:

    Adding the following to the app.config

    <runtime>
          <useLegacyJit enabled="1" />
        </runtime>
    

    Setting the environment variable

    COMPLUS_useLegacyJit=1
    

    I didn't try the registry option.

    Thanks

  3. Log in to comment