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 | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 80 | |
Kostya Serebryany | 65f5086 | 2015-09-10 18:48:38 +0000 | [diff] [blame] | 81 | #if !LLVM_FUZZER_SUPPORTS_DFSAN |
| 82 | // Stubs for dfsan for platforms where dfsan does not exist and weak |
| 83 | // functions don't work. |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 84 | extern "C" { |
Kostya Serebryany | 65f5086 | 2015-09-10 18:48:38 +0000 | [diff] [blame] | 85 | dfsan_label dfsan_create_label(const char *desc, void *userdata) { return 0; } |
| 86 | void dfsan_set_label(dfsan_label label, void *addr, size_t size) {} |
| 87 | void dfsan_add_label(dfsan_label label, void *addr, size_t size) {} |
| 88 | const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label) { |
| 89 | return nullptr; |
| 90 | } |
| 91 | 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] | 92 | } // extern "C" |
Kostya Serebryany | 65f5086 | 2015-09-10 18:48:38 +0000 | [diff] [blame] | 93 | #endif // !LLVM_FUZZER_SUPPORTS_DFSAN |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 94 | |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 95 | namespace fuzzer { |
| 96 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 97 | // These values are copied from include/llvm/IR/InstrTypes.h. |
| 98 | // We do not include the LLVM headers here to remain independent. |
| 99 | // If these values ever change, an assertion in ComputeCmp will fail. |
| 100 | enum Predicate { |
| 101 | ICMP_EQ = 32, ///< equal |
| 102 | ICMP_NE = 33, ///< not equal |
| 103 | ICMP_UGT = 34, ///< unsigned greater than |
| 104 | ICMP_UGE = 35, ///< unsigned greater or equal |
| 105 | ICMP_ULT = 36, ///< unsigned less than |
| 106 | ICMP_ULE = 37, ///< unsigned less or equal |
| 107 | ICMP_SGT = 38, ///< signed greater than |
| 108 | ICMP_SGE = 39, ///< signed greater or equal |
| 109 | ICMP_SLT = 40, ///< signed less than |
| 110 | ICMP_SLE = 41, ///< signed less or equal |
| 111 | }; |
| 112 | |
| 113 | template <class U, class S> |
| 114 | bool ComputeCmp(size_t CmpType, U Arg1, U Arg2) { |
| 115 | switch(CmpType) { |
| 116 | case ICMP_EQ : return Arg1 == Arg2; |
| 117 | case ICMP_NE : return Arg1 != Arg2; |
| 118 | case ICMP_UGT: return Arg1 > Arg2; |
| 119 | case ICMP_UGE: return Arg1 >= Arg2; |
| 120 | case ICMP_ULT: return Arg1 < Arg2; |
| 121 | case ICMP_ULE: return Arg1 <= Arg2; |
| 122 | case ICMP_SGT: return (S)Arg1 > (S)Arg2; |
| 123 | case ICMP_SGE: return (S)Arg1 >= (S)Arg2; |
| 124 | case ICMP_SLT: return (S)Arg1 < (S)Arg2; |
| 125 | case ICMP_SLE: return (S)Arg1 <= (S)Arg2; |
| 126 | default: assert(0 && "unsupported CmpType"); |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | static bool ComputeCmp(size_t CmpSize, size_t CmpType, uint64_t Arg1, |
| 132 | uint64_t Arg2) { |
| 133 | if (CmpSize == 8) return ComputeCmp<uint64_t, int64_t>(CmpType, Arg1, Arg2); |
| 134 | if (CmpSize == 4) return ComputeCmp<uint32_t, int32_t>(CmpType, Arg1, Arg2); |
| 135 | if (CmpSize == 2) return ComputeCmp<uint16_t, int16_t>(CmpType, Arg1, Arg2); |
| 136 | if (CmpSize == 1) return ComputeCmp<uint8_t, int8_t>(CmpType, Arg1, Arg2); |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 137 | // Other size, == |
| 138 | if (CmpType == ICMP_EQ) return Arg1 == Arg2; |
Kostya Serebryany | 8ce7424 | 2015-08-01 01:42:51 +0000 | [diff] [blame] | 139 | // assert(0 && "unsupported cmp and type size combination"); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 140 | return true; |
| 141 | } |
| 142 | |
| 143 | // As a simplification we use the range of input bytes instead of a set of input |
| 144 | // bytes. |
| 145 | struct LabelRange { |
| 146 | uint16_t Beg, End; // Range is [Beg, End), thus Beg==End is an empty range. |
| 147 | |
| 148 | LabelRange(uint16_t Beg = 0, uint16_t End = 0) : Beg(Beg), End(End) {} |
| 149 | |
| 150 | static LabelRange Join(LabelRange LR1, LabelRange LR2) { |
| 151 | if (LR1.Beg == LR1.End) return LR2; |
| 152 | if (LR2.Beg == LR2.End) return LR1; |
| 153 | return {std::min(LR1.Beg, LR2.Beg), std::max(LR1.End, LR2.End)}; |
| 154 | } |
| 155 | LabelRange &Join(LabelRange LR) { |
| 156 | return *this = Join(*this, LR); |
| 157 | } |
| 158 | static LabelRange Singleton(const dfsan_label_info *LI) { |
| 159 | uint16_t Idx = (uint16_t)(uintptr_t)LI->userdata; |
| 160 | assert(Idx > 0); |
| 161 | return {(uint16_t)(Idx - 1), Idx}; |
| 162 | } |
| 163 | }; |
| 164 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 165 | // For now, very simple: put Size bytes of Data at position Pos. |
| 166 | struct TraceBasedMutation { |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 167 | static const size_t kMaxSize = 28; |
| 168 | uint32_t Pos : 24; |
| 169 | uint32_t Size : 8; |
| 170 | uint8_t Data[kMaxSize]; |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 173 | const size_t TraceBasedMutation::kMaxSize; |
| 174 | |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 175 | // Declared as static globals for faster checks inside the hooks. |
| 176 | static bool RecordingTraces = false; |
| 177 | static bool RecordingMemcmp = false; |
| 178 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 179 | class TraceState { |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 180 | public: |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 181 | TraceState(UserSuppliedFuzzer &USF, const Fuzzer::FuzzingOptions &Options, |
| 182 | uint8_t **CurrentUnitData, size_t *CurrentUnitSize) |
| 183 | : USF(USF), Options(Options), CurrentUnitData(CurrentUnitData), |
| 184 | CurrentUnitSize(CurrentUnitSize) { |
| 185 | // Current trace collection is not thread-friendly and it probably |
| 186 | // does not have to be such, but at least we should not crash in presence |
| 187 | // of threads. So, just ignore all traces coming from all threads but one. |
| 188 | IsMyThread = true; |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame] | 189 | } |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 190 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 191 | LabelRange GetLabelRange(dfsan_label L); |
| 192 | void DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
| 193 | uint64_t Arg1, uint64_t Arg2, dfsan_label L1, |
| 194 | dfsan_label L2); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 195 | void DFSanMemcmpCallback(size_t CmpSize, const uint8_t *Data1, |
| 196 | const uint8_t *Data2, dfsan_label L1, |
| 197 | dfsan_label L2); |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 198 | void DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits, uint64_t Val, |
| 199 | size_t NumCases, uint64_t *Cases, dfsan_label L); |
Kostya Serebryany | e641dd6 | 2015-09-04 22:32:25 +0000 | [diff] [blame] | 200 | void TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
| 201 | uint64_t Arg1, uint64_t Arg2); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 202 | void TraceMemcmpCallback(size_t CmpSize, const uint8_t *Data1, |
| 203 | const uint8_t *Data2); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 204 | |
| 205 | void TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits, uint64_t Val, |
| 206 | size_t NumCases, uint64_t *Cases); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 207 | int TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData, |
| 208 | size_t DataSize); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 209 | int TryToAddDesiredData(const uint8_t *PresentData, |
| 210 | const uint8_t *DesiredData, size_t DataSize); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 211 | |
| 212 | void StartTraceRecording() { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 213 | if (!Options.UseTraces && !Options.UseMemcmp) return; |
| 214 | RecordingTraces = Options.UseTraces; |
| 215 | RecordingMemcmp = Options.UseMemcmp; |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 216 | NumMutations = 0; |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 217 | USF.GetMD().ClearAutoDictionary(); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 220 | void StopTraceRecording() { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 221 | if (!RecordingTraces && !RecordingMemcmp) return; |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 222 | RecordingTraces = false; |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 223 | RecordingMemcmp = false; |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 224 | for (size_t i = 0; i < NumMutations; i++) { |
| 225 | auto &M = Mutations[i]; |
Kostya Serebryany | 859e86d | 2016-01-12 02:08:37 +0000 | [diff] [blame] | 226 | Unit U(M.Data, M.Data + M.Size); |
| 227 | if (Options.Verbosity >= 2) { |
| 228 | AutoDictUnitCounts[U]++; |
| 229 | AutoDictAdds++; |
| 230 | if ((AutoDictAdds & (AutoDictAdds - 1)) == 0) { |
| 231 | typedef std::pair<size_t, Unit> CU; |
| 232 | std::vector<CU> CountedUnits; |
| 233 | for (auto &I : AutoDictUnitCounts) |
| 234 | CountedUnits.push_back(std::make_pair(I.second, I.first)); |
| 235 | std::sort(CountedUnits.begin(), CountedUnits.end(), |
| 236 | [](const CU &a, const CU &b) { return a.first > b.first; }); |
| 237 | Printf("AutoDict:\n"); |
| 238 | for (auto &I : CountedUnits) { |
| 239 | Printf(" %zd ", I.first); |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 240 | PrintASCII(I.second); |
Kostya Serebryany | 859e86d | 2016-01-12 02:08:37 +0000 | [diff] [blame] | 241 | Printf("\n"); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | USF.GetMD().AddWordToAutoDictionary(U, M.Pos); |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 246 | } |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 249 | void AddMutation(uint32_t Pos, uint32_t Size, const uint8_t *Data) { |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 250 | if (NumMutations >= kMaxMutations) return; |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 251 | assert(Size <= TraceBasedMutation::kMaxSize); |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 252 | auto &M = Mutations[NumMutations++]; |
| 253 | M.Pos = Pos; |
| 254 | M.Size = Size; |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 255 | memcpy(M.Data, Data, Size); |
| 256 | } |
| 257 | |
| 258 | void AddMutation(uint32_t Pos, uint32_t Size, uint64_t Data) { |
| 259 | assert(Size <= sizeof(Data)); |
| 260 | AddMutation(Pos, Size, reinterpret_cast<uint8_t*>(&Data)); |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 263 | private: |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 264 | bool IsTwoByteData(uint64_t Data) { |
| 265 | int64_t Signed = static_cast<int64_t>(Data); |
| 266 | Signed >>= 16; |
| 267 | return Signed == 0 || Signed == -1L; |
| 268 | } |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 269 | static const size_t kMaxMutations = 1 << 16; |
| 270 | size_t NumMutations; |
| 271 | TraceBasedMutation Mutations[kMaxMutations]; |
Kostya Serebryany | 4d62322 | 2015-11-18 01:08:30 +0000 | [diff] [blame] | 272 | LabelRange LabelRanges[1 << (sizeof(dfsan_label) * 8)]; |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 273 | UserSuppliedFuzzer &USF; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 274 | const Fuzzer::FuzzingOptions &Options; |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 275 | uint8_t **CurrentUnitData; |
| 276 | size_t *CurrentUnitSize; |
Kostya Serebryany | 859e86d | 2016-01-12 02:08:37 +0000 | [diff] [blame] | 277 | std::map<Unit, size_t> AutoDictUnitCounts; |
| 278 | size_t AutoDictAdds = 0; |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame] | 279 | static thread_local bool IsMyThread; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 280 | }; |
| 281 | |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame] | 282 | thread_local bool TraceState::IsMyThread; |
| 283 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 284 | LabelRange TraceState::GetLabelRange(dfsan_label L) { |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 285 | LabelRange &LR = LabelRanges[L]; |
| 286 | if (LR.Beg < LR.End || L == 0) |
| 287 | return LR; |
| 288 | const dfsan_label_info *LI = dfsan_get_label_info(L); |
| 289 | if (LI->l1 || LI->l2) |
| 290 | return LR = LabelRange::Join(GetLabelRange(LI->l1), GetLabelRange(LI->l2)); |
| 291 | return LR = LabelRange::Singleton(LI); |
| 292 | } |
| 293 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 294 | void TraceState::DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 295 | uint64_t Arg1, uint64_t Arg2, dfsan_label L1, |
| 296 | dfsan_label L2) { |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 297 | assert(ReallyHaveDFSan()); |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame] | 298 | if (!RecordingTraces || !IsMyThread) return; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 299 | if (L1 == 0 && L2 == 0) |
| 300 | return; // Not actionable. |
| 301 | if (L1 != 0 && L2 != 0) |
| 302 | return; // Probably still actionable. |
| 303 | bool Res = ComputeCmp(CmpSize, CmpType, Arg1, Arg2); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 304 | uint64_t Data = L1 ? Arg2 : Arg1; |
| 305 | LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 306 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 307 | for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) { |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 308 | AddMutation(Pos, CmpSize, Data); |
| 309 | AddMutation(Pos, CmpSize, Data + 1); |
| 310 | AddMutation(Pos, CmpSize, Data - 1); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | if (CmpSize > LR.End - LR.Beg) |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 314 | AddMutation(LR.Beg, (unsigned)(LR.End - LR.Beg), Data); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 315 | |
| 316 | |
| 317 | if (Options.Verbosity >= 3) |
Kostya Serebryany | ae7df1c | 2015-07-28 01:25:00 +0000 | [diff] [blame] | 318 | Printf("DFSanCmpCallback: PC %lx S %zd T %zd A1 %llx A2 %llx R %d L1 %d L2 " |
| 319 | "%d MU %zd\n", |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 320 | PC, CmpSize, CmpType, Arg1, Arg2, Res, L1, L2, NumMutations); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 323 | void TraceState::DFSanMemcmpCallback(size_t CmpSize, const uint8_t *Data1, |
| 324 | const uint8_t *Data2, dfsan_label L1, |
| 325 | dfsan_label L2) { |
| 326 | |
| 327 | assert(ReallyHaveDFSan()); |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 328 | if (!RecordingMemcmp || !IsMyThread) return; |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 329 | if (L1 == 0 && L2 == 0) |
| 330 | return; // Not actionable. |
| 331 | if (L1 != 0 && L2 != 0) |
| 332 | return; // Probably still actionable. |
| 333 | |
| 334 | const uint8_t *Data = L1 ? Data2 : Data1; |
| 335 | LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2); |
| 336 | for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) { |
| 337 | AddMutation(Pos, CmpSize, Data); |
| 338 | if (Options.Verbosity >= 3) |
| 339 | Printf("DFSanMemcmpCallback: Pos %d Size %d\n", Pos, CmpSize); |
| 340 | } |
| 341 | } |
| 342 | |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 343 | void TraceState::DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits, |
| 344 | uint64_t Val, size_t NumCases, |
| 345 | uint64_t *Cases, dfsan_label L) { |
| 346 | assert(ReallyHaveDFSan()); |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame] | 347 | if (!RecordingTraces || !IsMyThread) return; |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 348 | if (!L) return; // Not actionable. |
| 349 | LabelRange LR = GetLabelRange(L); |
| 350 | size_t ValSize = ValSizeInBits / 8; |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 351 | bool TryShort = IsTwoByteData(Val); |
| 352 | for (size_t i = 0; i < NumCases; i++) |
| 353 | TryShort &= IsTwoByteData(Cases[i]); |
| 354 | |
| 355 | for (size_t Pos = LR.Beg; Pos + ValSize <= LR.End; Pos++) |
| 356 | for (size_t i = 0; i < NumCases; i++) |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 357 | AddMutation(Pos, ValSize, Cases[i]); |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 358 | |
| 359 | if (TryShort) |
| 360 | for (size_t Pos = LR.Beg; Pos + 2 <= LR.End; Pos++) |
| 361 | for (size_t i = 0; i < NumCases; i++) |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 362 | AddMutation(Pos, 2, Cases[i]); |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 363 | |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 364 | if (Options.Verbosity >= 3) |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 365 | Printf("DFSanSwitchCallback: PC %lx Val %zd SZ %zd # %zd L %d: {%d, %d} " |
| 366 | "TryShort %d\n", |
| 367 | PC, Val, ValSize, NumCases, L, LR.Beg, LR.End, TryShort); |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 370 | int TraceState::TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData, |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 371 | size_t DataSize) { |
| 372 | int Res = 0; |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 373 | const uint8_t *Beg = *CurrentUnitData; |
| 374 | const uint8_t *End = Beg + *CurrentUnitSize; |
Kostya Serebryany | e641dd6 | 2015-09-04 22:32:25 +0000 | [diff] [blame] | 375 | for (const uint8_t *Cur = Beg; Cur < End; Cur++) { |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 376 | Cur = (uint8_t *)memmem(Cur, End - Cur, &PresentData, DataSize); |
| 377 | if (!Cur) |
| 378 | break; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 379 | size_t Pos = Cur - Beg; |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 380 | assert(Pos < *CurrentUnitSize); |
Kostya Serebryany | e7583d2 | 2016-01-09 00:38:40 +0000 | [diff] [blame] | 381 | AddMutation(Pos, DataSize, DesiredData); |
| 382 | AddMutation(Pos, DataSize, DesiredData + 1); |
| 383 | AddMutation(Pos, DataSize, DesiredData - 1); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 384 | Res++; |
| 385 | } |
| 386 | return Res; |
| 387 | } |
| 388 | |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 389 | int TraceState::TryToAddDesiredData(const uint8_t *PresentData, |
| 390 | const uint8_t *DesiredData, |
| 391 | size_t DataSize) { |
| 392 | int Res = 0; |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 393 | const uint8_t *Beg = *CurrentUnitData; |
| 394 | const uint8_t *End = Beg + *CurrentUnitSize; |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 395 | for (const uint8_t *Cur = Beg; Cur < End; Cur++) { |
| 396 | Cur = (uint8_t *)memmem(Cur, End - Cur, PresentData, DataSize); |
| 397 | if (!Cur) |
| 398 | break; |
| 399 | size_t Pos = Cur - Beg; |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 400 | assert(Pos < *CurrentUnitSize); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 401 | AddMutation(Pos, DataSize, DesiredData); |
| 402 | Res++; |
| 403 | } |
| 404 | return Res; |
| 405 | } |
| 406 | |
Kostya Serebryany | e641dd6 | 2015-09-04 22:32:25 +0000 | [diff] [blame] | 407 | void TraceState::TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
| 408 | uint64_t Arg1, uint64_t Arg2) { |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame] | 409 | if (!RecordingTraces || !IsMyThread) return; |
Kostya Serebryany | 859e86d | 2016-01-12 02:08:37 +0000 | [diff] [blame] | 410 | if ((CmpType == ICMP_EQ || CmpType == ICMP_NE) && Arg1 == Arg2) |
| 411 | return; // No reason to mutate. |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 412 | int Added = 0; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 413 | Added += TryToAddDesiredData(Arg1, Arg2, CmpSize); |
| 414 | Added += TryToAddDesiredData(Arg2, Arg1, CmpSize); |
| 415 | if (!Added && CmpSize == 4 && IsTwoByteData(Arg1) && IsTwoByteData(Arg2)) { |
| 416 | Added += TryToAddDesiredData(Arg1, Arg2, 2); |
| 417 | Added += TryToAddDesiredData(Arg2, Arg1, 2); |
| 418 | } |
Kostya Serebryany | 859e86d | 2016-01-12 02:08:37 +0000 | [diff] [blame] | 419 | if (Options.Verbosity >= 3 && Added) |
| 420 | 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] | 421 | } |
| 422 | |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 423 | void TraceState::TraceMemcmpCallback(size_t CmpSize, const uint8_t *Data1, |
| 424 | const uint8_t *Data2) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 425 | if (!RecordingMemcmp || !IsMyThread) return; |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 426 | CmpSize = std::min(CmpSize, TraceBasedMutation::kMaxSize); |
Kostya Serebryany | 1f9c40d | 2016-01-09 03:46:08 +0000 | [diff] [blame] | 427 | int Added2 = TryToAddDesiredData(Data1, Data2, CmpSize); |
| 428 | int Added1 = TryToAddDesiredData(Data2, Data1, CmpSize); |
| 429 | if ((Added1 || Added2) && Options.Verbosity >= 3) { |
| 430 | Printf("MemCmp Added %d%d: ", Added1, Added2); |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 431 | if (Added1) PrintASCII(Data1, CmpSize); |
| 432 | if (Added2) PrintASCII(Data2, CmpSize); |
Kostya Serebryany | 1f9c40d | 2016-01-09 03:46:08 +0000 | [diff] [blame] | 433 | Printf("\n"); |
| 434 | } |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 437 | void TraceState::TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits, |
| 438 | uint64_t Val, size_t NumCases, |
| 439 | uint64_t *Cases) { |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame] | 440 | if (!RecordingTraces || !IsMyThread) return; |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 441 | size_t ValSize = ValSizeInBits / 8; |
| 442 | bool TryShort = IsTwoByteData(Val); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 443 | for (size_t i = 0; i < NumCases; i++) |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 444 | TryShort &= IsTwoByteData(Cases[i]); |
| 445 | |
| 446 | if (Options.Verbosity >= 3) |
| 447 | Printf("TraceSwitch: %p %zd # %zd; TryShort %d\n", PC, Val, NumCases, |
| 448 | TryShort); |
| 449 | |
| 450 | for (size_t i = 0; i < NumCases; i++) { |
| 451 | TryToAddDesiredData(Val, Cases[i], ValSize); |
| 452 | if (TryShort) |
| 453 | TryToAddDesiredData(Val, Cases[i], 2); |
| 454 | } |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 457 | static TraceState *TS; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 458 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 459 | void Fuzzer::StartTraceRecording() { |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 460 | if (!TS) return; |
| 461 | TS->StartTraceRecording(); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 464 | void Fuzzer::StopTraceRecording() { |
| 465 | if (!TS) return; |
| 466 | TS->StopTraceRecording(); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Kostya Serebryany | d50a3ee | 2016-01-13 23:02:30 +0000 | [diff] [blame] | 469 | void Fuzzer::AssignTaintLabels(uint8_t *Data, size_t Size) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 470 | if (!Options.UseTraces && !Options.UseMemcmp) return; |
Kostya Serebryany | d50a3ee | 2016-01-13 23:02:30 +0000 | [diff] [blame] | 471 | if (!ReallyHaveDFSan()) return; |
| 472 | for (size_t i = 0; i < Size; i++) |
| 473 | dfsan_set_label(i + 1, &Data[i], 1); |
| 474 | } |
| 475 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 476 | void Fuzzer::InitializeTraceState() { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 477 | if (!Options.UseTraces && !Options.UseMemcmp) return; |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 478 | TS = new TraceState(USF, Options, &CurrentUnitData, &CurrentUnitSize); |
Kostya Serebryany | d50a3ee | 2016-01-13 23:02:30 +0000 | [diff] [blame] | 479 | if (ReallyHaveDFSan()) { |
| 480 | for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++) { |
| 481 | dfsan_label L = dfsan_create_label("input", (void *)(i + 1)); |
| 482 | // We assume that no one else has called dfsan_create_label before. |
| 483 | if (L != i + 1) { |
| 484 | Printf("DFSan labels are not starting from 1, exiting\n"); |
| 485 | exit(1); |
| 486 | } |
Kostya Serebryany | d46369d | 2015-08-05 23:44:42 +0000 | [diff] [blame] | 487 | } |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Kostya Serebryany | c9dc96b | 2015-07-30 21:22:22 +0000 | [diff] [blame] | 491 | static size_t InternalStrnlen(const char *S, size_t MaxLen) { |
| 492 | size_t Len = 0; |
| 493 | for (; Len < MaxLen && S[Len]; Len++) {} |
| 494 | return Len; |
| 495 | } |
| 496 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 497 | } // namespace fuzzer |
| 498 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 499 | using fuzzer::TS; |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 500 | using fuzzer::RecordingTraces; |
| 501 | using fuzzer::RecordingMemcmp; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 502 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 503 | extern "C" { |
| 504 | void __dfsw___sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1, |
| 505 | uint64_t Arg2, dfsan_label L0, |
| 506 | dfsan_label L1, dfsan_label L2) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 507 | if (!RecordingTraces) return; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 508 | assert(L0 == 0); |
| 509 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 510 | uint64_t CmpSize = (SizeAndType >> 32) / 8; |
| 511 | uint64_t Type = (SizeAndType << 32) >> 32; |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 512 | TS->DFSanCmpCallback(PC, CmpSize, Type, Arg1, Arg2, L1, L2); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 513 | } |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 514 | |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 515 | void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases, |
| 516 | dfsan_label L1, dfsan_label L2) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 517 | if (!RecordingTraces) return; |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 518 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 519 | TS->DFSanSwitchCallback(PC, Cases[1], Val, Cases[0], Cases+2, L1); |
| 520 | } |
| 521 | |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 522 | void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2, |
| 523 | size_t n, dfsan_label s1_label, |
| 524 | dfsan_label s2_label, dfsan_label n_label) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 525 | if (!RecordingMemcmp) return; |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 526 | dfsan_label L1 = dfsan_read_label(s1, n); |
| 527 | dfsan_label L2 = dfsan_read_label(s2, n); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 528 | TS->DFSanMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1), |
| 529 | reinterpret_cast<const uint8_t *>(s2), L1, L2); |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 530 | } |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 531 | |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 532 | void dfsan_weak_hook_strncmp(void *caller_pc, const char *s1, const char *s2, |
| 533 | size_t n, dfsan_label s1_label, |
| 534 | dfsan_label s2_label, dfsan_label n_label) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 535 | if (!RecordingMemcmp) return; |
Kostya Serebryany | c9dc96b | 2015-07-30 21:22:22 +0000 | [diff] [blame] | 536 | n = std::min(n, fuzzer::InternalStrnlen(s1, n)); |
| 537 | n = std::min(n, fuzzer::InternalStrnlen(s2, n)); |
Kostya Serebryany | c9dc96b | 2015-07-30 21:22:22 +0000 | [diff] [blame] | 538 | dfsan_label L1 = dfsan_read_label(s1, n); |
| 539 | dfsan_label L2 = dfsan_read_label(s2, n); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 540 | TS->DFSanMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1), |
| 541 | reinterpret_cast<const uint8_t *>(s2), L1, L2); |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 544 | void dfsan_weak_hook_strcmp(void *caller_pc, const char *s1, const char *s2, |
| 545 | dfsan_label s1_label, dfsan_label s2_label) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 546 | if (!RecordingMemcmp) return; |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 547 | size_t Len1 = strlen(s1); |
| 548 | size_t Len2 = strlen(s2); |
| 549 | size_t N = std::min(Len1, Len2); |
| 550 | if (N <= 1) return; // Not interesting. |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 551 | dfsan_label L1 = dfsan_read_label(s1, Len1); |
| 552 | dfsan_label L2 = dfsan_read_label(s2, Len2); |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 553 | TS->DFSanMemcmpCallback(N, reinterpret_cast<const uint8_t *>(s1), |
| 554 | reinterpret_cast<const uint8_t *>(s2), L1, L2); |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Kostya Serebryany | 4b83a4f | 2016-01-12 16:50:18 +0000 | [diff] [blame] | 557 | // We may need to avoid defining weak hooks to stay compatible with older clang. |
| 558 | #ifndef LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS |
| 559 | # define LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS 1 |
| 560 | #endif |
| 561 | |
| 562 | #if LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS |
Kostya Serebryany | 0e776a2 | 2015-07-30 01:34:58 +0000 | [diff] [blame] | 563 | void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1, |
Kostya Serebryany | e358095 | 2016-01-12 00:43:42 +0000 | [diff] [blame] | 564 | const void *s2, size_t n, int result) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 565 | if (!RecordingMemcmp) return; |
Kostya Serebryany | e358095 | 2016-01-12 00:43:42 +0000 | [diff] [blame] | 566 | if (result == 0) return; // No reason to mutate. |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 567 | if (n <= 1) return; // Not interesting. |
| 568 | TS->TraceMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1), |
| 569 | reinterpret_cast<const uint8_t *>(s2)); |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1, |
Kostya Serebryany | e358095 | 2016-01-12 00:43:42 +0000 | [diff] [blame] | 573 | const char *s2, size_t n, int result) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 574 | if (!RecordingMemcmp) return; |
Kostya Serebryany | e358095 | 2016-01-12 00:43:42 +0000 | [diff] [blame] | 575 | if (result == 0) return; // No reason to mutate. |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 576 | size_t Len1 = fuzzer::InternalStrnlen(s1, n); |
| 577 | size_t Len2 = fuzzer::InternalStrnlen(s2, n); |
| 578 | n = std::min(n, Len1); |
| 579 | n = std::min(n, Len2); |
| 580 | if (n <= 1) return; // Not interesting. |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 581 | TS->TraceMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1), |
| 582 | reinterpret_cast<const uint8_t *>(s2)); |
Kostya Serebryany | 0e776a2 | 2015-07-30 01:34:58 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 585 | void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1, |
Kostya Serebryany | e358095 | 2016-01-12 00:43:42 +0000 | [diff] [blame] | 586 | const char *s2, int result) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 587 | if (!RecordingMemcmp) return; |
Kostya Serebryany | e358095 | 2016-01-12 00:43:42 +0000 | [diff] [blame] | 588 | if (result == 0) return; // No reason to mutate. |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 589 | size_t Len1 = strlen(s1); |
| 590 | size_t Len2 = strlen(s2); |
| 591 | size_t N = std::min(Len1, Len2); |
| 592 | if (N <= 1) return; // Not interesting. |
Kostya Serebryany | c573316 | 2016-01-09 01:39:55 +0000 | [diff] [blame] | 593 | TS->TraceMemcmpCallback(N, reinterpret_cast<const uint8_t *>(s1), |
| 594 | reinterpret_cast<const uint8_t *>(s2)); |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Kostya Serebryany | 4b83a4f | 2016-01-12 16:50:18 +0000 | [diff] [blame] | 597 | #endif // LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS |
| 598 | |
Kostya Serebryany | 3287d7a | 2015-09-30 22:22:37 +0000 | [diff] [blame] | 599 | __attribute__((visibility("default"))) |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 600 | void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1, |
| 601 | uint64_t Arg2) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 602 | if (!RecordingTraces) return; |
Kostya Serebryany | 3595959 | 2015-07-28 00:59:53 +0000 | [diff] [blame] | 603 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 604 | uint64_t CmpSize = (SizeAndType >> 32) / 8; |
| 605 | uint64_t Type = (SizeAndType << 32) >> 32; |
Kostya Serebryany | 3595959 | 2015-07-28 00:59:53 +0000 | [diff] [blame] | 606 | TS->TraceCmpCallback(PC, CmpSize, Type, Arg1, Arg2); |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Kostya Serebryany | 3287d7a | 2015-09-30 22:22:37 +0000 | [diff] [blame] | 609 | __attribute__((visibility("default"))) |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 610 | void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) { |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame^] | 611 | if (!RecordingTraces) return; |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 612 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 613 | TS->TraceSwitchCallback(PC, Cases[1], Val, Cases[0], Cases + 2); |
| 614 | } |
| 615 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 616 | } // extern "C" |