malloc, mmap, and unexec issue

From: Darren Kwan (dkwan_at_axiscorp.com)
Date: 08/08/03


Date: 8 Aug 2003 11:28:16 -0700

Hi Everybody,

I have a problem regarding malloc() using mmap() for large memory
allocations. I'll try to describe it the best I can. I have an
executable I'll call 'foo'. Arguments can be passed to 'foo' to change
its functionality. ie. 'foo -a -b'. Using code derived from emacs
unexec(), I create a new binary executable I'll call 'foo2'.
Therefore, running 'foo2' is equivalent to running 'foo -a -b'.

The way I understand it, unexec() lifts the data segment from the
source binary and patches it into the destination binary. The problem
is if the source binary 'foo' uses malloc() which uses mmap(), the
allocated memory doesn't appear in the 'foo' data segment which then
doesn't get patched to ‘foo2' and I get a segmentation fault if I run
‘foo2'. I can use mallopt() to deny malloc() from using mmap() and
then it works, but I'll be limited in the amount of memory I can
allocate.

There's no problem running in Solaris.

Can anyone suggest a workaround? I'm using Red Hat 7.2/7.3, gcc and
glibc, and the binaries are ELF.

Thanks!

Darren Kwan
Axis Systems
dkwan@axiscorp.com



Relevant Pages

  • Re: dont let mmap allocate down to zero
    ... > use mmap for small allocations? ... A returned value of 0 is perfectly correct for mmap() ... The seg-fault you get when you de-reference a pointer to NULL ... You are attempting to access memory ...
    (Linux-Kernel)
  • Re: memory management related system calls
    ... In particular if you intend to use brk again at a later time, ... probably not a good idea to mess with it's allocations. ... should use mmap to allocate rather than brk. ... That depends on how the memory was allocated. ...
    (comp.os.linux.development.system)
  • Re: memory management related system calls
    ... For small allocations it will primarily use brk ... > and for large allocations it will use mmap. ... happens to be at the top of memory and is reasonably large. ...
    (comp.os.linux.development.system)
  • Re: Memory management
    ... The unused memory is of course freed, ... > is not directly returned to the kernel. ... allocations are through mmap: surely when M_MMAP_MAX is exceeded all ...
    (comp.os.linux.development.apps)
  • RE: 2003 SBS stalling randomly
    ... A memory leak occurs in an application using the Volume Shadow Copy Service ... Poolmon displays data that the ... The data is grouped by pool allocation tag. ... Press P twice to display allocations from only the paged pool. ...
    (microsoft.public.windows.server.sbs)