minix/minix
David van Moolenbroek b7725c8552 Fix mmap leak in malloc code upon state transfer
The NetBSD libc malloc implementation uses a memory-mapped area for
its page directory.  Since the process heap is reconstructed upon
state transfer for live update, this memory-mapped area must not be
transferred to the new process.  However, as the new instance of the
process being updated inherits all memory-mapped areas of the old
instance, it also automatically inherits the malloc implementation's
page directory.  Thus, we must explicitly free this area in order to
avoid a memory leak.

The magic pass already detects (de)allocation functions called from
within other (de)allocation functions, which is why the mmap(2) and
munmap(2) calls of the malloc code are not instrumented as it is.
This patch changes that particular case to allow a different hook
function to be called for such "nested" allocation calls, for a
particular set of nested calls.  In particular, the malloc(3) code's
mmap(2) and munmap(2) calls are replaced with magic_nested_mmap and
magic_nested_munmap calls, respectively.  The magic library then
tracks memory mapping allocations of the malloc code by providing an
implementation for these two wrappers, and frees the allocations upon
state transfer.

This approach was chosen over various alternatives:

- While it appears that nesting could be established by setting a
  flag while the malloc(3) wrapper is active, and testing the flag in
  the mmap(2)/munmap(2) wrappers, this approach would fail to detect
  memory-mapped allocations made from uninstrumented malloc(3) calls,
  and therefore not a viable option.
- It would be possible to obtain the value of the variables that
  store the information about the memory-mapped area in the malloc
  code.  However, this is rather difficult in practice due to the way
  the libc malloc implementation stores the size of the are, and it
  would make the solution more dependent on the specific libc malloc
  implementation.
- It would be possible to use the special "nested" instrumentation
  for allocations made from certain marked sections.  Since we mark
  the data section of the malloc code already, this would not be hard
  to do.  Switching to this alternative would change very little, and
  if for any reason this approach yields more advantages in the
  future, we can still choose to do so.

Change-Id: Id977405da86a72458dd10f18e076d8460fd2fb75
2015-09-17 14:04:43 +00:00
..
bin New sources layout 2014-07-31 16:00:30 +02:00
commands RS/VM: proper preparation for multi-VM live update 2015-09-17 14:01:06 +00:00
drivers SEF: query VM about holes during state transfer 2015-09-17 13:43:06 +00:00
fs VM: set recovery policy to restart 2015-09-17 13:45:43 +00:00
include RS/VM: proper preparation for multi-VM live update 2015-09-17 14:01:06 +00:00
kernel Kernel: IPC filter support for VM memory requests 2015-09-17 13:46:23 +00:00
lib Do not instrument malloc implementation variables 2015-09-17 14:03:47 +00:00
llvm Fix mmap leak in malloc code upon state transfer 2015-09-17 14:04:43 +00:00
man Import NetBSD syslogd(8) 2015-07-26 11:57:04 +00:00
net services: Selectively enable stateful restart. 2015-09-17 13:36:01 +00:00
sbin New sources layout 2014-07-31 16:00:30 +02:00
servers RS/VM: proper preparation for multi-VM live update 2015-09-17 14:01:06 +00:00
share New sources layout 2014-07-31 16:00:30 +02:00
tests VM: set recovery policy to restart 2015-09-17 13:45:43 +00:00
usr.bin VFS: add support for F_DUPFD_CLOEXEC 2015-07-20 13:55:10 +00:00
usr.sbin mkfs.mfs: remove Linux mount check 2015-07-20 10:59:49 +00:00
Makefile benchmarks: remove unixbench 2015-07-20 11:05:28 +00:00
Makefile.inc New sources layout 2014-07-31 16:00:30 +02:00