What is the best way to intentionally crash my windows application?

I added a mini-core-dump function (via __try / __ except and MiniDumpWriteDump ()) to the Windows build of my Qt application, so that if / when my application ever works, a .dmp file will be written to disk for me to look and debug later.

This works very well, but for testing, I would like to have a well-known and reliable method to crash my program. For example, the graphical interface may have a “crash now” button, and when the user clicks on it, this will lead to an unintentional crash of the application.

One way to do this, of course, is:

int * badPointer = NULL; *badPointer = 666; 

And it works for me, but I don’t like this approach because it relies on undefined behavior - in particular, the C ++ standard does not require the above code to crash, so it’s possible (from the point of view of a lawyer language), that some future version of the compiler will not work when the above code is executed.

As a more “official” approach, I tried this:

 abort(); 

... which terminates the program, but this does not cause Windows Structured Exception to run the MiniCrashDump handler, so the .dmp file is not written.

My question is, is there an “Official Right Way” for breaking down my program? I see that the Windows API has a RaiseException () function that I could call, but I'm not sure what the correct arguments should be. Is this the way, or is there an even more specific challenge that I would be better off using?

+5
source share
1 answer

It is perfectly normal to dereference a null pointer to cause an access violation if you know that you are running on Windows. Windows provides more reliable guarantees than C ++. C ++ says that dereferencing a null pointer has an Undefined Behavior value, but Windows defines this as an access violation (which is perfectly acceptable for C ++, because an access violation is one of the possible outcomes of Undefined Behavior).

From Virtual Memory Management :

Windows NT creates security in every process address space. Both the upper and lower 65 536 bytes of each process are constantly reserved by the system. These parts of the address space are reserved for catching pointers that are trying to address memory in the range 00000000 16 -0000FFFF 16 or 7FFF0000 16 -7FFFFFFF <sub> 16sub>. It is no coincidence that pointers are easy to detect in this range by simply ignoring the bottom four nibbles (the rightmost two bytes) at these addresses. In fact, the pointer is invalid if the top four nibbles are: 0000 16 or 7FFF 16 ; all other values ​​represent valid addresses.

The first page of memory is always displayed as PAGE_NOACCESS , so if you try to read or write a null pointer (or any pointer inside a +/- 64 KB null pointer), you will always raise an access violation exception.

+3
source

Source: https://habr.com/ru/post/1205141/


All Articles