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 | { |
Robert Swiecki | a95854a | 2016-11-14 17:55:43 +0100 | [diff] [blame] | 45 | int fd = open(fileName, O_RDONLY | O_CLOEXEC); |
robert.swiecki@gmail.com | 4a7a9d8 | 2015-03-01 01:25:16 +0000 | [diff] [blame] | 46 | if (fd == -1) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 47 | PLOG_W("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 | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 56 | LOG_W("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 | 731f295 | 2017-01-30 23:30:22 +0100 | [diff] [blame] | 68 | PLOG_W("Couldn't open '%s' for R/W", 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 | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 76 | PLOG_W("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 | { |
Jagger | 1b2d482 | 2016-09-25 16:19:45 +0200 | [diff] [blame] | 149 | DIR *dir = opendir(hfuzz->inputDir); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 150 | if (!dir) { |
Jagger | 1b2d482 | 2016-09-25 16:19:45 +0200 | [diff] [blame] | 151 | PLOG_W("Couldn't open dir '%s'", hfuzz->inputDir); |
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 | |
Jagger | f4a6056 | 2016-09-25 15:40:23 +0200 | [diff] [blame] | 158 | size_t maxSize = 0UL; |
| 159 | unsigned count = 0; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 160 | for (;;) { |
Jagger | ad07693 | 2016-08-24 02:40:19 +0200 | [diff] [blame] | 161 | errno = 0; |
Jagger | f2723e1 | 2016-08-23 23:47:19 +0200 | [diff] [blame] | 162 | struct dirent *res = readdir(dir); |
| 163 | if (res == NULL && errno != 0) { |
Jagger | 1b2d482 | 2016-09-25 16:19:45 +0200 | [diff] [blame] | 164 | PLOG_W("Couldn't read the '%s' dir", hfuzz->inputDir); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 165 | return false; |
| 166 | } |
| 167 | |
Jagger | f4a6056 | 2016-09-25 15:40:23 +0200 | [diff] [blame] | 168 | if (res == NULL) { |
| 169 | break; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | char path[PATH_MAX]; |
Jagger | 1b2d482 | 2016-09-25 16:19:45 +0200 | [diff] [blame] | 173 | snprintf(path, sizeof(path), "%s/%s", hfuzz->inputDir, res->d_name); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 174 | struct stat st; |
| 175 | if (stat(path, &st) == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 176 | LOG_W("Couldn't stat() the '%s' file", path); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 177 | continue; |
| 178 | } |
| 179 | |
| 180 | if (!S_ISREG(st.st_mode)) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 181 | LOG_D("'%s' is not a regular file, skipping", path); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 182 | continue; |
| 183 | } |
| 184 | |
robert.swiecki@gmail.com | c1b759b | 2015-03-01 18:41:15 +0000 | [diff] [blame] | 185 | if (st.st_size == 0ULL) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 186 | LOG_D("'%s' is empty", path); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 187 | continue; |
| 188 | } |
| 189 | |
Jagger | f4a6056 | 2016-09-25 15:40:23 +0200 | [diff] [blame] | 190 | if (hfuzz->maxFileSz != 0UL && st.st_size > (off_t) hfuzz->maxFileSz) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 191 | LOG_W("File '%s' is bigger than maximal defined file size (-F): %" PRId64 " > %" |
| 192 | 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] | 193 | continue; |
| 194 | } |
| 195 | |
Jagger | f4a6056 | 2016-09-25 15:40:23 +0200 | [diff] [blame] | 196 | if ((size_t) st.st_size > maxSize) { |
| 197 | maxSize = st.st_size; |
| 198 | } |
Robert Swiecki | e8f8e8d | 2016-10-03 23:51:32 +0200 | [diff] [blame] | 199 | |
| 200 | struct paths_t *file = (struct paths_t *)util_Malloc(sizeof(struct paths_t)); |
| 201 | snprintf(file->path, sizeof(file->path), "%s", path); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 202 | hfuzz->fileCnt = ++count; |
Robert Swiecki | e8f8e8d | 2016-10-03 23:51:32 +0200 | [diff] [blame] | 203 | TAILQ_INSERT_TAIL(&hfuzz->fileq, file, pointers); |
| 204 | |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 205 | LOG_D("Added '%s' to the list of input files", path); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Jagger | f4a6056 | 2016-09-25 15:40:23 +0200 | [diff] [blame] | 208 | if (count == 0) { |
Jagger | 1b2d482 | 2016-09-25 16:19:45 +0200 | [diff] [blame] | 209 | LOG_W("Directory '%s' doesn't contain any regular files", hfuzz->inputDir); |
Jagger | f4a6056 | 2016-09-25 15:40:23 +0200 | [diff] [blame] | 210 | return false; |
| 211 | } |
| 212 | |
| 213 | if (hfuzz->maxFileSz == 0UL) { |
Robert Swiecki | a81f317 | 2016-11-22 21:31:40 +0100 | [diff] [blame] | 214 | if (maxSize < 128U) { |
| 215 | hfuzz->maxFileSz = 128U; |
| 216 | } else { |
| 217 | hfuzz->maxFileSz = maxSize; |
| 218 | } |
Jagger | f4a6056 | 2016-09-25 15:40:23 +0200 | [diff] [blame] | 219 | } |
| 220 | LOG_I("%zu input files have been added to the list. Max file size: %zu", hfuzz->fileCnt, |
| 221 | hfuzz->maxFileSz); |
| 222 | return true; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | bool files_init(honggfuzz_t * hfuzz) |
| 226 | { |
Jagger | a34b302 | 2016-04-02 02:20:40 +0200 | [diff] [blame] | 227 | if (hfuzz->externalCommand) { |
Robert Swiecki | e8f8e8d | 2016-10-03 23:51:32 +0200 | [diff] [blame] | 228 | struct paths_t *file = (struct paths_t *)util_Malloc(sizeof(struct paths_t)); |
| 229 | snprintf(file->path, sizeof(file->path), "NONE"); |
| 230 | hfuzz->fileCnt = 1; |
| 231 | TAILQ_INSERT_TAIL(&hfuzz->fileq, file, pointers); |
| 232 | |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 233 | LOG_I |
Jagger | a34b302 | 2016-04-02 02:20:40 +0200 | [diff] [blame] | 234 | ("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] | 235 | hfuzz->externalCommand); |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 236 | return true; |
| 237 | } |
| 238 | |
Jagger | 1b2d482 | 2016-09-25 16:19:45 +0200 | [diff] [blame] | 239 | if (!hfuzz->inputDir) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 240 | LOG_W("No input file/dir specified"); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | |
| 244 | struct stat st; |
Jagger | 1b2d482 | 2016-09-25 16:19:45 +0200 | [diff] [blame] | 245 | if (stat(hfuzz->inputDir, &st) == -1) { |
| 246 | PLOG_W("Couldn't stat the input dir '%s'", hfuzz->inputDir); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 247 | return false; |
| 248 | } |
| 249 | |
Robert Swiecki | e8f8e8d | 2016-10-03 23:51:32 +0200 | [diff] [blame] | 250 | if (!S_ISDIR(st.st_mode)) { |
| 251 | LOG_W("The initial corpus directory '%s' is not a directory", hfuzz->inputDir); |
| 252 | return false; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Robert Swiecki | e8f8e8d | 2016-10-03 23:51:32 +0200 | [diff] [blame] | 255 | return files_readdir(hfuzz); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 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 | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 268 | PLOG_W("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; |
Jagger | c64c9eb | 2016-09-22 04:04:34 +0200 | [diff] [blame] | 278 | ssize_t len = getdelim(&lineptr, &n, '\n', fDict); |
| 279 | if (len == -1) { |
robert.swiecki@gmail.com | 0440c53 | 2015-04-21 17:27:08 +0000 | [diff] [blame] | 280 | break; |
| 281 | } |
Jagger | c64c9eb | 2016-09-22 04:04:34 +0200 | [diff] [blame] | 282 | if (n > 1 && lineptr[len - 1] == '\n') { |
Robert Swiecki | dfef854 | 2016-09-13 18:06:46 +0200 | [diff] [blame] | 283 | lineptr[len - 1] = '\0'; |
Jagger | c64c9eb | 2016-09-22 04:04:34 +0200 | [diff] [blame] | 284 | len--; |
Robert Swiecki | dfef854 | 2016-09-13 18:06:46 +0200 | [diff] [blame] | 285 | } |
Robert Swiecki | 531438a | 2016-09-13 19:05:11 +0200 | [diff] [blame] | 286 | |
| 287 | struct strings_t *str = (struct strings_t *)util_Malloc(sizeof(struct strings_t)); |
Jagger | d619ed1 | 2016-09-22 04:08:49 +0200 | [diff] [blame] | 288 | str->len = util_decodeCString(lineptr); |
| 289 | str->s = lineptr; |
robert.swiecki@gmail.com | 4f1124f | 2015-04-21 17:12:22 +0000 | [diff] [blame] | 290 | hfuzz->dictionaryCnt += 1; |
Robert Swiecki | 3a57226 | 2016-10-04 01:48:34 +0200 | [diff] [blame] | 291 | TAILQ_INSERT_TAIL(&hfuzz->dictq, str, pointers); |
Robert Swiecki | 531438a | 2016-09-13 19:05:11 +0200 | [diff] [blame] | 292 | |
Jagger | c64c9eb | 2016-09-22 04:04:34 +0200 | [diff] [blame] | 293 | LOG_D("Dictionary: loaded word: '%s' (len=%zu)", str->s, str->len); |
robert.swiecki@gmail.com | 4f1124f | 2015-04-21 17:12:22 +0000 | [diff] [blame] | 294 | } |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 295 | LOG_I("Loaded %zu words from the dictionary", hfuzz->dictionaryCnt); |
robert.swiecki@gmail.com | 4f1124f | 2015-04-21 17:12:22 +0000 | [diff] [blame] | 296 | return true; |
| 297 | } |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 298 | |
| 299 | /* |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 300 | * dstExists argument can be used by caller for cases where existing destination |
| 301 | * file requires special handling (e.g. save unique crashes) |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 302 | */ |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 303 | bool files_copyFile(const char *source, const char *destination, bool * dstExists) |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 304 | { |
Anestis Bechtsoudis | 3877321 | 2015-09-08 23:34:28 +0300 | [diff] [blame] | 305 | if (dstExists) |
| 306 | *dstExists = false; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 307 | if (link(source, destination) == 0) { |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 308 | return true; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 309 | } else { |
| 310 | if (errno == EEXIST) { |
| 311 | // Should kick-in before MAC, so avoid the hassle |
Anestis Bechtsoudis | 3877321 | 2015-09-08 23:34:28 +0300 | [diff] [blame] | 312 | if (dstExists) |
| 313 | *dstExists = true; |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 314 | return false; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 315 | } else { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 316 | PLOG_D("Couldn't link '%s' as '%s'", source, destination); |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 317 | /* |
| 318 | * Don't fail yet as we might have a running env which doesn't allow |
| 319 | * hardlinks (e.g. SELinux) |
| 320 | */ |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // Now try with a verbose POSIX alternative |
| 325 | int inFD, outFD, dstOpenFlags; |
| 326 | mode_t dstFilePerms; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 327 | |
| 328 | // O_EXCL is important for saving unique crashes |
Robert Swiecki | a95854a | 2016-11-14 17:55:43 +0100 | [diff] [blame] | 329 | dstOpenFlags = O_CREAT | O_WRONLY | O_EXCL | O_CLOEXEC; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 330 | dstFilePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; |
| 331 | |
Robert Swiecki | a95854a | 2016-11-14 17:55:43 +0100 | [diff] [blame] | 332 | inFD = open(source, O_RDONLY | O_CLOEXEC); |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 333 | if (inFD == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 334 | PLOG_D("Couldn't open '%s' source", source); |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 335 | return false; |
| 336 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 337 | defer { |
| 338 | close(inFD); |
| 339 | }; |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 340 | |
| 341 | struct stat inSt; |
| 342 | if (fstat(inFD, &inSt) == -1) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 343 | PLOG_W("Couldn't fstat(fd='%d' fileName='%s')", inFD, source); |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 344 | return false; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | outFD = open(destination, dstOpenFlags, dstFilePerms); |
| 348 | if (outFD == -1) { |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 349 | if (errno == EEXIST) { |
Anestis Bechtsoudis | 3877321 | 2015-09-08 23:34:28 +0300 | [diff] [blame] | 350 | if (dstExists) |
| 351 | *dstExists = true; |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 352 | } |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 353 | PLOG_D("Couldn't open '%s' destination", destination); |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 354 | return false; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 355 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 356 | defer { |
| 357 | close(outFD); |
| 358 | }; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 359 | |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 360 | uint8_t *inFileBuf = malloc(inSt.st_size); |
| 361 | if (!inFileBuf) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 362 | PLOG_W("malloc(%zu) failed", (size_t) inSt.st_size); |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 363 | return false; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 364 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 365 | defer { |
| 366 | free(inFileBuf); |
| 367 | }; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 368 | |
Robert Swiecki | decf14b | 2016-03-31 15:09:28 +0200 | [diff] [blame] | 369 | ssize_t readSz = files_readFromFd(inFD, inFileBuf, (size_t) inSt.st_size); |
| 370 | if (readSz < 0) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 371 | PLOG_W("Couldn't read '%s' to a buf", source); |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 372 | return false; |
| 373 | } |
| 374 | |
Jagger | 4d10985 | 2016-02-04 02:28:44 +0100 | [diff] [blame] | 375 | if (files_writeToFd(outFD, inFileBuf, readSz) == false) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 376 | PLOG_W("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] | 377 | destination, outFD); |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 378 | unlink(destination); |
| 379 | return false; |
| 380 | } |
| 381 | |
Anestis Bechtsoudis | 1ae1ce1 | 2015-09-06 23:43:48 +0300 | [diff] [blame] | 382 | return true; |
Anestis Bechtsoudis | 7d42f9d | 2015-09-06 14:30:20 +0300 | [diff] [blame] | 383 | } |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 384 | |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 385 | bool files_parseBlacklist(honggfuzz_t * hfuzz) |
| 386 | { |
| 387 | FILE *fBl = fopen(hfuzz->blacklistFile, "rb"); |
| 388 | if (fBl == NULL) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 389 | PLOG_W("Couldn't open '%s' - R/O mode", hfuzz->blacklistFile); |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 390 | return false; |
| 391 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 392 | defer { |
| 393 | fclose(fBl); |
| 394 | }; |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 395 | |
Anestis Bechtsoudis | 3a8e16f | 2016-01-10 15:06:09 +0200 | [diff] [blame] | 396 | char *lineptr = NULL; |
Jagger | 1dad182 | 2016-03-29 23:56:14 +0200 | [diff] [blame] | 397 | /* lineptr can be NULL, but it's fine for free() */ |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 398 | defer { |
| 399 | free(lineptr); |
| 400 | }; |
Anestis Bechtsoudis | 3a8e16f | 2016-01-10 15:06:09 +0200 | [diff] [blame] | 401 | size_t n = 0; |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 402 | for (;;) { |
Anestis Bechtsoudis | 7d6185c | 2015-09-21 15:59:56 +0300 | [diff] [blame] | 403 | if (getline(&lineptr, &n, fBl) == -1) { |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 404 | break; |
| 405 | } |
Anestis Bechtsoudis | 99db874 | 2015-09-23 11:37:26 +0300 | [diff] [blame] | 406 | |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 407 | if ((hfuzz->blacklist = |
Jagger | f49962d | 2016-07-21 22:49:54 +0200 | [diff] [blame] | 408 | util_Realloc(hfuzz->blacklist, |
| 409 | (hfuzz->blacklistCnt + 1) * sizeof(hfuzz->blacklist[0]))) == NULL) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 410 | PLOG_W("realloc failed (sz=%zu)", |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 411 | (hfuzz->blacklistCnt + 1) * sizeof(hfuzz->blacklist[0])); |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 412 | return false; |
| 413 | } |
Anestis Bechtsoudis | 99db874 | 2015-09-23 11:37:26 +0300 | [diff] [blame] | 414 | |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 415 | hfuzz->blacklist[hfuzz->blacklistCnt] = strtoull(lineptr, 0, 16); |
Anestis Bechtsoudis | 07e14ce | 2015-12-30 14:15:42 +0200 | [diff] [blame] | 416 | LOG_D("Blacklist: loaded %'" PRIu64 "'", hfuzz->blacklist[hfuzz->blacklistCnt]); |
Anestis Bechtsoudis | 99db874 | 2015-09-23 11:37:26 +0300 | [diff] [blame] | 417 | |
| 418 | // Verify entries are sorted so we can use interpolation search |
| 419 | if (hfuzz->blacklistCnt > 1) { |
| 420 | if (hfuzz->blacklist[hfuzz->blacklistCnt - 1] > hfuzz->blacklist[hfuzz->blacklistCnt]) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 421 | LOG_F |
| 422 | ("Blacklist file not sorted. Use 'tools/createStackBlacklist.sh' to sort records"); |
Anestis Bechtsoudis | 99db874 | 2015-09-23 11:37:26 +0300 | [diff] [blame] | 423 | return false; |
| 424 | } |
| 425 | } |
Anestis Bechtsoudis | 612baa5 | 2015-09-23 18:21:43 +0300 | [diff] [blame] | 426 | hfuzz->blacklistCnt += 1; |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 427 | } |
Anestis Bechtsoudis | 99db874 | 2015-09-23 11:37:26 +0300 | [diff] [blame] | 428 | |
Anestis Bechtsoudis | ebf879e | 2015-09-27 18:11:25 +0300 | [diff] [blame] | 429 | if (hfuzz->blacklistCnt > 0) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 430 | 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] | 431 | } else { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 432 | LOG_F("Empty stack hashes blacklist file '%s'", hfuzz->blacklistFile); |
Anestis Bechtsoudis | 99db874 | 2015-09-23 11:37:26 +0300 | [diff] [blame] | 433 | } |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 434 | return true; |
| 435 | } |
Anestis Bechtsoudis | ee5316d | 2015-09-27 19:11:48 +0300 | [diff] [blame] | 436 | |
Anestis Bechtsoudis | 3dce6ac | 2016-10-31 08:51:14 +0200 | [diff] [blame] | 437 | /* |
| 438 | * Reads symbols from src file (one per line) and append them to filterList. The |
| 439 | * total number of added symbols is returned. |
| 440 | * |
| 441 | * Simple wildcard strings are also supported (e.g. mem*) |
| 442 | */ |
| 443 | size_t files_parseSymbolFilter(const char *srcFile, char ***filterList) |
Anestis Bechtsoudis | ba68b38 | 2016-10-29 20:44:15 +0300 | [diff] [blame] | 444 | { |
Anestis Bechtsoudis | 3dce6ac | 2016-10-31 08:51:14 +0200 | [diff] [blame] | 445 | FILE *f = fopen(srcFile, "rb"); |
| 446 | if (f == NULL) { |
| 447 | PLOG_W("Couldn't open '%s' - R/O mode", srcFile); |
Anestis Bechtsoudis | ba68b38 | 2016-10-29 20:44:15 +0300 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | defer { |
Anestis Bechtsoudis | 3dce6ac | 2016-10-31 08:51:14 +0200 | [diff] [blame] | 451 | fclose(f); |
Anestis Bechtsoudis | ba68b38 | 2016-10-29 20:44:15 +0300 | [diff] [blame] | 452 | }; |
| 453 | |
| 454 | char *lineptr = NULL; |
| 455 | defer { |
| 456 | free(lineptr); |
| 457 | }; |
| 458 | |
| 459 | size_t symbolsRead = 0, n = 0; |
| 460 | for (;;) { |
Anestis Bechtsoudis | 3dce6ac | 2016-10-31 08:51:14 +0200 | [diff] [blame] | 461 | if (getline(&lineptr, &n, f) == -1) { |
Anestis Bechtsoudis | ba68b38 | 2016-10-29 20:44:15 +0300 | [diff] [blame] | 462 | break; |
| 463 | } |
| 464 | |
| 465 | if (strlen(lineptr) < 3) { |
| 466 | LOG_F("Input symbol '%s' too short (strlen < 3)", lineptr); |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | if ((*filterList = |
Anestis Bechtsoudis | 710c856 | 2016-10-31 09:24:49 +0200 | [diff] [blame] | 471 | (char **)util_Realloc(*filterList, |
| 472 | (symbolsRead + 1) * sizeof((*filterList)[0]))) == NULL) { |
| 473 | PLOG_W("realloc failed (sz=%zu)", (symbolsRead + 1) * sizeof((*filterList)[0])); |
Anestis Bechtsoudis | ba68b38 | 2016-10-29 20:44:15 +0300 | [diff] [blame] | 474 | return 0; |
| 475 | } |
Anestis Bechtsoudis | 710c856 | 2016-10-31 09:24:49 +0200 | [diff] [blame] | 476 | (*filterList)[symbolsRead] = malloc(strlen(lineptr)); |
| 477 | if (!(*filterList)[symbolsRead]) { |
Anestis Bechtsoudis | ba68b38 | 2016-10-29 20:44:15 +0300 | [diff] [blame] | 478 | PLOG_E("malloc(%zu) failed", strlen(lineptr)); |
| 479 | return 0; |
| 480 | } |
Anestis Bechtsoudis | 710c856 | 2016-10-31 09:24:49 +0200 | [diff] [blame] | 481 | strncpy((*filterList)[symbolsRead], lineptr, strlen(lineptr)); |
Anestis Bechtsoudis | ba68b38 | 2016-10-29 20:44:15 +0300 | [diff] [blame] | 482 | symbolsRead++; |
| 483 | } |
| 484 | |
| 485 | LOG_I("%zu filter symbols added to list", symbolsRead); |
| 486 | return symbolsRead; |
| 487 | } |
| 488 | |
Anestis Bechtsoudis | ecb0a66 | 2015-09-27 18:19:46 +0300 | [diff] [blame] | 489 | 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] | 490 | { |
| 491 | int mmapProt = PROT_READ; |
| 492 | if (isWritable) { |
| 493 | mmapProt |= PROT_WRITE; |
| 494 | } |
| 495 | |
| 496 | if ((*fd = open(fileName, O_RDONLY)) == -1) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 497 | PLOG_W("Couldn't open() '%s' file in R/O mode", fileName); |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 498 | return NULL; |
| 499 | } |
| 500 | |
| 501 | struct stat st; |
| 502 | if (fstat(*fd, &st) == -1) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 503 | PLOG_W("Couldn't stat() the '%s' file", fileName); |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 504 | close(*fd); |
| 505 | return NULL; |
| 506 | } |
| 507 | |
| 508 | uint8_t *buf; |
| 509 | if ((buf = mmap(NULL, st.st_size, mmapProt, MAP_PRIVATE, *fd, 0)) == MAP_FAILED) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 510 | PLOG_W("Couldn't mmap() the '%s' file", fileName); |
Anestis Bechtsoudis | 5c86ebc | 2015-09-27 18:06:43 +0300 | [diff] [blame] | 511 | close(*fd); |
| 512 | return NULL; |
| 513 | } |
| 514 | |
| 515 | *fileSz = st.st_size; |
| 516 | return buf; |
| 517 | } |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 518 | |
Jagger | 41e7ffc | 2016-07-23 03:50:32 +0200 | [diff] [blame] | 519 | uint8_t *files_mapFileShared(char *fileName, off_t * fileSz, int *fd) |
| 520 | { |
| 521 | if ((*fd = open(fileName, O_RDONLY)) == -1) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 522 | PLOG_W("Couldn't open() '%s' file in R/O mode", fileName); |
Jagger | 41e7ffc | 2016-07-23 03:50:32 +0200 | [diff] [blame] | 523 | return NULL; |
| 524 | } |
| 525 | |
| 526 | struct stat st; |
| 527 | if (fstat(*fd, &st) == -1) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 528 | PLOG_W("Couldn't stat() the '%s' file", fileName); |
Jagger | 41e7ffc | 2016-07-23 03:50:32 +0200 | [diff] [blame] | 529 | close(*fd); |
| 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | uint8_t *buf; |
| 534 | if ((buf = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, *fd, 0)) == MAP_FAILED) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 535 | PLOG_W("Couldn't mmap() the '%s' file", fileName); |
Jagger | 41e7ffc | 2016-07-23 03:50:32 +0200 | [diff] [blame] | 536 | close(*fd); |
| 537 | return NULL; |
| 538 | } |
| 539 | |
| 540 | *fileSz = st.st_size; |
| 541 | return buf; |
| 542 | } |
| 543 | |
Jagger | 5b81a50 | 2016-09-07 20:48:32 +0200 | [diff] [blame] | 544 | void *files_mapSharedMem(size_t sz, int *fd, const char *dir) |
Jagger | 08a7e27 | 2016-08-28 17:25:41 +0200 | [diff] [blame] | 545 | { |
Jagger | 5b81a50 | 2016-09-07 20:48:32 +0200 | [diff] [blame] | 546 | char template[PATH_MAX]; |
| 547 | snprintf(template, sizeof(template), "%s/hfuzz.XXXXXX", dir); |
Jagger | 08a7e27 | 2016-08-28 17:25:41 +0200 | [diff] [blame] | 548 | if ((*fd = mkstemp(template)) == -1) { |
| 549 | PLOG_W("mkstemp('%s')", template); |
| 550 | return MAP_FAILED; |
| 551 | } |
| 552 | unlink(template); |
| 553 | if (ftruncate(*fd, sz) == -1) { |
| 554 | PLOG_W("ftruncate(%d, %zu)", *fd, sz); |
| 555 | close(*fd); |
Robert Swiecki | 19fa37a | 2016-10-27 19:20:13 +0200 | [diff] [blame] | 556 | *fd = -1; |
Jagger | 08a7e27 | 2016-08-28 17:25:41 +0200 | [diff] [blame] | 557 | return MAP_FAILED; |
| 558 | } |
| 559 | void *ret = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_SHARED, *fd, 0); |
| 560 | if (ret == MAP_FAILED) { |
| 561 | PLOG_W("mmap(sz=%zu, fd=%d)", sz, *fd); |
Robert Swiecki | 19fa37a | 2016-10-27 19:20:13 +0200 | [diff] [blame] | 562 | *fd = -1; |
Jagger | 08a7e27 | 2016-08-28 17:25:41 +0200 | [diff] [blame] | 563 | close(*fd); |
| 564 | return MAP_FAILED; |
| 565 | } |
| 566 | return ret; |
| 567 | } |
| 568 | |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 569 | bool files_readPidFromFile(const char *fileName, pid_t * pidPtr) |
| 570 | { |
Jagger | ff2d20c | 2016-09-10 03:38:58 +0200 | [diff] [blame] | 571 | FILE *fPID = fopen(fileName, "rbe"); |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 572 | if (fPID == NULL) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 573 | PLOG_W("Couldn't open '%s' - R/O mode", fileName); |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 574 | return false; |
| 575 | } |
Jagger | 4fe1869 | 2016-04-22 23:15:07 +0200 | [diff] [blame] | 576 | defer { |
| 577 | fclose(fPID); |
| 578 | }; |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 579 | |
| 580 | char *lineptr = NULL; |
| 581 | size_t lineSz = 0; |
Jagger | ebaf01e | 2016-05-15 15:13:34 +0200 | [diff] [blame] | 582 | defer { |
| 583 | free(lineptr); |
| 584 | }; |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 585 | if (getline(&lineptr, &lineSz, fPID) == -1) { |
| 586 | if (lineSz == 0) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 587 | LOG_W("Empty PID file (%s)", fileName); |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 588 | return false; |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | *pidPtr = atoi(lineptr); |
| 593 | if (*pidPtr < 1) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 594 | LOG_W("Invalid PID read from '%s' file", fileName); |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 595 | return false; |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 596 | } |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 597 | |
Robert Swiecki | a9db9dd | 2016-03-09 16:29:37 +0100 | [diff] [blame] | 598 | return true; |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 599 | } |
Robert Swiecki | e8f8e8d | 2016-10-03 23:51:32 +0200 | [diff] [blame] | 600 | |
| 601 | struct paths_t *files_getFileFromFileq(honggfuzz_t * hfuzz, size_t index) |
| 602 | { |
| 603 | if (index >= hfuzz->fileCnt) { |
| 604 | LOG_F("File index: %zu >= fileCnt: %zu", index, hfuzz->fileCnt); |
| 605 | } |
| 606 | |
| 607 | struct paths_t *file; |
| 608 | size_t i = 0; |
| 609 | TAILQ_FOREACH(file, &hfuzz->fileq, pointers) { |
| 610 | if (i++ == index) { |
| 611 | return file; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | LOG_F("File index: %zu bigger than fileq size", index); |
| 616 | return NULL; |
| 617 | } |