Allocating a buffer of more a page size on stack w

2019-06-21 05:15发布

问题:

In Windows, stack is implemented as followed: a specified page is followed committed stack pages. It's protection flag is as guarded. So when thead references an address on the guared page, an memory fault rises which makes memory manager commits the guarded page to the stack and clean the page's guarded flag, then it reserves a new page as guarded.

when I allocate an buffer which size is more than one page(4KB), however, an expected error haven't happen. Why?

回答1:

Excellent question (+1).

There's a trick, and few people know about it (besides driver writers).

When you allocate large buffer on the stack - the compiler automatically adds so-called stack probes. It's an extra code (implemented in CRT usually), which probes the allocated region, page-by-page, in the needed order.

EDIT:

The function is _chkstk.



回答2:

The fault doesn't reach your program - it is handled by the operating system. Similar thing happens when your program tries to read memory that happens to be written into the swap file - a trap occurs and the operating system unswaps the page and your program continues.