| 1 | /* |
|---|
| 2 | * zzuf - general purpose fuzzer |
|---|
| 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * $Id$ |
|---|
| 7 | * |
|---|
| 8 | * This program is free software. It comes without any warranty, to |
|---|
| 9 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | /* |
|---|
| 16 | * load-stream.c: loaded stream functions |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include "config.h" |
|---|
| 20 | |
|---|
| 21 | /* Needed for getline() and getdelim() */ |
|---|
| 22 | #define _GNU_SOURCE |
|---|
| 23 | /* Needed for getc_unlocked() on OpenSolaris */ |
|---|
| 24 | #define __EXTENSIONS__ |
|---|
| 25 | |
|---|
| 26 | #if defined HAVE___SREFILL || defined HAVE___FILBUF |
|---|
| 27 | # define HAVE_REFILL_STDIO |
|---|
| 28 | #endif |
|---|
| 29 | |
|---|
| 30 | #if defined HAVE_STDINT_H |
|---|
| 31 | # include <stdint.h> |
|---|
| 32 | #elif defined HAVE_INTTYPES_H |
|---|
| 33 | # include <inttypes.h> |
|---|
| 34 | #endif |
|---|
| 35 | #include <stdlib.h> |
|---|
| 36 | |
|---|
| 37 | #include <stdio.h> |
|---|
| 38 | #include <sys/types.h> |
|---|
| 39 | #if defined HAVE_REFILL_STDIO && defined HAVE_UNISTD_H |
|---|
| 40 | # include <unistd.h> /* Needed for __srefill’s lseek() call */ |
|---|
| 41 | #endif |
|---|
| 42 | |
|---|
| 43 | #include "libzzuf.h" |
|---|
| 44 | #include "lib-load.h" |
|---|
| 45 | #include "debug.h" |
|---|
| 46 | #include "fuzz.h" |
|---|
| 47 | #include "fd.h" |
|---|
| 48 | |
|---|
| 49 | #if defined HAVE___SREFILL |
|---|
| 50 | int NEW(__srefill)(FILE *fp); |
|---|
| 51 | #endif |
|---|
| 52 | |
|---|
| 53 | #if defined HAVE___FILBUF |
|---|
| 54 | int NEW(__filbuf)(FILE *fp); |
|---|
| 55 | #endif |
|---|
| 56 | |
|---|
| 57 | /* Library functions that we divert */ |
|---|
| 58 | static FILE * (*ORIG(fopen)) (const char *path, const char *mode); |
|---|
| 59 | #if defined HAVE_FOPEN64 |
|---|
| 60 | static FILE * (*ORIG(fopen64)) (const char *path, const char *mode); |
|---|
| 61 | #endif |
|---|
| 62 | #if defined HAVE___FOPEN64 |
|---|
| 63 | static FILE * (*ORIG(__fopen64))(const char *path, const char *mode); |
|---|
| 64 | #endif |
|---|
| 65 | static FILE * (*ORIG(freopen)) (const char *path, const char *mode, |
|---|
| 66 | FILE *stream); |
|---|
| 67 | #if defined HAVE_FREOPEN64 |
|---|
| 68 | static FILE * (*ORIG(freopen64))(const char *path, const char *mode, |
|---|
| 69 | FILE *stream); |
|---|
| 70 | #endif |
|---|
| 71 | #if defined HAVE___FREOPEN64 |
|---|
| 72 | static FILE * (*ORIG(__freopen64)) (const char *path, const char *mode, |
|---|
| 73 | FILE *stream); |
|---|
| 74 | #endif |
|---|
| 75 | static int (*ORIG(fseek)) (FILE *stream, long offset, int whence); |
|---|
| 76 | #if defined HAVE_FSEEKO |
|---|
| 77 | static int (*ORIG(fseeko)) (FILE *stream, off_t offset, int whence); |
|---|
| 78 | #endif |
|---|
| 79 | #if defined HAVE_FSEEKO64 |
|---|
| 80 | static int (*ORIG(fseeko64)) (FILE *stream, off_t offset, int whence); |
|---|
| 81 | #endif |
|---|
| 82 | #if defined HAVE___FSEEKO64 |
|---|
| 83 | static int (*ORIG(__fseeko64)) (FILE *stream, off_t offset, int whence); |
|---|
| 84 | #endif |
|---|
| 85 | #if defined HAVE_FSETPOS64 |
|---|
| 86 | static int (*ORIG(fsetpos64))(FILE *stream, const fpos64_t *pos); |
|---|
| 87 | #endif |
|---|
| 88 | #if defined HAVE___FSETPOS64 |
|---|
| 89 | static int (*ORIG(__fsetpos64)) (FILE *stream, const fpos64_t *pos); |
|---|
| 90 | #endif |
|---|
| 91 | static void (*ORIG(rewind)) (FILE *stream); |
|---|
| 92 | static size_t (*ORIG(fread)) (void *ptr, size_t size, size_t nmemb, |
|---|
| 93 | FILE *stream); |
|---|
| 94 | #if defined HAVE_FREAD_UNLOCKED |
|---|
| 95 | static size_t (*ORIG(fread_unlocked)) (void *ptr, size_t size, size_t nmemb, |
|---|
| 96 | FILE *stream); |
|---|
| 97 | #endif |
|---|
| 98 | static int (*ORIG(getc)) (FILE *stream); |
|---|
| 99 | static int (*ORIG(getchar)) (void); |
|---|
| 100 | static int (*ORIG(fgetc)) (FILE *stream); |
|---|
| 101 | #if defined HAVE__IO_GETC |
|---|
| 102 | static int (*ORIG(_IO_getc)) (FILE *stream); |
|---|
| 103 | #endif |
|---|
| 104 | #if defined HAVE_GETC_UNLOCKED |
|---|
| 105 | static int (*ORIG(getc_unlocked)) (FILE *stream); |
|---|
| 106 | #endif |
|---|
| 107 | #if defined HAVE_GETCHAR_UNLOCKED |
|---|
| 108 | static int (*ORIG(getchar_unlocked)) (void); |
|---|
| 109 | #endif |
|---|
| 110 | #if defined HAVE_FGETC_UNLOCKED |
|---|
| 111 | static int (*ORIG(fgetc_unlocked)) (FILE *stream); |
|---|
| 112 | #endif |
|---|
| 113 | static char * (*ORIG(fgets)) (char *s, int size, FILE *stream); |
|---|
| 114 | #if defined HAVE_FGETS_UNLOCKED |
|---|
| 115 | static char * (*ORIG(fgets_unlocked)) (char *s, int size, FILE *stream); |
|---|
| 116 | #endif |
|---|
| 117 | static int (*ORIG(ungetc)) (int c, FILE *stream); |
|---|
| 118 | static int (*ORIG(fclose)) (FILE *fp); |
|---|
| 119 | |
|---|
| 120 | /* Additional GNUisms */ |
|---|
| 121 | #if defined HAVE_GETLINE |
|---|
| 122 | static ssize_t (*ORIG(getline)) (char **lineptr, size_t *n, FILE *stream); |
|---|
| 123 | #endif |
|---|
| 124 | #if defined HAVE_GETDELIM |
|---|
| 125 | static ssize_t (*ORIG(getdelim)) (char **lineptr, size_t *n, int delim, |
|---|
| 126 | FILE *stream); |
|---|
| 127 | #endif |
|---|
| 128 | #if defined HAVE___GETDELIM |
|---|
| 129 | static ssize_t (*ORIG(__getdelim)) (char **lineptr, size_t *n, int delim, |
|---|
| 130 | FILE *stream); |
|---|
| 131 | #endif |
|---|
| 132 | |
|---|
| 133 | /* Additional BSDisms */ |
|---|
| 134 | #if defined HAVE_FGETLN |
|---|
| 135 | static char * (*ORIG(fgetln)) (FILE *stream, size_t *len); |
|---|
| 136 | #endif |
|---|
| 137 | #if defined HAVE___SREFILL |
|---|
| 138 | int (*ORIG(__srefill)) (FILE *fp); |
|---|
| 139 | #endif |
|---|
| 140 | |
|---|
| 141 | /* Additional HP-UXisms */ |
|---|
| 142 | #if defined HAVE___FILBUF |
|---|
| 143 | int (*ORIG(__filbuf)) (FILE *fp); |
|---|
| 144 | #endif |
|---|
| 145 | |
|---|
| 146 | /* Our function wrappers */ |
|---|
| 147 | #if defined HAVE_REFILL_STDIO /* Fuzz fp if we have __srefill() */ |
|---|
| 148 | # define FOPEN_FUZZ() \ |
|---|
| 149 | _zz_fuzz(fd, ret->FILE_PTR, ret->FILE_CNT) |
|---|
| 150 | #else |
|---|
| 151 | # define FOPEN_FUZZ() |
|---|
| 152 | #endif |
|---|
| 153 | |
|---|
| 154 | #define FOPEN(fn) \ |
|---|
| 155 | do \ |
|---|
| 156 | { \ |
|---|
| 157 | LOADSYM(fn); \ |
|---|
| 158 | if(!_zz_ready) \ |
|---|
| 159 | return ORIG(fn)(path, mode); \ |
|---|
| 160 | _zz_lock(-1); \ |
|---|
| 161 | ret = ORIG(fn)(path, mode); \ |
|---|
| 162 | _zz_unlock(-1); \ |
|---|
| 163 | if(ret && _zz_mustwatch(path)) \ |
|---|
| 164 | { \ |
|---|
| 165 | int fd = fileno(ret); \ |
|---|
| 166 | _zz_register(fd); \ |
|---|
| 167 | debug("%s(\"%s\", \"%s\") = [%i]", __func__, path, mode, fd); \ |
|---|
| 168 | FOPEN_FUZZ(); \ |
|---|
| 169 | } \ |
|---|
| 170 | } while(0) |
|---|
| 171 | |
|---|
| 172 | FILE *NEW(fopen)(const char *path, const char *mode) |
|---|
| 173 | { |
|---|
| 174 | FILE *ret; FOPEN(fopen); return ret; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | #if defined HAVE_FOPEN64 |
|---|
| 178 | FILE *NEW(fopen64)(const char *path, const char *mode) |
|---|
| 179 | { |
|---|
| 180 | FILE *ret; FOPEN(fopen64); return ret; |
|---|
| 181 | } |
|---|
| 182 | #endif |
|---|
| 183 | |
|---|
| 184 | #if defined HAVE___FOPEN64 |
|---|
| 185 | FILE *NEW(__fopen64)(const char *path, const char *mode) |
|---|
| 186 | { |
|---|
| 187 | FILE *ret; FOPEN(__fopen64); return ret; |
|---|
| 188 | } |
|---|
| 189 | #endif |
|---|
| 190 | |
|---|
| 191 | #define FREOPEN(fn) \ |
|---|
| 192 | do \ |
|---|
| 193 | { \ |
|---|
| 194 | int fd0 = -1, fd1 = -1, disp = 0; \ |
|---|
| 195 | LOADSYM(fn); \ |
|---|
| 196 | if(_zz_ready && (fd0 = fileno(stream)) >= 0 && _zz_iswatched(fd0)) \ |
|---|
| 197 | { \ |
|---|
| 198 | _zz_unregister(fd0); \ |
|---|
| 199 | disp = 1; \ |
|---|
| 200 | } \ |
|---|
| 201 | _zz_lock(-1); \ |
|---|
| 202 | ret = ORIG(fn)(path, mode, stream); \ |
|---|
| 203 | _zz_unlock(-1); \ |
|---|
| 204 | if(ret && _zz_mustwatch(path)) \ |
|---|
| 205 | { \ |
|---|
| 206 | fd1 = fileno(ret); \ |
|---|
| 207 | _zz_register(fd1); \ |
|---|
| 208 | disp = 1; \ |
|---|
| 209 | } \ |
|---|
| 210 | if(disp) \ |
|---|
| 211 | debug("%s(\"%s\", \"%s\", [%i]) = [%i]", __func__, \ |
|---|
| 212 | path, mode, fd0, fd1); \ |
|---|
| 213 | } while(0) |
|---|
| 214 | |
|---|
| 215 | FILE *NEW(freopen)(const char *path, const char *mode, FILE *stream) |
|---|
| 216 | { |
|---|
| 217 | FILE *ret; FREOPEN(freopen); return ret; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | #if defined HAVE_FREOPEN64 |
|---|
| 221 | FILE *NEW(freopen64)(const char *path, const char *mode, FILE *stream) |
|---|
| 222 | { |
|---|
| 223 | FILE *ret; FREOPEN(freopen64); return ret; |
|---|
| 224 | } |
|---|
| 225 | #endif |
|---|
| 226 | |
|---|
| 227 | #if defined HAVE___FREOPEN64 |
|---|
| 228 | FILE *NEW(__freopen64)(const char *path, const char *mode, FILE *stream) |
|---|
| 229 | { |
|---|
| 230 | FILE *ret; FREOPEN(__freopen64); return ret; |
|---|
| 231 | } |
|---|
| 232 | #endif |
|---|
| 233 | |
|---|
| 234 | #if defined HAVE_REFILL_STDIO /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 235 | # define FSEEK_FUZZ(fn2) |
|---|
| 236 | #else |
|---|
| 237 | # define FSEEK_FUZZ(fn2) \ |
|---|
| 238 | if(ret == 0) \ |
|---|
| 239 | { \ |
|---|
| 240 | /* FIXME: check what happens when fseek()ing a pipe */ \ |
|---|
| 241 | switch(whence) \ |
|---|
| 242 | { \ |
|---|
| 243 | case SEEK_END: \ |
|---|
| 244 | offset = fn2(stream); \ |
|---|
| 245 | /* fall through */ \ |
|---|
| 246 | case SEEK_SET: \ |
|---|
| 247 | _zz_setpos(fd, offset); \ |
|---|
| 248 | break; \ |
|---|
| 249 | case SEEK_CUR: \ |
|---|
| 250 | _zz_addpos(fd, offset); \ |
|---|
| 251 | break; \ |
|---|
| 252 | } \ |
|---|
| 253 | } |
|---|
| 254 | #endif |
|---|
| 255 | |
|---|
| 256 | #define FSEEK(fn, fn2) \ |
|---|
| 257 | do \ |
|---|
| 258 | { \ |
|---|
| 259 | int fd; \ |
|---|
| 260 | LOADSYM(fn); \ |
|---|
| 261 | fd = fileno(stream); \ |
|---|
| 262 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) \ |
|---|
| 263 | return ORIG(fn)(stream, offset, whence); \ |
|---|
| 264 | _zz_lock(fd); \ |
|---|
| 265 | ret = ORIG(fn)(stream, offset, whence); \ |
|---|
| 266 | _zz_unlock(fd); \ |
|---|
| 267 | debug("%s([%i], %lli, %i) = %i", __func__, \ |
|---|
| 268 | fd, (long long int)offset, whence, ret); \ |
|---|
| 269 | FSEEK_FUZZ(fn2) \ |
|---|
| 270 | } while(0) |
|---|
| 271 | |
|---|
| 272 | int NEW(fseek)(FILE *stream, long offset, int whence) |
|---|
| 273 | { |
|---|
| 274 | int ret; FSEEK(fseek, ftell); return ret; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | #if defined HAVE_FSEEKO |
|---|
| 278 | int NEW(fseeko)(FILE *stream, off_t offset, int whence) |
|---|
| 279 | { |
|---|
| 280 | int ret; FSEEK(fseeko, ftello); return ret; |
|---|
| 281 | } |
|---|
| 282 | #endif |
|---|
| 283 | |
|---|
| 284 | #if defined HAVE_FSEEKO64 |
|---|
| 285 | int NEW(fseeko64)(FILE *stream, off64_t offset, int whence) |
|---|
| 286 | { |
|---|
| 287 | int ret; FSEEK(fseeko64, ftello); return ret; |
|---|
| 288 | } |
|---|
| 289 | #endif |
|---|
| 290 | |
|---|
| 291 | #if defined HAVE___FSEEKO64 |
|---|
| 292 | int NEW(__fseeko64)(FILE *stream, off64_t offset, int whence) |
|---|
| 293 | { |
|---|
| 294 | int ret; FSEEK(__fseeko64, ftello); return ret; |
|---|
| 295 | } |
|---|
| 296 | #endif |
|---|
| 297 | |
|---|
| 298 | #define FSETPOS(fn) \ |
|---|
| 299 | do \ |
|---|
| 300 | { \ |
|---|
| 301 | int fd; \ |
|---|
| 302 | LOADSYM(fn); \ |
|---|
| 303 | fd = fileno(stream); \ |
|---|
| 304 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) \ |
|---|
| 305 | return ORIG(fn)(stream, pos); \ |
|---|
| 306 | _zz_lock(fd); \ |
|---|
| 307 | ret = ORIG(fn)(stream, pos); \ |
|---|
| 308 | _zz_unlock(fd); \ |
|---|
| 309 | debug("%s([%i], %lli) = %i", __func__, \ |
|---|
| 310 | fd, (long long int)*pos, ret); \ |
|---|
| 311 | /* On HP-UX at least, fpos64_t == int64_t */ \ |
|---|
| 312 | _zz_setpos(fd, (int64_t)*pos); \ |
|---|
| 313 | } \ |
|---|
| 314 | while(0) |
|---|
| 315 | |
|---|
| 316 | #if defined HAVE_FSETPOS64 |
|---|
| 317 | int NEW(fsetpos64)(FILE *stream, const fpos64_t *pos) |
|---|
| 318 | { |
|---|
| 319 | int ret; FSETPOS(fsetpos64); return ret; |
|---|
| 320 | } |
|---|
| 321 | #endif |
|---|
| 322 | |
|---|
| 323 | #if defined HAVE___FSETPOS64 |
|---|
| 324 | int NEW(__fsetpos64)(FILE *stream, const fpos64_t *pos) |
|---|
| 325 | { |
|---|
| 326 | int ret; FSETPOS(__fsetpos64); return ret; |
|---|
| 327 | } |
|---|
| 328 | #endif |
|---|
| 329 | |
|---|
| 330 | void NEW(rewind)(FILE *stream) |
|---|
| 331 | { |
|---|
| 332 | int fd; |
|---|
| 333 | |
|---|
| 334 | LOADSYM(rewind); |
|---|
| 335 | fd = fileno(stream); |
|---|
| 336 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) |
|---|
| 337 | { |
|---|
| 338 | ORIG(rewind)(stream); |
|---|
| 339 | return; |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | _zz_lock(fd); |
|---|
| 343 | ORIG(rewind)(stream); |
|---|
| 344 | _zz_unlock(fd); |
|---|
| 345 | debug("%s([%i])", __func__, fd); |
|---|
| 346 | |
|---|
| 347 | #if defined HAVE_REFILL_STDIO /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 348 | #else |
|---|
| 349 | /* FIXME: check what happens when rewind()ing a pipe */ |
|---|
| 350 | _zz_setpos(fd, 0); |
|---|
| 351 | #endif |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | #if defined HAVE_REFILL_STDIO /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 355 | # define FREAD_FUZZ() \ |
|---|
| 356 | do \ |
|---|
| 357 | { \ |
|---|
| 358 | debug("%s(%p, %li, %li, [%i]) = %li", __func__, ptr, \ |
|---|
| 359 | (long int)size, (long int)nmemb, fd, (long int)ret); \ |
|---|
| 360 | } while(0) |
|---|
| 361 | #else |
|---|
| 362 | # define FREAD_FUZZ() \ |
|---|
| 363 | do \ |
|---|
| 364 | { \ |
|---|
| 365 | int64_t newpos = ftell(stream); \ |
|---|
| 366 | /* XXX: the number of bytes read is not ret * size, because \ |
|---|
| 367 | * a partial read may have advanced the stream pointer. However, \ |
|---|
| 368 | * when reading from a pipe ftell() will return 0, and ret * size \ |
|---|
| 369 | * is then better than nothing. */ \ |
|---|
| 370 | if(newpos <= 0) \ |
|---|
| 371 | { \ |
|---|
| 372 | pos = _zz_getpos(fd); \ |
|---|
| 373 | newpos = pos + ret * size; \ |
|---|
| 374 | } \ |
|---|
| 375 | if(newpos != pos) \ |
|---|
| 376 | { \ |
|---|
| 377 | char *b = ptr; \ |
|---|
| 378 | _zz_fuzz(fd, ptr, newpos - pos); \ |
|---|
| 379 | _zz_setpos(fd, newpos); \ |
|---|
| 380 | if(newpos >= pos + 4) \ |
|---|
| 381 | debug("%s(%p, %li, %li, [%i]) = %li \"%c%c%c%c...", __func__, \ |
|---|
| 382 | ptr, (long int)size, (long int)nmemb, fd, \ |
|---|
| 383 | (long int)ret, b[0], b[1], b[2], b[3]); \ |
|---|
| 384 | else \ |
|---|
| 385 | debug("%s(%p, %li, %li, [%i]) = %li \"%c...", __func__, ptr, \ |
|---|
| 386 | (long int)size, (long int)nmemb, fd, \ |
|---|
| 387 | (long int)ret, b[0]); \ |
|---|
| 388 | } \ |
|---|
| 389 | else \ |
|---|
| 390 | debug("%s(%p, %li, %li, [%i]) = %li", __func__, ptr, \ |
|---|
| 391 | (long int)size, (long int)nmemb, fd, (long int)ret); \ |
|---|
| 392 | } while(0) |
|---|
| 393 | #endif |
|---|
| 394 | |
|---|
| 395 | #define FREAD(fn) \ |
|---|
| 396 | do \ |
|---|
| 397 | { \ |
|---|
| 398 | int64_t pos; \ |
|---|
| 399 | int fd; \ |
|---|
| 400 | LOADSYM(fn); \ |
|---|
| 401 | fd = fileno(stream); \ |
|---|
| 402 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) \ |
|---|
| 403 | return ORIG(fn)(ptr, size, nmemb, stream); \ |
|---|
| 404 | pos = ftell(stream); \ |
|---|
| 405 | _zz_lock(fd); \ |
|---|
| 406 | ret = ORIG(fn)(ptr, size, nmemb, stream); \ |
|---|
| 407 | _zz_unlock(fd); \ |
|---|
| 408 | FREAD_FUZZ(); \ |
|---|
| 409 | } while(0) |
|---|
| 410 | |
|---|
| 411 | size_t NEW(fread)(void *ptr, size_t size, size_t nmemb, FILE *stream) |
|---|
| 412 | { |
|---|
| 413 | size_t ret; FREAD(fread); return ret; |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | #if defined HAVE_FREAD_UNLOCKED |
|---|
| 417 | #undef fread_unlocked /* can be a macro; we don’t want that */ |
|---|
| 418 | size_t NEW(fread_unlocked)(void *ptr, size_t size, size_t nmemb, FILE *stream) |
|---|
| 419 | { |
|---|
| 420 | size_t ret; FREAD(fread_unlocked); return ret; |
|---|
| 421 | } |
|---|
| 422 | #endif |
|---|
| 423 | |
|---|
| 424 | #if defined HAVE_REFILL_STDIO /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 425 | # define FGETC_FUZZ |
|---|
| 426 | #else |
|---|
| 427 | # define FGETC_FUZZ \ |
|---|
| 428 | if(ret != EOF) \ |
|---|
| 429 | { \ |
|---|
| 430 | uint8_t ch = ret; \ |
|---|
| 431 | _zz_fuzz(fd, &ch, 1); \ |
|---|
| 432 | _zz_addpos(fd, 1); \ |
|---|
| 433 | ret = ch; \ |
|---|
| 434 | } |
|---|
| 435 | #endif |
|---|
| 436 | |
|---|
| 437 | #define FGETC(fn, s, arg) \ |
|---|
| 438 | do { \ |
|---|
| 439 | int fd; \ |
|---|
| 440 | LOADSYM(fn); \ |
|---|
| 441 | fd = fileno(s); \ |
|---|
| 442 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) \ |
|---|
| 443 | return ORIG(fn)(arg); \ |
|---|
| 444 | _zz_lock(fd); \ |
|---|
| 445 | ret = ORIG(fn)(arg); \ |
|---|
| 446 | _zz_unlock(fd); \ |
|---|
| 447 | FGETC_FUZZ \ |
|---|
| 448 | if(ret == EOF) \ |
|---|
| 449 | debug("%s([%i]) = EOF", __func__, fd); \ |
|---|
| 450 | else \ |
|---|
| 451 | debug("%s([%i]) = '%c'", __func__, fd, ret); \ |
|---|
| 452 | } while(0) |
|---|
| 453 | |
|---|
| 454 | #undef getc /* can be a macro; we don’t want that */ |
|---|
| 455 | int NEW(getc)(FILE *stream) |
|---|
| 456 | { |
|---|
| 457 | int ret; FGETC(getc, stream, stream); return ret; |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | #undef getchar /* can be a macro; we don’t want that */ |
|---|
| 461 | int NEW(getchar)(void) |
|---|
| 462 | { |
|---|
| 463 | int ret; FGETC(getchar, stdin, /* empty */); return ret; |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | int NEW(fgetc)(FILE *stream) |
|---|
| 467 | { |
|---|
| 468 | int ret; FGETC(fgetc, stream, stream); return ret; |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | #if defined HAVE__IO_GETC |
|---|
| 472 | int NEW(_IO_getc)(FILE *stream) |
|---|
| 473 | { |
|---|
| 474 | int ret; FGETC(_IO_getc, stream, stream); return ret; |
|---|
| 475 | } |
|---|
| 476 | #endif |
|---|
| 477 | |
|---|
| 478 | #if defined HAVE_GETC_UNLOCKED |
|---|
| 479 | #undef getc_unlocked /* can be a macro; we don’t want that */ |
|---|
| 480 | int NEW(getc_unlocked)(FILE *stream) |
|---|
| 481 | { |
|---|
| 482 | int ret; FGETC(getc_unlocked, stream, stream); return ret; |
|---|
| 483 | } |
|---|
| 484 | #endif |
|---|
| 485 | |
|---|
| 486 | #if defined HAVE_GETCHAR_UNLOCKED |
|---|
| 487 | #undef getchar_unlocked /* can be a macro; we don’t want that */ |
|---|
| 488 | int NEW(getchar_unlocked)(void) |
|---|
| 489 | { |
|---|
| 490 | int ret; FGETC(getchar_unlocked, stdin, /* empty */); return ret; |
|---|
| 491 | } |
|---|
| 492 | #endif |
|---|
| 493 | |
|---|
| 494 | #if defined HAVE_FGETC_UNLOCKED |
|---|
| 495 | #undef fgetc_unlocked /* can be a macro; we don’t want that */ |
|---|
| 496 | int NEW(fgetc_unlocked)(FILE *stream) |
|---|
| 497 | { |
|---|
| 498 | int ret; FGETC(fgetc_unlocked, stream, stream); return ret; |
|---|
| 499 | } |
|---|
| 500 | #endif |
|---|
| 501 | |
|---|
| 502 | #if defined HAVE_REFILL_STDIO /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 503 | # define FGETS_FUZZ(fn, fn2) \ |
|---|
| 504 | _zz_lock(fd); \ |
|---|
| 505 | ret = ORIG(fn)(s, size, stream); \ |
|---|
| 506 | _zz_unlock(fd); |
|---|
| 507 | #else |
|---|
| 508 | # define FGETS_FUZZ(fn, fn2) \ |
|---|
| 509 | if(size <= 0) \ |
|---|
| 510 | ret = NULL; \ |
|---|
| 511 | else if(size == 1) \ |
|---|
| 512 | s[0] = '\0'; \ |
|---|
| 513 | else \ |
|---|
| 514 | { \ |
|---|
| 515 | int i; \ |
|---|
| 516 | for(i = 0; i < size - 1; i++) \ |
|---|
| 517 | { \ |
|---|
| 518 | int ch; \ |
|---|
| 519 | _zz_lock(fd); \ |
|---|
| 520 | ch = ORIG(fn2)(stream); \ |
|---|
| 521 | _zz_unlock(fd); \ |
|---|
| 522 | if(ch == EOF) \ |
|---|
| 523 | { \ |
|---|
| 524 | s[i] = '\0'; \ |
|---|
| 525 | if(!i) \ |
|---|
| 526 | ret = NULL; \ |
|---|
| 527 | break; \ |
|---|
| 528 | } \ |
|---|
| 529 | s[i] = (char)(unsigned char)ch; \ |
|---|
| 530 | _zz_fuzz(fd, (uint8_t *)s + i, 1); /* rather inefficient */ \ |
|---|
| 531 | _zz_addpos(fd, 1); \ |
|---|
| 532 | if(s[i] == '\n') \ |
|---|
| 533 | { \ |
|---|
| 534 | s[i + 1] = '\0'; \ |
|---|
| 535 | break; \ |
|---|
| 536 | } \ |
|---|
| 537 | } \ |
|---|
| 538 | } |
|---|
| 539 | #endif |
|---|
| 540 | |
|---|
| 541 | #define FGETS(fn, fn2) \ |
|---|
| 542 | do \ |
|---|
| 543 | { \ |
|---|
| 544 | int fd; \ |
|---|
| 545 | ret = s; \ |
|---|
| 546 | LOADSYM(fn); \ |
|---|
| 547 | LOADSYM(fn2); \ |
|---|
| 548 | fd = fileno(stream); \ |
|---|
| 549 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) \ |
|---|
| 550 | return ORIG(fn)(s, size, stream); \ |
|---|
| 551 | FGETS_FUZZ(fn, fn2) \ |
|---|
| 552 | debug("%s(%p, %i, [%i]) = %p", __func__, s, size, fd, ret); \ |
|---|
| 553 | } while(0) |
|---|
| 554 | |
|---|
| 555 | char *NEW(fgets)(char *s, int size, FILE *stream) |
|---|
| 556 | { |
|---|
| 557 | char *ret; FGETS(fgets, fgetc); return ret; |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | #if defined HAVE_FGETS_UNLOCKED |
|---|
| 561 | char *NEW(fgets_unlocked)(char *s, int size, FILE *stream) |
|---|
| 562 | { |
|---|
| 563 | char *ret; FGETS(fgets_unlocked, fgetc_unlocked); return ret; |
|---|
| 564 | } |
|---|
| 565 | #endif |
|---|
| 566 | |
|---|
| 567 | int NEW(ungetc)(int c, FILE *stream) |
|---|
| 568 | { |
|---|
| 569 | int ret, fd; |
|---|
| 570 | |
|---|
| 571 | LOADSYM(ungetc); |
|---|
| 572 | fd = fileno(stream); |
|---|
| 573 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) |
|---|
| 574 | return ORIG(ungetc)(c, stream); |
|---|
| 575 | |
|---|
| 576 | _zz_lock(fd); |
|---|
| 577 | ret = ORIG(ungetc)(c, stream); |
|---|
| 578 | _zz_unlock(fd); |
|---|
| 579 | |
|---|
| 580 | if(ret != EOF) |
|---|
| 581 | { |
|---|
| 582 | struct fuzz *fuzz = _zz_getfuzz(fd); |
|---|
| 583 | fuzz->uflag = 1; |
|---|
| 584 | fuzz->upos = _zz_getpos(fd) - 1; |
|---|
| 585 | fuzz->uchar = c; |
|---|
| 586 | #if defined HAVE_REFILL_STDIO /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 587 | #else |
|---|
| 588 | _zz_addpos(fd, -1); |
|---|
| 589 | #endif |
|---|
| 590 | } |
|---|
| 591 | |
|---|
| 592 | if(ret == EOF) |
|---|
| 593 | debug("%s(0x%02x, [%i]) = EOF", __func__, c, fd); |
|---|
| 594 | else |
|---|
| 595 | debug("%s(0x%02x, [%i]) = '%c'", __func__, c, fd, ret); |
|---|
| 596 | |
|---|
| 597 | return ret; |
|---|
| 598 | } |
|---|
| 599 | |
|---|
| 600 | int NEW(fclose)(FILE *fp) |
|---|
| 601 | { |
|---|
| 602 | int ret, fd; |
|---|
| 603 | |
|---|
| 604 | LOADSYM(fclose); |
|---|
| 605 | fd = fileno(fp); |
|---|
| 606 | if(!_zz_ready || !_zz_iswatched(fd)) |
|---|
| 607 | return ORIG(fclose)(fp); |
|---|
| 608 | |
|---|
| 609 | _zz_lock(fd); |
|---|
| 610 | ret = ORIG(fclose)(fp); |
|---|
| 611 | _zz_unlock(fd); |
|---|
| 612 | debug("%s([%i]) = %i", __func__, fd, ret); |
|---|
| 613 | _zz_unregister(fd); |
|---|
| 614 | |
|---|
| 615 | return ret; |
|---|
| 616 | } |
|---|
| 617 | |
|---|
| 618 | #define GETDELIM(fn, delim, need_delim) \ |
|---|
| 619 | do { \ |
|---|
| 620 | char *line; \ |
|---|
| 621 | ssize_t done, size; \ |
|---|
| 622 | int fd, finished = 0; \ |
|---|
| 623 | LOADSYM(fn); \ |
|---|
| 624 | LOADSYM(getdelim); \ |
|---|
| 625 | LOADSYM(fgetc); \ |
|---|
| 626 | fd = fileno(stream); \ |
|---|
| 627 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) \ |
|---|
| 628 | return ORIG(getdelim)(lineptr, n, delim, stream); \ |
|---|
| 629 | line = *lineptr; \ |
|---|
| 630 | size = line ? *n : 0; \ |
|---|
| 631 | ret = done = finished = 0; \ |
|---|
| 632 | for(;;) \ |
|---|
| 633 | { \ |
|---|
| 634 | int ch; \ |
|---|
| 635 | if(done >= size) /* highly inefficient but I don't care */ \ |
|---|
| 636 | line = realloc(line, size = done + 1); \ |
|---|
| 637 | if(finished) \ |
|---|
| 638 | { \ |
|---|
| 639 | line[done] = '\0'; \ |
|---|
| 640 | *n = size; \ |
|---|
| 641 | *lineptr = line; \ |
|---|
| 642 | break; \ |
|---|
| 643 | } \ |
|---|
| 644 | _zz_lock(fd); \ |
|---|
| 645 | ch = ORIG(fgetc)(stream); \ |
|---|
| 646 | _zz_unlock(fd); \ |
|---|
| 647 | if(ch == EOF) \ |
|---|
| 648 | { \ |
|---|
| 649 | finished = 1; \ |
|---|
| 650 | ret = done; \ |
|---|
| 651 | } \ |
|---|
| 652 | else \ |
|---|
| 653 | { \ |
|---|
| 654 | unsigned char c = ch; \ |
|---|
| 655 | _zz_fuzz(fd, &c, 1); /* even more inefficient */ \ |
|---|
| 656 | line[done++] = c; \ |
|---|
| 657 | _zz_addpos(fd, 1); \ |
|---|
| 658 | if(c == delim) \ |
|---|
| 659 | { \ |
|---|
| 660 | finished = 1; \ |
|---|
| 661 | ret = done; \ |
|---|
| 662 | } \ |
|---|
| 663 | } \ |
|---|
| 664 | } \ |
|---|
| 665 | if(need_delim) \ |
|---|
| 666 | debug("%s(%p, %p, '%c', [%i]) = %li", __func__, \ |
|---|
| 667 | lineptr, n, delim, fd, (long int)ret); \ |
|---|
| 668 | else \ |
|---|
| 669 | debug("%s(%p, %p, [%i]) = %li", __func__, \ |
|---|
| 670 | lineptr, n, fd, (long int)ret); \ |
|---|
| 671 | return ret; \ |
|---|
| 672 | } while(0) |
|---|
| 673 | |
|---|
| 674 | #if defined HAVE_GETLINE |
|---|
| 675 | ssize_t NEW(getline)(char **lineptr, size_t *n, FILE *stream) |
|---|
| 676 | { |
|---|
| 677 | ssize_t ret; GETDELIM(getline, '\n', 0); return ret; |
|---|
| 678 | } |
|---|
| 679 | #endif |
|---|
| 680 | |
|---|
| 681 | #if defined HAVE_GETDELIM |
|---|
| 682 | ssize_t NEW(getdelim)(char **lineptr, size_t *n, int delim, FILE *stream) |
|---|
| 683 | { |
|---|
| 684 | ssize_t ret; GETDELIM(getdelim, delim, 1); return ret; |
|---|
| 685 | } |
|---|
| 686 | #endif |
|---|
| 687 | |
|---|
| 688 | #if defined HAVE___GETDELIM |
|---|
| 689 | ssize_t NEW(__getdelim)(char **lineptr, size_t *n, int delim, FILE *stream) |
|---|
| 690 | { |
|---|
| 691 | ssize_t ret; GETDELIM(__getdelim, delim, 1); return ret; |
|---|
| 692 | } |
|---|
| 693 | #endif |
|---|
| 694 | |
|---|
| 695 | #if defined HAVE_FGETLN |
|---|
| 696 | char *NEW(fgetln)(FILE *stream, size_t *len) |
|---|
| 697 | { |
|---|
| 698 | char *ret; |
|---|
| 699 | #if defined HAVE_REFILL_STDIO /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 700 | #else |
|---|
| 701 | struct fuzz *fuzz; |
|---|
| 702 | size_t i, size; |
|---|
| 703 | #endif |
|---|
| 704 | int fd; |
|---|
| 705 | |
|---|
| 706 | LOADSYM(fgetln); |
|---|
| 707 | LOADSYM(fgetc); |
|---|
| 708 | fd = fileno(stream); |
|---|
| 709 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) |
|---|
| 710 | return ORIG(fgetln)(stream, len); |
|---|
| 711 | |
|---|
| 712 | #if defined HAVE_REFILL_STDIO /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 713 | _zz_lock(fd); |
|---|
| 714 | ret = ORIG(fgetln)(stream, len); |
|---|
| 715 | _zz_unlock(fd); |
|---|
| 716 | #else |
|---|
| 717 | fuzz = _zz_getfuzz(fd); |
|---|
| 718 | |
|---|
| 719 | for(i = size = 0; ; /* i is incremented below */) |
|---|
| 720 | { |
|---|
| 721 | int ch; |
|---|
| 722 | |
|---|
| 723 | _zz_lock(fd); |
|---|
| 724 | ch = ORIG(fgetc)(stream); |
|---|
| 725 | _zz_unlock(fd); |
|---|
| 726 | |
|---|
| 727 | if(ch == EOF) |
|---|
| 728 | break; |
|---|
| 729 | |
|---|
| 730 | if(i >= size) |
|---|
| 731 | fuzz->tmp = realloc(fuzz->tmp, (size += 80)); |
|---|
| 732 | |
|---|
| 733 | fuzz->tmp[i] = (char)(unsigned char)ch; |
|---|
| 734 | _zz_fuzz(fd, (uint8_t *)fuzz->tmp + i, 1); /* rather inefficient */ |
|---|
| 735 | _zz_addpos(fd, 1); |
|---|
| 736 | |
|---|
| 737 | if(fuzz->tmp[i++] == '\n') |
|---|
| 738 | break; |
|---|
| 739 | } |
|---|
| 740 | |
|---|
| 741 | *len = i; |
|---|
| 742 | ret = fuzz->tmp; |
|---|
| 743 | #endif |
|---|
| 744 | |
|---|
| 745 | debug("%s([%i], &%li) = %p", __func__, fd, (long int)*len, ret); |
|---|
| 746 | return ret; |
|---|
| 747 | } |
|---|
| 748 | #endif |
|---|
| 749 | |
|---|
| 750 | #if defined HAVE___SREFILL |
|---|
| 751 | int NEW(__srefill)(FILE *fp) |
|---|
| 752 | { |
|---|
| 753 | off_t newpos; |
|---|
| 754 | int ret, fd; |
|---|
| 755 | |
|---|
| 756 | LOADSYM(__srefill); |
|---|
| 757 | fd = fileno(fp); |
|---|
| 758 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) |
|---|
| 759 | return ORIG(__srefill)(fp); |
|---|
| 760 | |
|---|
| 761 | _zz_lock(fd); |
|---|
| 762 | ret = ORIG(__srefill)(fp); |
|---|
| 763 | newpos = lseek(fd, 0, SEEK_CUR); |
|---|
| 764 | _zz_unlock(fd); |
|---|
| 765 | if(ret != EOF) |
|---|
| 766 | { |
|---|
| 767 | /* FIXME: do we have to fuzz ret, too, like in __filbuf? */ |
|---|
| 768 | if(newpos != -1) |
|---|
| 769 | _zz_setpos(fd, newpos - fp->_r); |
|---|
| 770 | _zz_fuzz(fd, fp->_p, fp->_r); |
|---|
| 771 | _zz_addpos(fd, fp->_r); |
|---|
| 772 | } |
|---|
| 773 | |
|---|
| 774 | if(!_zz_islocked(fd)) |
|---|
| 775 | debug("%s([%i]) = %i", __func__, fd, ret); |
|---|
| 776 | |
|---|
| 777 | return ret; |
|---|
| 778 | } |
|---|
| 779 | #endif |
|---|
| 780 | |
|---|
| 781 | #if defined HAVE___FILBUF |
|---|
| 782 | int NEW(__filbuf)(FILE *fp) |
|---|
| 783 | { |
|---|
| 784 | off_t newpos; |
|---|
| 785 | int ret, fd; |
|---|
| 786 | |
|---|
| 787 | LOADSYM(__filbuf); |
|---|
| 788 | fd = fileno(fp); |
|---|
| 789 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) |
|---|
| 790 | return ORIG(__filbuf)(fp); |
|---|
| 791 | |
|---|
| 792 | _zz_lock(fd); |
|---|
| 793 | ret = ORIG(__filbuf)(fp); |
|---|
| 794 | newpos = lseek(fd, 0, SEEK_CUR); |
|---|
| 795 | _zz_unlock(fd); |
|---|
| 796 | if(ret != EOF) |
|---|
| 797 | { |
|---|
| 798 | if(newpos != -1) |
|---|
| 799 | _zz_setpos(fd, newpos - fp->FILE_CNT - 1); |
|---|
| 800 | _zz_fuzz(fd, fp->FILE_PTR - 1, fp->FILE_CNT + 1); |
|---|
| 801 | ret = (uint8_t)fp->FILE_PTR[-1]; |
|---|
| 802 | _zz_addpos(fd, fp->FILE_CNT + 1); |
|---|
| 803 | } |
|---|
| 804 | |
|---|
| 805 | if(!_zz_islocked(fd)) |
|---|
| 806 | debug("%s([%i]) = %i", __func__, fd, ret); |
|---|
| 807 | |
|---|
| 808 | return ret; |
|---|
| 809 | } |
|---|
| 810 | #endif |
|---|
| 811 | |
|---|