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 |
| 44 | // to every byte of the input string (Fuzzer::CurrentUnit) so that for any |
| 45 | // chunk of data we know which input bytes it has derived from. |
| 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 | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 49 | // * Fuzzer::ApplyTraceBasedMutation() tries to use the data recorded |
| 50 | // by __dfsw_* hooks to guide the fuzzing towards new application states. |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 51 | // |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 52 | // 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] | 53 | // Instead of using ifdefs and thus requiring a separate build of lib/Fuzzer |
| 54 | // we redeclare the dfsan_* interface functions as weak and check if they |
| 55 | // are nullptr before calling. |
| 56 | // If this approach proves to be useful we may add attribute(weak) to the |
| 57 | // dfsan declarations in dfsan_interface.h |
| 58 | // |
| 59 | // This module is in the "proof of concept" stage. |
| 60 | // It is capable of solving only the simplest puzzles |
| 61 | // like test/dfsan/DFSanSimpleCmpTest.cpp. |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 64 | /* Example of manual usage (-fsanitize=dataflow is optional): |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 65 | ( |
| 66 | cd $LLVM/lib/Fuzzer/ |
| 67 | clang -fPIC -c -g -O2 -std=c++11 Fuzzer*.cpp |
Alexey Samsonov | 21a3381 | 2015-05-07 23:33:24 +0000 | [diff] [blame] | 68 | clang++ -O0 -std=c++11 -fsanitize-coverage=edge,trace-cmp \ |
Kostya Serebryany | 3befe94 | 2015-05-06 22:47:24 +0000 | [diff] [blame] | 69 | -fsanitize=dataflow \ |
Kostya Serebryany | 65f5086 | 2015-09-10 18:48:38 +0000 | [diff] [blame] | 70 | test/SimpleCmpTest.cpp Fuzzer*.o |
| 71 | ./a.out -use_traces=1 |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 72 | ) |
| 73 | */ |
| 74 | |
Kostya Serebryany | 65f5086 | 2015-09-10 18:48:38 +0000 | [diff] [blame] | 75 | #include "FuzzerDFSan.h" |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 76 | #include "FuzzerInternal.h" |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 77 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 78 | #include <algorithm> |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 79 | #include <cstring> |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame^] | 80 | #include <thread> |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 81 | #include <unordered_map> |
| 82 | |
Kostya Serebryany | 65f5086 | 2015-09-10 18:48:38 +0000 | [diff] [blame] | 83 | #if !LLVM_FUZZER_SUPPORTS_DFSAN |
| 84 | // Stubs for dfsan for platforms where dfsan does not exist and weak |
| 85 | // functions don't work. |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 86 | extern "C" { |
Kostya Serebryany | 65f5086 | 2015-09-10 18:48:38 +0000 | [diff] [blame] | 87 | dfsan_label dfsan_create_label(const char *desc, void *userdata) { return 0; } |
| 88 | void dfsan_set_label(dfsan_label label, void *addr, size_t size) {} |
| 89 | void dfsan_add_label(dfsan_label label, void *addr, size_t size) {} |
| 90 | const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label) { |
| 91 | return nullptr; |
| 92 | } |
| 93 | 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] | 94 | } // extern "C" |
Kostya Serebryany | 65f5086 | 2015-09-10 18:48:38 +0000 | [diff] [blame] | 95 | #endif // !LLVM_FUZZER_SUPPORTS_DFSAN |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 96 | |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 97 | namespace fuzzer { |
| 98 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 99 | // These values are copied from include/llvm/IR/InstrTypes.h. |
| 100 | // We do not include the LLVM headers here to remain independent. |
| 101 | // If these values ever change, an assertion in ComputeCmp will fail. |
| 102 | enum Predicate { |
| 103 | ICMP_EQ = 32, ///< equal |
| 104 | ICMP_NE = 33, ///< not equal |
| 105 | ICMP_UGT = 34, ///< unsigned greater than |
| 106 | ICMP_UGE = 35, ///< unsigned greater or equal |
| 107 | ICMP_ULT = 36, ///< unsigned less than |
| 108 | ICMP_ULE = 37, ///< unsigned less or equal |
| 109 | ICMP_SGT = 38, ///< signed greater than |
| 110 | ICMP_SGE = 39, ///< signed greater or equal |
| 111 | ICMP_SLT = 40, ///< signed less than |
| 112 | ICMP_SLE = 41, ///< signed less or equal |
| 113 | }; |
| 114 | |
| 115 | template <class U, class S> |
| 116 | bool ComputeCmp(size_t CmpType, U Arg1, U Arg2) { |
| 117 | switch(CmpType) { |
| 118 | case ICMP_EQ : return Arg1 == Arg2; |
| 119 | case ICMP_NE : return Arg1 != Arg2; |
| 120 | case ICMP_UGT: return Arg1 > Arg2; |
| 121 | case ICMP_UGE: return Arg1 >= Arg2; |
| 122 | case ICMP_ULT: return Arg1 < Arg2; |
| 123 | case ICMP_ULE: return Arg1 <= Arg2; |
| 124 | case ICMP_SGT: return (S)Arg1 > (S)Arg2; |
| 125 | case ICMP_SGE: return (S)Arg1 >= (S)Arg2; |
| 126 | case ICMP_SLT: return (S)Arg1 < (S)Arg2; |
| 127 | case ICMP_SLE: return (S)Arg1 <= (S)Arg2; |
| 128 | default: assert(0 && "unsupported CmpType"); |
| 129 | } |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | static bool ComputeCmp(size_t CmpSize, size_t CmpType, uint64_t Arg1, |
| 134 | uint64_t Arg2) { |
| 135 | if (CmpSize == 8) return ComputeCmp<uint64_t, int64_t>(CmpType, Arg1, Arg2); |
| 136 | if (CmpSize == 4) return ComputeCmp<uint32_t, int32_t>(CmpType, Arg1, Arg2); |
| 137 | if (CmpSize == 2) return ComputeCmp<uint16_t, int16_t>(CmpType, Arg1, Arg2); |
| 138 | if (CmpSize == 1) return ComputeCmp<uint8_t, int8_t>(CmpType, Arg1, Arg2); |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 139 | // Other size, == |
| 140 | if (CmpType == ICMP_EQ) return Arg1 == Arg2; |
Kostya Serebryany | 8ce7424 | 2015-08-01 01:42:51 +0000 | [diff] [blame] | 141 | // assert(0 && "unsupported cmp and type size combination"); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 142 | return true; |
| 143 | } |
| 144 | |
| 145 | // As a simplification we use the range of input bytes instead of a set of input |
| 146 | // bytes. |
| 147 | struct LabelRange { |
| 148 | uint16_t Beg, End; // Range is [Beg, End), thus Beg==End is an empty range. |
| 149 | |
| 150 | LabelRange(uint16_t Beg = 0, uint16_t End = 0) : Beg(Beg), End(End) {} |
| 151 | |
| 152 | static LabelRange Join(LabelRange LR1, LabelRange LR2) { |
| 153 | if (LR1.Beg == LR1.End) return LR2; |
| 154 | if (LR2.Beg == LR2.End) return LR1; |
| 155 | return {std::min(LR1.Beg, LR2.Beg), std::max(LR1.End, LR2.End)}; |
| 156 | } |
| 157 | LabelRange &Join(LabelRange LR) { |
| 158 | return *this = Join(*this, LR); |
| 159 | } |
| 160 | static LabelRange Singleton(const dfsan_label_info *LI) { |
| 161 | uint16_t Idx = (uint16_t)(uintptr_t)LI->userdata; |
| 162 | assert(Idx > 0); |
| 163 | return {(uint16_t)(Idx - 1), Idx}; |
| 164 | } |
| 165 | }; |
| 166 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 167 | // For now, very simple: put Size bytes of Data at position Pos. |
| 168 | struct TraceBasedMutation { |
| 169 | size_t Pos; |
| 170 | size_t Size; |
| 171 | uint64_t Data; |
| 172 | }; |
| 173 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 174 | class TraceState { |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 175 | public: |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame^] | 176 | TraceState(const Fuzzer::FuzzingOptions &Options, const Unit &CurrentUnit) |
| 177 | : Options(Options), CurrentUnit(CurrentUnit) { |
| 178 | // Current trace collection is not thread-friendly and it probably |
| 179 | // does not have to be such, but at least we should not crash in presence |
| 180 | // of threads. So, just ignore all traces coming from all threads but one. |
| 181 | IsMyThread = true; |
| 182 | } |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 183 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 184 | LabelRange GetLabelRange(dfsan_label L); |
| 185 | void DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
| 186 | uint64_t Arg1, uint64_t Arg2, dfsan_label L1, |
| 187 | dfsan_label L2); |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 188 | void DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits, uint64_t Val, |
| 189 | size_t NumCases, uint64_t *Cases, dfsan_label L); |
Kostya Serebryany | e641dd6 | 2015-09-04 22:32:25 +0000 | [diff] [blame] | 190 | void TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
| 191 | uint64_t Arg1, uint64_t Arg2); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 192 | |
| 193 | void TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits, uint64_t Val, |
| 194 | size_t NumCases, uint64_t *Cases); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 195 | int TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData, |
| 196 | size_t DataSize); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 197 | |
| 198 | void StartTraceRecording() { |
Kostya Serebryany | 8817e86 | 2015-05-11 23:25:28 +0000 | [diff] [blame] | 199 | if (!Options.UseTraces) return; |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 200 | RecordingTraces = true; |
| 201 | Mutations.clear(); |
| 202 | } |
| 203 | |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 204 | size_t StopTraceRecording(FuzzerRandomBase &Rand) { |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 205 | RecordingTraces = false; |
Kostya Serebryany | 12c7837 | 2015-08-12 01:55:37 +0000 | [diff] [blame] | 206 | return Mutations.size(); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 210 | |
| 211 | private: |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 212 | bool IsTwoByteData(uint64_t Data) { |
| 213 | int64_t Signed = static_cast<int64_t>(Data); |
| 214 | Signed >>= 16; |
| 215 | return Signed == 0 || Signed == -1L; |
| 216 | } |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 217 | bool RecordingTraces = false; |
| 218 | std::vector<TraceBasedMutation> Mutations; |
Kostya Serebryany | 4d62322 | 2015-11-18 01:08:30 +0000 | [diff] [blame] | 219 | LabelRange LabelRanges[1 << (sizeof(dfsan_label) * 8)]; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 220 | const Fuzzer::FuzzingOptions &Options; |
| 221 | const Unit &CurrentUnit; |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame^] | 222 | static thread_local bool IsMyThread; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame^] | 225 | thread_local bool TraceState::IsMyThread; |
| 226 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 227 | LabelRange TraceState::GetLabelRange(dfsan_label L) { |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 228 | LabelRange &LR = LabelRanges[L]; |
| 229 | if (LR.Beg < LR.End || L == 0) |
| 230 | return LR; |
| 231 | const dfsan_label_info *LI = dfsan_get_label_info(L); |
| 232 | if (LI->l1 || LI->l2) |
| 233 | return LR = LabelRange::Join(GetLabelRange(LI->l1), GetLabelRange(LI->l2)); |
| 234 | return LR = LabelRange::Singleton(LI); |
| 235 | } |
| 236 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 237 | void TraceState::ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U) { |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 238 | assert(Idx < Mutations.size()); |
| 239 | auto &M = Mutations[Idx]; |
| 240 | if (Options.Verbosity >= 3) |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame] | 241 | Printf("TBM %zd %zd %zd\n", M.Pos, M.Size, M.Data); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 242 | if (M.Pos + M.Size > U->size()) return; |
| 243 | memcpy(U->data() + M.Pos, &M.Data, M.Size); |
| 244 | } |
| 245 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 246 | void TraceState::DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 247 | uint64_t Arg1, uint64_t Arg2, dfsan_label L1, |
| 248 | dfsan_label L2) { |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 249 | assert(ReallyHaveDFSan()); |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame^] | 250 | if (!RecordingTraces || !IsMyThread) return; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 251 | if (L1 == 0 && L2 == 0) |
| 252 | return; // Not actionable. |
| 253 | if (L1 != 0 && L2 != 0) |
| 254 | return; // Probably still actionable. |
| 255 | bool Res = ComputeCmp(CmpSize, CmpType, Arg1, Arg2); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 256 | uint64_t Data = L1 ? Arg2 : Arg1; |
| 257 | LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 258 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 259 | for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) { |
| 260 | Mutations.push_back({Pos, CmpSize, Data}); |
| 261 | Mutations.push_back({Pos, CmpSize, Data + 1}); |
| 262 | Mutations.push_back({Pos, CmpSize, Data - 1}); |
| 263 | } |
| 264 | |
| 265 | if (CmpSize > LR.End - LR.Beg) |
| 266 | Mutations.push_back({LR.Beg, (unsigned)(LR.End - LR.Beg), Data}); |
| 267 | |
| 268 | |
| 269 | if (Options.Verbosity >= 3) |
Kostya Serebryany | ae7df1c | 2015-07-28 01:25:00 +0000 | [diff] [blame] | 270 | Printf("DFSanCmpCallback: PC %lx S %zd T %zd A1 %llx A2 %llx R %d L1 %d L2 " |
| 271 | "%d MU %zd\n", |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame] | 272 | PC, CmpSize, CmpType, Arg1, Arg2, Res, L1, L2, Mutations.size()); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 275 | void TraceState::DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits, |
| 276 | uint64_t Val, size_t NumCases, |
| 277 | uint64_t *Cases, dfsan_label L) { |
| 278 | assert(ReallyHaveDFSan()); |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame^] | 279 | if (!RecordingTraces || !IsMyThread) return; |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 280 | if (!L) return; // Not actionable. |
| 281 | LabelRange LR = GetLabelRange(L); |
| 282 | size_t ValSize = ValSizeInBits / 8; |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 283 | bool TryShort = IsTwoByteData(Val); |
| 284 | for (size_t i = 0; i < NumCases; i++) |
| 285 | TryShort &= IsTwoByteData(Cases[i]); |
| 286 | |
| 287 | for (size_t Pos = LR.Beg; Pos + ValSize <= LR.End; Pos++) |
| 288 | for (size_t i = 0; i < NumCases; i++) |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 289 | Mutations.push_back({Pos, ValSize, Cases[i]}); |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 290 | |
| 291 | if (TryShort) |
| 292 | for (size_t Pos = LR.Beg; Pos + 2 <= LR.End; Pos++) |
| 293 | for (size_t i = 0; i < NumCases; i++) |
| 294 | Mutations.push_back({Pos, 2, Cases[i]}); |
| 295 | |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 296 | if (Options.Verbosity >= 3) |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 297 | Printf("DFSanSwitchCallback: PC %lx Val %zd SZ %zd # %zd L %d: {%d, %d} " |
| 298 | "TryShort %d\n", |
| 299 | PC, Val, ValSize, NumCases, L, LR.Beg, LR.End, TryShort); |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 302 | int TraceState::TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData, |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 303 | size_t DataSize) { |
| 304 | int Res = 0; |
| 305 | const uint8_t *Beg = CurrentUnit.data(); |
| 306 | const uint8_t *End = Beg + CurrentUnit.size(); |
Kostya Serebryany | e641dd6 | 2015-09-04 22:32:25 +0000 | [diff] [blame] | 307 | for (const uint8_t *Cur = Beg; Cur < End; Cur++) { |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 308 | Cur = (uint8_t *)memmem(Cur, End - Cur, &PresentData, DataSize); |
| 309 | if (!Cur) |
| 310 | break; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 311 | size_t Pos = Cur - Beg; |
| 312 | assert(Pos < CurrentUnit.size()); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 313 | if (Mutations.size() > 100000U) return Res; // Just in case. |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 314 | Mutations.push_back({Pos, DataSize, DesiredData}); |
| 315 | Mutations.push_back({Pos, DataSize, DesiredData + 1}); |
| 316 | Mutations.push_back({Pos, DataSize, DesiredData - 1}); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 317 | Res++; |
| 318 | } |
| 319 | return Res; |
| 320 | } |
| 321 | |
Kostya Serebryany | e641dd6 | 2015-09-04 22:32:25 +0000 | [diff] [blame] | 322 | void TraceState::TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
| 323 | uint64_t Arg1, uint64_t Arg2) { |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame^] | 324 | if (!RecordingTraces || !IsMyThread) return; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 325 | int Added = 0; |
| 326 | if (Options.Verbosity >= 3) |
Kostya Serebryany | 4b82de2 | 2015-09-08 20:40:10 +0000 | [diff] [blame] | 327 | 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] | 328 | Added += TryToAddDesiredData(Arg1, Arg2, CmpSize); |
| 329 | Added += TryToAddDesiredData(Arg2, Arg1, CmpSize); |
| 330 | if (!Added && CmpSize == 4 && IsTwoByteData(Arg1) && IsTwoByteData(Arg2)) { |
| 331 | Added += TryToAddDesiredData(Arg1, Arg2, 2); |
| 332 | Added += TryToAddDesiredData(Arg2, Arg1, 2); |
| 333 | } |
| 334 | } |
| 335 | |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 336 | void TraceState::TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits, |
| 337 | uint64_t Val, size_t NumCases, |
| 338 | uint64_t *Cases) { |
Kostya Serebryany | 226b734 | 2016-01-06 00:03:35 +0000 | [diff] [blame^] | 339 | if (!RecordingTraces || !IsMyThread) return; |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 340 | size_t ValSize = ValSizeInBits / 8; |
| 341 | bool TryShort = IsTwoByteData(Val); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 342 | for (size_t i = 0; i < NumCases; i++) |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 343 | TryShort &= IsTwoByteData(Cases[i]); |
| 344 | |
| 345 | if (Options.Verbosity >= 3) |
| 346 | Printf("TraceSwitch: %p %zd # %zd; TryShort %d\n", PC, Val, NumCases, |
| 347 | TryShort); |
| 348 | |
| 349 | for (size_t i = 0; i < NumCases; i++) { |
| 350 | TryToAddDesiredData(Val, Cases[i], ValSize); |
| 351 | if (TryShort) |
| 352 | TryToAddDesiredData(Val, Cases[i], 2); |
| 353 | } |
| 354 | |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 357 | static TraceState *TS; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 358 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 359 | void Fuzzer::StartTraceRecording() { |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 360 | if (!TS) return; |
Kostya Serebryany | 4cc10d4 | 2015-08-05 23:02:57 +0000 | [diff] [blame] | 361 | if (ReallyHaveDFSan()) |
| 362 | for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++) |
| 363 | dfsan_set_label(i + 1, &CurrentUnit[i], 1); |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 364 | TS->StartTraceRecording(); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | size_t Fuzzer::StopTraceRecording() { |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 368 | if (!TS) return 0; |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 369 | return TS->StopTraceRecording(USF.GetRand()); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void Fuzzer::ApplyTraceBasedMutation(size_t Idx, Unit *U) { |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 373 | assert(TS); |
| 374 | TS->ApplyTraceBasedMutation(Idx, U); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 377 | void Fuzzer::InitializeTraceState() { |
Kostya Serebryany | d8c5472 | 2015-05-12 01:58:34 +0000 | [diff] [blame] | 378 | if (!Options.UseTraces) return; |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 379 | TS = new TraceState(Options, CurrentUnit); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 380 | CurrentUnit.resize(Options.MaxLen); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 381 | // The rest really requires DFSan. |
Kostya Serebryany | d8c5472 | 2015-05-12 01:58:34 +0000 | [diff] [blame] | 382 | if (!ReallyHaveDFSan()) return; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 383 | for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++) { |
| 384 | dfsan_label L = dfsan_create_label("input", (void*)(i + 1)); |
| 385 | // We assume that no one else has called dfsan_create_label before. |
Kostya Serebryany | d46369d | 2015-08-05 23:44:42 +0000 | [diff] [blame] | 386 | if (L != i + 1) { |
| 387 | Printf("DFSan labels are not starting from 1, exiting\n"); |
| 388 | exit(1); |
| 389 | } |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Kostya Serebryany | c9dc96b | 2015-07-30 21:22:22 +0000 | [diff] [blame] | 393 | static size_t InternalStrnlen(const char *S, size_t MaxLen) { |
| 394 | size_t Len = 0; |
| 395 | for (; Len < MaxLen && S[Len]; Len++) {} |
| 396 | return Len; |
| 397 | } |
| 398 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 399 | } // namespace fuzzer |
| 400 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 401 | using fuzzer::TS; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 402 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 403 | extern "C" { |
| 404 | void __dfsw___sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1, |
| 405 | uint64_t Arg2, dfsan_label L0, |
| 406 | dfsan_label L1, dfsan_label L2) { |
Kostya Serebryany | 3fe7682 | 2015-05-29 20:31:17 +0000 | [diff] [blame] | 407 | if (!TS) return; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 408 | assert(L0 == 0); |
| 409 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 410 | uint64_t CmpSize = (SizeAndType >> 32) / 8; |
| 411 | uint64_t Type = (SizeAndType << 32) >> 32; |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 412 | TS->DFSanCmpCallback(PC, CmpSize, Type, Arg1, Arg2, L1, L2); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 413 | } |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 414 | |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 415 | void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases, |
| 416 | dfsan_label L1, dfsan_label L2) { |
| 417 | if (!TS) return; |
| 418 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 419 | TS->DFSanSwitchCallback(PC, Cases[1], Val, Cases[0], Cases+2, L1); |
| 420 | } |
| 421 | |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 422 | void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2, |
| 423 | size_t n, dfsan_label s1_label, |
| 424 | dfsan_label s2_label, dfsan_label n_label) { |
Kostya Serebryany | 3fe7682 | 2015-05-29 20:31:17 +0000 | [diff] [blame] | 425 | if (!TS) return; |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 426 | uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 427 | uint64_t S1 = 0, S2 = 0; |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 428 | // Simplification: handle only first 8 bytes. |
| 429 | memcpy(&S1, s1, std::min(n, sizeof(S1))); |
| 430 | memcpy(&S2, s2, std::min(n, sizeof(S2))); |
| 431 | dfsan_label L1 = dfsan_read_label(s1, n); |
| 432 | dfsan_label L2 = dfsan_read_label(s2, n); |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 433 | TS->DFSanCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2, L1, L2); |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 434 | } |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 435 | |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 436 | void dfsan_weak_hook_strncmp(void *caller_pc, const char *s1, const char *s2, |
| 437 | size_t n, dfsan_label s1_label, |
| 438 | dfsan_label s2_label, dfsan_label n_label) { |
Kostya Serebryany | c9dc96b | 2015-07-30 21:22:22 +0000 | [diff] [blame] | 439 | if (!TS) return; |
| 440 | uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc); |
| 441 | uint64_t S1 = 0, S2 = 0; |
| 442 | n = std::min(n, fuzzer::InternalStrnlen(s1, n)); |
| 443 | n = std::min(n, fuzzer::InternalStrnlen(s2, n)); |
| 444 | // Simplification: handle only first 8 bytes. |
| 445 | memcpy(&S1, s1, std::min(n, sizeof(S1))); |
| 446 | memcpy(&S2, s2, std::min(n, sizeof(S2))); |
| 447 | dfsan_label L1 = dfsan_read_label(s1, n); |
| 448 | dfsan_label L2 = dfsan_read_label(s2, n); |
| 449 | TS->DFSanCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2, L1, L2); |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 452 | void dfsan_weak_hook_strcmp(void *caller_pc, const char *s1, const char *s2, |
| 453 | dfsan_label s1_label, dfsan_label s2_label) { |
| 454 | if (!TS) return; |
| 455 | uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc); |
| 456 | uint64_t S1 = 0, S2 = 0; |
| 457 | size_t Len1 = strlen(s1); |
| 458 | size_t Len2 = strlen(s2); |
| 459 | size_t N = std::min(Len1, Len2); |
| 460 | if (N <= 1) return; // Not interesting. |
| 461 | // Simplification: handle only first 8 bytes. |
| 462 | memcpy(&S1, s1, std::min(N, sizeof(S1))); |
| 463 | memcpy(&S2, s2, std::min(N, sizeof(S2))); |
| 464 | dfsan_label L1 = dfsan_read_label(s1, Len1); |
| 465 | dfsan_label L2 = dfsan_read_label(s2, Len2); |
| 466 | TS->DFSanCmpCallback(PC, N, fuzzer::ICMP_EQ, S1, S2, L1, L2); |
| 467 | } |
| 468 | |
Kostya Serebryany | 0e776a2 | 2015-07-30 01:34:58 +0000 | [diff] [blame] | 469 | void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1, |
| 470 | const void *s2, size_t n) { |
| 471 | if (!TS) return; |
| 472 | uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc); |
| 473 | uint64_t S1 = 0, S2 = 0; |
| 474 | // Simplification: handle only first 8 bytes. |
| 475 | memcpy(&S1, s1, std::min(n, sizeof(S1))); |
| 476 | memcpy(&S2, s2, std::min(n, sizeof(S2))); |
| 477 | TS->TraceCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2); |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1, |
| 481 | const char *s2, size_t n) { |
Kostya Serebryany | c9dc96b | 2015-07-30 21:22:22 +0000 | [diff] [blame] | 482 | if (!TS) return; |
| 483 | uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc); |
| 484 | uint64_t S1 = 0, S2 = 0; |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 485 | size_t Len1 = fuzzer::InternalStrnlen(s1, n); |
| 486 | size_t Len2 = fuzzer::InternalStrnlen(s2, n); |
| 487 | n = std::min(n, Len1); |
| 488 | n = std::min(n, Len2); |
| 489 | if (n <= 1) return; // Not interesting. |
Kostya Serebryany | c9dc96b | 2015-07-30 21:22:22 +0000 | [diff] [blame] | 490 | // Simplification: handle only first 8 bytes. |
| 491 | memcpy(&S1, s1, std::min(n, sizeof(S1))); |
| 492 | memcpy(&S2, s2, std::min(n, sizeof(S2))); |
| 493 | TS->TraceCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2); |
Kostya Serebryany | 0e776a2 | 2015-07-30 01:34:58 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 496 | void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1, |
| 497 | const char *s2) { |
| 498 | if (!TS) return; |
| 499 | uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc); |
| 500 | uint64_t S1 = 0, S2 = 0; |
| 501 | size_t Len1 = strlen(s1); |
| 502 | size_t Len2 = strlen(s2); |
| 503 | size_t N = std::min(Len1, Len2); |
| 504 | if (N <= 1) return; // Not interesting. |
| 505 | // Simplification: handle only first 8 bytes. |
| 506 | memcpy(&S1, s1, std::min(N, sizeof(S1))); |
| 507 | memcpy(&S2, s2, std::min(N, sizeof(S2))); |
| 508 | TS->TraceCmpCallback(PC, N, fuzzer::ICMP_EQ, S1, S2); |
| 509 | } |
| 510 | |
Kostya Serebryany | 3287d7a | 2015-09-30 22:22:37 +0000 | [diff] [blame] | 511 | __attribute__((visibility("default"))) |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 512 | void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1, |
| 513 | uint64_t Arg2) { |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 514 | if (!TS) return; |
Kostya Serebryany | 3595959 | 2015-07-28 00:59:53 +0000 | [diff] [blame] | 515 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 516 | uint64_t CmpSize = (SizeAndType >> 32) / 8; |
| 517 | uint64_t Type = (SizeAndType << 32) >> 32; |
Kostya Serebryany | 3595959 | 2015-07-28 00:59:53 +0000 | [diff] [blame] | 518 | TS->TraceCmpCallback(PC, CmpSize, Type, Arg1, Arg2); |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Kostya Serebryany | 3287d7a | 2015-09-30 22:22:37 +0000 | [diff] [blame] | 521 | __attribute__((visibility("default"))) |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 522 | void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) { |
| 523 | if (!TS) return; |
| 524 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 525 | TS->TraceSwitchCallback(PC, Cases[1], Val, Cases[0], Cases + 2); |
| 526 | } |
| 527 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 528 | } // extern "C" |