| 1 |
/* |
| 2 |
* fuzz-decode: special program for fuzzing. It still reads encoded |
| 3 |
* files just like interop-decode, but tries to do it faster and |
| 4 |
* forgoes several advanced options. |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifdef WIN32 |
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
|
| 11 |
int |
| 12 |
main (int argc, char **argv) |
| 13 |
{ |
| 14 |
fprintf(stderr, "%s is not supported on Windows: need mmap(2)\n", argv[0]); |
| 15 |
return 1; |
| 16 |
} |
| 17 |
|
| 18 |
#else |
| 19 |
|
| 20 |
#include <assert.h> |
| 21 |
|
| 22 |
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) |
| 23 |
#include <sys/endian.h> |
| 24 |
#define bswap_16 bswap16 |
| 25 |
#define bswap_32 bswap32 |
| 26 |
#define bswap_64 bswap64 |
| 27 |
#elif defined(__OpenBSD__) |
| 28 |
#define bswap_16 swap16 |
| 29 |
#define bswap_32 swap32 |
| 30 |
#define bswap_64 swap64 |
| 31 |
#elif defined(__APPLE__) |
| 32 |
#include <libkern/OSByteOrder.h> |
| 33 |
#define bswap_16 OSSwapInt16 |
| 34 |
#define bswap_32 OSSwapInt32 |
| 35 |
#define bswap_64 OSSwapInt64 |
| 36 |
#elif defined(WIN32) |
| 37 |
#define bswap_16 _byteswap_ushort |
| 38 |
#define bswap_32 _byteswap_ulong |
| 39 |
#define bswap_64 _byteswap_uint64 |
| 40 |
#else |
| 41 |
#include <byteswap.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#include <errno.h> |
| 45 |
#include <inttypes.h> |
| 46 |
#include <stddef.h> |
| 47 |
#include <stdio.h> |
| 48 |
#include <stdlib.h> |
| 49 |
#include <string.h> |
| 50 |
#include <unistd.h> |
| 51 |
#include <sys/queue.h> |
| 52 |
#include <sys/types.h> |
| 53 |
#include <sys/stat.h> |
| 54 |
#include <sys/mman.h> |
| 55 |
#include <fcntl.h> |
| 56 |
|
| 57 |
#include "lsqpack.h" |
| 58 |
#include "lsxpack_header.h" |
| 59 |
|
| 60 |
#define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 61 |
|
| 62 |
static void |
| 63 |
usage (const char *name) |
| 64 |
{ |
| 65 |
fprintf(stderr, |
| 66 |
"Usage: %s [options] [-i input] [-o output]\n" |
| 67 |
"\n" |
| 68 |
"Options:\n" |
| 69 |
" -i FILE Input file.\n" |
| 70 |
" -f FILE Fuzz file: this is the stuff the fuzzer will change.\n" |
| 71 |
" -s NUMBER Maximum number of risked streams. Defaults to %u.\n" |
| 72 |
" -t NUMBER Dynamic table size. Defaults to %u.\n" |
| 73 |
"\n" |
| 74 |
" -h Print this help screen and exit\n" |
| 75 |
, name, LSQPACK_DEF_MAX_RISKED_STREAMS, LSQPACK_DEF_DYN_TABLE_SIZE); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
static void |
| 80 |
hblock_unblocked (void *buf_p) |
| 81 |
{ |
| 82 |
exit(1); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
struct header |
| 87 |
{ |
| 88 |
LIST_ENTRY(header) next_header; |
| 89 |
struct lsxpack_header xhdr; |
| 90 |
char buf[0x10000]; |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
LIST_HEAD(, header) s_headers; |
| 95 |
|
| 96 |
|
| 97 |
static struct lsxpack_header * |
| 98 |
prepare_decode (void *hblock_ctx, struct lsxpack_header *xhdr, size_t space) |
| 99 |
{ |
| 100 |
struct header *header; |
| 101 |
|
| 102 |
if (xhdr) |
| 103 |
{ |
| 104 |
/* If the original buffer is not enough, we don't reallocate */ |
| 105 |
header = (struct header *) ((char *) xhdr |
| 106 |
- offsetof(struct header, xhdr)); |
| 107 |
LIST_REMOVE(header, next_header); |
| 108 |
free(header); |
| 109 |
return NULL; |
| 110 |
} |
| 111 |
else if (space <= LSXPACK_MAX_STRLEN) |
| 112 |
{ |
| 113 |
header = malloc(sizeof(*header)); |
| 114 |
if (!header) |
| 115 |
return NULL; |
| 116 |
LIST_INSERT_HEAD(&s_headers, header, next_header); |
| 117 |
lsxpack_header_prepare_decode(&header->xhdr, header->buf, |
| 118 |
0, sizeof(header->buf)); |
| 119 |
return &header->xhdr; |
| 120 |
} |
| 121 |
else |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
static int |
| 127 |
process_header (void *hblock_ctx, struct lsxpack_header *xhdr) |
| 128 |
{ |
| 129 |
struct header *header; |
| 130 |
|
| 131 |
header = (struct header *) ((char *) xhdr - offsetof(struct header, xhdr)); |
| 132 |
LIST_REMOVE(header, next_header); |
| 133 |
free(header); |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
static const struct lsqpack_dec_hset_if hset_if = { |
| 139 |
.dhi_unblocked = hblock_unblocked, |
| 140 |
.dhi_prepare_decode = prepare_decode, |
| 141 |
.dhi_process_header = process_header, |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
int |
| 146 |
main (int argc, char **argv) |
| 147 |
{ |
| 148 |
int in_fd = -1, fuzz_fd = STDIN_FILENO; |
| 149 |
int opt; |
| 150 |
unsigned dyn_table_size = LSQPACK_DEF_DYN_TABLE_SIZE, |
| 151 |
max_risked_streams = LSQPACK_DEF_MAX_RISKED_STREAMS; |
| 152 |
struct lsqpack_dec decoder; |
| 153 |
const unsigned char *p, *end; |
| 154 |
unsigned char *begin; |
| 155 |
struct stat st; |
| 156 |
uint64_t stream_id; |
| 157 |
uint32_t size; |
| 158 |
int r; |
| 159 |
struct header *header; |
| 160 |
|
| 161 |
while (-1 != (opt = getopt(argc, argv, "i:f:s:t:h"))) |
| 162 |
{ |
| 163 |
switch (opt) |
| 164 |
{ |
| 165 |
case 'i': |
| 166 |
in_fd = open(optarg, O_RDONLY); |
| 167 |
if (in_fd < 0) |
| 168 |
{ |
| 169 |
fprintf(stderr, "cannot open `%s' for reading: %s\n", |
| 170 |
optarg, strerror(errno)); |
| 171 |
exit(EXIT_FAILURE); |
| 172 |
} |
| 173 |
break; |
| 174 |
case 'f': |
| 175 |
fuzz_fd = open(optarg, O_RDONLY); |
| 176 |
if (fuzz_fd < 0) |
| 177 |
{ |
| 178 |
fprintf(stderr, "cannot open `%s' for reading: %s\n", |
| 179 |
optarg, strerror(errno)); |
| 180 |
exit(EXIT_FAILURE); |
| 181 |
} |
| 182 |
break; |
| 183 |
case 's': |
| 184 |
max_risked_streams = atoi(optarg); |
| 185 |
break; |
| 186 |
case 't': |
| 187 |
dyn_table_size = atoi(optarg); |
| 188 |
break; |
| 189 |
case 'h': |
| 190 |
usage(argv[0]); |
| 191 |
exit(EXIT_SUCCESS); |
| 192 |
default: |
| 193 |
exit(EXIT_FAILURE); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
LIST_INIT(&s_headers); |
| 198 |
lsqpack_dec_init(&decoder, NULL, dyn_table_size, max_risked_streams, |
| 199 |
&hset_if, 0 /* TODO: add command-line option */); |
| 200 |
|
| 201 |
if (in_fd < 0) |
| 202 |
{ |
| 203 |
fprintf(stderr, "Specify input using `-i' option\n"); |
| 204 |
exit(1); |
| 205 |
} |
| 206 |
|
| 207 |
if (0 != fstat(in_fd, &st)) |
| 208 |
{ |
| 209 |
perror("fstat"); |
| 210 |
exit(1); |
| 211 |
} |
| 212 |
|
| 213 |
begin = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, in_fd, 0); |
| 214 |
if (!begin) |
| 215 |
{ |
| 216 |
perror("mmap"); |
| 217 |
exit(1); |
| 218 |
} |
| 219 |
|
| 220 |
p = begin; |
| 221 |
end = begin + st.st_size; |
| 222 |
while (p + sizeof(stream_id) + sizeof(size) < end) |
| 223 |
{ |
| 224 |
stream_id = * (uint64_t *) p; |
| 225 |
p += sizeof(uint64_t); |
| 226 |
size = * (uint32_t *) p; |
| 227 |
p += sizeof(uint32_t); |
| 228 |
#if __BYTE_ORDER == __LITTLE_ENDIAN |
| 229 |
stream_id = bswap_64(stream_id); |
| 230 |
size = bswap_32(size); |
| 231 |
#endif |
| 232 |
if (p + size > end) |
| 233 |
{ |
| 234 |
fprintf(stderr, "truncated input at offset %u", |
| 235 |
(unsigned) (p - begin)); |
| 236 |
abort(); |
| 237 |
} |
| 238 |
if (stream_id == 0) |
| 239 |
{ |
| 240 |
r = lsqpack_dec_enc_in(&decoder, p, size); |
| 241 |
if (r != 0) |
| 242 |
abort(); |
| 243 |
} |
| 244 |
else |
| 245 |
{ |
| 246 |
const unsigned char *cur = p; |
| 247 |
enum lsqpack_read_header_status rhs; |
| 248 |
rhs = lsqpack_dec_header_in(&decoder, NULL, stream_id, |
| 249 |
size, &cur, size, NULL, NULL); |
| 250 |
if (rhs != LQRHS_DONE || (uint32_t) (cur - p) != size) |
| 251 |
abort(); |
| 252 |
} |
| 253 |
p += size; |
| 254 |
} |
| 255 |
|
| 256 |
munmap(begin, st.st_size); |
| 257 |
(void) close(in_fd); |
| 258 |
|
| 259 |
/* Now let's read the fuzzed part */ |
| 260 |
|
| 261 |
if (0 != fstat(fuzz_fd, &st)) |
| 262 |
{ |
| 263 |
perror("fstat"); |
| 264 |
exit(1); |
| 265 |
} |
| 266 |
|
| 267 |
begin = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fuzz_fd, 0); |
| 268 |
if (!begin) |
| 269 |
{ |
| 270 |
perror("mmap"); |
| 271 |
exit(1); |
| 272 |
} |
| 273 |
|
| 274 |
p = begin; |
| 275 |
end = begin + st.st_size; |
| 276 |
while (p + sizeof(stream_id) + sizeof(size) < end) |
| 277 |
{ |
| 278 |
stream_id = * (uint64_t *) p; |
| 279 |
p += sizeof(uint64_t); |
| 280 |
size = * (uint32_t *) p; |
| 281 |
p += sizeof(uint32_t); |
| 282 |
#if __BYTE_ORDER == __LITTLE_ENDIAN |
| 283 |
stream_id = bswap_64(stream_id); |
| 284 |
size = bswap_32(size); |
| 285 |
#endif |
| 286 |
if (stream_id == 0) |
| 287 |
{ |
| 288 |
r = lsqpack_dec_enc_in(&decoder, p, MIN(size, (uint32_t) (end - p))); |
| 289 |
(void) r; |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
const unsigned char *cur = p; |
| 294 |
enum lsqpack_read_header_status rhs; |
| 295 |
size = MIN(size, (uint32_t) (end - p)); |
| 296 |
rhs = lsqpack_dec_header_in(&decoder, NULL, stream_id, |
| 297 |
size, &cur, size, NULL, NULL); |
| 298 |
(void) rhs; |
| 299 |
} |
| 300 |
break; |
| 301 |
} |
| 302 |
|
| 303 |
munmap(begin, st.st_size); |
| 304 |
(void) close(fuzz_fd); |
| 305 |
|
| 306 |
lsqpack_dec_cleanup(&decoder); |
| 307 |
while (!LIST_EMPTY(&s_headers)) |
| 308 |
{ |
| 309 |
header = LIST_FIRST(&s_headers); |
| 310 |
LIST_REMOVE(header, next_header); |
| 311 |
free(header); |
| 312 |
} |
| 313 |
|
| 314 |
exit(0); |
| 315 |
} |
| 316 |
|
| 317 |
#endif |