blob: 80744f0eb638f02bf22871311dc45eb55a74137f [file] [log] [blame]
robert.swiecki3bb518c2010-10-14 00:48:24 +00001/*
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +00002 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +00003 * honggfuzz - core structures and macros
4 * -----------------------------------------
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +00005 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +00006 * Author: Robert Swiecki <swiecki@google.com>
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +00007 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +00008 * Copyright 2010-2015 by Google Inc. All Rights Reserved.
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +00009 *
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.com772b33d2015-02-14 20:35:00 +000012 * a copy of the License at
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000013 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000014 * http://www.apache.org/licenses/LICENSE-2.0
robert.swiecki@gmail.com3b630b42015-02-16 10:53:53 +000015 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000016 * 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.com3b630b42015-02-16 10:53:53 +000021 *
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +000022 */
robert.swiecki3bb518c2010-10-14 00:48:24 +000023
24#ifndef _COMMON_H_
25#define _COMMON_H_
26
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000027#include <limits.h>
robert.swiecki@gmail.com41d8e052015-02-19 01:10:41 +000028#include <pthread.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000029#include <stdbool.h>
30#include <stdint.h>
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000031#include <sys/param.h>
32#include <sys/types.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000033
Anestis Bechtsoudis4d9e8c92016-01-31 16:18:48 +020034#ifdef __clang__
35#include <stdatomic.h>
36#endif
37
Anestis Bechtsoudis2ff92d12015-12-20 15:33:20 +020038#ifndef UNUSED
39#define UNUSED __attribute__((unused))
40#endif
41
robert.swiecki3bb518c2010-10-14 00:48:24 +000042#define PROG_NAME "honggfuzz"
robert.swiecki@gmail.coma51662b2015-03-01 19:12:41 +000043#define PROG_VERSION "0.6rc"
robert.swiecki32b69c92015-02-26 14:56:36 +000044#define PROG_AUTHORS "Robert Swiecki <swiecki@google.com> et al.,\nCopyright 2010-2015 by Google Inc. All Rights Reserved."
robert.swiecki3bb518c2010-10-14 00:48:24 +000045
robert.swiecki@gmail.com64dc2a02015-02-17 22:21:30 +000046/* Name of the template which will be replaced with the proper name of the file */
robert.swiecki@gmail.coma0d87142015-02-14 13:11:18 +000047#define _HF_FILE_PLACEHOLDER "___FILE___"
robert.swiecki@gmail.com64dc2a02015-02-17 22:21:30 +000048
49/* Default name of the report created with some architectures */
robert.swiecki@gmail.come7190b92015-02-14 23:05:42 +000050#define _HF_REPORT_FILE "HONGGFUZZ.REPORT.TXT"
robert.swiecki3bb518c2010-10-14 00:48:24 +000051
robert.swiecki@gmail.com64dc2a02015-02-17 22:21:30 +000052/* Default stack-size of created threads. Must be bigger then _HF_DYNAMIC_FILE_MAX_SZ */
robert.swiecki@gmail.com23b3a2f2015-03-01 03:40:12 +000053#define _HF_PTHREAD_STACKSIZE (1024 * 1024 * 8) /* 8MB */
robert.swiecki@gmail.com01b6dd42015-02-16 18:11:28 +000054
robert.swiecki@gmail.come7680522015-02-22 22:22:37 +000055/* Align to the upper-page boundary */
robert.swiecki87f7c7e2015-02-26 14:11:57 +000056#define _HF_PAGE_ALIGN_UP(x) (((size_t)x + (size_t)getpagesize() - (size_t)1) & ~((size_t)getpagesize() - (size_t)1))
robert.swiecki@gmail.come7680522015-02-22 22:22:37 +000057
Anestis Bechtsoudiscfc39fb2015-08-06 10:31:36 +030058/* String buffer size for function names in stack traces produced from libunwind */
Jaggerd628a702015-08-23 12:59:37 +020059#define _HF_FUNC_NAME_SZ 256 // Should be alright for mangled C++ procs too
Anestis Bechtsoudiscfc39fb2015-08-06 10:31:36 +030060
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +030061/* Number of crash verifier iterations before tag crash as stable */
62#define _HF_VERIFIER_ITER 5
63
Anestis Bechtsoudis60ea5e72015-11-24 19:10:06 +020064/* Constant prefix used for single frame crashes stackhash masking */
Anestis Bechtsoudisc06f8b32015-12-26 14:48:05 +020065#define _HF_SINGLE_FRAME_MASK 0xBADBAD0000000000
66
67/* Size (in bytes) for report data to be stored in stack before written to file */
68#define _HF_REPORT_SIZE 8192
Anestis Bechtsoudis0ef90002015-11-22 21:17:06 +020069
Robert Swiecki72d2bef2016-01-19 14:39:26 +010070/*
Anestis Bechtsoudis02b99be2015-12-27 11:53:01 +020071 * Maximum number of iterations to keep same base seed file for dynamic preparation.
72 * Maintained iterations counters is set to zero if unique crash is detected or
73 * zero-set two MSB using following mask if crash is detected (might not be unique).
74 */
75#define _HF_MAX_DYNFILE_ITER 0x2000UL
76#define _HF_DYNFILE_SUB_MASK 0xFFFUL // Zero-set two MSB
77
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +020078/* Bitmap size */
Robert Swiecki94335d62016-01-26 17:06:20 +010079#define _HF_BITMAP_SIZE 0x2AFFFFF
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +020080
81/* Directory in workspace to store sanitizer coverage data */
82#define _HF_SANCOV_DIR "HF_SANCOV"
83
Anestis Bechtsoudis8e634c32016-01-13 13:07:26 +020084#if defined(__ANDROID__)
85#define _HF_MONITOR_SIGABRT 0
86#else
87#define _HF_MONITOR_SIGABRT 1
88#endif
89
robert.swiecki@gmail.comcac22fd2015-02-19 14:03:28 +000090typedef enum {
robert.swiecki@gmail.com81e26dc2015-03-03 04:26:04 +000091 _HF_DYNFILE_NONE = 0x0,
92 _HF_DYNFILE_INSTR_COUNT = 0x1,
93 _HF_DYNFILE_BRANCH_COUNT = 0x2,
Jagger3abc5602016-02-04 00:53:43 +010094 _HF_DYNFILE_BTS_BLOCK = 0x8,
95 _HF_DYNFILE_BTS_EDGE = 0x10,
Jagger39bd2b02016-02-04 01:16:15 +010096 _HF_DYNFILE_IPT_BLOCK = 0x20,
97 _HF_DYNFILE_IPT_EDGE = 0x40,
98 _HF_DYNFILE_CUSTOM = 0x80,
robert.swiecki@gmail.comcac22fd2015-02-19 14:03:28 +000099} dynFileMethod_t;
100
robert.swiecki3bb518c2010-10-14 00:48:24 +0000101typedef struct {
Jaggerb409ee12015-09-09 02:02:32 +0200102 uint64_t cpuInstrCnt;
103 uint64_t cpuBranchCnt;
Jagger39bd2b02016-02-04 01:16:15 +0100104 uint64_t cpuBtsBlockCnt;
105 uint64_t cpuBtsEdgeCnt;
106 uint64_t cpuIptBlockCnt;
107 uint64_t cpuIptEdgeCnt;
Jaggerb409ee12015-09-09 02:02:32 +0200108 uint64_t customCnt;
109} hwcnt_t;
110
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200111/* Sanitizer coverage specific data structures */
Jaggerb409ee12015-09-09 02:02:32 +0200112typedef struct {
Anestis Bechtsoudis56e360f2016-01-11 14:29:17 +0200113 uint64_t hitBBCnt;
114 uint64_t totalBBCnt;
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200115 uint64_t dsoCnt;
116 uint64_t iDsoCnt;
Anestis Bechtsoudis56e360f2016-01-11 14:29:17 +0200117 uint64_t newBBCnt;
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200118 uint64_t crashesCnt;
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200119} sancovcnt_t;
120
121typedef struct {
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200122 uint32_t capacity;
123 uint32_t *pChunks;
124 uint32_t nChunks;
125} bitmap_t;
126
127/* Memory map struct */
128typedef struct __attribute__ ((packed)) {
129 uint64_t start; // region start addr
130 uint64_t end; // region end addr
131 uint64_t base; // region base addr
132 char mapName[NAME_MAX]; // bin/DSO name
Anestis Bechtsoudis56e360f2016-01-11 14:29:17 +0200133 uint64_t bbCnt;
134 uint64_t newBBCnt;
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200135} memMap_t;
136
137/* Trie node data struct */
138typedef struct __attribute__ ((packed)) {
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200139 bitmap_t *pBM;
140} trieData_t;
141
142/* Trie node struct */
143typedef struct __attribute__ ((packed)) node {
144 char key;
145 trieData_t data;
146 struct node *next;
147 struct node *prev;
148 struct node *children;
149 struct node *parent;
150} node_t;
Anestis Bechtsoudisb78cf602016-01-07 13:10:50 +0200151
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200152/* EOF Sanitizer coverage specific data structures */
153
154typedef struct {
Anestis Bechtsoudis61b5ab12016-01-08 16:07:02 +0200155 char *asanOpts;
156 char *msanOpts;
157 char *ubsanOpts;
158} sanOpts_t;
159
160typedef struct {
robert.swiecki3bb518c2010-10-14 00:48:24 +0000161 char **cmdline;
Robert Swiecki72d2bef2016-01-19 14:39:26 +0100162 char cmdline_txt[PATH_MAX];
robert.swiecki3bb518c2010-10-14 00:48:24 +0000163 char *inputFile;
164 bool nullifyStdio;
165 bool fuzzStdin;
166 bool saveUnique;
Jagger0764ad72015-09-06 01:11:08 +0200167 bool useScreen;
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300168 bool useVerifier;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000169 char *fileExtn;
Anestis Bechtsoudisd9680532015-09-06 17:37:05 +0300170 char *workDir;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000171 double flipRate;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000172 char *externalCommand;
robert.swiecki@gmail.com4f1124f2015-04-21 17:12:22 +0000173 const char *dictionaryFile;
Anestis Bechtsoudis70f2fbd2016-01-14 13:15:19 +0200174 char **dictionary;
Anestis Bechtsoudisd59af692015-09-21 15:15:05 +0300175 const char *blacklistFile;
176 uint64_t *blacklist;
177 size_t blacklistCnt;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000178 long tmOut;
Jaggerea39a8f2015-09-05 00:57:22 +0200179 size_t dictionaryCnt;
180 size_t mutationsMax;
Jaggerea39a8f2015-09-05 00:57:22 +0200181 size_t threadsMax;
182 size_t threadsFinished;
robert.swiecki@gmail.com4da86bf2015-02-22 14:24:58 +0000183 size_t maxFileSz;
robert.swiecki@gmail.come7190b92015-02-14 23:05:42 +0000184 char *reportFile;
Robert Swieckic578d642015-09-08 16:13:36 +0200185 uint64_t asLimit;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000186 char **files;
Anestis Bechtsoudis46ea10e2015-11-07 18:16:25 +0200187 size_t fileCnt;
188 size_t lastCheckedFileIndex;
robert.swiecki@gmail.com15eca6f2015-03-04 03:31:36 +0000189 pid_t pid;
190 char *envs[128];
robert.swiecki@gmail.com41d8e052015-02-19 01:10:41 +0000191
Jagger630aa7f2015-09-06 02:53:51 +0200192 time_t timeStart;
Jagger0764ad72015-09-06 01:11:08 +0200193 size_t mutationsCnt;
194 size_t crashesCnt;
Anestis Bechtsoudisd7e8ed22015-09-10 18:29:34 +0300195 size_t uniqueCrashesCnt;
Anestis Bechtsoudis79b799e2015-11-01 00:02:25 +0200196 size_t verifiedCrashesCnt;
Anestis Bechtsoudisd59af692015-09-21 15:15:05 +0300197 size_t blCrashesCnt;
Jagger4b5281e2015-09-06 02:35:37 +0200198 size_t timeoutedCnt;
Jagger0764ad72015-09-06 01:11:08 +0200199
robert.swiecki@gmail.comcac22fd2015-02-19 14:03:28 +0000200 /* For the linux/ code */
robert.swiecki@gmail.comcd74cfc2015-02-19 16:37:49 +0000201 uint8_t *dynamicFileBest;
robert.swiecki@gmail.com6d6f7562015-02-17 22:18:51 +0000202 size_t dynamicFileBestSz;
robert.swiecki@gmail.comcac22fd2015-02-19 14:03:28 +0000203 dynFileMethod_t dynFileMethod;
Jaggerb409ee12015-09-09 02:02:32 +0200204 hwcnt_t hwCnts;
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200205 sancovcnt_t sanCovCnts;
robert.swiecki@gmail.com684f60c2015-03-01 17:39:18 +0000206 uint64_t dynamicCutOffAddr;
robert.swiecki@gmail.com41d8e052015-02-19 01:10:41 +0000207 pthread_mutex_t dynamicFile_mutex;
Jaggerdbc4a152015-09-08 01:26:58 +0200208 bool disableRandomization;
209 bool msanReportUMRS;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200210 void *ignoreAddr;
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200211 bool useSanCov;
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200212 node_t *covMetadata;
Anestis Bechtsoudis1fd10c72016-01-07 12:38:45 +0200213 bool clearCovMetadata;
Anestis Bechtsoudis02b99be2015-12-27 11:53:01 +0200214 size_t dynFileIterExpire;
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200215 pthread_mutex_t sanCov_mutex;
Anestis Bechtsoudisac054802016-01-07 23:48:06 +0200216 pthread_mutex_t workersBlock_mutex;
Anestis Bechtsoudis61b5ab12016-01-08 16:07:02 +0200217 sanOpts_t sanOpts;
Anestis Bechtsoudisa1b0a652016-01-08 16:14:55 +0200218 size_t numMajorFrames;
Anestis Bechtsoudisa1f8a032016-01-14 16:45:30 +0200219 bool isDynFileLocked;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000220} honggfuzz_t;
221
robert.swiecki@gmail.com882900b2015-02-11 13:56:22 +0000222typedef struct fuzzer_t {
223 pid_t pid;
robert.swiecki@gmail.com3213a112015-03-12 01:42:02 +0000224 int64_t timeStartedMillis;
robert.swiecki@gmail.com882900b2015-02-11 13:56:22 +0000225 char origFileName[PATH_MAX];
226 char fileName[PATH_MAX];
Anestis Bechtsoudis5c86ebc2015-09-27 18:06:43 +0300227 char crashFileName[PATH_MAX];
robert.swiecki@gmail.com882900b2015-02-11 13:56:22 +0000228 uint64_t pc;
229 uint64_t backtrace;
230 uint64_t access;
231 int exception;
Anestis Bechtsoudisc06f8b32015-12-26 14:48:05 +0200232 char report[_HF_REPORT_SIZE];
Anestis Bechtsoudis6b9e83d2015-10-02 11:10:50 -0700233 bool mainWorker;
robert.swiecki@gmail.comd4dd4df2015-02-18 00:50:12 +0000234
235 /* For linux/ code */
robert.swiecki@gmail.comcd74cfc2015-02-19 16:37:49 +0000236 uint8_t *dynamicFile;
Jaggerb409ee12015-09-09 02:02:32 +0200237 hwcnt_t hwCnts;
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200238 sancovcnt_t sanCovCnts;
robert.swiecki@gmail.com4da86bf2015-02-22 14:24:58 +0000239 size_t dynamicFileSz;
robert.swiecki@gmail.com882900b2015-02-11 13:56:22 +0000240} fuzzer_t;
robert.swiecki3bb518c2010-10-14 00:48:24 +0000241
Anestis Bechtsoudiscfc39fb2015-08-06 10:31:36 +0300242#define _HF_MAX_FUNCS 80
robert.swiecki@gmail.coma0d87142015-02-14 13:11:18 +0000243typedef struct {
244 void *pc;
Anestis Bechtsoudiscfc39fb2015-08-06 10:31:36 +0300245 char func[_HF_FUNC_NAME_SZ];
246 size_t line;
robert.swiecki@gmail.coma0d87142015-02-14 13:11:18 +0000247} funcs_t;
248
robert.swiecki@gmail.com4da86bf2015-02-22 14:24:58 +0000249#define ARRAYSIZE(x) (sizeof(x) / sizeof(*x))
250
Jaggercc42cd62016-02-06 17:43:39 +0100251#define rmb() __asm__ __volatile__("":::"memory")
252#define wmb() __sync_synchronize()
253
robert.swiecki3bb518c2010-10-14 00:48:24 +0000254#endif