Re: Copyovering on SIG{SEGV,BUS,..}

From: James Turner (turnerjh@XTN.NET)
Date: 05/01/98


"Erwin S. Andreasen" <erwin@pip.dknet.dk> writes:

> For dynamic memory allocation, the Electric Fence library by Bruce Perens
> does exactly that - if you have a Redhat system, just adding -lefence to
> the libs will link it in, replacing malloc,calloc and free. man efence.

I used efence once, but it didn't seem to like my code very much -- it
has _tremendous_ overhead.  I assume it adds one page before and after
every allocation... 8k overhead per malloc.  My code uses about 12000
mallocs in booting, not counting what happens when players log
on... that's 96meg overhead!

The guarded memory stuff I posted has the advantage of only having
horrid overhead when you explicitly choose to use it :)

> Hmm.. come to think of it, stack_guard should be *before* buf shouldn't
> it? It will be placed on the stack like this:
>
> Address = 1000 buf
> Adddress = 900 stack_guard
>
> printing to buf will start at 1000 and continue upwards.

It could be done either way... my intent wasn't to protect buf but to
protect against deep stack troubles.  Using the macros, you get
something like

(normal memory)
(protected memory)
(normal memory)
(protected memory)

and so forth.  It keeps angry strcpy's from tracing up the stack too
far ;)


> Hm, and also... I think memory needs to be aligned to be mprotected.

It does, that's why I used valloc... glibc2 I believe.

> You can do the alignment manually however, allocate a buffer of twice the
> size.

Nah, not twice the size.  For a buffer of size n, you would need to
allocate ((n/pagesize) + 2)*pagesize bytes.  Given any piece of memory
pagesize bytes long, you are guaranteed of crossing a page boundary,
hence the added pagesize.

> Hmm, another thing that's problematic about the C++ code - sizeof(buf)
> will be wrong, returning the size of the whole structure.. I wonder if
> there's a operator sizeof :)

Well, sizeof(buf) fails (ie returns sizeof (char *)) when buf is
declared extern, so it's not a very big issue :)

--
James Turner               turnerjh@xtn.net
                           http://www.vuse.vanderbilt.edu/~turnerjh/


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST