Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * honggfuzz - sanitizer coverage feedback parsing |
| 4 | * ----------------------------------------------- |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | * not use this file except in compliance with the License. You may obtain |
| 8 | * a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 15 | * implied. See the License for the specific language governing |
| 16 | * permissions and limitations under the License. |
| 17 | * |
| 18 | */ |
| 19 | |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 20 | /* |
| 21 | * Clang sanitizer coverage (sancov) data parsing functions. Supported methods: |
| 22 | * - raw unified data (preferred method) |
| 23 | * - individual data per executable/DSO (not preferred since lots of data lost if instrumented |
| 24 | * code exits abnormally or with sanitizer unhandled signal (common in Android OS) |
Robert Swiecki | 72d2bef | 2016-01-19 14:39:26 +0100 | [diff] [blame] | 25 | * |
| 26 | * For raw-unpack method a global (shared across workers) Trie is created for the chosen |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 27 | * initial seed and maintained until seed is replaced. Trie nodes store the loaded (as exposed |
| 28 | * from *.sancov.map file) execs/DSOs from target application using the map name as key. Trie node |
| 29 | * data struct (trieData_t) maintains information for each instrumented map including a bitmap with |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 30 | * all hit relative BB addresses (realBBAddr - baseAddr to circumvent ASLR). Map's bitmap is updated while |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 31 | * new areas on target application are discovered based on absolute elitism implemented at |
| 32 | * fuzz_sanCovFeedback(). |
Robert Swiecki | 72d2bef | 2016-01-19 14:39:26 +0100 | [diff] [blame] | 33 | * |
Anestis Bechtsoudis | a7c56ce | 2016-02-07 12:53:20 +0200 | [diff] [blame] | 34 | * For individual data files a pid (fuzzer's thread or remote process) based filename search is performed |
| 35 | * to identify all files belonging to examined execution. This method doesn't implement yet bitmap runtime |
| 36 | * data to detect newly discovered areas. It's mainly used so far as a comparison metric for raw-unpack method |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 37 | * and stability check for sancov experimental features such as coverage counters: |
| 38 | * http://clang.llvm.org/docs/SanitizerCoverage.html |
| 39 | */ |
| 40 | |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 41 | #include "common.h" |
| 42 | #include "sancov.h" |
| 43 | |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 44 | #include <ctype.h> |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 45 | #include <dirent.h> |
| 46 | #include <inttypes.h> |
| 47 | #include <stdio.h> |
| 48 | #include <stdlib.h> |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 49 | #include <string.h> |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 50 | #include <sys/mman.h> |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 51 | #include <sys/stat.h> |
| 52 | #include <sys/types.h> |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 53 | |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 54 | #include "files.h" |
| 55 | #include "log.h" |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 56 | #include "util.h" |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 57 | |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 58 | /* sancov files magic values */ |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 59 | #define kMagic32 0xC0BFFFFFFFFFFF32 |
| 60 | #define kMagic64 0xC0BFFFFFFFFFFF64 |
| 61 | |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 62 | /* Stringify */ |
| 63 | #define XSTR(x) #x |
| 64 | #define STR(x) XSTR(x) |
| 65 | |
| 66 | /* Common sanitizer flags */ |
| 67 | #if _HF_MONITOR_SIGABRT |
| 68 | #define ABORT_FLAG "abort_on_error=1" |
| 69 | #else |
| 70 | #define ABORT_FLAG "abort_on_error=0" |
| 71 | #endif |
| 72 | |
| 73 | #if defined(__ANDROID__) |
| 74 | /* |
| 75 | * symbolize: Disable symbolication since it changes logs (which are parsed) format |
| 76 | * start_deactivated: Enable on Android to reduce memory usage (useful when not all |
| 77 | * target's DSOs are compiled with sanitizer enabled |
| 78 | * abort_on_error: Disable for platforms where SIGABRT is not monitored |
| 79 | */ |
| 80 | #define kSAN_COMMON_ARCH "symbolize=0:"ABORT_FLAG":start_deactivated=1" |
| 81 | #else |
| 82 | #define kSAN_COMMON_ARCH "symbolize=0:"ABORT_FLAG |
| 83 | #endif |
| 84 | |
| 85 | /* Sanitizer specific flags (set 'abort_on_error has priority over exitcode') */ |
| 86 | #define kASAN_OPTS "allow_user_segv_handler=1:"\ |
| 87 | "handle_segv=0:"\ |
| 88 | "allocator_may_return_null=1:"\ |
| 89 | kSAN_COMMON_ARCH":exitcode=" STR(HF_ASAN_EXIT_CODE) |
| 90 | |
| 91 | #define kUBSAN_OPTS kSAN_COMMON_ARCH":exitcode=" STR(HF_UBSAN_EXIT_CODE) |
| 92 | |
| 93 | #define kMSAN_OPTS "exit_code=" STR(HF_MSAN_EXIT_CODE) ":"\ |
| 94 | "wrap_signals=0:print_stats=1" |
| 95 | |
| 96 | /* 'log_path' ouput directory for sanitizer reports */ |
| 97 | #define kSANLOGDIR "log_path=" |
| 98 | |
| 99 | /* 'coverage_dir' output directory for coverage data files is set dynamically */ |
| 100 | #define kSANCOVDIR "coverage_dir=" |
| 101 | |
| 102 | /* |
| 103 | * If the program ends with a signal that ASan does not handle (or can not |
| 104 | * handle at all, like SIGKILL), coverage data will be lost. This is a big |
| 105 | * problem on Android, where SIGKILL is a normal way of evicting applications |
| 106 | * from memory. With 'coverage_direct=1' coverage data is written to a |
| 107 | * memory-mapped file as soon as it collected. Non-Android targets can disable |
| 108 | * coverage direct when more coverage data collection methods are implemented. |
| 109 | */ |
| 110 | #if defined(__ANDROID__) |
| 111 | #define kSAN_COV_OPTS "coverage=1:coverage_direct=1" |
| 112 | #else |
| 113 | #define kSAN_COV_OPTS "coverage=1:coverage_direct=1" |
| 114 | #endif |
| 115 | |
Robert Swiecki | 72d2bef | 2016-01-19 14:39:26 +0100 | [diff] [blame] | 116 | /* |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 117 | * bitmap implementation |
| 118 | */ |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 119 | static bitmap_t *sancov_newBitmap(uint32_t capacity) |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 120 | { |
Jagger | 679c205 | 2016-03-18 22:53:53 +0100 | [diff] [blame] | 121 | bitmap_t *pBM = util_Malloc(sizeof(bitmap_t)); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 122 | pBM->capacity = capacity; |
| 123 | pBM->nChunks = (capacity + 31) / 32; |
Jagger | 74d0d10 | 2016-03-18 22:55:59 +0100 | [diff] [blame] | 124 | pBM->pChunks = util_Calloc(pBM->nChunks * sizeof(uint32_t)); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 125 | return pBM; |
| 126 | } |
| 127 | |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 128 | static inline bool sancov_queryBitmap(bitmap_t * pBM, uint32_t index) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 129 | { |
Anestis Bechtsoudis | 58c45d2 | 2016-01-10 15:05:39 +0200 | [diff] [blame] | 130 | if (index > pBM->capacity) { |
| 131 | LOG_E("bitmap overflow (%u)", index); |
| 132 | return false; |
| 133 | } |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 134 | if (pBM->pChunks[index / 32] & (1 << (index % 32))) { |
| 135 | return true; |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 140 | static inline void sancov_setBitmap(bitmap_t * pBM, uint32_t index) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 141 | { |
| 142 | /* This will be removed. So far checks only to verify accepted ranges. */ |
| 143 | if (index >= pBM->capacity) { |
| 144 | LOG_E("Out of range index (%u > %u)", index, pBM->capacity); |
| 145 | } |
| 146 | pBM->pChunks[index / 32] |= (1 << (index % 32)); |
| 147 | } |
| 148 | |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 149 | static inline void sancov_destroyBitmap(bitmap_t * pBM) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 150 | { |
| 151 | free(pBM->pChunks); |
| 152 | free(pBM); |
| 153 | } |
| 154 | |
Robert Swiecki | 72d2bef | 2016-01-19 14:39:26 +0100 | [diff] [blame] | 155 | /* |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 156 | * Trie implementation |
| 157 | */ |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 158 | static node_t *sancov_trieCreateNode(char key) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 159 | { |
Jagger | 679c205 | 2016-03-18 22:53:53 +0100 | [diff] [blame] | 160 | node_t *node = (node_t *) util_Malloc(sizeof(node_t)); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 161 | node->key = key; |
| 162 | node->next = NULL; |
| 163 | node->children = NULL; |
| 164 | node->parent = NULL; |
| 165 | node->prev = NULL; |
| 166 | |
| 167 | /* Zero init node's data struct */ |
| 168 | memset(&node->data, 0, sizeof(trieData_t)); |
| 169 | return node; |
| 170 | } |
| 171 | |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 172 | static node_t *sancov_trieSearch(node_t * root, const char *key) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 173 | { |
| 174 | node_t *pNodeLevel = root, *pNodePtr = NULL; |
| 175 | int nodeLevelId = 0; |
| 176 | while (1) { |
| 177 | node_t *pNodeFound = NULL, *pCurNode = NULL; |
| 178 | for (pCurNode = pNodeLevel; pCurNode != NULL; pCurNode = pCurNode->next) { |
| 179 | if (pCurNode->key == *key) { |
| 180 | pNodeFound = pCurNode; |
| 181 | nodeLevelId++; |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | if (pNodeFound == NULL) { |
| 186 | return NULL; |
| 187 | } |
| 188 | if (*key == '\0') { |
| 189 | pNodePtr = pCurNode; |
| 190 | return pNodePtr; |
| 191 | } |
| 192 | pNodeLevel = pNodeFound->children; |
| 193 | key++; |
| 194 | } |
| 195 | } |
| 196 | |
Robert Swiecki | 40ef840 | 2016-03-10 15:14:13 +0100 | [diff] [blame] | 197 | static void sancov_trieAdd(node_t ** root, const char *key) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 198 | { |
| 199 | if (*root == NULL) { |
| 200 | LOG_E("Invalid Trie (NULL root node)"); |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 204 | /* Traverse Trie */ |
| 205 | node_t *pTravNode = (*root)->children; |
| 206 | if (pTravNode == NULL) { |
| 207 | /* First node */ |
Anestis Bechtsoudis | 267f0d8 | 2016-01-08 16:02:50 +0200 | [diff] [blame] | 208 | for (pTravNode = *root; *key != '\0'; pTravNode = pTravNode->children) { |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 209 | pTravNode->children = sancov_trieCreateNode(*key); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 210 | pTravNode->children->parent = pTravNode; |
| 211 | key++; |
| 212 | } |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 213 | pTravNode->children = sancov_trieCreateNode('\0'); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 214 | pTravNode->children->parent = pTravNode; |
| 215 | return; |
| 216 | } |
| 217 | |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 218 | while (*key != '\0') { |
| 219 | if (*key == pTravNode->key) { |
| 220 | key++; |
| 221 | pTravNode = pTravNode->children; |
| 222 | } else { |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | while (pTravNode->next) { |
| 227 | if (*key == pTravNode->next->key) { |
| 228 | key++; |
Robert Swiecki | 40ef840 | 2016-03-10 15:14:13 +0100 | [diff] [blame] | 229 | sancov_trieAdd(&(pTravNode->next), key); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 230 | return; |
| 231 | } |
| 232 | pTravNode = pTravNode->next; |
| 233 | } |
| 234 | if (*key) { |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 235 | pTravNode->next = sancov_trieCreateNode(*key); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 236 | } else { |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 237 | pTravNode->next = sancov_trieCreateNode(*key); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 238 | } |
| 239 | pTravNode->next->parent = pTravNode->parent; |
| 240 | pTravNode->next->prev = pTravNode; |
| 241 | if (!*key) { |
| 242 | return; |
| 243 | } |
| 244 | key++; |
| 245 | for (pTravNode = pTravNode->next; *key; pTravNode = pTravNode->children) { |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 246 | pTravNode->children = sancov_trieCreateNode(*key); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 247 | pTravNode->children->parent = pTravNode; |
| 248 | key++; |
| 249 | } |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 250 | pTravNode->children = sancov_trieCreateNode('\0'); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 251 | pTravNode->children->parent = pTravNode; |
| 252 | |
| 253 | return; |
| 254 | } |
| 255 | |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 256 | static inline void sancov_trieFreeNode(node_t * node) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 257 | { |
| 258 | /* First destroy bitmap heap buffers allocated for instrumented maps */ |
| 259 | if (node->data.pBM) { |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 260 | sancov_destroyBitmap(node->data.pBM); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 261 | } |
| 262 | free(node); |
| 263 | } |
| 264 | |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 265 | static inline void sancov_trieCreate(node_t ** root) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 266 | { |
| 267 | /* Create root node if new Trie */ |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 268 | *root = sancov_trieCreateNode('\0'); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* Destroy Trie - iterate nodes and free memory */ |
Robert Swiecki | 3bfc33c | 2016-03-14 18:12:41 +0100 | [diff] [blame] | 272 | UNUSED static void sancov_trieDestroy(node_t * root) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 273 | { |
| 274 | node_t *pNode = root; |
| 275 | node_t *pNodeTmp = root; |
| 276 | while (pNode) { |
| 277 | while (pNode->children) { |
| 278 | pNode = pNode->children; |
| 279 | } |
| 280 | |
| 281 | if (pNode->prev && pNode->next) { |
| 282 | pNodeTmp = pNode; |
| 283 | pNode->next->prev = pNode->prev; |
| 284 | pNode->prev->next = pNode->next; |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 285 | sancov_trieFreeNode(pNodeTmp); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 286 | } else if (pNode->prev && !pNode->next) { |
| 287 | pNodeTmp = pNode; |
| 288 | pNode->prev->next = NULL; |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 289 | sancov_trieFreeNode(pNodeTmp); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 290 | } else if (!pNode->prev && pNode->next) { |
| 291 | pNodeTmp = pNode; |
| 292 | pNode->parent->children = pNode->next; |
| 293 | pNode->next->prev = NULL; |
| 294 | pNode = pNode->next; |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 295 | sancov_trieFreeNode(pNodeTmp); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 296 | } else { |
| 297 | pNodeTmp = pNode; |
| 298 | if (pNode->parent == NULL) { |
| 299 | /* Root */ |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 300 | sancov_trieFreeNode(pNodeTmp); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 301 | return; |
| 302 | } |
| 303 | pNode = pNode->parent; |
| 304 | pNode->children = NULL; |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 305 | sancov_trieFreeNode(pNodeTmp); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /* Modified interpolation search algorithm to search for nearest address fit */ |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 311 | static inline uint64_t sancov_interpSearch(uint64_t * buf, uint64_t size, uint64_t key) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 312 | { |
| 313 | /* Avoid extra checks assuming caller always provides non-zero array size */ |
| 314 | uint64_t low = 0; |
| 315 | uint64_t high = size - 1; |
| 316 | uint64_t mid = high; |
| 317 | |
| 318 | while (buf[high] != buf[low] && key >= buf[low] && key <= buf[high]) { |
| 319 | mid = low + (key - buf[low]) * ((high - low) / (buf[high] - buf[low])); |
| 320 | if (buf[mid] < key) { |
| 321 | low = mid + 1; |
| 322 | } else if (key < buf[mid]) { |
| 323 | high = mid - 1; |
| 324 | } else { |
| 325 | return mid; |
| 326 | } |
| 327 | } |
| 328 | return mid; |
| 329 | } |
| 330 | |
| 331 | /* qsort struct comparison function (memMap_t struct start addr field) */ |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 332 | static int sancov_qsortCmp(const void *a, const void *b) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 333 | { |
| 334 | memMap_t *pA = (memMap_t *) a; |
| 335 | memMap_t *pB = (memMap_t *) b; |
| 336 | if (pA->start < pB->start) { |
| 337 | return -1; |
| 338 | } else if (pA->start > pB->start) { |
| 339 | return 1; |
| 340 | } else { |
| 341 | /* Normally we should never hit that case */ |
| 342 | LOG_W("Duplicate map start addr detected"); |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | } |
| 347 | |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 348 | static bool sancov_sanCovParseRaw(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 349 | { |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 350 | int dataFd = -1; |
| 351 | uint8_t *dataBuf = NULL; |
| 352 | off_t dataFileSz = 0, pos = 0; |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 353 | bool is32bit = true; |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 354 | char covFile[PATH_MAX] = { 0 }; |
Jagger | 247c3b4 | 2016-03-21 23:24:05 +0100 | [diff] [blame] | 355 | pid_t targetPid = (hfuzz->linux.pid > 0) ? hfuzz->linux.pid : fuzzer->pid; |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 356 | |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 357 | /* Fuzzer local runtime data structs - need free() before exit */ |
| 358 | uint64_t *startMapsIndex = NULL; |
| 359 | memMap_t *mapsBuf = NULL; |
| 360 | |
| 361 | /* Local counters */ |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 362 | uint64_t nBBs = 0; /* Total BB hits found in raw file */ |
| 363 | uint64_t nZeroBBs = 0; /* Number of non-hit instrumented BBs */ |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 364 | uint64_t mapsNum = 0; /* Total number of entries in map file */ |
| 365 | uint64_t noCovMapsNum = 0; /* Loaded DSOs not compiled with coverage */ |
| 366 | |
Anestis Bechtsoudis | 267f0d8 | 2016-01-08 16:02:50 +0200 | [diff] [blame] | 367 | /* File line-by-line read help buffers */ |
Jagger | d6f74bb | 2016-03-19 02:51:18 +0100 | [diff] [blame] | 368 | __block char *pLine = NULL; |
Anestis Bechtsoudis | 267f0d8 | 2016-01-08 16:02:50 +0200 | [diff] [blame] | 369 | size_t lineSz = 0; |
| 370 | |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 371 | /* Coverage data analysis starts by parsing map file listing */ |
| 372 | snprintf(covFile, sizeof(covFile), "%s/%s/%d.sancov.map", hfuzz->workDir, _HF_SANCOV_DIR, |
Anestis Bechtsoudis | a7c56ce | 2016-02-07 12:53:20 +0200 | [diff] [blame] | 373 | targetPid); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 374 | if (!files_exists(covFile)) { |
| 375 | LOG_D("sancov map file not found"); |
| 376 | return false; |
| 377 | } |
| 378 | FILE *fCovMap = fopen(covFile, "rb"); |
| 379 | if (fCovMap == NULL) { |
| 380 | PLOG_E("Couldn't open '%s' - R/O mode", covFile); |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 381 | return false; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 382 | } |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 383 | DEFER(fclose(fCovMap)); |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 384 | |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 385 | /* First line contains PC length (32/64-bit) */ |
Anestis Bechtsoudis | 267f0d8 | 2016-01-08 16:02:50 +0200 | [diff] [blame] | 386 | if (getline(&pLine, &lineSz, fCovMap) == -1) { |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 387 | LOG_E("Invalid map file '%s'", covFile); |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 388 | return false; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 389 | } |
Jagger | 799a4d4 | 2016-04-01 23:27:51 +0200 | [diff] [blame] | 390 | DEFER(free(pLine); |
| 391 | pLine = NULL); |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 392 | |
Anestis Bechtsoudis | 267f0d8 | 2016-01-08 16:02:50 +0200 | [diff] [blame] | 393 | int pcLen = atoi(pLine); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 394 | if (pcLen == 32) { |
| 395 | is32bit = true; |
| 396 | } else if (pcLen == 64) { |
| 397 | is32bit = false; |
| 398 | } else { |
| 399 | LOG_E("Invalid PC length (%d) in map file '%s'", pcLen, covFile); |
| 400 | } |
Anestis Bechtsoudis | 1fc7cd4 | 2015-12-26 17:54:15 +0200 | [diff] [blame] | 401 | |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 402 | /* See if #maps is available from previous run to avoid realloc inside loop */ |
Jagger | d34417d | 2016-03-16 01:26:54 +0100 | [diff] [blame] | 403 | uint64_t prevMapsNum = ATOMIC_GET(hfuzz->sanCovCnts.dsoCnt); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 404 | if (prevMapsNum > 0) { |
Jagger | 679c205 | 2016-03-18 22:53:53 +0100 | [diff] [blame] | 405 | mapsBuf = util_Malloc(prevMapsNum * sizeof(memMap_t)); |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 406 | } |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 407 | /* It's OK to free(NULL) */ |
| 408 | DEFER(free(mapsBuf)); |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 409 | |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 410 | /* Iterate map entries */ |
| 411 | for (;;) { |
Anestis Bechtsoudis | 267f0d8 | 2016-01-08 16:02:50 +0200 | [diff] [blame] | 412 | if (getline(&pLine, &lineSz, fCovMap) == -1) { |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 413 | break; |
| 414 | } |
| 415 | |
| 416 | /* Trim trailing whitespaces, not sure if needed copied from upstream sancov.py */ |
Anestis Bechtsoudis | 267f0d8 | 2016-01-08 16:02:50 +0200 | [diff] [blame] | 417 | char *lineEnd = pLine + strlen(pLine) - 1; |
Jagger | 43c33e5 | 2016-03-11 22:16:26 +0100 | [diff] [blame] | 418 | while (lineEnd > pLine && isspace((int)*lineEnd)) { |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 419 | lineEnd--; |
| 420 | } |
| 421 | *(lineEnd + 1) = 0; |
| 422 | |
Robert Swiecki | 72d2bef | 2016-01-19 14:39:26 +0100 | [diff] [blame] | 423 | /* |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 424 | * Each line has following format: |
| 425 | * Start End Base bin/DSO name |
| 426 | * b5843000 b584e6ac b5843000 liblog.so |
| 427 | */ |
Jagger | 05c79c5 | 2016-01-31 18:00:51 +0100 | [diff] [blame] | 428 | memMap_t mapData = {.start = 0 }; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 429 | char *savePtr = NULL; |
Anestis Bechtsoudis | 267f0d8 | 2016-01-08 16:02:50 +0200 | [diff] [blame] | 430 | mapData.start = strtoull(strtok_r(pLine, " ", &savePtr), NULL, 16); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 431 | mapData.end = strtoull(strtok_r(NULL, " ", &savePtr), NULL, 16); |
| 432 | mapData.base = strtoull(strtok_r(NULL, " ", &savePtr), NULL, 16); |
| 433 | char *mapName = strtok_r(NULL, " ", &savePtr); |
| 434 | memcpy(mapData.mapName, mapName, strlen(mapName)); |
| 435 | |
| 436 | /* Interaction with global Trie should mutex wrap to avoid threads races */ |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 437 | { |
Robert Swiecki | 76ecd5e | 2016-03-16 14:57:03 +0100 | [diff] [blame] | 438 | MX_SCOPED_LOCK(&hfuzz->sanCov_mutex); |
Robert Swiecki | 5d6e734 | 2016-03-16 15:36:11 +0100 | [diff] [blame] | 439 | |
Anestis Bechtsoudis | 1fd10c7 | 2016-01-07 12:38:45 +0200 | [diff] [blame] | 440 | /* Add entry to Trie with zero data if not already */ |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 441 | if (!sancov_trieSearch(hfuzz->covMetadata->children, mapData.mapName)) { |
Robert Swiecki | 40ef840 | 2016-03-10 15:14:13 +0100 | [diff] [blame] | 442 | sancov_trieAdd(&hfuzz->covMetadata, mapData.mapName); |
Anestis Bechtsoudis | 1fd10c7 | 2016-01-07 12:38:45 +0200 | [diff] [blame] | 443 | } |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 444 | } |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 445 | |
| 446 | /* If not DSO number history (first run) or new DSO loaded, realloc local maps metadata buf */ |
| 447 | if (prevMapsNum == 0 || prevMapsNum < mapsNum) { |
| 448 | if ((mapsBuf = realloc(mapsBuf, (size_t) (mapsNum + 1) * sizeof(memMap_t))) == NULL) { |
| 449 | PLOG_E("realloc failed (sz=%" PRIu64 ")", (mapsNum + 1) * sizeof(memMap_t)); |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 450 | return false; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 451 | } |
| 452 | } |
Anestis Bechtsoudis | 1fd10c7 | 2016-01-07 12:38:45 +0200 | [diff] [blame] | 453 | |
| 454 | /* Add entry to local maps metadata array */ |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 455 | memcpy(&mapsBuf[mapsNum], &mapData, sizeof(memMap_t)); |
| 456 | |
| 457 | /* Increase loaded maps counter (includes non-instrumented DSOs too) */ |
| 458 | mapsNum++; |
| 459 | } |
| 460 | |
| 461 | /* Delete .sancov.map file */ |
Robert Swiecki | 8656cbb | 2016-03-30 19:06:37 +0200 | [diff] [blame] | 462 | if (hfuzz->linux.pid == 0 && hfuzz->persistent == false) { |
Jagger | 1ebc6dc | 2016-03-12 01:39:09 +0100 | [diff] [blame] | 463 | unlink(covFile); |
| 464 | } |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 465 | |
| 466 | /* Create a quick index array with maps start addresses */ |
Jagger | 679c205 | 2016-03-18 22:53:53 +0100 | [diff] [blame] | 467 | startMapsIndex = util_Malloc(mapsNum * sizeof(uint64_t)); |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 468 | DEFER(free(startMapsIndex)); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 469 | |
| 470 | /* Sort quick maps index */ |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 471 | qsort(mapsBuf, mapsNum, sizeof(memMap_t), sancov_qsortCmp); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 472 | for (size_t i = 0; i < mapsNum; i++) { |
| 473 | startMapsIndex[i] = mapsBuf[i].start; |
| 474 | } |
| 475 | |
| 476 | /* mmap() .sancov.raw file */ |
| 477 | snprintf(covFile, sizeof(covFile), "%s/%s/%d.sancov.raw", hfuzz->workDir, _HF_SANCOV_DIR, |
Anestis Bechtsoudis | a7c56ce | 2016-02-07 12:53:20 +0200 | [diff] [blame] | 478 | targetPid); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 479 | dataBuf = files_mapFile(covFile, &dataFileSz, &dataFd, false); |
| 480 | if (dataBuf == NULL) { |
| 481 | LOG_E("Couldn't open and map '%s' in R/O mode", covFile); |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 482 | return false; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 483 | } |
Jagger | 799a4d4 | 2016-04-01 23:27:51 +0200 | [diff] [blame] | 484 | DEFER(munmap(dataBuf, dataFileSz); close(dataFd)); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 485 | |
Robert Swiecki | 72d2bef | 2016-01-19 14:39:26 +0100 | [diff] [blame] | 486 | /* |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 487 | * Avoid cost of size checks inside raw data read loop by defining the read function |
| 488 | * & pivot size based on PC length. |
| 489 | */ |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 490 | uint64_t(*pReadRawBBAddrFunc) (const uint8_t *) = NULL; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 491 | uint8_t pivot = 0; |
| 492 | if (is32bit) { |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 493 | pReadRawBBAddrFunc = &util_getUINT32; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 494 | pivot = 4; |
| 495 | } else { |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 496 | pReadRawBBAddrFunc = &util_getUINT64; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 497 | pivot = 8; |
| 498 | } |
| 499 | |
Robert Swiecki | 72d2bef | 2016-01-19 14:39:26 +0100 | [diff] [blame] | 500 | /* |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 501 | * Take advantage of data locality (next processed addr is very likely to belong |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 502 | * to same map) to avoid Trie node search for each read entry. |
| 503 | */ |
| 504 | node_t *curMap = NULL; |
| 505 | uint64_t prevIndex = 0; |
| 506 | |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 507 | /* Iterate over data buffer containing list of hit BB addresses */ |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 508 | while (pos < dataFileSz) { |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 509 | uint64_t bbAddr = pReadRawBBAddrFunc(dataBuf + pos); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 510 | pos += pivot; |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 511 | /* Don't bother for zero BB addr (inserted checks without hit) */ |
| 512 | if (bbAddr == 0x0) { |
| 513 | nZeroBBs++; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 514 | continue; |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 515 | } else { |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 516 | /* Find best hit based on start addr & verify range for errors */ |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 517 | uint64_t bestFit = sancov_interpSearch(startMapsIndex, mapsNum, bbAddr); |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 518 | if (bbAddr >= mapsBuf[bestFit].start && bbAddr < mapsBuf[bestFit].end) { |
| 519 | /* Increase exe/DSO total BB counter */ |
| 520 | mapsBuf[bestFit].bbCnt++; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 521 | |
| 522 | /* Update current Trie node if map changed */ |
| 523 | if (curMap == NULL || (prevIndex != bestFit)) { |
| 524 | prevIndex = bestFit; |
| 525 | |
| 526 | /* Interaction with global Trie should mutex wrap to avoid threads races */ |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 527 | { |
Robert Swiecki | 76ecd5e | 2016-03-16 14:57:03 +0100 | [diff] [blame] | 528 | MX_SCOPED_LOCK(&hfuzz->sanCov_mutex); |
Robert Swiecki | 5d6e734 | 2016-03-16 15:36:11 +0100 | [diff] [blame] | 529 | |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 530 | curMap = |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 531 | sancov_trieSearch(hfuzz->covMetadata->children, |
| 532 | mapsBuf[bestFit].mapName); |
Anestis Bechtsoudis | 1fd10c7 | 2016-01-07 12:38:45 +0200 | [diff] [blame] | 533 | if (curMap == NULL) { |
| 534 | LOG_E("Corrupted Trie - '%s' not found", mapsBuf[bestFit].mapName); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 535 | continue; |
| 536 | } |
| 537 | |
| 538 | /* Maintain bitmaps only for exec/DSOs with coverage enabled - allocate on first use */ |
| 539 | if (curMap->data.pBM == NULL) { |
Anestis Bechtsoudis | 1fd10c7 | 2016-01-07 12:38:45 +0200 | [diff] [blame] | 540 | LOG_D("Allocating bitmap for map '%s'", mapsBuf[bestFit].mapName); |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 541 | curMap->data.pBM = sancov_newBitmap(_HF_BITMAP_SIZE); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 542 | |
Robert Swiecki | 72d2bef | 2016-01-19 14:39:26 +0100 | [diff] [blame] | 543 | /* |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 544 | * If bitmap allocation failed, unset cached Trie node ptr |
| 545 | * to execute this selection branch again. |
| 546 | */ |
| 547 | if (curMap->data.pBM == NULL) { |
| 548 | curMap = NULL; |
Anestis Bechtsoudis | 1fd10c7 | 2016-01-07 12:38:45 +0200 | [diff] [blame] | 549 | continue; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | } |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 555 | /* If new relative BB addr update DSO's bitmap */ |
| 556 | uint32_t relAddr = (uint32_t) (bbAddr - mapsBuf[bestFit].base); |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 557 | if (!sancov_queryBitmap(curMap->data.pBM, relAddr)) { |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 558 | |
| 559 | /* Interaction with global Trie should mutex wrap to avoid threads races */ |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 560 | { |
Robert Swiecki | 76ecd5e | 2016-03-16 14:57:03 +0100 | [diff] [blame] | 561 | MX_SCOPED_LOCK(&hfuzz->sanCov_mutex); |
Robert Swiecki | 5d6e734 | 2016-03-16 15:36:11 +0100 | [diff] [blame] | 562 | |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 563 | sancov_setBitmap(curMap->data.pBM, relAddr); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 564 | } |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 565 | |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 566 | /* Also increase new BBs counter at worker's thread runtime data */ |
| 567 | mapsBuf[bestFit].newBBCnt++; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 568 | } |
| 569 | } else { |
Robert Swiecki | 72d2bef | 2016-01-19 14:39:26 +0100 | [diff] [blame] | 570 | /* |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 571 | * Normally this should never get executed. If hit, sanitizer |
| 572 | * coverage data collection come across some kind of bug. |
| 573 | */ |
Robert Swiecki | 37778e0 | 2016-03-15 15:45:28 +0100 | [diff] [blame] | 574 | LOG_E("Invalid BB addr (%#" PRIx64 ") at offset %" PRId64, bbAddr, (uint64_t) pos); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 575 | } |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 576 | } |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 577 | nBBs++; |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 578 | } |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 579 | |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 580 | /* Finally iterate over all instrumented maps to sum-up the number of newly met BB addresses */ |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 581 | for (uint64_t i = 0; i < mapsNum; i++) { |
Robert Swiecki | 142f941 | 2016-03-14 19:22:01 +0100 | [diff] [blame] | 582 | if (mapsBuf[i].bbCnt > 0) { |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 583 | fuzzer->sanCovCnts.newBBCnt += mapsBuf[i].newBBCnt; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 584 | } else { |
| 585 | noCovMapsNum++; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /* Successful parsing - update fuzzer worker's counters */ |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 590 | fuzzer->sanCovCnts.hitBBCnt = nBBs; |
| 591 | fuzzer->sanCovCnts.totalBBCnt = nBBs + nZeroBBs; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 592 | fuzzer->sanCovCnts.dsoCnt = mapsNum; |
| 593 | fuzzer->sanCovCnts.iDsoCnt = mapsNum - noCovMapsNum; /* Instrumented DSOs */ |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 594 | |
Robert Swiecki | 8656cbb | 2016-03-30 19:06:37 +0200 | [diff] [blame] | 595 | if (hfuzz->linux.pid == 0 && hfuzz->persistent == false) { |
Jagger | 1ebc6dc | 2016-03-12 01:39:09 +0100 | [diff] [blame] | 596 | unlink(covFile); |
| 597 | } |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 598 | return true; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 599 | } |
| 600 | |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 601 | static bool sancov_sanCovParse(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 602 | { |
| 603 | int dataFd = -1; |
| 604 | uint8_t *dataBuf = NULL; |
| 605 | off_t dataFileSz = 0, pos = 0; |
| 606 | bool is32bit = true; |
| 607 | char covFile[PATH_MAX] = { 0 }; |
| 608 | DIR *pSanCovDir = NULL; |
Jagger | 247c3b4 | 2016-03-21 23:24:05 +0100 | [diff] [blame] | 609 | pid_t targetPid = (hfuzz->linux.pid > 0) ? hfuzz->linux.pid : fuzzer->pid; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 610 | |
| 611 | snprintf(covFile, sizeof(covFile), "%s/%s/%s.%d.sancov", hfuzz->workDir, _HF_SANCOV_DIR, |
Anestis Bechtsoudis | a7c56ce | 2016-02-07 12:53:20 +0200 | [diff] [blame] | 612 | files_basename(hfuzz->cmdline[0]), targetPid); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 613 | if (!files_exists(covFile)) { |
| 614 | LOG_D("Target sancov file not found"); |
| 615 | return false; |
| 616 | } |
| 617 | |
Anestis Bechtsoudis | a7c56ce | 2016-02-07 12:53:20 +0200 | [diff] [blame] | 618 | /* Local cache file suffix to use for file search of target pid data */ |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 619 | char pidFSuffix[13] = { 0 }; |
Anestis Bechtsoudis | a7c56ce | 2016-02-07 12:53:20 +0200 | [diff] [blame] | 620 | snprintf(pidFSuffix, sizeof(pidFSuffix), "%d.sancov", targetPid); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 621 | |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 622 | /* Total BBs counter summarizes all DSOs */ |
| 623 | uint64_t nBBs = 0; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 624 | |
Anestis Bechtsoudis | a7c56ce | 2016-02-07 12:53:20 +0200 | [diff] [blame] | 625 | /* Iterate sancov dir for files generated against target pid */ |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 626 | snprintf(covFile, sizeof(covFile), "%s/%s", hfuzz->workDir, _HF_SANCOV_DIR); |
| 627 | pSanCovDir = opendir(covFile); |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 628 | if (pSanCovDir == NULL) { |
| 629 | PLOG_E("opendir('%s')", covFile); |
| 630 | return false; |
| 631 | } |
| 632 | DEFER(closedir(pSanCovDir)); |
| 633 | |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 634 | struct dirent *pDir = NULL; |
| 635 | while ((pDir = readdir(pSanCovDir)) != NULL) { |
Anestis Bechtsoudis | a7c56ce | 2016-02-07 12:53:20 +0200 | [diff] [blame] | 636 | /* Parse files with target's pid */ |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 637 | if (strstr(pDir->d_name, pidFSuffix)) { |
| 638 | snprintf(covFile, sizeof(covFile), "%s/%s/%s", hfuzz->workDir, _HF_SANCOV_DIR, |
| 639 | pDir->d_name); |
| 640 | dataBuf = files_mapFile(covFile, &dataFileSz, &dataFd, false); |
| 641 | if (dataBuf == NULL) { |
| 642 | LOG_E("Couldn't open and map '%s' in R/O mode", covFile); |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 643 | return false; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 644 | } |
Jagger | 799a4d4 | 2016-04-01 23:27:51 +0200 | [diff] [blame] | 645 | DEFER(munmap(dataBuf, dataFileSz); close(dataFd)); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 646 | |
| 647 | if (dataFileSz < 8) { |
| 648 | LOG_E("Coverage data file too short"); |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 649 | return false; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* Check magic values & derive PC length */ |
| 653 | uint64_t magic = util_getUINT64(dataBuf); |
| 654 | if (magic == kMagic32) { |
| 655 | is32bit = true; |
| 656 | } else if (magic == kMagic64) { |
| 657 | is32bit = false; |
| 658 | } else { |
| 659 | LOG_E("Invalid coverage data file"); |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 660 | return false; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 661 | } |
| 662 | pos += 8; |
| 663 | |
Robert Swiecki | 72d2bef | 2016-01-19 14:39:26 +0100 | [diff] [blame] | 664 | /* |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 665 | * Avoid cost of size checks inside raw data read loop by defining the read function |
| 666 | * & pivot size based on PC length. |
| 667 | */ |
| 668 | uint64_t(*pReadRawBBAddrFunc) (const uint8_t *) = NULL; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 669 | uint8_t pivot = 0; |
| 670 | if (is32bit) { |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 671 | pReadRawBBAddrFunc = &util_getUINT32; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 672 | pivot = 4; |
| 673 | } else { |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 674 | pReadRawBBAddrFunc = &util_getUINT64; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 675 | pivot = 8; |
| 676 | } |
| 677 | |
| 678 | while (pos < dataFileSz) { |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 679 | uint32_t bbAddr = pReadRawBBAddrFunc(dataBuf + pos); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 680 | pos += pivot; |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 681 | if (bbAddr == 0x0) { |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 682 | continue; |
| 683 | } |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 684 | nBBs++; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /* Successful parsing - update fuzzer worker counters */ |
Anestis Bechtsoudis | 56e360f | 2016-01-11 14:29:17 +0200 | [diff] [blame] | 690 | fuzzer->sanCovCnts.hitBBCnt = nBBs; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 691 | |
Robert Swiecki | 8656cbb | 2016-03-30 19:06:37 +0200 | [diff] [blame] | 692 | if (hfuzz->linux.pid == 0 && hfuzz->persistent == false) { |
Jagger | 1ebc6dc | 2016-03-12 01:39:09 +0100 | [diff] [blame] | 693 | unlink(covFile); |
| 694 | } |
Jagger | f26b1b6 | 2016-03-19 01:59:30 +0100 | [diff] [blame] | 695 | return true; |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 696 | } |
| 697 | |
Robert Swiecki | 72d2bef | 2016-01-19 14:39:26 +0100 | [diff] [blame] | 698 | /* |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 699 | * Sanitizer coverage data are stored in FS can be parsed via two methods: |
Robert Swiecki | 72d2bef | 2016-01-19 14:39:26 +0100 | [diff] [blame] | 700 | * raw unpack & separate bin/DSO sancov file. Separate bin/DSO sancov file |
| 701 | * method is usually avoided since coverage data are lost if sanitizer unhandled |
| 702 | * signal. Additionally, the FS I/O overhead is bigger compared to raw unpack |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 703 | * method which uses runtime data structures. |
Robert Swiecki | 72d2bef | 2016-01-19 14:39:26 +0100 | [diff] [blame] | 704 | * |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 705 | * Enabled methods are controlled from sanitizer flags in arch.c |
| 706 | */ |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 707 | void sancov_Analyze(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 708 | { |
| 709 | if (!hfuzz->useSanCov) { |
| 710 | return; |
| 711 | } |
| 712 | |
Robert Swiecki | 72d2bef | 2016-01-19 14:39:26 +0100 | [diff] [blame] | 713 | /* |
Anestis Bechtsoudis | 97633cc | 2016-01-13 16:25:57 +0200 | [diff] [blame] | 714 | * For now supported methods are implemented in fail-over nature. This will |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 715 | * change in the future when best method is concluded. |
| 716 | */ |
Jagger | 3db1d95 | 2016-03-10 02:02:46 +0100 | [diff] [blame] | 717 | if (sancov_sanCovParseRaw(hfuzz, fuzzer) == false) { |
| 718 | sancov_sanCovParse(hfuzz, fuzzer); |
Anestis Bechtsoudis | a16f70f | 2016-01-03 13:03:21 +0200 | [diff] [blame] | 719 | } |
Anestis Bechtsoudis | d76c3b8 | 2015-12-26 17:35:25 +0200 | [diff] [blame] | 720 | } |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 721 | |
| 722 | bool sancov_Init(honggfuzz_t * hfuzz) |
| 723 | { |
Jagger | b0168a5 | 2016-03-14 21:35:28 +0100 | [diff] [blame] | 724 | if (hfuzz->useSanCov == false) { |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 725 | return true; |
| 726 | } |
| 727 | |
Jagger | b0168a5 | 2016-03-14 21:35:28 +0100 | [diff] [blame] | 728 | sancov_trieCreate(&hfuzz->covMetadata); |
| 729 | |
Jagger | 247c3b4 | 2016-03-21 23:24:05 +0100 | [diff] [blame] | 730 | if (hfuzz->linux.pid > 0) { |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 731 | return true; |
| 732 | } |
| 733 | |
| 734 | char sanCovOutDir[PATH_MAX] = { 0 }; |
| 735 | snprintf(sanCovOutDir, sizeof(sanCovOutDir), "%s/%s", hfuzz->workDir, _HF_SANCOV_DIR); |
| 736 | if (!files_exists(sanCovOutDir)) { |
| 737 | if (mkdir(sanCovOutDir, S_IRWXU | S_IXGRP | S_IXOTH) != 0) { |
| 738 | PLOG_E("mkdir() '%s' failed", sanCovOutDir); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | /* Set sanitizer flags once to avoid performance overhead per worker spawn */ |
| 743 | size_t flagsSz = 0; |
| 744 | size_t bufSz = sizeof(kASAN_OPTS) + (2 * PATH_MAX); // Larger constant + 2 dynamic paths |
Jagger | 679c205 | 2016-03-18 22:53:53 +0100 | [diff] [blame] | 745 | char *san_opts = util_Calloc(bufSz); |
Jagger | 28aebbd | 2016-03-10 22:58:51 +0100 | [diff] [blame] | 746 | DEFER(free(san_opts)); |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 747 | |
| 748 | /* AddressSanitizer (ASan) */ |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 749 | if (hfuzz->useSanCov) { |
| 750 | #if !_HF_MONITOR_SIGABRT |
| 751 | /* Write reports in FS only if abort_on_error is disabled */ |
| 752 | snprintf(san_opts, bufSz, "%s:%s:%s%s/%s:%s%s/%s", kASAN_OPTS, kSAN_COV_OPTS, |
| 753 | kSANCOVDIR, hfuzz->workDir, _HF_SANCOV_DIR, kSANLOGDIR, hfuzz->workDir, |
| 754 | kLOGPREFIX); |
| 755 | #else |
| 756 | snprintf(san_opts, bufSz, "%s:%s:%s%s/%s", kASAN_OPTS, kSAN_COV_OPTS, |
| 757 | kSANCOVDIR, hfuzz->workDir, _HF_SANCOV_DIR); |
| 758 | #endif |
| 759 | } else { |
| 760 | snprintf(san_opts, bufSz, "%s:%s%s/%s", kASAN_OPTS, kSANLOGDIR, hfuzz->workDir, kLOGPREFIX); |
| 761 | } |
| 762 | |
| 763 | flagsSz = strlen(san_opts) + 1; |
Jagger | 679c205 | 2016-03-18 22:53:53 +0100 | [diff] [blame] | 764 | hfuzz->sanOpts.asanOpts = util_Calloc(flagsSz); |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 765 | memcpy(hfuzz->sanOpts.asanOpts, san_opts, flagsSz); |
| 766 | LOG_D("ASAN_OPTIONS=%s", hfuzz->sanOpts.asanOpts); |
| 767 | |
| 768 | /* Undefined Behavior (UBSan) */ |
| 769 | memset(san_opts, 0, bufSz); |
| 770 | if (hfuzz->useSanCov) { |
| 771 | #if !_HF_MONITOR_SIGABRT |
| 772 | /* Write reports in FS only if abort_on_error is disabled */ |
| 773 | snprintf(san_opts, bufSz, "%s:%s:%s%s/%s:%s%s/%s", kUBSAN_OPTS, kSAN_COV_OPTS, |
| 774 | kSANCOVDIR, hfuzz->workDir, _HF_SANCOV_DIR, kSANLOGDIR, hfuzz->workDir, |
| 775 | kLOGPREFIX); |
| 776 | #else |
| 777 | snprintf(san_opts, bufSz, "%s:%s:%s%s/%s", kUBSAN_OPTS, kSAN_COV_OPTS, |
| 778 | kSANCOVDIR, hfuzz->workDir, _HF_SANCOV_DIR); |
| 779 | #endif |
| 780 | } else { |
| 781 | snprintf(san_opts, bufSz, "%s:%s%s/%s", kUBSAN_OPTS, kSANLOGDIR, hfuzz->workDir, |
| 782 | kLOGPREFIX); |
| 783 | } |
| 784 | |
| 785 | flagsSz = strlen(san_opts) + 1; |
Jagger | 679c205 | 2016-03-18 22:53:53 +0100 | [diff] [blame] | 786 | hfuzz->sanOpts.ubsanOpts = util_Calloc(flagsSz); |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 787 | memcpy(hfuzz->sanOpts.ubsanOpts, san_opts, flagsSz); |
| 788 | LOG_D("UBSAN_OPTIONS=%s", hfuzz->sanOpts.ubsanOpts); |
| 789 | |
| 790 | /* MemorySanitizer (MSan) */ |
| 791 | memset(san_opts, 0, bufSz); |
| 792 | const char *msan_reports_flag = "report_umrs=0"; |
| 793 | if (hfuzz->msanReportUMRS) { |
| 794 | msan_reports_flag = "report_umrs=1"; |
| 795 | } |
| 796 | |
| 797 | if (hfuzz->useSanCov) { |
| 798 | #if !_HF_MONITOR_SIGABRT |
| 799 | /* Write reports in FS only if abort_on_error is disabled */ |
| 800 | snprintf(san_opts, bufSz, "%s:%s:%s:%s%s/%s:%s%s/%s", kMSAN_OPTS, msan_reports_flag, |
| 801 | kSAN_COV_OPTS, kSANCOVDIR, hfuzz->workDir, _HF_SANCOV_DIR, kSANLOGDIR, |
| 802 | hfuzz->workDir, kLOGPREFIX); |
| 803 | #else |
| 804 | snprintf(san_opts, bufSz, "%s:%s:%s:%s%s/%s", kMSAN_OPTS, msan_reports_flag, |
| 805 | kSAN_COV_OPTS, kSANCOVDIR, hfuzz->workDir, _HF_SANCOV_DIR); |
| 806 | #endif |
| 807 | } else { |
| 808 | snprintf(san_opts, bufSz, "%s:%s:%s%s/%s", kMSAN_OPTS, msan_reports_flag, kSANLOGDIR, |
| 809 | hfuzz->workDir, kLOGPREFIX); |
| 810 | } |
| 811 | |
| 812 | flagsSz = strlen(san_opts) + 1; |
Jagger | 679c205 | 2016-03-18 22:53:53 +0100 | [diff] [blame] | 813 | hfuzz->sanOpts.msanOpts = util_Calloc(flagsSz); |
Jagger | 0026560 | 2016-03-10 02:36:27 +0100 | [diff] [blame] | 814 | memcpy(hfuzz->sanOpts.msanOpts, san_opts, flagsSz); |
| 815 | LOG_D("MSAN_OPTIONS=%s", hfuzz->sanOpts.msanOpts); |
| 816 | |
| 817 | return true; |
| 818 | } |
Jagger | 2b97dc9 | 2016-03-10 04:28:18 +0100 | [diff] [blame] | 819 | |
| 820 | bool sancov_prepareExecve(honggfuzz_t * hfuzz) |
| 821 | { |
| 822 | /* Address Sanitizer (ASan) */ |
| 823 | if (hfuzz->sanOpts.asanOpts) { |
| 824 | if (setenv("ASAN_OPTIONS", hfuzz->sanOpts.asanOpts, 1) == -1) { |
| 825 | PLOG_E("setenv(ASAN_OPTIONS) failed"); |
| 826 | return false; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | /* Memory Sanitizer (MSan) */ |
| 831 | if (hfuzz->sanOpts.msanOpts) { |
| 832 | if (setenv("MSAN_OPTIONS", hfuzz->sanOpts.msanOpts, 1) == -1) { |
| 833 | PLOG_E("setenv(MSAN_OPTIONS) failed"); |
| 834 | return false; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | /* Undefined Behavior Sanitizer (UBSan) */ |
| 839 | if (hfuzz->sanOpts.ubsanOpts) { |
| 840 | if (setenv("UBSAN_OPTIONS", hfuzz->sanOpts.ubsanOpts, 1) == -1) { |
| 841 | PLOG_E("setenv(UBSAN_OPTIONS) failed"); |
| 842 | return false; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | return true; |
| 847 | } |