Unix Programming - mmap/mprotect on MacOSX

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > September 2007 > mmap/mprotect on MacOSX





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author mmap/mprotect on MacOSX
SM Ryan

2007-09-30, 1:31 am

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?

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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
She broke your heart and inadvertently drove men to deviant lifestyles.
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.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com