| fjblurt@yahoo.com 2007-09-30, 7:18 pm |
| On Sep 29, 11:01 pm, SM Ryan <wyrm...@tango-sierra-oscar-foxtrot-
tango.fake.org> wrote:
> I would like to allocate a segment and then guard pages to detect
> underflow and overflow. I was thinking of using mmap to create
> an anonymous segment and then mprotect to deny any access to the
> first and last pages. Does anyone know if MacOSX supports a page
> level mprotect to allow this?
Very likely. Why not just try it?
> The segment is to hold a stack with simple *sp++ and *--sp and
> I would like to use page protection to detect overflow or underflow
> instead of explicitly checking the pointer all the time.
Okay, but if you explicitly check the pointer you will be able to
handle an overflow or underflow more gracefully. With mprotect, if
you touch the guard page you'll get a SIGSEGV. In principle you could
handle the signal and longjmp back to your main loop, but the problem
is that it's difficult to distinguish the signals caused by stack
overflow from those caused by other bugs in your program, which should
not be resumed.
Checking the stack pointer should only use one or two instructions on
each access, and if you wrap your stack access in push()/pop()
functions (a good idea anyway), you only have to write it once.
|