Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 1 | //===- FuzzerTraceState.cpp - Trace-based fuzzer mutator ------------------===// |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 9 | // This file implements a mutation algorithm based on instruction traces and |
| 10 | // on taint analysis feedback from DFSan. |
| 11 | // |
| 12 | // Instruction traces are special hooks inserted by the compiler around |
| 13 | // interesting instructions. Currently supported traces: |
| 14 | // * __sanitizer_cov_trace_cmp -- inserted before every ICMP instruction, |
| 15 | // receives the type, size and arguments of ICMP. |
| 16 | // |
| 17 | // Every time a traced event is intercepted we analyse the data involved |
| 18 | // in the event and suggest a mutation for future executions. |
| 19 | // For example if 4 bytes of data that derive from input bytes {4,5,6,7} |
| 20 | // are compared with a constant 12345, |
| 21 | // we try to insert 12345, 12344, 12346 into bytes |
| 22 | // {4,5,6,7} of the next fuzzed inputs. |
| 23 | // |
| 24 | // The fuzzer can work only with the traces, or with both traces and DFSan. |
| 25 | // |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 26 | // DataFlowSanitizer (DFSan) is a tool for |
| 27 | // generalised dynamic data flow (taint) analysis: |
| 28 | // http://clang.llvm.org/docs/DataFlowSanitizer.html . |
| 29 | // |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 30 | // The approach with DFSan-based fuzzing has some similarity to |
| 31 | // "Taint-based Directed Whitebox Fuzzing" |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 32 | // by Vijay Ganesh & Tim Leek & Martin Rinard: |
| 33 | // http://dspace.mit.edu/openaccess-disseminate/1721.1/59320, |
| 34 | // but it uses a full blown LLVM IR taint analysis and separate instrumentation |
| 35 | // to analyze all of the "attack points" at once. |
| 36 | // |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 37 | // Workflow with DFSan: |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 38 | // * lib/Fuzzer/Fuzzer*.cpp is compiled w/o any instrumentation. |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 39 | // * The code under test is compiled with DFSan *and* with instruction traces. |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 40 | // * Every call to HOOK(a,b) is replaced by DFSan with |
| 41 | // __dfsw_HOOK(a, b, label(a), label(b)) so that __dfsw_HOOK |
| 42 | // gets all the taint labels for the arguments. |
| 43 | // * At the Fuzzer startup we assign a unique DFSan label |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 44 | // to every byte of the input string (Fuzzer::CurrentUnitData) so that |
| 45 | // for any chunk of data we know which input bytes it has derived from. |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 46 | // * The __dfsw_* functions (implemented in this file) record the |
| 47 | // parameters (i.e. the application data and the corresponding taint labels) |
| 48 | // in a global state. |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 49 | // |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 50 | // Parts of this code will not function when DFSan is not linked in. |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 51 | // Instead of using ifdefs and thus requiring a separate build of lib/Fuzzer |
| 52 | // we redeclare the dfsan_* interface functions as weak and check if they |
| 53 | // are nullptr before calling. |
| 54 | // If this approach proves to be useful we may add attribute(weak) to the |
| 55 | // dfsan declarations in dfsan_interface.h |
| 56 | // |
| 57 | // This module is in the "proof of concept" stage. |
| 58 | // It is capable of solving only the simplest puzzles |
| 59 | // like test/dfsan/DFSanSimpleCmpTest.cpp. |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 62 | /* Example of manual usage (-fsanitize=dataflow is optional): |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 63 | ( |
| 64 | cd $LLVM/lib/Fuzzer/ |
| 65 | clang -fPIC -c -g -O2 -std=c++11 Fuzzer*.cpp |
Alexey Samsonov | 21a3381 | 2015-05-07 23:33:24 +0000 | [diff] [blame] | 66 | clang++ -O0 -std=c++11 -fsanitize-coverage=edge,trace-cmp \ |
Kostya Serebryany | 3befe94 | 2015-05-06 22:47:24 +0000 | [diff] [blame] | 67 | -fsanitize=dataflow \ |
Kostya Serebryany | 65f5086 | 2015-09-10 18:48:38 +0000 | [diff] [blame] | 68 | test/SimpleCmpTest.cpp Fuzzer*.o |
| 69 | ./a.out -use_traces=1 |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 70 | ) |
| 71 | */ |
| 72 | |
Kostya Serebryany | 65f5086 | 2015-09-10 18:48:38 +0000 | [diff] [blame] | 73 | #include "FuzzerDFSan.h" |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 74 | #include "FuzzerInternal.h" |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 75 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 76 | #include <algorithm> |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 77 | #include <cstring> |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame] | 78 | #include <thread> |
Kostya Serebryany | 859e86d | 2016-01-12 02:08:37 +0000 | [diff] [blame] | 79 | #include <map> |
Kostya Serebryany | c135b55 | 2016-07-15 23:27:19 +0000 | [diff] [blame] | 80 | #include <set> |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 81 | |
Kostya Serebryany | 65f5086 | 2015-09-10 18:48:38 +0000 | [diff] [blame] | 82 | #if !LLVM_FUZZER_SUPPORTS_DFSAN |
| 83 | // Stubs for dfsan for platforms where dfsan does not exist and weak |
| 84 | // functions don't work. |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 85 | extern "C" { |
Kostya Serebryany | 65f5086 | 2015-09-10 18:48:38 +0000 | [diff] [blame] | 86 | dfsan_label dfsan_create_label(const char *desc, void *userdata) { return 0; } |
| 87 | void dfsan_set_label(dfsan_label label, void *addr, size_t size) {} |
| 88 | void dfsan_add_label(dfsan_label label, void *addr, size_t size) {} |
| 89 | const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label) { |
| 90 | return nullptr; |
| 91 | } |
| 92 | dfsan_label dfsan_read_label(const void *addr, size_t size) { return 0; } |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 93 | } // extern "C" |
Kostya Serebryany | 65f5086 | 2015-09-10 18:48:38 +0000 | [diff] [blame] | 94 | #endif // !LLVM_FUZZER_SUPPORTS_DFSAN |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 95 | |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 96 | namespace fuzzer { |
| 97 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 98 | // These values are copied from include/llvm/IR/InstrTypes.h. |
| 99 | // We do not include the LLVM headers here to remain independent. |
| 100 | // If these values ever change, an assertion in ComputeCmp will fail. |
| 101 | enum Predicate { |
| 102 | ICMP_EQ = 32, ///< equal |
| 103 | ICMP_NE = 33, ///< not equal |
| 104 | ICMP_UGT = 34, ///< unsigned greater than |
| 105 | ICMP_UGE = 35, ///< unsigned greater or equal |
| 106 | ICMP_ULT = 36, ///< unsigned less than |
| 107 | ICMP_ULE = 37, ///< unsigned less or equal |
| 108 | ICMP_SGT = 38, ///< signed greater than |
| 109 | ICMP_SGE = 39, ///< signed greater or equal |
| 110 | ICMP_SLT = 40, ///< signed less than |
| 111 | ICMP_SLE = 41, ///< signed less or equal |
| 112 | }; |
| 113 | |
| 114 | template <class U, class S> |
| 115 | bool ComputeCmp(size_t CmpType, U Arg1, U Arg2) { |
| 116 | switch(CmpType) { |
| 117 | case ICMP_EQ : return Arg1 == Arg2; |
| 118 | case ICMP_NE : return Arg1 != Arg2; |
| 119 | case ICMP_UGT: return Arg1 > Arg2; |
| 120 | case ICMP_UGE: return Arg1 >= Arg2; |
| 121 | case ICMP_ULT: return Arg1 < Arg2; |
| 122 | case ICMP_ULE: return Arg1 <= Arg2; |
| 123 | case ICMP_SGT: return (S)Arg1 > (S)Arg2; |
| 124 | case ICMP_SGE: return (S)Arg1 >= (S)Arg2; |
| 125 | case ICMP_SLT: return (S)Arg1 < (S)Arg2; |
| 126 | case ICMP_SLE: return (S)Arg1 <= (S)Arg2; |
| 127 | default: assert(0 && "unsupported CmpType"); |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | static bool ComputeCmp(size_t CmpSize, size_t CmpType, uint64_t Arg1, |
| 133 | uint64_t Arg2) { |
| 134 | if (CmpSize == 8) return ComputeCmp<uint64_t, int64_t>(CmpType, Arg1, Arg2); |
| 135 | if (CmpSize == 4) return ComputeCmp<uint32_t, int32_t>(CmpType, Arg1, Arg2); |
| 136 | if (CmpSize == 2) return ComputeCmp<uint16_t, int16_t>(CmpType, Arg1, Arg2); |
| 137 | if (CmpSize == 1) return ComputeCmp<uint8_t, int8_t>(CmpType, Arg1, Arg2); |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 138 | // Other size, == |
| 139 | if (CmpType == ICMP_EQ) return Arg1 == Arg2; |
Kostya Serebryany | 8ce7424 | 2015-08-01 01:42:51 +0000 | [diff] [blame] | 140 | // assert(0 && "unsupported cmp and type size combination"); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
| 144 | // As a simplification we use the range of input bytes instead of a set of input |
| 145 | // bytes. |
| 146 | struct LabelRange { |
| 147 | uint16_t Beg, End; // Range is [Beg, End), thus Beg==End is an empty range. |
| 148 | |
| 149 | LabelRange(uint16_t Beg = 0, uint16_t End = 0) : Beg(Beg), End(End) {} |
| 150 | |
| 151 | static LabelRange Join(LabelRange LR1, LabelRange LR2) { |
| 152 | if (LR1.Beg == LR1.End) return LR2; |
| 153 | if (LR2.Beg == LR2.End) return LR1; |
| 154 | return {std::min(LR1.Beg, LR2.Beg), std::max(LR1.End, LR2.End)}; |
| 155 | } |
| 156 | LabelRange &Join(LabelRange LR) { |
| 157 | return *this = Join(*this, LR); |
| 158 | } |
| 159 | static LabelRange Singleton(const dfsan_label_info *LI) { |
| 160 | uint16_t Idx = (uint16_t)(uintptr_t)LI->userdata; |
| 161 | assert(Idx > 0); |
| 162 | return {(uint16_t)(Idx - 1), Idx}; |
| 163 | } |
| 164 | }; |
| 165 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 166 | // For now, very simple: put Size bytes of Data at position Pos. |
| 167 | struct TraceBasedMutation { |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 168 | uint32_t Pos; |
| 169 | Word W; |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 172 | // Declared as static globals for faster checks inside the hooks. |
| 173 | static bool RecordingTraces = false; |
| 174 | static bool RecordingMemcmp = false; |
Kostya Serebryany | c135b55 | 2016-07-15 23:27:19 +0000 | [diff] [blame] | 175 | static bool RecordingMemmem = false; |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 176 | static bool RecordingValueProfile = false; |
Kostya Serebryany | 6b08be9 | 2016-07-19 18:29:06 +0000 | [diff] [blame] | 177 | static bool DoingMyOwnMemmem = false; |
| 178 | |
| 179 | struct ScopedDoingMyOwnMemmem { |
| 180 | ScopedDoingMyOwnMemmem() { DoingMyOwnMemmem = true; } |
| 181 | ~ScopedDoingMyOwnMemmem() { DoingMyOwnMemmem = false; } |
| 182 | }; |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 183 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 184 | class TraceState { |
Mike Aizatsky | f0b3e85 | 2016-06-23 20:44:48 +0000 | [diff] [blame] | 185 | public: |
| 186 | TraceState(MutationDispatcher &MD, const FuzzingOptions &Options, |
Richard Smith | b62e7e3 | 2016-05-27 21:05:35 +0000 | [diff] [blame] | 187 | const Fuzzer *F) |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 188 | : MD(MD), Options(Options), F(F) {} |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 189 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 190 | LabelRange GetLabelRange(dfsan_label L); |
| 191 | void DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
| 192 | uint64_t Arg1, uint64_t Arg2, dfsan_label L1, |
| 193 | dfsan_label L2); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 194 | void DFSanMemcmpCallback(size_t CmpSize, const uint8_t *Data1, |
| 195 | const uint8_t *Data2, dfsan_label L1, |
| 196 | dfsan_label L2); |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 197 | void DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits, uint64_t Val, |
| 198 | size_t NumCases, uint64_t *Cases, dfsan_label L); |
Kostya Serebryany | e641dd6 | 2015-09-04 22:32:25 +0000 | [diff] [blame] | 199 | void TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
| 200 | uint64_t Arg1, uint64_t Arg2); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 201 | void TraceMemcmpCallback(size_t CmpSize, const uint8_t *Data1, |
| 202 | const uint8_t *Data2); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 203 | |
| 204 | void TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits, uint64_t Val, |
| 205 | size_t NumCases, uint64_t *Cases); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 206 | int TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData, |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 207 | size_t DataSize); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 208 | int TryToAddDesiredData(const uint8_t *PresentData, |
| 209 | const uint8_t *DesiredData, size_t DataSize); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 210 | |
| 211 | void StartTraceRecording() { |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 212 | if (!Options.UseTraces && !Options.UseMemcmp) |
| 213 | return; |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 214 | RecordingTraces = Options.UseTraces; |
| 215 | RecordingMemcmp = Options.UseMemcmp; |
Kostya Serebryany | c135b55 | 2016-07-15 23:27:19 +0000 | [diff] [blame] | 216 | RecordingMemmem = Options.UseMemmem; |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 217 | NumMutations = 0; |
Kostya Serebryany | c135b55 | 2016-07-15 23:27:19 +0000 | [diff] [blame] | 218 | InterestingWords.clear(); |
Kostya Serebryany | 7ec0c56 | 2016-02-13 03:25:16 +0000 | [diff] [blame] | 219 | MD.ClearAutoDictionary(); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 222 | void StopTraceRecording() { |
Mike Aizatsky | f0b3e85 | 2016-06-23 20:44:48 +0000 | [diff] [blame] | 223 | if (!RecordingTraces && !RecordingMemcmp) |
| 224 | return; |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 225 | RecordingTraces = false; |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 226 | RecordingMemcmp = false; |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 227 | for (size_t i = 0; i < NumMutations; i++) { |
| 228 | auto &M = Mutations[i]; |
Kostya Serebryany | 859e86d | 2016-01-12 02:08:37 +0000 | [diff] [blame] | 229 | if (Options.Verbosity >= 2) { |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 230 | AutoDictUnitCounts[M.W]++; |
Kostya Serebryany | 859e86d | 2016-01-12 02:08:37 +0000 | [diff] [blame] | 231 | AutoDictAdds++; |
| 232 | if ((AutoDictAdds & (AutoDictAdds - 1)) == 0) { |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 233 | typedef std::pair<size_t, Word> CU; |
Kostya Serebryany | 859e86d | 2016-01-12 02:08:37 +0000 | [diff] [blame] | 234 | std::vector<CU> CountedUnits; |
| 235 | for (auto &I : AutoDictUnitCounts) |
| 236 | CountedUnits.push_back(std::make_pair(I.second, I.first)); |
| 237 | std::sort(CountedUnits.begin(), CountedUnits.end(), |
| 238 | [](const CU &a, const CU &b) { return a.first > b.first; }); |
| 239 | Printf("AutoDict:\n"); |
| 240 | for (auto &I : CountedUnits) { |
| 241 | Printf(" %zd ", I.first); |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 242 | PrintASCII(I.second); |
Kostya Serebryany | 859e86d | 2016-01-12 02:08:37 +0000 | [diff] [blame] | 243 | Printf("\n"); |
| 244 | } |
| 245 | } |
| 246 | } |
Kostya Serebryany | c135b55 | 2016-07-15 23:27:19 +0000 | [diff] [blame] | 247 | MD.AddWordToAutoDictionary({M.W, M.Pos}); |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 248 | } |
Kostya Serebryany | c135b55 | 2016-07-15 23:27:19 +0000 | [diff] [blame] | 249 | for (auto &W : InterestingWords) |
| 250 | MD.AddWordToAutoDictionary({W}); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 253 | void AddMutation(uint32_t Pos, uint32_t Size, const uint8_t *Data) { |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 254 | if (NumMutations >= kMaxMutations) return; |
| 255 | auto &M = Mutations[NumMutations++]; |
| 256 | M.Pos = Pos; |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 257 | M.W.Set(Data, Size); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void AddMutation(uint32_t Pos, uint32_t Size, uint64_t Data) { |
| 261 | assert(Size <= sizeof(Data)); |
| 262 | AddMutation(Pos, Size, reinterpret_cast<uint8_t*>(&Data)); |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Kostya Serebryany | c135b55 | 2016-07-15 23:27:19 +0000 | [diff] [blame] | 265 | void AddInterestingWord(const uint8_t *Data, size_t Size) { |
| 266 | if (!RecordingMemmem || !F->InFuzzingThread()) return; |
| 267 | if (Size <= 1) return; |
| 268 | Size = std::min(Size, Word::GetMaxSize()); |
| 269 | Word W(Data, Size); |
| 270 | InterestingWords.insert(W); |
| 271 | } |
| 272 | |
Kostya Serebryany | 64d2457 | 2016-03-12 01:57:04 +0000 | [diff] [blame] | 273 | void EnsureDfsanLabels(size_t Size) { |
| 274 | for (; LastDfsanLabel < Size; LastDfsanLabel++) { |
| 275 | dfsan_label L = dfsan_create_label("input", (void *)(LastDfsanLabel + 1)); |
| 276 | // We assume that no one else has called dfsan_create_label before. |
| 277 | if (L != LastDfsanLabel + 1) { |
| 278 | Printf("DFSan labels are not starting from 1, exiting\n"); |
| 279 | exit(1); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 284 | private: |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 285 | bool IsTwoByteData(uint64_t Data) { |
| 286 | int64_t Signed = static_cast<int64_t>(Data); |
| 287 | Signed >>= 16; |
| 288 | return Signed == 0 || Signed == -1L; |
| 289 | } |
Kostya Serebryany | d88d130 | 2016-02-02 23:17:45 +0000 | [diff] [blame] | 290 | |
| 291 | // We don't want to create too many trace-based mutations as it is both |
| 292 | // expensive and useless. So after some number of mutations is collected, |
| 293 | // start rejecting some of them. The more there are mutations the more we |
| 294 | // reject. |
| 295 | bool WantToHandleOneMoreMutation() { |
| 296 | const size_t FirstN = 64; |
| 297 | // Gladly handle first N mutations. |
| 298 | if (NumMutations <= FirstN) return true; |
| 299 | size_t Diff = NumMutations - FirstN; |
| 300 | size_t DiffLog = sizeof(long) * 8 - __builtin_clzl((long)Diff); |
| 301 | assert(DiffLog > 0 && DiffLog < 64); |
Kostya Serebryany | 7ec0c56 | 2016-02-13 03:25:16 +0000 | [diff] [blame] | 302 | bool WantThisOne = MD.GetRand()(1 << DiffLog) == 0; // 1 out of DiffLog. |
Kostya Serebryany | d88d130 | 2016-02-02 23:17:45 +0000 | [diff] [blame] | 303 | return WantThisOne; |
| 304 | } |
| 305 | |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 306 | static const size_t kMaxMutations = 1 << 16; |
| 307 | size_t NumMutations; |
| 308 | TraceBasedMutation Mutations[kMaxMutations]; |
Kostya Serebryany | c135b55 | 2016-07-15 23:27:19 +0000 | [diff] [blame] | 309 | // TODO: std::set is too inefficient, need to have a custom DS here. |
| 310 | std::set<Word> InterestingWords; |
Kostya Serebryany | 4d62322 | 2015-11-18 01:08:30 +0000 | [diff] [blame] | 311 | LabelRange LabelRanges[1 << (sizeof(dfsan_label) * 8)]; |
Kostya Serebryany | 64d2457 | 2016-03-12 01:57:04 +0000 | [diff] [blame] | 312 | size_t LastDfsanLabel = 0; |
Kostya Serebryany | 7ec0c56 | 2016-02-13 03:25:16 +0000 | [diff] [blame] | 313 | MutationDispatcher &MD; |
Mike Aizatsky | f0b3e85 | 2016-06-23 20:44:48 +0000 | [diff] [blame] | 314 | const FuzzingOptions Options; |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 315 | const Fuzzer *F; |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 316 | std::map<Word, size_t> AutoDictUnitCounts; |
Kostya Serebryany | 859e86d | 2016-01-12 02:08:37 +0000 | [diff] [blame] | 317 | size_t AutoDictAdds = 0; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 318 | }; |
| 319 | |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame] | 320 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 321 | LabelRange TraceState::GetLabelRange(dfsan_label L) { |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 322 | LabelRange &LR = LabelRanges[L]; |
| 323 | if (LR.Beg < LR.End || L == 0) |
| 324 | return LR; |
| 325 | const dfsan_label_info *LI = dfsan_get_label_info(L); |
| 326 | if (LI->l1 || LI->l2) |
| 327 | return LR = LabelRange::Join(GetLabelRange(LI->l1), GetLabelRange(LI->l2)); |
| 328 | return LR = LabelRange::Singleton(LI); |
| 329 | } |
| 330 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 331 | void TraceState::DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 332 | uint64_t Arg1, uint64_t Arg2, dfsan_label L1, |
| 333 | dfsan_label L2) { |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 334 | assert(ReallyHaveDFSan()); |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 335 | if (!RecordingTraces || !F->InFuzzingThread()) return; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 336 | if (L1 == 0 && L2 == 0) |
| 337 | return; // Not actionable. |
| 338 | if (L1 != 0 && L2 != 0) |
| 339 | return; // Probably still actionable. |
| 340 | bool Res = ComputeCmp(CmpSize, CmpType, Arg1, Arg2); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 341 | uint64_t Data = L1 ? Arg2 : Arg1; |
| 342 | LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 343 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 344 | for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) { |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 345 | AddMutation(Pos, CmpSize, Data); |
| 346 | AddMutation(Pos, CmpSize, Data + 1); |
| 347 | AddMutation(Pos, CmpSize, Data - 1); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Dmitry Vyukov | 2eed121 | 2016-03-02 09:54:40 +0000 | [diff] [blame] | 350 | if (CmpSize > (size_t)(LR.End - LR.Beg)) |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 351 | AddMutation(LR.Beg, (unsigned)(LR.End - LR.Beg), Data); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 352 | |
| 353 | |
| 354 | if (Options.Verbosity >= 3) |
Kostya Serebryany | ae7df1c | 2015-07-28 01:25:00 +0000 | [diff] [blame] | 355 | Printf("DFSanCmpCallback: PC %lx S %zd T %zd A1 %llx A2 %llx R %d L1 %d L2 " |
| 356 | "%d MU %zd\n", |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 357 | PC, CmpSize, CmpType, Arg1, Arg2, Res, L1, L2, NumMutations); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 360 | void TraceState::DFSanMemcmpCallback(size_t CmpSize, const uint8_t *Data1, |
| 361 | const uint8_t *Data2, dfsan_label L1, |
| 362 | dfsan_label L2) { |
| 363 | |
| 364 | assert(ReallyHaveDFSan()); |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 365 | if (!RecordingMemcmp || !F->InFuzzingThread()) return; |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 366 | if (L1 == 0 && L2 == 0) |
| 367 | return; // Not actionable. |
| 368 | if (L1 != 0 && L2 != 0) |
| 369 | return; // Probably still actionable. |
| 370 | |
| 371 | const uint8_t *Data = L1 ? Data2 : Data1; |
| 372 | LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2); |
| 373 | for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) { |
| 374 | AddMutation(Pos, CmpSize, Data); |
| 375 | if (Options.Verbosity >= 3) |
| 376 | Printf("DFSanMemcmpCallback: Pos %d Size %d\n", Pos, CmpSize); |
| 377 | } |
| 378 | } |
| 379 | |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 380 | void TraceState::DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits, |
| 381 | uint64_t Val, size_t NumCases, |
| 382 | uint64_t *Cases, dfsan_label L) { |
| 383 | assert(ReallyHaveDFSan()); |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 384 | if (!RecordingTraces || !F->InFuzzingThread()) return; |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 385 | if (!L) return; // Not actionable. |
| 386 | LabelRange LR = GetLabelRange(L); |
| 387 | size_t ValSize = ValSizeInBits / 8; |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 388 | bool TryShort = IsTwoByteData(Val); |
| 389 | for (size_t i = 0; i < NumCases; i++) |
| 390 | TryShort &= IsTwoByteData(Cases[i]); |
| 391 | |
| 392 | for (size_t Pos = LR.Beg; Pos + ValSize <= LR.End; Pos++) |
| 393 | for (size_t i = 0; i < NumCases; i++) |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 394 | AddMutation(Pos, ValSize, Cases[i]); |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 395 | |
| 396 | if (TryShort) |
| 397 | for (size_t Pos = LR.Beg; Pos + 2 <= LR.End; Pos++) |
| 398 | for (size_t i = 0; i < NumCases; i++) |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 399 | AddMutation(Pos, 2, Cases[i]); |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 400 | |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 401 | if (Options.Verbosity >= 3) |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 402 | Printf("DFSanSwitchCallback: PC %lx Val %zd SZ %zd # %zd L %d: {%d, %d} " |
| 403 | "TryShort %d\n", |
| 404 | PC, Val, ValSize, NumCases, L, LR.Beg, LR.End, TryShort); |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 407 | int TraceState::TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData, |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 408 | size_t DataSize) { |
Kostya Serebryany | d88d130 | 2016-02-02 23:17:45 +0000 | [diff] [blame] | 409 | if (NumMutations >= kMaxMutations || !WantToHandleOneMoreMutation()) return 0; |
Kostya Serebryany | 6b08be9 | 2016-07-19 18:29:06 +0000 | [diff] [blame] | 410 | ScopedDoingMyOwnMemmem scoped_doing_my_own_memmem; |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 411 | const uint8_t *UnitData; |
| 412 | auto UnitSize = F->GetCurrentUnitInFuzzingThead(&UnitData); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 413 | int Res = 0; |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 414 | const uint8_t *Beg = UnitData; |
| 415 | const uint8_t *End = Beg + UnitSize; |
Kostya Serebryany | e641dd6 | 2015-09-04 22:32:25 +0000 | [diff] [blame] | 416 | for (const uint8_t *Cur = Beg; Cur < End; Cur++) { |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 417 | Cur = (uint8_t *)memmem(Cur, End - Cur, &PresentData, DataSize); |
| 418 | if (!Cur) |
| 419 | break; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 420 | size_t Pos = Cur - Beg; |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 421 | assert(Pos < UnitSize); |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 422 | AddMutation(Pos, DataSize, DesiredData); |
| 423 | AddMutation(Pos, DataSize, DesiredData + 1); |
| 424 | AddMutation(Pos, DataSize, DesiredData - 1); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 425 | Res++; |
| 426 | } |
| 427 | return Res; |
| 428 | } |
| 429 | |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 430 | int TraceState::TryToAddDesiredData(const uint8_t *PresentData, |
| 431 | const uint8_t *DesiredData, |
| 432 | size_t DataSize) { |
Kostya Serebryany | d88d130 | 2016-02-02 23:17:45 +0000 | [diff] [blame] | 433 | if (NumMutations >= kMaxMutations || !WantToHandleOneMoreMutation()) return 0; |
Kostya Serebryany | 6b08be9 | 2016-07-19 18:29:06 +0000 | [diff] [blame] | 434 | ScopedDoingMyOwnMemmem scoped_doing_my_own_memmem; |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 435 | const uint8_t *UnitData; |
| 436 | auto UnitSize = F->GetCurrentUnitInFuzzingThead(&UnitData); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 437 | int Res = 0; |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 438 | const uint8_t *Beg = UnitData; |
| 439 | const uint8_t *End = Beg + UnitSize; |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 440 | for (const uint8_t *Cur = Beg; Cur < End; Cur++) { |
| 441 | Cur = (uint8_t *)memmem(Cur, End - Cur, PresentData, DataSize); |
| 442 | if (!Cur) |
| 443 | break; |
| 444 | size_t Pos = Cur - Beg; |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 445 | assert(Pos < UnitSize); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 446 | AddMutation(Pos, DataSize, DesiredData); |
| 447 | Res++; |
| 448 | } |
| 449 | return Res; |
| 450 | } |
| 451 | |
Kostya Serebryany | e641dd6 | 2015-09-04 22:32:25 +0000 | [diff] [blame] | 452 | void TraceState::TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
| 453 | uint64_t Arg1, uint64_t Arg2) { |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 454 | if (!RecordingTraces || !F->InFuzzingThread()) return; |
Kostya Serebryany | 859e86d | 2016-01-12 02:08:37 +0000 | [diff] [blame] | 455 | if ((CmpType == ICMP_EQ || CmpType == ICMP_NE) && Arg1 == Arg2) |
| 456 | return; // No reason to mutate. |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 457 | int Added = 0; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 458 | Added += TryToAddDesiredData(Arg1, Arg2, CmpSize); |
| 459 | Added += TryToAddDesiredData(Arg2, Arg1, CmpSize); |
| 460 | if (!Added && CmpSize == 4 && IsTwoByteData(Arg1) && IsTwoByteData(Arg2)) { |
| 461 | Added += TryToAddDesiredData(Arg1, Arg2, 2); |
| 462 | Added += TryToAddDesiredData(Arg2, Arg1, 2); |
| 463 | } |
Kostya Serebryany | 859e86d | 2016-01-12 02:08:37 +0000 | [diff] [blame] | 464 | if (Options.Verbosity >= 3 && Added) |
| 465 | Printf("TraceCmp %zd/%zd: %p %zd %zd\n", CmpSize, CmpType, PC, Arg1, Arg2); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 468 | void TraceState::TraceMemcmpCallback(size_t CmpSize, const uint8_t *Data1, |
| 469 | const uint8_t *Data2) { |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 470 | if (!RecordingMemcmp || !F->InFuzzingThread()) return; |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 471 | CmpSize = std::min(CmpSize, Word::GetMaxSize()); |
Kostya Serebryany | 1f9c40d | 2016-01-09 03:46:08 +0000 | [diff] [blame] | 472 | int Added2 = TryToAddDesiredData(Data1, Data2, CmpSize); |
| 473 | int Added1 = TryToAddDesiredData(Data2, Data1, CmpSize); |
| 474 | if ((Added1 || Added2) && Options.Verbosity >= 3) { |
| 475 | Printf("MemCmp Added %d%d: ", Added1, Added2); |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 476 | if (Added1) PrintASCII(Data1, CmpSize); |
| 477 | if (Added2) PrintASCII(Data2, CmpSize); |
Kostya Serebryany | 1f9c40d | 2016-01-09 03:46:08 +0000 | [diff] [blame] | 478 | Printf("\n"); |
| 479 | } |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 482 | void TraceState::TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits, |
| 483 | uint64_t Val, size_t NumCases, |
| 484 | uint64_t *Cases) { |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 485 | if (!RecordingTraces || !F->InFuzzingThread()) return; |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 486 | size_t ValSize = ValSizeInBits / 8; |
| 487 | bool TryShort = IsTwoByteData(Val); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 488 | for (size_t i = 0; i < NumCases; i++) |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 489 | TryShort &= IsTwoByteData(Cases[i]); |
| 490 | |
| 491 | if (Options.Verbosity >= 3) |
| 492 | Printf("TraceSwitch: %p %zd # %zd; TryShort %d\n", PC, Val, NumCases, |
| 493 | TryShort); |
| 494 | |
| 495 | for (size_t i = 0; i < NumCases; i++) { |
| 496 | TryToAddDesiredData(Val, Cases[i], ValSize); |
| 497 | if (TryShort) |
| 498 | TryToAddDesiredData(Val, Cases[i], 2); |
| 499 | } |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 502 | static TraceState *TS; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 503 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 504 | void Fuzzer::StartTraceRecording() { |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 505 | if (!TS) return; |
| 506 | TS->StartTraceRecording(); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 509 | void Fuzzer::StopTraceRecording() { |
| 510 | if (!TS) return; |
| 511 | TS->StopTraceRecording(); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Kostya Serebryany | d50a3ee | 2016-01-13 23:02:30 +0000 | [diff] [blame] | 514 | void Fuzzer::AssignTaintLabels(uint8_t *Data, size_t Size) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 515 | if (!Options.UseTraces && !Options.UseMemcmp) return; |
Kostya Serebryany | d50a3ee | 2016-01-13 23:02:30 +0000 | [diff] [blame] | 516 | if (!ReallyHaveDFSan()) return; |
Kostya Serebryany | 64d2457 | 2016-03-12 01:57:04 +0000 | [diff] [blame] | 517 | TS->EnsureDfsanLabels(Size); |
Kostya Serebryany | d50a3ee | 2016-01-13 23:02:30 +0000 | [diff] [blame] | 518 | for (size_t i = 0; i < Size; i++) |
| 519 | dfsan_set_label(i + 1, &Data[i], 1); |
| 520 | } |
| 521 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 522 | void Fuzzer::InitializeTraceState() { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 523 | if (!Options.UseTraces && !Options.UseMemcmp) return; |
Kostya Serebryany | f26017b | 2016-05-26 21:32:30 +0000 | [diff] [blame] | 524 | TS = new TraceState(MD, Options, this); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Kostya Serebryany | c9dc96b | 2015-07-30 21:22:22 +0000 | [diff] [blame] | 527 | static size_t InternalStrnlen(const char *S, size_t MaxLen) { |
| 528 | size_t Len = 0; |
| 529 | for (; Len < MaxLen && S[Len]; Len++) {} |
| 530 | return Len; |
| 531 | } |
| 532 | |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 533 | // Value profile. |
| 534 | // We keep track of various values that affect control flow. |
| 535 | // These values are inserted into a bit-set-based hash map (ValueBitMap VP). |
| 536 | // Every new bit in the map is treated as a new coverage. |
| 537 | // |
| 538 | // For memcmp/strcmp/etc the interesting value is the length of the common |
| 539 | // prefix of the parameters. |
| 540 | // For cmp instructions the interesting value is a XOR of the parameters. |
| 541 | // The interesting value is mixed up with the PC and is then added to the map. |
| 542 | static ValueBitMap VP; |
| 543 | |
| 544 | void EnableValueProfile() { RecordingValueProfile = true; } |
| 545 | |
| 546 | size_t VPMapMergeFromCurrent(ValueBitMap &M) { |
| 547 | if (!RecordingValueProfile) return 0; |
| 548 | return M.MergeFrom(VP); |
| 549 | } |
| 550 | |
| 551 | static void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2, |
| 552 | size_t n) { |
| 553 | if (!n) return; |
| 554 | size_t Len = std::min(n, (size_t)32); |
Kostya Serebryany | d4492f8 | 2016-08-30 03:05:50 +0000 | [diff] [blame] | 555 | const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1); |
| 556 | const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2); |
| 557 | size_t I = 0; |
| 558 | for (; I < Len; I++) |
| 559 | if (A1[I] != A2[I]) |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 560 | break; |
| 561 | size_t PC = reinterpret_cast<size_t>(caller_pc); |
Kostya Serebryany | d4492f8 | 2016-08-30 03:05:50 +0000 | [diff] [blame] | 562 | size_t Idx = I * 8; |
| 563 | if (I < Len) |
| 564 | Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1; |
| 565 | VP.AddValue((PC & 4095) | (Idx << 12)); |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static void AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2, |
| 569 | size_t n) { |
| 570 | if (!n) return; |
| 571 | size_t Len = std::min(n, (size_t)32); |
Kostya Serebryany | d4492f8 | 2016-08-30 03:05:50 +0000 | [diff] [blame] | 572 | const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1); |
| 573 | const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2); |
| 574 | size_t I = 0; |
| 575 | for (; I < Len; I++) |
| 576 | if (A1[I] != A2[I] || A1[I] == 0) |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 577 | break; |
| 578 | size_t PC = reinterpret_cast<size_t>(caller_pc); |
Kostya Serebryany | d4492f8 | 2016-08-30 03:05:50 +0000 | [diff] [blame] | 579 | size_t Idx = I * 8; |
| 580 | if (I < Len && A1[I]) |
| 581 | Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1; |
| 582 | VP.AddValue((PC & 4095) | (Idx << 12)); |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Kostya Serebryany | bceadcf | 2016-08-24 01:38:42 +0000 | [diff] [blame] | 585 | ATTRIBUTE_TARGET_POPCNT |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 586 | static void AddValueForCmp(void *PCptr, uint64_t Arg1, uint64_t Arg2) { |
Kostya Serebryany | ac524cf | 2016-08-23 23:37:37 +0000 | [diff] [blame] | 587 | if (Arg1 == Arg2) |
| 588 | return; |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 589 | uintptr_t PC = reinterpret_cast<uintptr_t>(PCptr); |
Kostya Serebryany | ac524cf | 2016-08-23 23:37:37 +0000 | [diff] [blame] | 590 | uint64_t ArgDistance = __builtin_popcountl(Arg1 ^ Arg2) - 1; // [0,63] |
| 591 | uintptr_t Idx = (PC & 4095) | (ArgDistance << 12); |
| 592 | VP.AddValue(Idx); |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Kostya Serebryany | 4d22e4f | 2016-08-30 01:30:14 +0000 | [diff] [blame] | 595 | static void AddValueForSingleVal(void *PCptr, uintptr_t Val) { |
| 596 | if (!Val) return; |
| 597 | uintptr_t PC = reinterpret_cast<uintptr_t>(PCptr); |
| 598 | uint64_t ArgDistance = __builtin_popcountl(Val) - 1; // [0,63] |
| 599 | uintptr_t Idx = (PC & 4095) | (ArgDistance << 12); |
| 600 | VP.AddValue(Idx); |
| 601 | } |
| 602 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 603 | } // namespace fuzzer |
| 604 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 605 | using fuzzer::TS; |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 606 | using fuzzer::RecordingTraces; |
| 607 | using fuzzer::RecordingMemcmp; |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 608 | using fuzzer::RecordingValueProfile; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 609 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 610 | extern "C" { |
| 611 | void __dfsw___sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1, |
| 612 | uint64_t Arg2, dfsan_label L0, |
| 613 | dfsan_label L1, dfsan_label L2) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 614 | if (!RecordingTraces) return; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 615 | assert(L0 == 0); |
| 616 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 617 | uint64_t CmpSize = (SizeAndType >> 32) / 8; |
| 618 | uint64_t Type = (SizeAndType << 32) >> 32; |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 619 | TS->DFSanCmpCallback(PC, CmpSize, Type, Arg1, Arg2, L1, L2); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 620 | } |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 621 | |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 622 | #define DFSAN_CMP_CALLBACK(N) \ |
| 623 | void __dfsw___sanitizer_cov_trace_cmp##N(uint64_t Arg1, uint64_t Arg2, \ |
| 624 | dfsan_label L1, dfsan_label L2) { \ |
| 625 | if (RecordingTraces) \ |
| 626 | TS->DFSanCmpCallback( \ |
| 627 | reinterpret_cast<uintptr_t>(__builtin_return_address(0)), N, \ |
| 628 | fuzzer::ICMP_EQ, Arg1, Arg2, L1, L2); \ |
| 629 | } |
| 630 | |
| 631 | DFSAN_CMP_CALLBACK(1) |
| 632 | DFSAN_CMP_CALLBACK(2) |
| 633 | DFSAN_CMP_CALLBACK(4) |
| 634 | DFSAN_CMP_CALLBACK(8) |
| 635 | #undef DFSAN_CMP_CALLBACK |
| 636 | |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 637 | void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases, |
| 638 | dfsan_label L1, dfsan_label L2) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 639 | if (!RecordingTraces) return; |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 640 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 641 | TS->DFSanSwitchCallback(PC, Cases[1], Val, Cases[0], Cases+2, L1); |
| 642 | } |
| 643 | |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 644 | void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2, |
| 645 | size_t n, dfsan_label s1_label, |
| 646 | dfsan_label s2_label, dfsan_label n_label) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 647 | if (!RecordingMemcmp) return; |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 648 | dfsan_label L1 = dfsan_read_label(s1, n); |
| 649 | dfsan_label L2 = dfsan_read_label(s2, n); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 650 | TS->DFSanMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1), |
| 651 | reinterpret_cast<const uint8_t *>(s2), L1, L2); |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 652 | } |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 653 | |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 654 | void dfsan_weak_hook_strncmp(void *caller_pc, const char *s1, const char *s2, |
| 655 | size_t n, dfsan_label s1_label, |
| 656 | dfsan_label s2_label, dfsan_label n_label) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 657 | if (!RecordingMemcmp) return; |
Kostya Serebryany | c9dc96b | 2015-07-30 21:22:22 +0000 | [diff] [blame] | 658 | n = std::min(n, fuzzer::InternalStrnlen(s1, n)); |
| 659 | n = std::min(n, fuzzer::InternalStrnlen(s2, n)); |
Kostya Serebryany | c9dc96b | 2015-07-30 21:22:22 +0000 | [diff] [blame] | 660 | dfsan_label L1 = dfsan_read_label(s1, n); |
| 661 | dfsan_label L2 = dfsan_read_label(s2, n); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 662 | TS->DFSanMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1), |
| 663 | reinterpret_cast<const uint8_t *>(s2), L1, L2); |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 666 | void dfsan_weak_hook_strcmp(void *caller_pc, const char *s1, const char *s2, |
| 667 | dfsan_label s1_label, dfsan_label s2_label) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 668 | if (!RecordingMemcmp) return; |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 669 | size_t Len1 = strlen(s1); |
| 670 | size_t Len2 = strlen(s2); |
| 671 | size_t N = std::min(Len1, Len2); |
| 672 | if (N <= 1) return; // Not interesting. |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 673 | dfsan_label L1 = dfsan_read_label(s1, Len1); |
| 674 | dfsan_label L2 = dfsan_read_label(s2, Len2); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 675 | TS->DFSanMemcmpCallback(N, reinterpret_cast<const uint8_t *>(s1), |
| 676 | reinterpret_cast<const uint8_t *>(s2), L1, L2); |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Kostya Serebryany | 4b83a4f | 2016-01-12 16:50:18 +0000 | [diff] [blame] | 679 | // We may need to avoid defining weak hooks to stay compatible with older clang. |
| 680 | #ifndef LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS |
| 681 | # define LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS 1 |
| 682 | #endif |
| 683 | |
| 684 | #if LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS |
Kostya Serebryany | 0e776a2 | 2015-07-30 01:34:58 +0000 | [diff] [blame] | 685 | void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1, |
Kostya Serebryany | e358095 | 2016-01-12 00:43:42 +0000 | [diff] [blame] | 686 | const void *s2, size_t n, int result) { |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 687 | if (RecordingValueProfile) |
| 688 | fuzzer::AddValueForMemcmp(caller_pc, s1, s2, n); |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 689 | if (!RecordingMemcmp) return; |
Kostya Serebryany | e358095 | 2016-01-12 00:43:42 +0000 | [diff] [blame] | 690 | if (result == 0) return; // No reason to mutate. |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 691 | if (n <= 1) return; // Not interesting. |
| 692 | TS->TraceMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1), |
| 693 | reinterpret_cast<const uint8_t *>(s2)); |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1, |
Kostya Serebryany | e358095 | 2016-01-12 00:43:42 +0000 | [diff] [blame] | 697 | const char *s2, size_t n, int result) { |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 698 | if (RecordingValueProfile) |
| 699 | fuzzer::AddValueForStrcmp(caller_pc, s1, s2, n); |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 700 | if (!RecordingMemcmp) return; |
Kostya Serebryany | e358095 | 2016-01-12 00:43:42 +0000 | [diff] [blame] | 701 | if (result == 0) return; // No reason to mutate. |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 702 | size_t Len1 = fuzzer::InternalStrnlen(s1, n); |
| 703 | size_t Len2 = fuzzer::InternalStrnlen(s2, n); |
| 704 | n = std::min(n, Len1); |
| 705 | n = std::min(n, Len2); |
| 706 | if (n <= 1) return; // Not interesting. |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 707 | TS->TraceMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1), |
| 708 | reinterpret_cast<const uint8_t *>(s2)); |
Kostya Serebryany | 0e776a2 | 2015-07-30 01:34:58 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 711 | void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1, |
Kostya Serebryany | e358095 | 2016-01-12 00:43:42 +0000 | [diff] [blame] | 712 | const char *s2, int result) { |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 713 | if (RecordingValueProfile) |
| 714 | fuzzer::AddValueForStrcmp(caller_pc, s1, s2, 64); |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 715 | if (!RecordingMemcmp) return; |
Kostya Serebryany | e358095 | 2016-01-12 00:43:42 +0000 | [diff] [blame] | 716 | if (result == 0) return; // No reason to mutate. |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 717 | size_t Len1 = strlen(s1); |
| 718 | size_t Len2 = strlen(s2); |
| 719 | size_t N = std::min(Len1, Len2); |
| 720 | if (N <= 1) return; // Not interesting. |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 721 | TS->TraceMemcmpCallback(N, reinterpret_cast<const uint8_t *>(s1), |
| 722 | reinterpret_cast<const uint8_t *>(s2)); |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Kostya Serebryany | c135b55 | 2016-07-15 23:27:19 +0000 | [diff] [blame] | 725 | void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1, |
| 726 | const char *s2, size_t n, int result) { |
| 727 | return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result); |
| 728 | } |
| 729 | void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1, |
| 730 | const char *s2, int result) { |
| 731 | return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result); |
| 732 | } |
| 733 | void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1, |
| 734 | const char *s2, char *result) { |
| 735 | TS->AddInterestingWord(reinterpret_cast<const uint8_t *>(s2), strlen(s2)); |
| 736 | } |
| 737 | void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1, |
| 738 | const char *s2, char *result) { |
| 739 | TS->AddInterestingWord(reinterpret_cast<const uint8_t *>(s2), strlen(s2)); |
| 740 | } |
| 741 | void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1, |
| 742 | const void *s2, size_t len2, void *result) { |
Kostya Serebryany | 6b08be9 | 2016-07-19 18:29:06 +0000 | [diff] [blame] | 743 | if (fuzzer::DoingMyOwnMemmem) return; |
| 744 | TS->AddInterestingWord(reinterpret_cast<const uint8_t *>(s2), len2); |
Kostya Serebryany | c135b55 | 2016-07-15 23:27:19 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Kostya Serebryany | 4b83a4f | 2016-01-12 16:50:18 +0000 | [diff] [blame] | 747 | #endif // LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS |
| 748 | |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 749 | // TODO: this one will not be used with the newest clang. Remove it. |
Kostya Serebryany | 3287d7a | 2015-09-30 22:22:37 +0000 | [diff] [blame] | 750 | __attribute__((visibility("default"))) |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 751 | void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1, |
| 752 | uint64_t Arg2) { |
Kostya Serebryany | d46a59f | 2016-08-16 19:33:51 +0000 | [diff] [blame] | 753 | if (RecordingTraces) { |
| 754 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 755 | uint64_t CmpSize = (SizeAndType >> 32) / 8; |
| 756 | uint64_t Type = (SizeAndType << 32) >> 32; |
| 757 | TS->TraceCmpCallback(PC, CmpSize, Type, Arg1, Arg2); |
| 758 | } |
| 759 | if (RecordingValueProfile) |
Kostya Serebryany | 524c3f3 | 2016-08-18 01:25:28 +0000 | [diff] [blame] | 760 | fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2); |
| 761 | } |
| 762 | |
| 763 | // Adding if(RecordingTraces){...} slows down the VP callbacks. |
| 764 | // Once we prove that VP is as strong as traces, delete this. |
| 765 | #define MAYBE_RECORD_TRACE(N) \ |
| 766 | if (RecordingTraces) { \ |
| 767 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); \ |
| 768 | TS->TraceCmpCallback(PC, N, fuzzer::ICMP_EQ, Arg1, Arg2); \ |
| 769 | } |
| 770 | |
| 771 | __attribute__((visibility("default"))) |
| 772 | void __sanitizer_cov_trace_cmp8(uint64_t Arg1, int64_t Arg2) { |
| 773 | fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2); |
| 774 | MAYBE_RECORD_TRACE(8); |
| 775 | } |
| 776 | __attribute__((visibility("default"))) |
| 777 | void __sanitizer_cov_trace_cmp4(uint32_t Arg1, int32_t Arg2) { |
| 778 | fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2); |
| 779 | MAYBE_RECORD_TRACE(4); |
| 780 | } |
| 781 | __attribute__((visibility("default"))) |
| 782 | void __sanitizer_cov_trace_cmp2(uint16_t Arg1, int16_t Arg2) { |
| 783 | fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2); |
| 784 | MAYBE_RECORD_TRACE(2); |
| 785 | } |
| 786 | __attribute__((visibility("default"))) |
| 787 | void __sanitizer_cov_trace_cmp1(uint8_t Arg1, int8_t Arg2) { |
| 788 | fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2); |
| 789 | MAYBE_RECORD_TRACE(1); |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Kostya Serebryany | 3287d7a | 2015-09-30 22:22:37 +0000 | [diff] [blame] | 792 | __attribute__((visibility("default"))) |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 793 | void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 794 | if (!RecordingTraces) return; |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 795 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 796 | TS->TraceSwitchCallback(PC, Cases[1], Val, Cases[0], Cases + 2); |
| 797 | } |
| 798 | |
Kostya Serebryany | 4d22e4f | 2016-08-30 01:30:14 +0000 | [diff] [blame] | 799 | __attribute__((visibility("default"))) |
| 800 | void __sanitizer_cov_trace_div4(uint32_t Val) { |
| 801 | fuzzer::AddValueForSingleVal(__builtin_return_address(0), Val); |
| 802 | } |
| 803 | __attribute__((visibility("default"))) |
| 804 | void __sanitizer_cov_trace_div8(uint64_t Val) { |
| 805 | fuzzer::AddValueForSingleVal(__builtin_return_address(0), Val); |
| 806 | } |
| 807 | __attribute__((visibility("default"))) |
| 808 | void __sanitizer_cov_trace_gep(uintptr_t Idx) { |
| 809 | fuzzer::AddValueForSingleVal(__builtin_return_address(0), Idx); |
| 810 | } |
| 811 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 812 | } // extern "C" |