Changeset 1494 for zzuf/trunk


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.
Location:
zzuf/trunk
Files:
3 edited
1 copied
2 moved

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/README

    r1493 r1494  
    2828the behaviour without using zzuf:
    2929
    30   # zzuf -s 87423 -r 0.01 vlc -- movie.avi
     30  # zzuf -s 87423 -r 0.01 vlc movie.avi
    3131
    3232  # zzuf -s 87423 -r 0.01 cp movie.avi fuzzy-movie.avi
  • zzuf/trunk/src/Makefile.am

    r1487 r1494  
    55
    66pkglib_LTLIBRARIES = libzzuf.la
    7 libzzuf_la_SOURCES = libzzuf.c libzzuf.h fuzz.c fuzz.h debug.c debug.h preload.c preload.h random.c random.h
     7libzzuf_la_SOURCES = libzzuf.c libzzuf.h fuzz.c fuzz.h debug.c debug.h \
     8                     load-fd.c load-stream.c load..h random.c random.h
    89libzzuf_la_LDFLAGS = -module
    910libzzuf_la_LIBADD = -ldl
  • zzuf/trunk/src/libzzuf.c

    r1490 r1494  
    3636#include "libzzuf.h"
    3737#include "debug.h"
    38 #include "preload.h"
     38#include "load.h"
    3939
    4040/* Global variables */
     
    8888        files[i].managed = 0;
    8989
    90     zzuf_preload_libc();
     90    zzuf_load_fd();
     91    zzuf_load_stream();
    9192
    9293    _zzuf_ready = 1;
  • 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
  • zzuf/trunk/src/load-stream.c

    r1490 r1494  
    1414
    1515/*
    16  *  preload.c: preloaded library functions
     16 *  load-stream.c: loaded stream functions
    1717 */
    1818
     
    2121/* Can't remember what that's for */
    2222#define _GNU_SOURCE
    23 /* Use this to get lseek64() */
    24 #define _LARGEFILE64_SOURCE
    2523
    2624#if defined HAVE_STDINT_H
     
    2927#   include <inttypes.h>
    3028#endif
     29#include <stdlib.h>
     30#include <regex.h>
     31#include <dlfcn.h>
     32
    3133#include <stdio.h>
    32 #include <sys/types.h>
    33 #include <unistd.h>
    34 #include <stdlib.h>
    35 #include <fcntl.h>
    36 #include <regex.h>
    37 
    38 #include <stdarg.h>
    39 #include <dlfcn.h>
    4034
    4135#include "libzzuf.h"
    4236#include "debug.h"
    4337#include "fuzz.h"
    44 #include "preload.h"
     38#include "load.h"
    4539
    4640/* Library functions that we divert */
     
    5246static int     (*fclose_orig)  (FILE *fp);
    5347
    54 static int     (*open_orig)    (const char *file, int oflag, ...);
    55 static int     (*open64_orig)  (const char *file, int oflag, ...);
    56 static ssize_t (*read_orig)    (int fd, void *buf, size_t count);
    57 static off_t   (*lseek_orig)   (int fd, off_t offset, int whence);
    58 static off64_t (*lseek64_orig) (int fd, off64_t offset, int whence);
    59 static int     (*close_orig)   (int fd);
    60 
    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)
     48void zzuf_load_stream(void)
    7249{
    7350    LOADSYM(fopen);
     
    7653    LOADSYM(fread);
    7754    LOADSYM(fclose);
    78 
    79     LOADSYM(open);
    80     LOADSYM(open64);
    81     LOADSYM(read);
    82     LOADSYM(lseek);
    83     LOADSYM(lseek64);
    84     LOADSYM(close);
    8555}
    8656
     
    194164}
    195165
    196 #define OPEN(fn) \
    197     do \
    198     { \
    199         int mode = 0; \
    200         if(!_zzuf_ready) \
    201             LOADSYM(fn); \
    202         if(oflag & O_CREAT) \
    203         { \
    204             va_list va; \
    205             va_start(va, oflag); \
    206             mode = va_arg(va, int); \
    207             va_end(va); \
    208             ret = ORIG(fn)(file, oflag, mode); \
    209         } \
    210         else \
    211         { \
    212             ret = ORIG(fn)(file, oflag); \
    213         } \
    214         if(!_zzuf_ready) \
    215             return ret; \
    216         if(ret >= 0 \
    217             && ((oflag & (O_RDONLY | O_RDWR | O_WRONLY)) != O_WRONLY)) \
    218         { \
    219             if(_zzuf_include && \
    220                 regexec(_zzuf_include, file, 0, NULL, 0) == REG_NOMATCH) \
    221                 /* not included: ignore */ ; \
    222             else if(_zzuf_exclude && \
    223                     regexec(_zzuf_exclude, file, 0, NULL, 0) != REG_NOMATCH) \
    224                 /* excluded: ignore */ ; \
    225             else \
    226             { \
    227                 if(oflag & O_CREAT) \
    228                     debug(STR(fn) "(\"%s\", %i, %i) = %i", \
    229                           file, oflag, mode, ret); \
    230                 else \
    231                     debug(STR(fn) "(\"%s\", %i) = %i", file, oflag, ret); \
    232                 files[ret].managed = 1; \
    233                 files[ret].pos = 0; \
    234             } \
    235         } \
    236     } while(0)
    237 
    238 int open(const char *file, int oflag, ...)
    239 {
    240     int ret; OPEN(open); return ret;
    241 }
    242 
    243 int open64(const char *file, int oflag, ...)
    244 {
    245     int ret; OPEN(open64); return ret;
    246 }
    247 
    248 ssize_t read(int fd, void *buf, size_t count)
    249 {
    250     int ret;
    251 
    252     if(!_zzuf_ready)
    253         LOADSYM(read);
    254     ret = read_orig(fd, buf, count);
    255     if(!_zzuf_ready)
    256         return ret;
    257 
    258     if(!files[fd].managed)
    259         return ret;
    260 
    261     debug("read(%i, %p, %li) = %i", fd, buf, (long int)count, ret);
    262     if(ret > 0)
    263     {
    264         zzuf_fuzz(fd, buf, ret);
    265         files[fd].pos += ret;
    266     }
    267 
    268     /* Sanity check, can be OK though (for instance with a character device) */
    269     if((uint64_t)lseek64_orig(fd, 0, SEEK_CUR) != files[fd].pos)
    270         debug("warning: offset inconsistency");
    271 
    272     return ret;
    273 }
    274 
    275 #define LSEEK(fn, off_t) \
    276     do { \
    277         if(!_zzuf_ready) \
    278             LOADSYM(fn); \
    279         ret = ORIG(fn)(fd, offset, whence); \
    280         if(!_zzuf_ready) \
    281             return ret; \
    282         if(!files[fd].managed) \
    283             return ret; \
    284         debug(STR(fn)"(%i, %lli, %i) = %lli", \
    285               fd, (long long int)offset, whence, (long long int)ret); \
    286         if(ret != (off_t)-1) \
    287             files[fd].pos = (int64_t)ret; \
    288     } while(0)
    289 
    290 off_t lseek(int fd, off_t offset, int whence)
    291 {
    292     off_t ret;
    293     LSEEK(lseek, off_t);
    294     return ret;
    295 }
    296 
    297 off64_t lseek64(int fd, off64_t offset, int whence)
    298 {
    299     off64_t ret;
    300     LSEEK(lseek64, off64_t);
    301     return ret;
    302 }
    303 
    304 int close(int fd)
    305 {
    306     int ret;
    307 
    308     if(!_zzuf_ready)
    309         LOADSYM(close);
    310     ret = close_orig(fd);
    311     if(!_zzuf_ready)
    312         return ret;
    313 
    314     if(!files[fd].managed)
    315         return ret;
    316 
    317     debug("close(%i) = %i", fd, ret);
    318     files[fd].managed = 0;
    319 
    320     return ret;
    321 }
    322 
  • zzuf/trunk/src/load.h

    r1490 r1494  
    1717 */
    1818
    19 extern void zzuf_preload_libc(void);
     19#define STR(x) #x
     20#define ORIG(x) x##_orig
    2021
     22#define LOADSYM(x) \
     23    do { \
     24        ORIG(x) = dlsym(RTLD_NEXT, STR(x)); \
     25        if(!ORIG(x)) \
     26            abort(); \
     27    } while(0)
     28
     29extern void zzuf_load_fd(void);
     30extern void zzuf_load_stream(void);
     31
Note: See TracChangeset for help on using the changeset viewer.