views:

88

answers:

2

Environment is VC++ 9 on various Win platforms (XP and later)

I'm writing an unhandled exception handler. I have a vague recollection from my kernel days that it was bad to catch an EXCEPTION_GUARD_PAGE, as this was generated to tell the OS to enlarge the stack.

My question is twofold:

  1. Can such an exception occur in user space?

  2. If so, is it safe to catch it?

I'm not especially interested in doing anything with it. I just want to know if I need to put special code in to not catch it (as I'm catching everything at the moment).

_Update:_

I've recalled my source, it is Raymond Chen's blog (http://blogs.msdn.com/b/oldnewthing/archive/2006/09/27/773741.aspx)

My initial concern is that when catching all exceptions, if I catch a EXCEPTION_GUARD_PAGE, I might prevent the OS from seeing the exception and enlarging the stack.

On further thought, I suspect that the EXCEPTION_GUARD_PAGE might be handled in the Kernel during the read/write operation that generated it and thus will never reach my user-space code.

I guess I am looking for someone to confirm (or contradict) that.

_Second Update:_

I haven't selected an answer yet as nobody has really answered the question. I'll leave it open in the hope that somebody may yet offer the information that I seek.

_Third Update_

Still living in (faint) hope.

_Fourth Update_

Well, I never did get a usable answer. I've long finished the module I was writing when I asked the question. I think the question is, for practical purposes, dead. I probably won't update again.

A: 

According to MSDN:

The thread accessed memory allocated with the PAGE_GUARD modifier.

Which sounds like something that should never happen unless you've got a bug; so I'd handle it accordingly.

Edit

The OS exception handlers are registered before yours, so handling it yourself doesn't mean the OS didn't get to see it first.

egrunin
The last page in the stack has the GUARD PAGE attribute. When you access it, it generates an exception to warn the OS to enlarge the stack. At least, that's what happens in Kernel mode. Not sure how it works in user space.
Michael J
+3  A: 

Setting up a page to generate a STATUS_GUARD_PAGE_VIOLATION with VirtualProtectEx() and the PAGE_GUARD flag is a documented procedure. The exception can be generated only once for a page so there's no danger you'll die while processing the exception.

I don't do anything special for the exception but that doesn't proof too much, getting this exception would be extraordinarily rare. Never seen it once in any of our crash reports.

The feature is really meant for generating stack overflow exceptions. Which you really do need to handle specially since you have so little stack left. I assume that's where the warning came from that you mention in your question. It will however never generate a page guard exception, that's handled at kernel level before getting translated to a stack overflow.

Hans Passant