Page country exceptions in Delphi?

There is a message from Raymond Chen, where he talks about how the bad function of IsBadXxxPtr is the use of a protective page exception .

I do not quite understand how it applies to Delphi. Who and how should normally (i.e., without calling IsBadXxxPtr) handle this exception? I know that Delphi inserts code that (for example) provides memory access for large static arrays - precisely for this reason: to expand the stack.

But if there is a warning about the watch page: who will handle it in the Delphi application? Can I accidentally bind it with try / except if this is not the case? Will the Delphi debugger notify me of these exceptions?

+6
exception delphi
source share
2 answers

Windows Structured Exception Handling (SEH) has a two-phase structure. When an exception occurs, Windows first looks for a handler for the exception, following the registered chain of exception handlers (the head of which is stored in fs: [0] on x86, that is, the first dword in the segment pointed to by the FS register segment - all this ugly 16-bit logic segment offsets did not disappear in 32-bit, this has become less relevant).

The search is performed by calling a function with a specific flag, a pointer to which is stored in each exception frame on the stack. fs: [0] indicates the top frame. Each frame indicates a previous frame. Ultimately, the last frame in the list is the one that was provided by the OS (this handler will open the crash application dialog box if it receives an unhandled exception).

These functions usually check the type of exception and return code indicating what to do. One of the codes that can be returned is basically "ignores this exception and continues." If Windows sees this, it will reset the instruction pointer to the exception point and resume execution. Another code indicates that this exception frame should handle this exception. Third code: "I will not catch this exception, continue the search." Windows continues to call these exception filter functions until it finds one that handles the exception anyway.

If Windows finds one that handles the exception, catching it, it will continue to unwind the stack back to this handler, which is to call all functions again, only passing another flag. At this point, the functions execute the finally logic, right down to the handler that runs the except logic.

However, excluding page protection on the stack, the process is different. None of the language exception handlers will decide to handle this exception, because otherwise the stack growth mechanism will break. Instead, the filter filter filters all the way to the base exception handler provided by the OS, which increases the allocation of the stack by assigning the appropriate memory, and then returns the appropriate return code to indicate that the OS should continue where it was stopped, and not spin the stack .

The tool and debugging infrastructure is designed to ensure that these specific exceptions are reproduced correctly, so you don't need to worry about handling them.

You can learn more about SEH at Matt Pietrek, an excellent article on MSJ over a decade ago .

+13
source share

From looking at the comments, it seems to me that the clutter of the groundless “country page” occurs completely inside the kernel, and that’s not what you need to worry about from user space.

You should remember that this article was written for C ++, which is nowhere as advanced as Delphi on the front of memory management. The problem of uninitialized pointers is much less clutter in Delphi than in C / C ++ for two reasons:

  • Delphi checks uninitialized variables at compile time, which (for some reason) have problems with many C compilers.
  • Delphi initializes all of its dynamic memory to 0, so you don't have random heap garbage to deal with what might seem like a good pointer when it really is not. This means that most bad pointers give you access to violations that are easy to debug, rather than failing and corrupting memory without problems.
+2
source share

All Articles