Ignore:
Timestamp:
Dec 17, 2006, 6:17:31 PM (16 years ago)
Author:
Sam Hocevar
Message:
  • Split preload.c into load-fd.c and load-stream.c.
File:
1 moved

Legend:

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

    r1490 r1494  
    1414
    1515/*
    16  *  preload.c: preloaded library functions
     16 *  load-fd.c: loaded file descriptor functions
    1717 */
    1818
     
    2929#   include <inttypes.h>
    3030#endif
    31 #include <stdio.h>
     31#include <stdlib.h>
     32#include <regex.h>
     33#include <dlfcn.h>
     34
    3235#include <sys/types.h>
    3336#include <unistd.h>
    34 #include <stdlib.h>
    3537#include <fcntl.h>
    36 #include <regex.h>
    37 
    3838#include <stdarg.h>
    39 #include <dlfcn.h>
    4039
    4140#include "libzzuf.h"
    4241#include "debug.h"
    4342#include "fuzz.h"
    44 #include "preload.h"
     43#include "load.h"
    4544
    4645/* Library functions that we divert */
    47 static FILE *  (*fopen_orig)   (const char *path, const char *mode);
    48 static FILE *  (*fopen64_orig) (const char *path, const char *mode);
    49 static int     (*fseek_orig)   (FILE *stream, long offset, int whence);
    50 static size_t  (*fread_orig)   (void *ptr, size_t size, size_t nmemb,
    51                                 FILE *stream);
    52 static int     (*fclose_orig)  (FILE *fp);
    53 
    5446static int     (*open_orig)    (const char *file, int oflag, ...);
    5547static int     (*open64_orig)  (const char *file, int oflag, ...);
     
    5951static int     (*close_orig)   (int fd);
    6052
    61 #define STR(x) #x
    62 #define ORIG(x) x##_orig
    63 
    64 #define LOADSYM(x) \
    65     do { \
    66         ORIG(x) = dlsym(RTLD_NEXT, STR(x)); \
    67         if(!ORIG(x)) \
    68             abort(); \
    69     } while(0)
    70 
    71 void zzuf_preload_libc(void)
     53void zzuf_load_fd(void)
    7254{
    73     LOADSYM(fopen);
    74     LOADSYM(fopen64);
    75     LOADSYM(fseek);
    76     LOADSYM(fread);
    77     LOADSYM(fclose);
    78 
    7955    LOADSYM(open);
    8056    LOADSYM(open64);
     
    8359    LOADSYM(lseek64);
    8460    LOADSYM(close);
    85 }
    86 
    87 /* Our function wrappers */
    88 #define FOPEN(fn) \
    89     do \
    90     { \
    91         if(!_zzuf_ready) \
    92             LOADSYM(fn); \
    93         ret = ORIG(fn)(path, mode); \
    94         if(!_zzuf_ready) \
    95             return ret; \
    96         if(ret) \
    97         { \
    98             if(_zzuf_include && \
    99                 regexec(_zzuf_include, path, 0, NULL, 0) == REG_NOMATCH) \
    100                 /* not included: ignore */ ; \
    101             else if(_zzuf_exclude && \
    102                     regexec(_zzuf_exclude, path, 0, NULL, 0) != REG_NOMATCH) \
    103                 /* excluded: ignore */ ; \
    104             else \
    105             { \
    106                 int fd = fileno(ret); \
    107                 files[fd].managed = 1; \
    108                 files[fd].pos = 0; \
    109                 debug(STR(fn) "(\"%s\", \"%s\") = %p", path, mode, ret); \
    110             } \
    111         } \
    112     } while(0)
    113 
    114 FILE *fopen(const char *path, const char *mode)
    115 {
    116     FILE *ret; FOPEN(fopen); return ret;
    117 }
    118 
    119 FILE *fopen64(const char *path, const char *mode)
    120 {
    121     FILE *ret; FOPEN(fopen64); return ret;
    122 }
    123 
    124 int fseek(FILE *stream, long offset, int whence)
    125 {
    126     int ret, fd;
    127 
    128     if(!_zzuf_ready)
    129         LOADSYM(fseek);
    130     ret = fseek_orig(stream, offset, whence);
    131     if(!_zzuf_ready)
    132         return ret;
    133 
    134     fd = fileno(stream);
    135     if(!files[fd].managed)
    136         return ret;
    137 
    138     debug("fseek(%p, %li, %i) = %i", stream, offset, whence, ret);
    139     if(ret == 0)
    140     {
    141         switch(whence)
    142         {
    143             case SEEK_SET: files[fd].pos = offset; break;
    144             case SEEK_CUR: files[fd].pos += offset; break;
    145             case SEEK_END: files[fd].pos = ftell(stream); break;
    146         }
    147     }
    148     return ret;
    149 }
    150 
    151 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
    152 {
    153     size_t ret;
    154     int fd;
    155 
    156     if(!_zzuf_ready)
    157         LOADSYM(fread);
    158     ret = fread_orig(ptr, size, nmemb, stream);
    159     if(!_zzuf_ready)
    160         return ret;
    161 
    162     fd = fileno(stream);
    163     if(!files[fd].managed)
    164         return ret;
    165 
    166     debug("fread(%p, %li, %li, %p) = %li",
    167           ptr, (long int)size, (long int)nmemb, stream, (long int)ret);
    168     if(ret > 0)
    169     {
    170         zzuf_fuzz(fd, ptr, ret * size);
    171         files[fd].pos += ret * size;
    172     }
    173     return ret;
    174 }
    175 
    176 int fclose(FILE *fp)
    177 {
    178     int ret, fd;
    179 
    180     if(!_zzuf_ready)
    181         LOADSYM(fclose);
    182     fd = fileno(fp);
    183     ret = fclose_orig(fp);
    184     if(!_zzuf_ready)
    185         return ret;
    186 
    187     if(!files[fd].managed)
    188         return ret;
    189 
    190     debug("fclose(%p) = %i", fp, ret);
    191     files[fd].managed = 0;
    192 
    193     return ret;
    19461}
    19562
Note: See TracChangeset for help on using the changeset viewer.