robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 1 | /* |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 2 | * |
robert.swiecki@gmail.com | 8118985 | 2015-02-14 23:44:55 +0000 | [diff] [blame] | 3 | * honggfuzz - file operations |
| 4 | * ----------------------------------------- |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 5 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 6 | * Author: Robert Swiecki <swiecki@google.com> |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 7 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 8 | * Copyright 2010-2015 by Google Inc. All Rights Reserved. |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 9 | * |
| 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 11 | * not use this file except in compliance with the License. You may obtain |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 12 | * a copy of the License at |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 13 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 15 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 19 | * implied. See the License for the specific language governing |
| 20 | * permissions and limitations under the License. |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 21 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 22 | */ |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 23 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 24 | #include "common.h" |
| 25 | #include "files.h" |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 26 | |
| 27 | #include <dirent.h> |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
robert.swiecki@gmail.com | 86941cb | 2015-02-22 15:15:08 +0000 | [diff] [blame] | 30 | #include <inttypes.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 31 | #include <stdint.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <sys/mman.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <unistd.h> |
| 39 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 40 | #include "log.h" |
Robert Swiecki | 0fc5e94 | 2016-03-15 15:49:12 +0100 | [diff] [blame] | 41 | #include "util.h" |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 42 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 43 | ssize_t files_readFileToBufMax(char *fileName, uint8_t * buf, size_t fileMaxSz) |
robert.swiecki@gmail.com | 4a7a9d8 | 2015-03-01 01:25:16 +0000 | [diff] [blame] | 44 | { |
| 45 | int fd = open(fileName, O_RDONLY); |
| 46 | if (fd == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 47 | PLOG_E("Couldn't open '%s' for R/O", fileName); |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 48 | return -1; |
robert.swiecki@gmail.com | 4a7a9d8 | 2015-03-01 01:25:16 +0000 | [diff] [blame] | 49 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 50 | defer { |
| 51 | close(fd); |
| 52 | }; |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 53 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 54 | ssize_t readSz = files_readFromFd(fd, buf, fileMaxSz); |
| 55 | if (readSz < 0) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 56 | LOG_E("Couldn't read '%s' to a buf", fileName); |
Jagger | 1aa94d7 | 2016-04-02 02:37:35 +0200 | [diff] [blame] | 57 | return -1; |
robert.swiecki@gmail.com | 4a7a9d8 | 2015-03-01 01:25:16 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Jagger | 4d10985 | 2016-02-04 02:28:44 +0100 | [diff] [blame] | 60 | LOG_D("Read '%zu' bytes from '%s'", readSz, fileName); |
Jagger | 4d10985 | 2016-02-04 02:28:44 +0100 | [diff] [blame] | 61 | return readSz; |
robert.swiecki@gmail.com | 4a7a9d8 | 2015-03-01 01:25:16 +0000 | [diff] [blame] | 62 | } |
| 63 | |
robert.swiecki@gmail.com | dc8403e | 2015-03-01 01:33:00 +0000 | [diff] [blame] | 64 | bool files_writeBufToFile(char *fileName, uint8_t * buf, size_t fileSz, int flags) |
| 65 | { |
| 66 | int fd = open(fileName, flags, 0644); |
| 67 | if (fd == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 68 | PLOG_E("Couldn't open '%s' for R/O", fileName); |
robert.swiecki@gmail.com | 276f9f1 | 2015-03-01 01:39:47 +0000 | [diff] [blame] | 69 | return false; |
robert.swiecki@gmail.com | dc8403e | 2015-03-01 01:33:00 +0000 | [diff] [blame] | 70 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 71 | defer { |
| 72 | close(fd); |
| 73 | }; |
robert.swiecki@gmail.com | dc8403e | 2015-03-01 01:33:00 +0000 | [diff] [blame] | 74 | |
| 75 | if (files_writeToFd(fd, buf, fileSz) == false) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 76 | PLOG_E("Couldn't write '%zu' bytes to file '%s' (fd='%d')", fileSz, fileName, fd); |
robert.swiecki@gmail.com | 9be63de | 2015-03-02 07:08:45 +0000 | [diff] [blame] | 77 | unlink(fileName); |
robert.swiecki@gmail.com | dc8403e | 2015-03-01 01:33:00 +0000 | [diff] [blame] | 78 | return false; |
| 79 | } |
robert.swiecki@gmail.com | dc8403e | 2015-03-01 01:33:00 +0000 | [diff] [blame] | 80 | |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 81 | LOG_D("Written '%zu' bytes to '%s'", fileSz, fileName); |
robert.swiecki@gmail.com | dc8403e | 2015-03-01 01:33:00 +0000 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
robert.swiecki@gmail.com | 26a9ab7 | 2015-02-22 13:41:18 +0000 | [diff] [blame] | 85 | bool files_writeToFd(int fd, uint8_t * buf, size_t fileSz) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 86 | { |
robert.swiecki@gmail.com | 26a9ab7 | 2015-02-22 13:41:18 +0000 | [diff] [blame] | 87 | size_t writtenSz = 0; |
| 88 | while (writtenSz < fileSz) { |
| 89 | ssize_t sz = write(fd, &buf[writtenSz], fileSz - writtenSz); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 90 | if (sz < 0 && errno == EINTR) |
| 91 | continue; |
| 92 | |
| 93 | if (sz < 0) |
| 94 | return false; |
| 95 | |
robert.swiecki@gmail.com | 26a9ab7 | 2015-02-22 13:41:18 +0000 | [diff] [blame] | 96 | writtenSz += sz; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 97 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | |
robert.swiecki@gmail.com | ad6af22 | 2015-02-14 01:56:08 +0000 | [diff] [blame] | 101 | bool files_writeStrToFd(int fd, char *str) |
| 102 | { |
| 103 | return files_writeToFd(fd, (uint8_t *) str, strlen(str)); |
| 104 | } |
| 105 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 106 | ssize_t files_readFromFd(int fd, uint8_t * buf, size_t fileSz) |
robert.swiecki@gmail.com | 26a9ab7 | 2015-02-22 13:41:18 +0000 | [diff] [blame] | 107 | { |
| 108 | size_t readSz = 0; |
| 109 | while (readSz < fileSz) { |
| 110 | ssize_t sz = read(fd, &buf[readSz], fileSz - readSz); |
| 111 | if (sz < 0 && errno == EINTR) |
| 112 | continue; |
| 113 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 114 | if (sz == 0) |
Jagger | 4d10985 | 2016-02-04 02:28:44 +0100 | [diff] [blame] | 115 | break; |
robert.swiecki@gmail.com | 26a9ab7 | 2015-02-22 13:41:18 +0000 | [diff] [blame] | 116 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 117 | if (sz < 0) |
| 118 | return -1; |
| 119 | |
robert.swiecki@gmail.com | 26a9ab7 | 2015-02-22 13:41:18 +0000 | [diff] [blame] | 120 | readSz += sz; |
| 121 | } |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 122 | return (ssize_t) readSz; |
robert.swiecki@gmail.com | 26a9ab7 | 2015-02-22 13:41:18 +0000 | [diff] [blame] | 123 | } |
| 124 | |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 125 | bool files_exists(char *fileName) |
| 126 | { |
| 127 | return (access(fileName, F_OK) != -1); |
| 128 | } |
| 129 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 130 | bool files_writePatternToFd(int fd, off_t size, unsigned char p) |
| 131 | { |
| 132 | void *buf = malloc(size); |
| 133 | if (!buf) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 134 | PLOG_W("Couldn't allocate memory"); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 135 | return false; |
| 136 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 137 | defer { |
| 138 | free(buf); |
| 139 | }; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 140 | |
| 141 | memset(buf, p, (size_t) size); |
| 142 | int ret = files_writeToFd(fd, buf, size); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 147 | static bool files_readdir(honggfuzz_t * hfuzz) |
| 148 | { |
| 149 | DIR *dir = opendir(hfuzz->inputFile); |
| 150 | if (!dir) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 151 | PLOG_E("Couldn't open dir '%s'", hfuzz->inputFile); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 152 | return false; |
| 153 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 154 | defer { |
| 155 | closedir(dir); |
| 156 | }; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 157 | |
| 158 | int count = 0; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 159 | for (;;) { |
| 160 | struct dirent de, *res; |
| 161 | if (readdir_r(dir, &de, &res) > 0) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 162 | PLOG_E("Couldn't read the '%s' dir", hfuzz->inputFile); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 163 | return false; |
| 164 | } |
| 165 | |
| 166 | if (res == NULL && count > 0) { |
Anestis Bechtsoudis | 46ea10e | 2015-11-07 18:16:25 +0200 | [diff] [blame] | 167 | LOG_I("%zu input files have been added to the list", hfuzz->fileCnt); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 168 | return true; |
| 169 | } |
| 170 | |
| 171 | if (res == NULL && count == 0) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 172 | LOG_E("Directory '%s' doesn't contain any regular files", hfuzz->inputFile); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
| 176 | char path[PATH_MAX]; |
| 177 | snprintf(path, sizeof(path), "%s/%s", hfuzz->inputFile, res->d_name); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 178 | struct stat st; |
| 179 | if (stat(path, &st) == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 180 | LOG_W("Couldn't stat() the '%s' file", path); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 181 | continue; |
| 182 | } |
| 183 | |
| 184 | if (!S_ISREG(st.st_mode)) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 185 | LOG_D("'%s' is not a regular file, skipping", path); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 186 | continue; |
| 187 | } |
| 188 | |
robert.swiecki@gmail.com | c1b759b | 2015-03-01 18:41:15 +0000 | [diff] [blame] | 189 | if (st.st_size == 0ULL) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 190 | LOG_D("'%s' is empty", path); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 191 | continue; |
| 192 | } |
| 193 | |
robert.swiecki@gmail.com | 86941cb | 2015-02-22 15:15:08 +0000 | [diff] [blame] | 194 | if (st.st_size > (off_t) hfuzz->maxFileSz) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 195 | LOG_W("File '%s' is bigger than maximal defined file size (-F): %" PRId64 " > %" |
| 196 | PRId64, path, (int64_t) st.st_size, (int64_t) hfuzz->maxFileSz); |
robert.swiecki@gmail.com | 86941cb | 2015-02-22 15:15:08 +0000 | [diff] [blame] | 197 | continue; |
| 198 | } |
| 199 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 200 | if (!(hfuzz->files = realloc(hfuzz->files, sizeof(char *) * (count + 1)))) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 201 | PLOG_E("Couldn't allocate memory"); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
Jagger | 6539b3a | 2016-04-06 02:32:50 +0200 | [diff] [blame] | 205 | hfuzz->files[count] = util_StrDup(path); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 206 | hfuzz->fileCnt = ++count; |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 207 | LOG_D("Added '%s' to the list of input files", path); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | abort(); /* NOTREACHED */ |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | bool files_init(honggfuzz_t * hfuzz) |
| 215 | { |
Robert Swiecki | 3a94db7 | 2016-03-15 15:48:33 +0100 | [diff] [blame] | 216 | hfuzz->files = util_Malloc(sizeof(char *)); |
Jagger | a34b302 | 2016-04-02 02:20:40 +0200 | [diff] [blame] | 217 | hfuzz->files[0] = "NONE"; |
Jagger | 30384ec | 2016-04-02 02:30:32 +0200 | [diff] [blame] | 218 | hfuzz->fileCnt = 1; |
Jagger | a34b302 | 2016-04-02 02:20:40 +0200 | [diff] [blame] | 219 | |
| 220 | if (hfuzz->externalCommand) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 221 | LOG_I |
Jagger | a34b302 | 2016-04-02 02:20:40 +0200 | [diff] [blame] | 222 | ("No input file corpus loaded, the external command '%s' is responsible for creating the fuzz files", |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 223 | hfuzz->externalCommand); |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 224 | return true; |
| 225 | } |
| 226 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 227 | if (!hfuzz->inputFile) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 228 | LOG_E("No input file/dir specified"); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 229 | return false; |
| 230 | } |
| 231 | |
| 232 | struct stat st; |
| 233 | if (stat(hfuzz->inputFile, &st) == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 234 | PLOG_E("Couldn't stat the input file/dir '%s'", hfuzz->inputFile); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 235 | return false; |
| 236 | } |
| 237 | |
| 238 | if (S_ISDIR(st.st_mode)) { |
| 239 | return files_readdir(hfuzz); |
| 240 | } |
| 241 | |
| 242 | if (!S_ISREG(st.st_mode)) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 243 | LOG_E("'%s' is not a regular file, nor a directory", hfuzz->inputFile); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | |
robert.swiecki@gmail.com | 2e0103f | 2015-03-30 01:38:12 +0000 | [diff] [blame] | 247 | if (st.st_size > (off_t) hfuzz->maxFileSz) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 248 | LOG_E("File '%s' is bigger than maximal defined file size (-F): %" PRId64 " > %" PRId64, |
| 249 | hfuzz->inputFile, (int64_t) st.st_size, (int64_t) hfuzz->maxFileSz); |
robert.swiecki@gmail.com | 2e0103f | 2015-03-30 01:38:12 +0000 | [diff] [blame] | 250 | return false; |
| 251 | } |
| 252 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 253 | hfuzz->files[0] = hfuzz->inputFile; |
| 254 | hfuzz->fileCnt = 1; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 255 | return true; |
| 256 | } |
groebert@google.com | 1c7e3b0 | 2013-06-19 09:27:38 +0000 | [diff] [blame] | 257 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 258 | const char *files_basename(char *path) |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 259 | { |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 260 | const char *base = strrchr(path, '/'); |
groebert@google.com | 1c7e3b0 | 2013-06-19 09:27:38 +0000 | [diff] [blame] | 261 | return base ? base + 1 : path; |
| 262 | } |
robert.swiecki@gmail.com | 4f1124f | 2015-04-21 17:12:22 +0000 | [diff] [blame] | 263 | |
| 264 | bool files_parseDictionary(honggfuzz_t * hfuzz) |
| 265 | { |
| 266 | FILE *fDict = fopen(hfuzz->dictionaryFile, "rb"); |
| 267 | if (fDict == NULL) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 268 | PLOG_E("Couldn't open '%s' - R/O mode", hfuzz->dictionaryFile); |
robert.swiecki@gmail.com | 4f1124f | 2015-04-21 17:12:22 +0000 | [diff] [blame] | 269 | return false; |
| 270 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 271 | defer { |
| 272 | fclose(fDict); |
| 273 | }; |
robert.swiecki@gmail.com | 4f1124f | 2015-04-21 17:12:22 +0000 | [diff] [blame] | 274 | |
robert.swiecki@gmail.com | 0440c53 | 2015-04-21 17:27:08 +0000 | [diff] [blame] | 275 | for (;;) { |
Robert Swiecki | caf9d76 | 2016-03-10 16:13:18 +0100 | [diff] [blame] | 276 | char *lineptr = NULL; |
| 277 | size_t n = 0; |
robert.swiecki@gmail.com | 0440c53 | 2015-04-21 17:27:08 +0000 | [diff] [blame] | 278 | if (getdelim(&lineptr, &n, '\0', fDict) == -1) { |
| 279 | break; |
| 280 | } |
robert.swiecki@gmail.com | 4f1124f | 2015-04-21 17:12:22 +0000 | [diff] [blame] | 281 | if ((hfuzz->dictionary = |
| 282 | realloc(hfuzz->dictionary, |
robert.swiecki@gmail.com | d9363be | 2015-04-21 17:24:02 +0000 | [diff] [blame] | 283 | (hfuzz->dictionaryCnt + 1) * sizeof(hfuzz->dictionary[0]))) == NULL) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 284 | PLOG_E("Realloc failed (sz=%zu)", |
| 285 | (hfuzz->dictionaryCnt + 1) * sizeof(hfuzz->dictionary[0])); |
robert.swiecki@gmail.com | 4f1124f | 2015-04-21 17:12:22 +0000 | [diff] [blame] | 286 | return false; |
| 287 | } |
Robert Swiecki | caf9d76 | 2016-03-10 16:13:18 +0100 | [diff] [blame] | 288 | hfuzz->dictionary[hfuzz->dictionaryCnt] = lineptr; |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 289 | LOG_D("Dictionary: loaded word: '%s' (len=%zu)", |
| 290 | hfuzz->dictionary[hfuzz->dictionaryCnt], |
| 291 | strlen(hfuzz->dictionary[hfuzz->dictionaryCnt])); |
robert.swiecki@gmail.com | 4f1124f | 2015-04-21 17:12:22 +0000 | [diff] [blame] | 292 | hfuzz->dictionaryCnt += 1; |
| 293 | } |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 294 | LOG_I("Loaded %zu words from the dictionary", hfuzz->dictionaryCnt); |
robert.swiecki@gmail.com | 4f1124f | 2015-04-21 17:12:22 +0000 | [diff] [blame] | 295 | return true; |
| 296 | } |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 297 | |
| 298 | /* |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 299 | * dstExists argument can be used by caller for cases where existing destination |
| 300 | * file requires special handling (e.g. save unique crashes) |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 301 | */ |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 302 | bool files_copyFile(const char *source, const char *destination, bool * dstExists) |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 303 | { |
Anestis Bechtsoudis | 3877321 | 2015-09-08 23:34:28 +0300 | [diff] [blame] | 304 | if (dstExists) |
| 305 | *dstExists = false; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 306 | if (link(source, destination) == 0) { |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 307 | return true; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 308 | } else { |
| 309 | if (errno == EEXIST) { |
| 310 | // Should kick-in before MAC, so avoid the hassle |
Anestis Bechtsoudis | 3877321 | 2015-09-08 23:34:28 +0300 | [diff] [blame] | 311 | if (dstExists) |
| 312 | *dstExists = true; |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 313 | return false; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 314 | } else { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 315 | PLOG_D("Couldn't link '%s' as '%s'", source, destination); |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 316 | /* |
| 317 | * Don't fail yet as we might have a running env which doesn't allow |
| 318 | * hardlinks (e.g. SELinux) |
| 319 | */ |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // Now try with a verbose POSIX alternative |
| 324 | int inFD, outFD, dstOpenFlags; |
| 325 | mode_t dstFilePerms; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 326 | |
| 327 | // O_EXCL is important for saving unique crashes |
| 328 | dstOpenFlags = O_CREAT | O_WRONLY | O_EXCL; |
| 329 | dstFilePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; |
| 330 | |
| 331 | inFD = open(source, O_RDONLY); |
| 332 | if (inFD == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 333 | PLOG_D("Couldn't open '%s' source", source); |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 334 | return false; |
| 335 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 336 | defer { |
| 337 | close(inFD); |
| 338 | }; |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 339 | |
| 340 | struct stat inSt; |
| 341 | if (fstat(inFD, &inSt) == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 342 | PLOG_E("Couldn't fstat(fd='%d' fileName='%s')", inFD, source); |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 343 | return false; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | outFD = open(destination, dstOpenFlags, dstFilePerms); |
| 347 | if (outFD == -1) { |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 348 | if (errno == EEXIST) { |
Anestis Bechtsoudis | 3877321 | 2015-09-08 23:34:28 +0300 | [diff] [blame] | 349 | if (dstExists) |
| 350 | *dstExists = true; |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 351 | } |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 352 | PLOG_D("Couldn't open '%s' destination", destination); |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 353 | return false; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 354 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 355 | defer { |
| 356 | close(outFD); |
| 357 | }; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 358 | |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 359 | uint8_t *inFileBuf = malloc(inSt.st_size); |
| 360 | if (!inFileBuf) { |
Anestis Bechtsoudis | c947332 | 2015-10-09 12:59:17 -0700 | [diff] [blame] | 361 | PLOG_E("malloc(%zu) failed", (size_t) inSt.st_size); |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 362 | return false; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 363 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 364 | defer { |
| 365 | free(inFileBuf); |
| 366 | }; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 367 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 368 | ssize_t readSz = files_readFromFd(inFD, inFileBuf, (size_t) inSt.st_size); |
| 369 | if (readSz < 0) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 370 | PLOG_E("Couldn't read '%s' to a buf", source); |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 371 | return false; |
| 372 | } |
| 373 | |
Jagger | 4d10985 | 2016-02-04 02:28:44 +0100 | [diff] [blame] | 374 | if (files_writeToFd(outFD, inFileBuf, readSz) == false) { |
| 375 | PLOG_E("Couldn't write '%zu' bytes to file '%s' (fd='%d')", (size_t) readSz, |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 376 | destination, outFD); |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 377 | unlink(destination); |
| 378 | return false; |
| 379 | } |
| 380 | |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 381 | return true; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 382 | } |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 383 | |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 384 | bool files_parseBlacklist(honggfuzz_t * hfuzz) |
| 385 | { |
| 386 | FILE *fBl = fopen(hfuzz->blacklistFile, "rb"); |
| 387 | if (fBl == NULL) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 388 | PLOG_E("Couldn't open '%s' - R/O mode", hfuzz->blacklistFile); |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 389 | return false; |
| 390 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 391 | defer { |
| 392 | fclose(fBl); |
| 393 | }; |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 394 | |
Anestis Bechtsoudis | 3a8e16f | 2016-01-10 15:06:09 +0200 | [diff] [blame] | 395 | char *lineptr = NULL; |
Jagger | 1dad182 | 2016-03-29 23:56:14 +0200 | [diff] [blame] | 396 | /* lineptr can be NULL, but it's fine for free() */ |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 397 | defer { |
| 398 | free(lineptr); |
| 399 | }; |
Anestis Bechtsoudis | 3a8e16f | 2016-01-10 15:06:09 +0200 | [diff] [blame] | 400 | size_t n = 0; |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 401 | for (;;) { |
Anestis Bechtsoudis | 7d6185c | 2015-09-21 15:59:56 +0300 | [diff] [blame] | 402 | if (getline(&lineptr, &n, fBl) == -1) { |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 403 | break; |
| 404 | } |
Anestis Bechtsoudis | 99db874 | 2015-09-23 11:37:26 +0300 | [diff] [blame] | 405 | |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 406 | if ((hfuzz->blacklist = |
| 407 | realloc(hfuzz->blacklist, |
| 408 | (hfuzz->blacklistCnt + 1) * sizeof(hfuzz->blacklist[0]))) == NULL) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 409 | PLOG_E("realloc failed (sz=%zu)", |
| 410 | (hfuzz->blacklistCnt + 1) * sizeof(hfuzz->blacklist[0])); |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 411 | return false; |
| 412 | } |
Anestis Bechtsoudis | 99db874 | 2015-09-23 11:37:26 +0300 | [diff] [blame] | 413 | |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 414 | hfuzz->blacklist[hfuzz->blacklistCnt] = strtoull(lineptr, 0, 16); |
Anestis Bechtsoudis | 07e14ce | 2015-12-30 14:15:42 +0200 | [diff] [blame] | 415 | LOG_D("Blacklist: loaded %'" PRIu64 "'", hfuzz->blacklist[hfuzz->blacklistCnt]); |
Anestis Bechtsoudis | 99db874 | 2015-09-23 11:37:26 +0300 | [diff] [blame] | 416 | |
| 417 | // Verify entries are sorted so we can use interpolation search |
| 418 | if (hfuzz->blacklistCnt > 1) { |
| 419 | if (hfuzz->blacklist[hfuzz->blacklistCnt - 1] > hfuzz->blacklist[hfuzz->blacklistCnt]) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 420 | LOG_F |
| 421 | ("Blacklist file not sorted. Use 'tools/createStackBlacklist.sh' to sort records"); |
Anestis Bechtsoudis | 99db874 | 2015-09-23 11:37:26 +0300 | [diff] [blame] | 422 | return false; |
| 423 | } |
| 424 | } |
Anestis Bechtsoudis | 612baa5 | 2015-09-23 18:21:43 +0300 | [diff] [blame] | 425 | hfuzz->blacklistCnt += 1; |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 426 | } |
Anestis Bechtsoudis | 99db874 | 2015-09-23 11:37:26 +0300 | [diff] [blame] | 427 | |
Anestis Bechtsoudis | ebf879e | 2015-09-27 18:11:25 +0300 | [diff] [blame] | 428 | if (hfuzz->blacklistCnt > 0) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 429 | LOG_I("Loaded %zu stack hash(es) from the blacklist file", hfuzz->blacklistCnt); |
Anestis Bechtsoudis | 99db874 | 2015-09-23 11:37:26 +0300 | [diff] [blame] | 430 | } else { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 431 | LOG_F("Empty stack hashes blacklist file '%s'", hfuzz->blacklistFile); |
Anestis Bechtsoudis | 99db874 | 2015-09-23 11:37:26 +0300 | [diff] [blame] | 432 | } |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 433 | return true; |
| 434 | } |
Anestis Bechtsoudis | ee5316d | 2015-09-27 19:11:48 +0300 | [diff] [blame] | 435 | |
Anestis Bechtsoudis | ecb0a66 | 2015-09-27 18:19:46 +0300 | [diff] [blame] | 436 | uint8_t *files_mapFile(char *fileName, off_t * fileSz, int *fd, bool isWritable) |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 437 | { |
| 438 | int mmapProt = PROT_READ; |
| 439 | if (isWritable) { |
| 440 | mmapProt |= PROT_WRITE; |
| 441 | } |
| 442 | |
| 443 | if ((*fd = open(fileName, O_RDONLY)) == -1) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 444 | PLOG_E("Couldn't open() '%s' file in R/O mode", fileName); |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 445 | return NULL; |
| 446 | } |
| 447 | |
| 448 | struct stat st; |
| 449 | if (fstat(*fd, &st) == -1) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 450 | PLOG_E("Couldn't stat() the '%s' file", fileName); |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 451 | close(*fd); |
| 452 | return NULL; |
| 453 | } |
| 454 | |
| 455 | uint8_t *buf; |
| 456 | if ((buf = mmap(NULL, st.st_size, mmapProt, MAP_PRIVATE, *fd, 0)) == MAP_FAILED) { |
Anestis Bechtsoudis | 0cde66f | 2015-10-11 19:37:11 -0700 | [diff] [blame] | 457 | PLOG_E("Couldn't mmap() the '%s' file", fileName); |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 458 | close(*fd); |
| 459 | return NULL; |
| 460 | } |
| 461 | |
| 462 | *fileSz = st.st_size; |
| 463 | return buf; |
| 464 | } |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 465 | |
| 466 | bool files_readPidFromFile(const char *fileName, pid_t * pidPtr) |
| 467 | { |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 468 | FILE *fPID = fopen(fileName, "rb"); |
| 469 | if (fPID == NULL) { |
| 470 | PLOG_E("Couldn't open '%s' - R/O mode", fileName); |
| 471 | return false; |
| 472 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 473 | defer { |
| 474 | fclose(fPID); |
| 475 | }; |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 476 | |
| 477 | char *lineptr = NULL; |
| 478 | size_t lineSz = 0; |
Jagger | ebaf01e | 2016-05-15 15:13:34 +0200 | [diff] [blame^] | 479 | defer { |
| 480 | free(lineptr); |
| 481 | }; |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 482 | if (getline(&lineptr, &lineSz, fPID) == -1) { |
| 483 | if (lineSz == 0) { |
| 484 | LOG_E("Empty PID file (%s)", fileName); |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 485 | return false; |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
| 489 | *pidPtr = atoi(lineptr); |
| 490 | if (*pidPtr < 1) { |
| 491 | LOG_E("Invalid PID read from '%s' file", fileName); |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 492 | return false; |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 493 | } |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 494 | |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 495 | return true; |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 496 | } |