alternate stacks for signal handlers

Matthew Fluet fluet@CS.Cornell.EDU
Mon, 31 Jul 2000 17:37:45 -0400 (EDT)


In cleaning up gc.c, I wanted add in the dead-zones around the alternate
signal stack.  Henry, does this look sufficient for mmapping an alternate
signal stack with dead-zones?  Also, how big dead zones should we need?
Currently, I'm mmaping a space 2 * 4 * SIGSTKSZ -- a 4 * SIGSTKSZ "real"
stack with the doubling trick.  Should just 1K or 2K be sufficient arround
the stack?  Or do I need any other page alignments?

/* A super-safe mmap.
 *  Allocates a region of memory with dead zones at the high and low ends.
 *  Any attempt to touch the dead zone (read or write) will cause a
 *   segmentation fault.
 */
static void *ssmmap(size_t length, size_t dead_low, size_t dead_high) {
  void *base,*low,*result,*high;

  base = smmap(length + dead_low + dead_high);
  smunmap(base, length + dead_low + dead_high);

  low = mmap(base, dead_low, 0, 
	     MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0);
  if (low == (void*)-1)
    die("mmap failed");

  result = mmap(low + dead_low, length, PROT_READ | PROT_WRITE, 
		MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0);
  if (result == (void*)-1)
    die("mmap failed");

  high = mmap(result + length, dead_high, 0, 
	      MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0);
  if (high == (void*)-1)
    die("mmap failed");

  return result;
}