18

我需要编写一个测试来验证我的代码是否可以处理 AccessViolationException(或任何其他 WIN32 损坏状态异常 - CSE),该异常发生在不安全的上下文中,通常是通过调用 3rd 方库。这一切都应该在 .net 4.0 上使用 C# 完成。

我发现了这个相关的问题How to handle AccessViolationException和这篇相关的文章http://dotnetslackers.com/articles/net/All-about-Corrupted-State-Exceptions-in-NET4.aspx,它解释了如何捕获这些 CSE 及其背景。

所以我想在测试中激发一个 WIN32 CSE,以确保在我的应用程序中正确处理。就像是:

一些要测试的示例类:

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
        }
    }
}

一个测试:

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();
        }
    }
}
4

2 回答 2

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

于 2012-05-31T19:01:54.607 回答
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.

于 2012-05-31T19:09:47.383 回答