18

I need to write a test which verifies that my code can handle an AccessViolationException (or any other WIN32 Corrupted State Exception - CSE), which occours in an unsafe context, typically by calling a 3rd party lib. This should all be done using C# on .net 4.0.

I found this related question How to handle AccessViolationException and this related article http://dotnetslackers.com/articles/net/All-about-Corrupted-State-Exceptions-in-NET4.aspx, which explains how to catch these CSE's and their background.

So i would like to provoke a WIN32 CSE in a test, to ensure correct handling in my application. Something like:

Some example class to test:

public class MyExceptionHandler
{
    [HandleProcessCorruptedStateExceptions]
    public void HandleCorruptedStateException()
    {
        try
        {
             //Force genuine unsafe AccessViolationException
             //not just a throw new AccessViolationException
        }
        catch(Exception e)
        {
             //Log/cleanup/other
        }
    }

    public void DoesNotHandleCorruptedStateException()
    {
        try
        {
             //Force genuine unsafe AccessViolationException
             //not just a throw new AccessViolationException
        }
        catch (Exception e)
        {
            //Log/cleanup/other
        }
    }
}

A Test:

class MyTest
{
    [Test]
    public void ShouldVerifyThatAnAccessViolationExceptionIsHandledCorrectly()
    {
        var handler = new MyExceptionHandler();

        Assert.DoesNotThrow(() => handler.HandleCorruptedStateException());
    }

    [Test]
    public void ShouldVerifyThatAnAccessViolationExceptionIsNotHandledCorrectly()
    {
        var handler = new MyExceptionHandler();

        Assert.Throws<AccessViolationException>(() => handler.DoesNotHandleCorruptedStateException());
    }
}

Does anyone have a suggestion of how to achive this without to much work (ex. writting a unsafe lib which causes this exception).

Kind regards

UPDATED: To match my final solution, thanks to JaredPar.

public class MyExceptionHandler
{
    [HandleProcessCorruptedStateExceptions]
    public void HandleCorruptedStateException()
    {
        try
        {
            var ptr = new IntPtr(42);
            Marshal.StructureToPtr(42, ptr, true);
        }
        catch(Exception e)
        {
             //Log/cleanup/other
        }
    }

    public void DoesNotHandleCorruptedStateException()
    {
        try
        {
            var ptr = new IntPtr(42);
            Marshal.StructureToPtr(42, ptr, true);
        }
        catch (Exception e)
        {
            //Log/cleanup/other
        }
    }
}

TIP: To verify this manually use a simple console app, from the commandline:

class Program
{
    static void Main(string[] args)
    {
        var handler = new MyExceptionHandler();

        if (args.Length > 1)
        {
            handler.HandleCorruptedStateException();
        }
        else
        {
            handler.DoesNotHandleCorruptedStateException();
        }
    }
}
Community
  • 1
  • 1
Christian Mikkelsen
  • 1,661
  • 2
  • 19
  • 44

2 Answers2

29

Try the following

var ptr = new IntPtr(42);
Marshal.StructureToPtr(42, ptr, true);

This isn't guaranteed to throw an AccessViolationException by the CLI spec but it will on every platform I'm aware of

almulo
  • 4,918
  • 1
  • 20
  • 29
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Thank you, but this do not generate a CSE, only an ArgumentNullException. It has to be a genuine Win32 CSE which will cause the application to crash if not handled in try catch + is marked with the [HandleProcessCorruptedStateExceptions] attribute, etc. as described in the article. – Christian Mikkelsen May 31 '12 at 19:08
  • I stand corrected. This DOES work :-D. I made an error in my test (typical). Thank you SOO much :). – Christian Mikkelsen May 31 '12 at 19:25
2
    private static unsafe void AccessViolation()
    {
        int* p = (int*)0xFF004324;
        int q = *p;
    }

Edited out the unsafe write. Reading from that address should still generate an AccessViolationException unless coincidentally that is within your address space.

Andrei Krasutski
  • 4,913
  • 1
  • 29
  • 35
Jis Ben
  • 155
  • 1
  • 7