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

From: Erwin S. Andreasen (erwin@PIP.DKNET.DK)
Date: 05/01/98

On Fri, 1 May 1998, James Turner wrote:

> For now I've decided to handle it through a pair of guard functions
> and ignore arbitrary free()ing.  It adds 2*getpagesize() to every
> malloc, so it's not very efficient and shouldn't be used for anything

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.

> frequently allocated.  Right now I'm using it for buf, buf1, buf2, and
> arg.  It has proven quite effective (in test cases... my mud currently

efence doesn't do that for the stack however, that's an interesting idea.
It screams for C++ however :)

class ProtectedBuffer
                char buf[MSL];
                char stack_guard[4096];
        ProtectedBuffer() { mprotect(stack_guard, ...); }
        ~ProtectedBuffer() { munprotect(...); }

        operator char* () { return buf; }

void fun()
        ProtectedBuffer buf;

        strcpy (buffer, 10k string);

No need to call STACK_GUARD, STACK_RETURN, let the destructors sort them

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.

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

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

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 :)

Erwin Andreasen   Herlev, Denmark <>  UNIX System Programmer
<URL:>     <*>         (not speaking for) DDE

     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | |

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