Ignore:
Timestamp:
Jan 10, 2007, 3:57:22 PM (15 years ago)
Author:
Sam Hocevar
Message:
  • Split load-fd.c into load-fd.c and load-mem.c (mmap, etc.).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/src/load-fd.c

    r1638 r1639  
    1919#include "config.h"
    2020
    21 /* Can't remember what that's for */
     21/* Need this for RTLD_NEXT */
    2222#define _GNU_SOURCE
    2323/* Use this to get lseek64() on glibc systems */
     
    3535#include <sys/types.h>
    3636#include <sys/socket.h>
    37 #include <sys/mman.h>
    3837#include <unistd.h>
    3938#include <fcntl.h>
    4039#include <stdarg.h>
    41 #if defined HAVE_LIBC_H
    42 #   include <libc.h>
    43 #endif
    4440
    4541#include "libzzuf.h"
     
    6864static off64_t (*lseek64_orig) (int fd, off64_t offset, int whence);
    6965#endif
    70 static void *  (*mmap_orig)    (void *start, size_t length, int prot,
    71                                 int flags, int fd, off_t offset);
    72 #ifdef HAVE_LSEEK64
    73 static void *  (*mmap64_orig)  (void *start, size_t length, int prot,
    74                                 int flags, int fd, off64_t offset);
    75 #endif
    76 static int     (*munmap_orig)  (void *start, size_t length);
    77 #ifdef HAVE_MAP_FD
    78 static kern_return_t (*map_fd_orig) (int fd, vm_offset_t offset,
    79                                      vm_offset_t *addr, boolean_t find_space,
    80                                      vm_size_t numbytes);
    81 #endif
    8266static int     (*close_orig)   (int fd);
    8367
     
    9579#ifdef HAVE_LSEEK64
    9680    LOADSYM(lseek64);
    97 #endif
    98     LOADSYM(mmap);
    99 #ifdef HAVE_MMAP64
    100     LOADSYM(mmap64);
    101 #endif
    102     LOADSYM(munmap);
    103 #ifdef HAVE_MAP_FD
    104     LOADSYM(map_fd);
    10581#endif
    10682    LOADSYM(close);
     
    258234#endif
    259235
    260 /* Used for mmap() and munmap() */
    261 void **maps = NULL;
    262 int nbmaps = 0;
    263 
    264 #define MMAP(fn, off_t) \
    265     do { \
    266         if(!_zz_ready) \
    267             LOADSYM(fn); \
    268         ret = ORIG(fn)(start, length, prot, flags, fd, offset); \
    269         if(!_zz_ready || !_zz_iswatched(fd) || _zz_disabled) \
    270             return ret; \
    271         if(ret && length) \
    272         { \
    273             char *b = malloc(length); \
    274             int i, oldpos; \
    275             for(i = 0; i < nbmaps; i += 2) \
    276                 if(maps[i] == NULL) \
    277                     break; \
    278             if(i == nbmaps) \
    279             { \
    280                 nbmaps += 2; \
    281                 maps = realloc(maps, nbmaps * sizeof(void *)); \
    282             } \
    283             maps[i] = b; \
    284             maps[i + 1] = ret; \
    285             oldpos = _zz_getpos(fd); \
    286             _zz_setpos(fd, offset); /* mmap() maps the fd at offset 0 */ \
    287             memcpy(b, ret, length); /* FIXME: get rid of this */ \
    288             _zz_fuzz(fd, (uint8_t *)b, length); \
    289             _zz_setpos(fd, oldpos); \
    290             ret = b; \
    291             if(length >= 4) \
    292                 debug(STR(fn)"(%p, %li, %i, %i, %i, %lli) = %p \"%c%c%c%c...", \
    293                       start, (long int)length, prot, flags, fd, \
    294                       (long long int)offset, ret, b[0], b[1], b[2], b[3]); \
    295             else \
    296                 debug(STR(fn)"(%p, %li, %i, %i, %i, %lli) = %p \"%c...", \
    297                       start, (long int)length, prot, flags, fd, \
    298                       (long long int)offset, ret, b[0]); \
    299         } \
    300         else \
    301             debug(STR(fn)"(%p, %li, %i, %i, %i, %lli) = %p", \
    302                   start, (long int)length, prot, flags, fd, \
    303                   (long long int)offset, ret); \
    304     } while(0)
    305 
    306 void *mmap(void *start, size_t length, int prot, int flags,
    307            int fd, off_t offset)
    308 {
    309     void *ret; MMAP(mmap, off_t); return ret;
    310 }
    311 
    312 #ifdef HAVE_MMAP64
    313 void *mmap64(void *start, size_t length, int prot, int flags,
    314              int fd, off64_t offset)
    315 {
    316     void *ret; MMAP(mmap64, off64_t); return ret;
    317 }
    318 #endif
    319 
    320 int munmap(void *start, size_t length)
    321 {
    322     int ret, i;
    323 
    324     if(!_zz_ready)
    325         LOADSYM(munmap);
    326     for(i = 0; i < nbmaps; i++)
    327     {
    328         if(maps[i] != start)
    329             continue;
    330 
    331         free(start);
    332         ret = munmap_orig(maps[i + 1], length);
    333         maps[i] = NULL;
    334         maps[i + 1] = NULL;
    335         debug("munmap(%p, %li) = %i", start, (long int)length, ret);
    336         return ret;
    337     }
    338 
    339     return munmap_orig(start, length);
    340 }
    341 
    342 #ifdef HAVE_MAP_FD
    343 kern_return_t map_fd(int fd, vm_offset_t offset, vm_offset_t *addr,
    344                      boolean_t find_space, vm_size_t numbytes)
    345 {
    346     kern_return_t ret;
    347 
    348     if(!_zz_ready)
    349         LOADSYM(map_fd);
    350     ret = map_fd_orig(fd, offset, addr, find_space, numbytes);
    351     if(!_zz_ready || !_zz_iswatched(fd) || _zz_disabled)
    352         return ret;
    353 
    354     if(ret == 0 && numbytes)
    355     {
    356         /* FIXME: do we also have to rewind the filedescriptor like in mmap? */
    357         void *b = malloc(numbytes);
    358         memcpy(b, (void *)*addr, numbytes);
    359         _zz_fuzz(fd, b, numbytes);
    360         *addr = (vm_offset_t)b;
    361         /* FIXME: the map is never freed; there is no such thing as unmap_fd,
    362          * but I suppose that kind of map should go when the filedescriptor is
    363          * closed (unlike mmap, which returns a persistent buffer). */
    364 
    365         if(numbytes >= 4)
    366            debug("map_fd(%i, %lli, &%p, %i, %lli) = %i \"%c%c%c%c", fd,
    367                  (long long int)offset, (void *)*addr, (int)find_space,
    368                  (long long int)numbytes, ret, b[0], b[1], b[2], b[3]);
    369         else
    370            debug("map_fd(%i, %lli, &%p, %i, %lli) = %i \"%c", fd,
    371                  (long long int)offset, (void *)*addr, (int)find_space,
    372                  (long long int)numbytes, ret, b[0]);
    373     }
    374     else
    375         debug("map_fd(%i, %lli, &%p, %i, %lli) = %i", fd, (long long int)offset,
    376               (void *)*addr, (int)find_space, (long long int)numbytes, ret);
    377 
    378     return ret;
    379 }
    380 #endif
    381 
    382236int close(int fd)
    383237{
Note: See TracChangeset for help on using the changeset viewer.