Changeset 34711 for platform

Show
Ignore:
Timestamp:
08/02/09 22:10:18 (5 years ago)
Author:
kazuho
Message:

reset memory mapping on certain syscalls

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • platform/linux/morememory/trunk/morememory.c

    r34707 r34711  
    11#define _XOPEN_SOURCE 500 
     2#include <features.h> 
    23#include <assert.h> 
     4#define __USE_GNU 
     5#include <dlfcn.h> 
     6#undef __USE_GNU 
    37#include <errno.h> 
    48#include <fcntl.h> 
    59#include <malloc.h> 
     10#include <stdio.h> 
    611#include <stdlib.h> 
    712#include <string.h> 
     
    1722static char* moremem; 
    1823static size_t moremem_size; 
     24 
     25#define REMAP() \ 
     26  assert(mmap(moremem, MAXMEM, PROT_READ | PROT_WRITE | PROT_EXEC, \ 
     27              MAP_SHARED | MAP_FIXED, memfd, 0) \ 
     28         == moremem) 
     29#define UNMAP() assert(munmap(moremem, MAXMEM) == 0) 
    1930 
    2031static void* my_morecore(ptrdiff_t incr) 
     
    4455static void before_fork(void) 
    4556{ 
     57  char buf[4096]; 
     58  size_t off; 
     59   
    4660  child_memfd = create_memfile(); 
    47   assert(pwrite(child_memfd, moremem, moremem_size, 0) == moremem_size); 
     61  UNMAP(); 
     62  for (off = 0; off < moremem_size; off += sizeof(buf)) { 
     63    assert(pread(memfd, buf, sizeof(buf), off) == sizeof(buf)); 
     64    assert(pwrite(child_memfd, buf, sizeof(buf), off) == sizeof(buf)); 
     65  } 
     66  REMAP(); 
    4867} 
    4968 
     
    5574static void after_fork_child(void) 
    5675{ 
    57   write(2, "after_fork_child\n", 17); 
    5876  /* close */ 
    59   assert(munmap(moremem, MAXMEM) == 0); 
     77  UNMAP(); 
    6078  assert(close(memfd) == 0); 
    6179  /* remap */ 
    6280  memfd = child_memfd; 
    63   assert(mmap(moremem, MAXMEM, PROT_READ | PROT_WRITE | PROT_EXEC, 
    64               MAP_SHARED | MAP_FIXED, memfd, 0) 
    65          == moremem); 
     81  REMAP(); 
    6682} 
     83 
     84#define DEF_ORIG(name, ret, def_args, call_args)                \ 
     85  typedef ret libc_##name##_t def_args;                         \ 
     86  static libc_##name##_t* orig_##name;                          \ 
     87  ret name def_args {                                           \ 
     88    UNMAP();                                                    \ 
     89    REMAP();                                                    \ 
     90    return (*orig_##name)call_args;                             \ 
     91  } 
     92   
     93/* listed functions should match them in DLSYM */ 
     94DEF_ORIG(read, ssize_t, (int fd, void* buf, size_t count), (fd, buf, count)); 
     95DEF_ORIG(write, ssize_t, (int fd, const void* buf, size_t count), 
     96         (fd, buf, count)); 
     97DEF_ORIG(wait, pid_t, (int* status), (status)); 
     98DEF_ORIG(waitpid, pid_t, (pid_t pid, int* status, int options), 
     99         (pid, status, options)); 
     100 
     101#undef DEF_ORIG 
    67102 
    68103__attribute__((constructor)) 
     
    78113  mallopt(M_MMAP_THRESHOLD, MAXMEM); 
    79114  pthread_atfork(before_fork, after_fork_parent, after_fork_child); 
     115   
     116#define DLSYM(n) orig_##n = (libc_##n##_t*)dlsym(RTLD_NEXT, #n) 
     117  /* listed functions should match them in DEF_ORIG */ 
     118  DLSYM(read); 
     119  DLSYM(write); 
     120  DLSYM(wait); 
     121  DLSYM(waitpid); 
     122#undef DLSYM 
    80123}