views:

126

answers:

2

Well, in .NET 4 Microsoft added the HandleProcessCorruptedStateExceptions attribute:

HandleProcessCorruptedStateExceptionsAttribute Class

I want to test this feature. How can I bring my application to a "corrupt state"?

+7  A: 

Screwing up the garbage collected heap is always a good way:

using System;
using System.Runtime.InteropServices;


class Program {
  unsafe static void Main(string[] args) {
    var obj = new byte[1];
    var pin = GCHandle.Alloc(obj, GCHandleType.Pinned);
    byte* p = (byte*)pin.AddrOfPinnedObject();
    for (int ix = 0; ix < 256; ++ix) *p-- = 0;
    GC.Collect();   // kaboom
  }
}
Hans Passant
Works like a charm. Thanks.
Yaakov Davis
+2  A: 

Just dereference a random number:

    private static unsafe void AccessViolation()
    {
        byte b = *(byte*) (8762765876);
    }

or overflow the stack:

    private static void StackOverflow()
    {
        StackOverflow();
    }
romkyns
+1 — much simpler than the accepted answer!
Timwi