Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test handling of AccessViolationException

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();
        }
    }
}
like image 610
Christian Mikkelsen Avatar asked May 31 '12 18:05

Christian Mikkelsen


2 Answers

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

like image 181
JaredPar Avatar answered Sep 23 '22 20:09

JaredPar


    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.

like image 38
Jis Ben Avatar answered Sep 22 '22 20:09

Jis Ben