Changeset 1494 for zzuf/trunk/src/load-stream.c
- Timestamp:
- Dec 17, 2006, 6:17:31 PM (16 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
zzuf/trunk/src/load-stream.c
r1490 r1494 14 14 15 15 /* 16 * preload.c: preloaded libraryfunctions16 * load-stream.c: loaded stream functions 17 17 */ 18 18 … … 21 21 /* Can't remember what that's for */ 22 22 #define _GNU_SOURCE 23 /* Use this to get lseek64() */24 #define _LARGEFILE64_SOURCE25 23 26 24 #if defined HAVE_STDINT_H … … 29 27 # include <inttypes.h> 30 28 #endif 29 #include <stdlib.h> 30 #include <regex.h> 31 #include <dlfcn.h> 32 31 33 #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>40 34 41 35 #include "libzzuf.h" 42 36 #include "debug.h" 43 37 #include "fuzz.h" 44 #include " preload.h"38 #include "load.h" 45 39 46 40 /* Library functions that we divert */ … … 52 46 static int (*fclose_orig) (FILE *fp); 53 47 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) 48 void zzuf_load_stream(void) 72 49 { 73 50 LOADSYM(fopen); … … 76 53 LOADSYM(fread); 77 54 LOADSYM(fclose); 78 79 LOADSYM(open);80 LOADSYM(open64);81 LOADSYM(read);82 LOADSYM(lseek);83 LOADSYM(lseek64);84 LOADSYM(close);85 55 } 86 56 … … 194 164 } 195 165 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
Note: See TracChangeset
for help on using the changeset viewer.